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

import com.yihu.jw.base.dao.doctor.DoctorServicePermissionsDao;
import com.yihu.jw.base.service.doctor.DoctorServicePermissionsService;
import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.care.common.DoctorServicePermissionsDO;
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.common.IdCardUtil;
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.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/9/7
 * @Description:
 */
@RestController
@RequestMapping(value = "doctorServicePermissions", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "医生服务权限管理", description = "医生服务权限管理", tags = {"wlyy基础服务 - 医生服务权限管理"})
public class DoctorServicePermissionsEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private DoctorServicePermissionsService permissionsService;
    @Autowired
    private DoctorServicePermissionsDao permissionsDao;
    @Autowired
    private BaseDoctorDao doctorDao;

    @PostMapping(value = "create")
    @ApiOperation(value = "创建")
    public ObjEnvelop<DoctorServicePermissionsDO> create (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) throws Exception {
        DoctorServicePermissionsDO permissionsDO = toEntity(jsonData, DoctorServicePermissionsDO.class);
        List<DoctorServicePermissionsDO> permissionsDOList = permissionsDao.findByDoctor(permissionsDO.getDoctor());
        if(permissionsDOList.size()>0){
            return failed("同一个人员只能添加一次", ObjEnvelop.class);
        }
        permissionsDO = permissionsService.save(permissionsDO);
        return success(permissionsDO);
    }

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

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

    @GetMapping(value = "page")
    @ApiOperation(value = "获取分页")
    public PageEnvelop<DoctorServicePermissionsDO> 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<DoctorServicePermissionsDO> permissionsDOs = permissionsService.search(fields, filters, sorts, page, size);
        for (DoctorServicePermissionsDO permissionsDO:permissionsDOs){
            BaseDoctorDO doctorDO = doctorDao.findById(permissionsDO.getDoctor());
            if(doctorDO!=null){
                permissionsDO.setIdcard(doctorDO.getIdcard());
                permissionsDO.setMobile(doctorDO.getMobile());
                permissionsDO.setSex(IdCardUtil.getSexForIdcard(doctorDO.getIdcard()));
            }
        }
        int count = (int)permissionsService.getCount(filters);
        return success(permissionsDOs, count, page, size, DoctorServicePermissionsDO.class);
    }

    @GetMapping(value = "list")
    @ApiOperation(value = "获取列表")
    public ListEnvelop<DoctorServicePermissionsDO> 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<DoctorServicePermissionsDO> permissionsDOs = permissionsService.search(fields, filters, sorts);
        return success(permissionsDOs, DoctorServicePermissionsDO.class);
    }

}
