package thyyxxk.webserver.controller.zhuyuanyizheng;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.zhuyuanyisheng.YiZhuLuRuDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.datamodify.SelectV2;
import thyyxxk.webserver.entity.datamodify.YzActOrder;
import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.inpatient.patient.Overview;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.SiLimitRequest;
import thyyxxk.webserver.entity.medicine.vo.YpZdGroupNameVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.OneClickOrder;
import thyyxxk.webserver.entity.zhuyuanyisheng.dto.doctorAuth.DoctorAuthParams;
import thyyxxk.webserver.entity.zhuyuanyisheng.vo.doctorAuth.DoctorAuthRest;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.*;
import thyyxxk.webserver.service.hutoolcache.CacheEnums;
import thyyxxk.webserver.service.hutoolcache.DrugCache;
import thyyxxk.webserver.service.hutoolcache.ExtraCache;
import thyyxxk.webserver.service.zhuyuanyisheng.YiZhuLuRuServer;
import thyyxxk.webserver.utils.R;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 描述： 医嘱录入
 * </p>
 *
 * @author xc
 * @date 2022-01-04 17:02
 */
@RestController
@RequestMapping("/yiZhuLuRu")
@RequiredArgsConstructor
public class YizhuLuRuController {
    private final YiZhuLuRuServer server;
    private final YiZhuLuRuDao dao;
    private final ExtraCache extraCache;

    @GetMapping("/getOrderNo")
    public ResultVo<String> getOrderNo() {
        return server.getOrderNo();
    }

    @GetMapping("/getMyPatient")
    public ResultVo<List<Patient>> getMyPatient() {
        return server.getMyPatient();
    }

    @PostMapping("/enterOrders")
    public ResultVo<Map<String, Object>> enterOrders(@RequestBody XinZhenYiZhu param) {
        return server.enterOrders(param);
    }

    @GetMapping("/toDeleteAnOrder")
    public ResultVo<String> toDeleteAnOrder(@RequestParam("orderNo") String orderNo) {
        return server.toDeleteAnOrder(orderNo);
    }

    @PostMapping("/confirmOrders")
    public ResultVo<Map<String, Object>> confirmOrders(@RequestBody XinZhenYiZhu param) {
        return server.confirmOrders(param);
    }

    @PostMapping("/huoQuYiZhuShuJu")
    public ResultVo<List<XinZhenYzActOrder>> huoQuYiZhuShuJu(@RequestBody @Validated YiZhuFeiYongChaXunTiaoJian param) {
        return server.huoQuYiZhuShuJu(param);
    }

    @GetMapping("/huoQuXiangMu")
    @PassToken
    public ResultVo<List<YiZhuMingChen>> huoQuXiangMu(@RequestParam("code") String code,
                                                      @RequestParam("groupNo") String groupNo) {
        return server.huoQuXiangMu(code, groupNo);
    }

    @GetMapping("/getFrequency")
    @PassToken
    public ResultVo<List<GetDropdownBox>> getFrequency() {
        return server.getFrequency();
    }

    @GetMapping("/getSupplyType")
    @PassToken
    public ResultVo<List<SelectV2>> getSupplyType() {
        return server.getSupplyType();
    }

    @GetMapping("/huoQuFeiYongXinXi")
    @PassToken
    public ResultVo<Map<String, Object>> huoQuFeiYongXinXi(@RequestParam("queryCode") String queryCode) {
        if (queryCode == null) {
            return ResultVoUtil.success();
        }
        String[] str = queryCode.split("_");
        return server.huoQuFeiYongXinXi(str[0], str[1], str[2], str[3], str[4], str[5]);
    }

