package thyyxxk.webserver.controller.dictionary.clinicalPathway;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.aop.interfaces.CheckClinicalPathway;
import thyyxxk.webserver.aop.interfaces.NeedMenu;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.dictionary.clinicalPathway.ClinicalPathwayDao;
import thyyxxk.webserver.dao.his.dictionary.clinicalPathway.ClinicalPathwayDayDao;
import thyyxxk.webserver.dao.his.dictionary.clinicalPathway.ClinicalPathwayDiagnoseDao;
import thyyxxk.webserver.dao.his.dictionary.clinicalPathway.ClinicalPathwayWorkDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayDay;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayDiagnose;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayTemplates;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayWork;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.res.CreateOrderRes;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.vo.*;
import thyyxxk.webserver.enums.authorize.MenuEnum;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.service.dictionary.clinicalPathway.ClinicalPathwayService;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.SnowFlakeId;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/clinicalPathway")
@RequiredArgsConstructor
public class ClinicalPathwayController {
    private final ClinicalPathwayService service;
    private final ClinicalPathwayDao dao;
    private final UserCache userCache;
    private final ClinicalPathwayDayDao dayDao;
    private final ClinicalPathwayWorkDao workDao;
    private final ClinicalPathwayDiagnoseDao clinicalPathwayDiagnoseDao;
    private final PublicServer publicServer;

    @GetMapping("/getIcdCode")
    public ResultVo<List<GetDropdownBox>> getIcdCode(@RequestParam("code") String code) {
        return ResultVoUtil.success(dao.getIcdCode(code));
    }

    @GetMapping("/getAllPathwayTemplates")
    public ResultVo<List<PathwayTemplatesVo>> getAllPathwayTemplates() {
        return ResultVoUtil.success(service.getTemplatesToTree(null));
    }

    @PostMapping("/createTemplate")
    public ResultVo<String> createTemplate(@RequestBody ClinicalPathwayTemplates params) {
        params.setId(SnowFlakeId.instance().nextId());
        params.setDeptCode(userCache.getUserInfoByToken().getDeptCode());
        dao.insert(params);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_NOTIFICATION);
    }

    @GetMapping("/getTemplateDay")
    public ResultVo<List<TemplateDayVo>> getTemplateDay(@RequestParam("templateId") String templateId) {
        return ResultVoUtil.success(service.getTemplateDay(templateId));
    }

    @GetMapping("/getOrderTemplateByWorkId")
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<List<CreateOrderVo>> getOrderTemplateByWorkId(@RequestParam("workId") String workId) {
        return ResultVoUtil.success(service.getOrderTemplateByWorkId(workId));
    }

    @GetMapping("/getOrderTemplateByDayId")
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<List<CreateOrderVo>> getOrderTemplateByDayId(@RequestParam("dayId") String dayId) {
        return ResultVoUtil.success(service.getOrderTemplateByDayId(dayId));
    }

    @GetMapping("/getJcJyItem")
    public ResultVo<List<JyJcItemVo>> getJcJyItem(@RequestParam("type") Integer type,
                                                  @RequestParam("key") String key) {
        return ResultVoUtil.success(service.getJcJyItem(type, key));
    }

    @GetMapping("/getJyZdSample")
    public ResultVo<List<GetDropdownBox>> getJyZdSample() {
        return ResultVoUtil.success(dao.getJyZdSample());
    }


    @GetMapping("/getDiagnoseByTemplateId")
    public ResultVo<List<ClinicalPathwayDiagnoseVo>> getDiagnoseByTemplateId(@RequestParam("templateId") String templateId) {
        return ResultVoUtil.success(dao.getDiagnoseByTemplateId(templateId));
    }

    //---------------------------------  下面都是需要判断模板是否发布的 ----------------------------------------------

    @PostMapping("/updateTemplate")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<String> updateTemplate(@RequestBody ClinicalPathwayTemplates params) {
        ClinicalPathwayTemplates templates = dao.selectById(params.getId());

        if (templates == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "可能已经被删除了。");
        }

        if (!templates.getCreateCode().equals(TokenUtil.getInstance().getTokenUserId())) {
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_NOTIFICATION, "创建人不是您无法操作。");
        }

        dao.updateById(params);
        return ResultVoUtil.success();
    }


    @PostMapping("/deleteTemplate")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<String> deleteTemplate(@RequestParam("templateId") String templateId) {
        ClinicalPathwayTemplates templates = dao.selectById(templateId);

        if (templates == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "可能已经被删除了。");
        }

        dao.deleteTemplate(templates.getId(), TokenUtil.getInstance().getTokenUserId());
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_NOTIFICATION);
    }

    @GetMapping("/addDay")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<ClinicalPathwayDay> addDay(@RequestParam("templateId") String templateId,
                                               @RequestParam(value = "execDay", defaultValue = "1") Integer execDay) {
        ClinicalPathwayDay build = ClinicalPathwayDay.builder()
                .id(SnowFlakeId.instance().nextId())
                .templateId(templateId)
                .executionDay(execDay)
                .sort(dayDao.getSort(templateId) + 1)
                .build();

        dayDao.insert(build);
        return ResultVoUtil.success(build);
    }

    @PostMapping("/updateDay")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<String> updateDay(@RequestBody ClinicalPathwayDay params) {
        dayDao.updateById(params);
        return ResultVoUtil.success();
    }

    @GetMapping("/delDay")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<String> delDay(@RequestParam("id") String id) {
        dayDao.delDayByTemplateId(id);
        return ResultVoUtil.success();
    }


    @PostMapping("/addWorkItem")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<ClinicalPathwayWork> addWorkItem(@RequestBody ClinicalPathwayWork params) {
        return ResultVoUtil.success(service.addWorkItem(params));
    }

    @PostMapping("/updateWorkItem")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<String> updateWorkItem(@RequestBody ClinicalPathwayWork params) {
        service.updateWorkItem(params);
        return ResultVoUtil.success();
    }

    @GetMapping("/deleteWorkItem")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<String> deleteWorkItem(@RequestParam("id") String id) {
        workDao.deleteById(id);
        return ResultVoUtil.success();
    }

    @PostMapping("/createOrderNo")
    @CheckClinicalPathway
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<Map<Integer, List<String>>> createOrderNo(@RequestBody CreateOrderRes res) {
        return service.createOrderNo(res);
    }


    @PostMapping("/createDiagnose")
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<ClinicalPathwayDiagnose> createDiagnose(@RequestBody ClinicalPathwayDiagnose params) {
        params.setId(SnowFlakeId.instance().nextId());
        if (params.getTemplateId() == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "模板id不能为空。");
        }

        clinicalPathwayDiagnoseDao.insert(params);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_NOTIFICATION, params);
    }

    @GetMapping("/deleteDiagnoseById")
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<String> deleteDiagnoseById(@RequestParam("id") String id) {
        clinicalPathwayDiagnoseDao.deleteById(id);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_NOTIFICATION);
    }

    @GetMapping("/copyClinicalPathway")
    @NeedMenu(MenuEnum.clinicalPathway)
    public ResultVo<String> copyClinicalPathway(@RequestParam("id") String id, @RequestParam("name") String name) {
        return service.copyClinicalPathway(id, name);
    }

    @GetMapping("/delTemplate")
    public ResultVo<String> delTemplate(@RequestParam("id") String id) {
        return service.delTemplate(id);
    }

}
