package com.yihu.jw.base.endpoint.course;

import com.yihu.jw.base.service.course.CourseService;
import com.yihu.jw.entity.care.course.CourseDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.ListEnvelop;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.restmodel.web.PageEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.util.tencent.TencentVODUtil;
import com.yihu.jw.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/5/12
 * @Description:
 */
@RestController
@RequestMapping(value = "course", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "课程管理", description = "课程管理服务接口", tags = {"care基础服务 - 课程管理服务接口"})
public class CourseEndpoint extends EnvelopRestEndpoint {


    @Autowired
    private CourseService courseService;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;

    @Value("${tencent.secretId}")
    private String secretId;
    @Value("${tencent.secretKey}")
    private String secretKey;

    @PostMapping(value = "create")
    @ApiOperation(value = "创建")
    public ObjEnvelop<CourseDO> create (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) throws Exception {
        CourseDO courseDO = toEntity(jsonData, CourseDO.class);
        courseDO.setDel(1);
        courseDO.setLiveStatus("1");
        if(StringUtil.isBlank(courseDO.getStatus())){
            courseDO.setStatus("1");
        }
        courseDO = courseService.save(courseDO);
        return success(courseDO);
    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除")
    public Envelop delete(
            @ApiParam(name = "ids", value = "id串，中间用,分隔", required = true)
            @RequestParam(value = "ids") String ids) {
        courseService.deletes(ids);
        return success("删除成功");
    }

    @PostMapping(value = "update")
    @ApiOperation(value = "更新")
    public ObjEnvelop<CourseDO> update (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) throws Exception {
        CourseDO courseDO = toEntity(jsonData, CourseDO.class);
        if (null == courseDO.getId()) {
            return failed("ID不能为空", ObjEnvelop.class);
        }
        courseDO = courseService.save(courseDO);
        return success(courseDO);
    }

    @GetMapping(value = "page")
    @ApiOperation(value = "获取分页")
    public PageEnvelop<CourseDO> page (
            @ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序，规则参见说明文档")
            @RequestParam(value = "sorts", required = false) String sorts,
            @ApiParam(name = "page", value = "分页大小", required = true, defaultValue = "1")
            @RequestParam(value = "page") int page,
            @ApiParam(name = "size", value = "页码", required = true, defaultValue = "15")
            @RequestParam(value = "size") int size) throws Exception {
        if(StringUtil.isBlank(filters)){
            filters = "del=1";
        }else{
            filters += ";del=1";
        }
        List<CourseDO> courseDOs = courseService.search(fields, filters, sorts, page, size);
        int count = (int)courseService.getCount(filters);
        return success(courseDOs, count, page, size, CourseDO.class);
    }

    @GetMapping(value = "list")
    @ApiOperation(value = "获取列表")
    public ListEnvelop<CourseDO> list (
            @ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序，规则参见说明文档")
            @RequestParam(value = "sorts", required = false) String sorts) throws Exception {
        if(StringUtil.isBlank(filters)){
            filters = "del=1";
        }else{
            filters += ";del=1";
        }
        List<CourseDO> courseDOs = courseService.search(fields, filters, sorts);
        return success(courseDOs, CourseDO.class);
    }

    @GetMapping(value = "findDoctorByOrg")
    @ApiOperation(value = "获取教师")
    public ListEnvelop findDoctorByOrg (
            @ApiParam(name = "orgCode", value = "机构code")
            @RequestParam(value = "orgCode", required = false) String orgCode) throws Exception {
        return success(courseService.findDoctorByOrg(orgCode));
    }

    @GetMapping(value = "getTXUploadSignature")
    @ApiOperation(value = "获取腾讯视频上传签名")
    public Envelop getTXUploadSignature() throws Exception {
        TencentVODUtil tencentVODUtil = new TencentVODUtil();
        tencentVODUtil.setSecretId(secretId);
        tencentVODUtil.setSecretKey(secretKey);
        return success(tencentVODUtil.getUploadSignature());
    }
}