    @PostMapping("/deleteMultipleOrders")
    public ResultVo<Map<String, String>> deleteMultipleOrders(@RequestBody XinZhenYiZhu param) {
        if (StringUtil.isBlank(param.getInpatientNo())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请选择患者.");
        }
        if (param.getList() == null) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请选择需要删除的医嘱");
        }
        return server.deleteMultipleOrders(param);
    }

    @PostMapping("/stopOrder")
    public ResultVo<Map<BigDecimal, String>> stopOrder(@RequestBody XinZhenYiZhu param) {
        return server.stopOrder(param);
    }

    @GetMapping("/huoQuZhiXinKeShi")
    public ResultVo<List<GetDropdownBox>> huoQuZhiXinKeShi(String code) {
        return server.huoQuZhiXinKeShi(code);
    }

    @PostMapping("/singleDataCheck")
    public ResultVo<Map<String, Object>> singleDataCheck(@RequestBody XinZhenYiZhu param) {
        return server.singleDataCheck(param);
    }

    @PostMapping("/insertTemplateOrder")
    public ResultVo<JSONObject> insertTemplateOrder(@RequestBody XinZhenYiZhu param) {
        return server.insertTemplateOrder(param);
    }

    @PostMapping("/saveTemplate")
    public ResultVo<String> saveTemplate(@RequestBody YzOrderPattern param) {
        return server.saveTemplate(param);
    }

    @GetMapping("/doesTheTemplateExist")
    public ResultVo<String> doesTheTemplateExist(@RequestParam("name") String name) {
        return server.doesTheTemplateExist(name);
    }

    @GetMapping("/huoQuYiZhuMuBan")
    public ResultVo<IPage<YzOrderPattern>> huoQuYiZhuMuBan(@RequestParam("code") String code,
                                                           @RequestParam("deptCode") String deptCode,
                                                           @RequestParam("muBanLeiXing") Integer muBanLeiXing,
                                                           @RequestParam("currentPage") long currentPage,
                                                           @RequestParam("total") long total) {
        return server.huoQuYiZhuMuBan(code, deptCode, muBanLeiXing, currentPage, total);
    }


    @GetMapping("/getDoctorSOrderTemplateMaxSortNo")
    public ResultVo<Integer> getDoctorSOrderTemplateMaxSortNo() {
        return server.getDoctorSOrderTemplateMaxSortNo();
    }

    @GetMapping("/huoQuMuBanShuJu")
    public ResultVo<List<YzActOrder>> huoQuMuBanShuJu(@RequestParam("code") String code) {
        return server.huoQuMuBanShuJu(code);
    }

    @DeleteMapping("/deleteADoctorSOrderTemplate")
    public ResultVo<String> deleteADoctorsOrderTemplate(@RequestParam("patternCode") String patternCode) {
        return server.deleteADoctorSOrderTemplate(patternCode);
    }

    @GetMapping("/collectDoctorSOrderTemplate")
    public ResultVo<String> collectDoctorsOrderTemplate(@RequestParam("patternCode") String patternCode) {
        return server.collectDoctorSOrderTemplate(patternCode);
    }

    @GetMapping("/muBanCaoZuo")
    public ResultVo<String> muBanCaoZuo(@RequestParam("patternCode") String patternCode,
                                        @RequestParam("patternName") String patternName,
                                        @RequestParam("deptCode") String deptCode,
                                        @RequestParam("sortNo") Integer sortNo,
                                        @RequestParam("flag") Integer flag) {
        return server.muBanCaoZuo(patternCode, patternName, deptCode, sortNo, flag);
    }

    @PostMapping("/saveTheThirdLevelDoctor")
    public ResultVo<String> saveTheThirdLevelDoctor(@RequestBody Overview param) {
        return server.saveTheThirdLevelDoctor(param);
    }

    @PostMapping("/associateOrders")
    public ResultVo<List<XinZhenYzActOrder>> associateOrders(@RequestBody XinZhenYiZhu param) {
        return server.associateOrders(param);
    }


    @GetMapping("/confirmTheDoctorSOrderWithMedicine")
    public ResultVo<Map<String, Object>> confirmTheDoctorSOrderWithMedicine(@RequestParam("patNo") String patNo,
                                                                            @RequestParam("times") Integer times) {
        return server.confirmTheDoctorSOrderWithMedicine(patNo, times);
    }

    @PostMapping("/copyOrder")
    public ResultVo<JSONObject> copyOrder(@RequestBody CopyOrder copyOrder) {
        return server.copyOrder(copyOrder);
    }

    @GetMapping("/queryFeeByOrderNo")
    public ResultVo<List<ZyDetailCharge>> queryFeeByOrderNo(@RequestParam("actOrderNo") BigDecimal actOrderNo) {
        return server.queryFeeByOrderNo(actOrderNo);
    }

    @PostMapping("/oneClickStopOrder")
    public ResultVo<String> oneClickStopOrder(@RequestBody OneClickOrder param) {
        return server.oneClickStopOrder(param);
    }

    @PostMapping("/doctorAuthorizationLogin")
    public ResultVo<DoctorAuthRest> doctorAuthorizationLogin(@RequestBody DoctorAuthParams userInfo) {
        return server.doctorAuthorizationLogin(userInfo);
    }

    @GetMapping("/updateOrderInstruction")
    public ResultVo<String> updateOrderInstruction(@RequestParam("orderNo") BigDecimal orderNo,
                                                   @RequestParam("str") String str) {
        return server.updateOrderInstruction(orderNo, str);
    }

    @PostMapping("/copyTableOrder")
    public ResultVo<JSONObject> copyTableOrder(@RequestBody XinZhenYiZhu param) {
        return server.copyTableOrder(param);
    }

    @GetMapping("/getItemInstructions")
    public ResultVo<List<JSONObject>> getItemInstructions(@RequestParam(value = "code", defaultValue = "") String code) {
        return ResultVoUtil.success(dao.getItemInstructions(code));
    }

    @PostMapping("/analyzeSiChargeLimit")
    public ResultVo<String> analyzeSiChargeLimit(@RequestBody SiLimitRequest request) {
        return server.analyzeSiChargeLimit(request);
    }

    @GetMapping("/confirmYzCheck")
    public ResultVo<String> confirmYzCheck(@RequestParam("patNo") String patNo,
                                           @RequestParam("times") Integer times,
                                           @RequestParam("ledgerSn") Integer ledgerSn) {
        return server.confirmYzCheck(patNo, times, ledgerSn);
    }

    @GetMapping("/getExceedingDischargeDays")
    public ResultVo<Integer> getExceedingDischargeDays() {
        return server.getExceedingDischargeDays();
    }


    @GetMapping("/getOrderCodeByName")
    public ResultVo<Set<String>> getOrderCodeByName(@RequestParam("name") String name) {
        return ResultVoUtil.success(dao.getOrderCodeByName(name));
    }


    @GetMapping("/getYzZdOrderItemConfirm")
    public ResultVo<Map<String, List<String>>> getYzZdOrderItemConfirm() {
        return ResultVoUtil.success(extraCache.getYzZdOrderItemMap());
    }

    @GetMapping("/getYaoPingJiLiang")
    public ResultVo<List<YaoPingJiLiang>> getYaoPingJiLiang(@RequestParam("code") String code,
                                                            @RequestParam("serial") String serial) {
        DrugCache.YpInfo yp = server.getYaoPingJiLiang(code, serial);
        return ResultVoUtil.success(yp.getJiLiang());
    }

    @GetMapping("/getYpZdGroup")
    @PassToken
    public ResultVo<Map<String, List<YpZdGroupNameVo>>> getYpZdGroup() {
        return R.ok(server.getYpZdGroup());
    }


}
