package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.*;
import cn.hnthyy.thmz.enums.ConfirmFlagEnum;
import cn.hnthyy.thmz.service.his.*;
import cn.hnthyy.thmz.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
public class YpMzFytjController {
    @Autowired
    private YpMzFytjService ypMzFytjService;
    @Autowired
    private YpZdGroupNameService ypZdGroupNameService;
    /**
     * 门诊医生查询发药记录单
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYfyjl", method = {RequestMethod.POST})
    public Map<String, Object> getYfyjl(@RequestBody ChargeFeeParamsVo chargeFeeParamsVo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (chargeFeeParamsVo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询门诊医生查询发药记录单失败,参数为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询门诊医生查询发药记录单成功");
            if(StringUtils.isBlank(chargeFeeParamsVo.getName())){
                chargeFeeParamsVo.setName(null);
            }else {
                chargeFeeParamsVo.setName("%"+chargeFeeParamsVo.getName()+"%");
            }
            if(StringUtils.isBlank(chargeFeeParamsVo.getSerialNo())){
                chargeFeeParamsVo.setSerialNo(null);
            }
            if(StringUtils.isBlank(chargeFeeParamsVo.getPatientId())){
                chargeFeeParamsVo.setPatientId(null);
            }
            chargeFeeParamsVo.setConfirmFlag(ConfirmFlagEnum.CONFIRM.code);
            List<YpMzFytj> ypMzFytjList = ypMzFytjService.queryYpMzFytj(chargeFeeParamsVo);
            if(ypMzFytjList!=null && ypMzFytjList.size()>0){
                for (YpMzFytj ypMzFytj:ypMzFytjList){
                    if(ypMzFytj==null || StringUtils.isBlank(ypMzFytj.getGroupNo())){
                        continue;
                    }
                    YpZdGroupName ypZdGroupName=ypZdGroupNameService.queryByGroupNo(ypMzFytj.getGroupNo());
                    if(ypZdGroupName==null){
                        ypMzFytj.setGroupNoOut(ypMzFytj.getGroupNo());
                        continue;
                    }
                    ypMzFytj.setGroupNoOut(ypZdGroupName.getGroupName());
                }
            }
            resultMap.put("data", ypMzFytjList);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询门诊医生查询发药记录单失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询门诊医生查询发药记录单失败");
            return resultMap;
        }
    }



    /**
     * 查询已经确认的药品记录明细
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpMx", method = {RequestMethod.POST})
    public Map<String, Object> getYpMx(@RequestBody ChargeFeeParamsVo chargeFeeParamsVo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (chargeFeeParamsVo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询已经确认的药品记录明细失败,参数为空");
                return resultMap;
            }
            if(StringUtils.isBlank(chargeFeeParamsVo.getPatientId())){
                resultMap.put("code", -1);
                resultMap.put("message", "门诊就诊号不能为空");
            }
            if(chargeFeeParamsVo.getTimes()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "门诊就诊次数不能为空");
            }
            if(StringUtils.isBlank(chargeFeeParamsVo.getOrderNo())){
                resultMap.put("code", -1);
                resultMap.put("message", "处方号不能为空");
            }
            if(StringUtils.isBlank(chargeFeeParamsVo.getReceiptNo())){
                resultMap.put("code", -1);
                resultMap.put("message", "门诊缴费次数不能为空");
            }
            if(StringUtils.isBlank(chargeFeeParamsVo.getGroupNoOut())){
                resultMap.put("code", -1);
                resultMap.put("message", "药房编码不能为空");
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询已经确认的药品记录明细成功");
            List<Map<String,Object>> ypMzFytjList = ypMzFytjService.queryYpMx(chargeFeeParamsVo);
            resultMap.put("data", ypMzFytjList);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询已经确认的药品记录明细失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询已经确认的药品记录明细失败");
            return resultMap;
        }
    }



    /**
     * 退药申请
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/refundMedicine", method = {RequestMethod.POST})
    public Map<String, Object> refundMedicine(@RequestBody ChargeFeeParamsVo chargeFeeParamsVo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (chargeFeeParamsVo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "退药申请失败,参数为空");
                return resultMap;
            }

            if(StringUtils.isBlank(chargeFeeParamsVo.getSerialNo())){
                resultMap.put("code", -1);
                resultMap.put("message", "流水号不能为空");
            }
            if(StringUtils.isBlank(chargeFeeParamsVo.getGroupNoOut())){
                resultMap.put("code", -1);
                resultMap.put("message", "药房编码不能为空");
            }
            if(StringUtils.isBlank(chargeFeeParamsVo.getOrderNo())){
                resultMap.put("code", -1);
                resultMap.put("message", "处方号不能为空");
            }
            if(chargeFeeParamsVo.getChargeItemCodeAndDecAmounts()==null || chargeFeeParamsVo.getChargeItemCodeAndDecAmounts().size()==0){
                resultMap.put("code", -1);
                resultMap.put("message", "退药记录不能为空");
            }
            resultMap.put("code", 0);
            resultMap.put("message", "退药申请成功");
            int num= ypMzFytjService.refundMedicine(chargeFeeParamsVo);
            resultMap.put("data", num);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("退药申请失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "退药申请失败");
            return resultMap;
        }
    }


}
