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

import com.yihu.jw.base.service.dict.DictHospitalDeptService;
import com.yihu.jw.restmodel.base.dict.DictHospitalDeptVO;
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.rm.base.BaseRequestMapping;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import com.yihu.jw.entity.base.dict.DictHospitalDeptDO;

/**
 * 医院科室字典控制器
 *
 * @version <pre>
 * Author	Version		Date		Changes
 * litaohong 	1.0  		2018年08月31日 	Created
 *
 * </pre>
 * @since 1.
 */
@RestController
@RequestMapping(value = BaseRequestMapping.DictHospitalDept.PREFIX)
@Api(value = "医院科室字典管理", description = "医院科室字典管理服务接口", tags = {"wlyy基础服务 - 医院科室字典管理服务接口"})
public class DictHospitalDeptEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private DictHospitalDeptService dictHospitalDeptService;

    @PostMapping(value = BaseRequestMapping.DictHospitalDept.CREATE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建")
    public ObjEnvelop<DictHospitalDeptVO> create(
            @ApiParam(name = "json_data", value = "Json数据", required = true)
            @RequestBody String jsonData) throws Exception {
        DictHospitalDeptDO dictHospitalDept = toEntity(jsonData, DictHospitalDeptDO.class);
        dictHospitalDept = dictHospitalDeptService.save(dictHospitalDept);
        return success(dictHospitalDept, DictHospitalDeptVO.class);
    }

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

    @PostMapping(value = BaseRequestMapping.DictHospitalDept.UPDATE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "更新")
    public ObjEnvelop<DictHospitalDeptVO> update(
            @ApiParam(name = "json_data", value = "Json数据", required = true)
            @RequestBody String jsonData) throws Exception {
        DictHospitalDeptDO dictHospitalDept = toEntity(jsonData, DictHospitalDeptDO.class);
        if (null == dictHospitalDept.getId()) {
            return failed("ID不能为空", ObjEnvelop.class);
        }
        dictHospitalDept = dictHospitalDeptService.save(dictHospitalDept);
        return success(dictHospitalDept, DictHospitalDeptVO.class);
    }

    @GetMapping(value = BaseRequestMapping.DictHospitalDept.PAGE)
    @ApiOperation(value = "获取分页")
    public PageEnvelop<DictHospitalDeptVO> 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 {
        List<DictHospitalDeptDO> dictHospitalDepts = dictHospitalDeptService.search(fields, filters, sorts, page, size);
        int count = (int) dictHospitalDeptService.getCount(filters);
        return success(dictHospitalDepts, count, page, size, DictHospitalDeptVO.class);
    }

    @GetMapping(value = BaseRequestMapping.DictHospitalDept.LIST)
    @ApiOperation(value = "获取列表")
    public ListEnvelop<DictHospitalDeptVO> 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 {
        List<DictHospitalDeptDO> dictHospitalDepts = dictHospitalDeptService.search(fields, filters, sorts);
        return success(dictHospitalDepts, DictHospitalDeptVO.class);
    }

    @GetMapping(value = BaseRequestMapping.DictHospitalDept.queryDeptByOrg)
    @ApiOperation(value = "根据机构获取科室")
    public ListEnvelop<DictHospitalDeptVO> queryDeptByOrg(
            @ApiParam(name = "orgCode", value = "机构标识")
            @RequestParam(value = "orgCode", required = true) String orgCode) throws Exception {
        List<DictHospitalDeptDO> dictHospitalDepts = dictHospitalDeptService.findDeptByOrgCode(orgCode);
        return success(dictHospitalDepts, DictHospitalDeptVO.class);
    }

    @GetMapping(value = BaseRequestMapping.DictHospitalDept.queryDeptDoctor)
    @ApiOperation(value = "获取部门医生")
    public ListEnvelop queryDeptDoctor(
            @ApiParam(name = "deptCode", value = "部门编码")
            @RequestParam(value = "deptCode", required = true) String deptCode,
            @ApiParam(name = "deptName", value = "部门名称")
            @RequestParam(value = "deptName", required = false) String deptName,
            @ApiParam(name = "doctorCode", value = "医生编码")
            @RequestParam(value = "doctorCode", required = false) String doctorCode,
            @ApiParam(name = "doctorName", value = "医生姓名")
            @RequestParam(value = "doctorName", required = false) String doctorName,
            @ApiParam(name = "orgCode", value = "机构编码")
            @RequestParam(value = "orgCode", required = false) String orgCode
            ) throws Exception {

        return success(dictHospitalDeptService.findDoctors(deptCode,deptName,doctorCode,doctorName,orgCode));
    }

    @GetMapping(value = BaseRequestMapping.DictHospitalDept.saveDeptDoctor)
    @ApiOperation(value = "保存部门医生")
    public Envelop saveDeptDoctor(
            @ApiParam(name = "doctorIds", value = "医生id列表")
            @RequestParam(value = "doctorIds", required = false) String doctorIds,
            @ApiParam(name = "deptCode", value = "部门名称")
            @RequestParam(value = "deptCode", required = false) String deptCode,
            @ApiParam(name = "orgCode", value = "部门名称")
            @RequestParam(value = "orgCode", required = false) String orgCode
    ) throws Exception {
        return success(dictHospitalDeptService.saveDeptDoctor(doctorIds,deptCode,orgCode));
    }

    @GetMapping(value = BaseRequestMapping.DictHospitalDept.saveDept)
    @ApiOperation(value = "保存部门信息")
    public Envelop saveDept(
            @ApiParam(name = "deptJson", value = "部门信息json")
            @RequestParam(value = "deptJson", required = false) String deptJson,
            @ApiParam(name = "doctorIds", value = "医生id")
            @RequestParam(value = "doctorIds", required = false) String doctorIds
    )  {
        try {
            return success(dictHospitalDeptService.addDept(deptJson,doctorIds));
        } catch (IOException e) {
            e.printStackTrace();
            return failed(e.getMessage());
        }
    }
    @GetMapping(value = BaseRequestMapping.DictHospitalDept.findSingleDept)
    @ApiOperation(value = "获取单个部门")
    public Envelop findSingleDept(
            @ApiParam(name = "id", value = "部门id")
            @RequestParam(value = "id", required = false) Integer id,
            @ApiParam(name = "page", value = "")
            @RequestParam(value = "page", required = false) Integer page,
            @ApiParam(name = "pageSize", value = "")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) throws Exception {
        return success(dictHospitalDeptService.findDept(id,page,pageSize));
    }


    @GetMapping(value = BaseRequestMapping.DictHospitalDept.findDept)
    @ApiOperation(value = "获取部门")
    public Envelop findDept(
            @ApiParam(name = "orgCode", value = "orgCode")
            @RequestParam(value = "orgCode", required = false) String orgCode,
            @ApiParam(name = "consultDeptFlag", value = "consultDeptFlag")
            @RequestParam(value = "consultDeptFlag", required = false) String consultDeptFlag,
            @ApiParam(name = "fatherDeptCode", value = "fatherDeptCode")
            @RequestParam(value = "fatherDeptCode", required = false) String fatherDeptCode,
            @ApiParam(name = "fatherDeptName", value = "fatherDeptName")
            @RequestParam(value = "fatherDeptName", required = false) String fatherDeptName,
            @ApiParam(name = "name", value = "name")
            @RequestParam(value = "name", required = false) String name,
            @ApiParam(name = "deptCode", value = "deptCode")
            @RequestParam(value = "deptCode", required = false) String deptCode,
            @ApiParam(name = "page", value = "")
            @RequestParam(value = "page", required = false) Integer page,
            @ApiParam(name = "size", value = "")
            @RequestParam(value = "size", required = false) Integer size
    ) throws Exception {
        return    dictHospitalDeptService.queryDept(orgCode,consultDeptFlag,fatherDeptCode,name,deptCode,fatherDeptName,page,size);
    }

    @GetMapping(value = BaseRequestMapping.DictHospitalDept.unUsedDept)
    @ApiOperation(value = "生效失效科室")
    public Envelop unUsedDept(
            @ApiParam(name = "id", value = "部门id")
            @RequestParam(value = "id", required = false) Integer id,
            @ApiParam(name = "consultDeptFlag", value = "")
            @RequestParam(value = "consultDeptFlag", required = false) String  consultDeptFlag
    ) throws Exception {
        return success(dictHospitalDeptService.unUsed(id,consultDeptFlag));
    }



}
