package cn.hnthyy.thmz.controller.yf;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.*;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.entity.his.zd.YpZdDictBase;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.pageDto.PageBean;
import cn.hnthyy.thmz.service.his.*;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.service.his.yf.YfDrugApplyService;
import cn.hnthyy.thmz.vo.YpBaseYfBoundVo;
import cn.hnthyy.thmz.vo.YpInPlanDrugVo;
import cn.hnthyy.thmz.vo.YpInPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:药品请领
 * @author: WANGJIALIANG
 * @time: 2021/7/21 11:29
 */
@Slf4j
@RestController
public class YfDrugApplyController {

    @Autowired
    private YpInPlanService ypInPlanService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private YpZdGroupNameService ypZdGroupNameService;
    @Autowired
    private YpZdManufactoryService ypZdManufactoryService;
    @Autowired
    private YpBaseYfService ypBaseYfService;
    @Autowired
    private YpMzFytjService ypMzFytjService;
    @Autowired
    private YpZdDictService ypZdDictService;
    @Autowired
    private YpBaseService ypBaseService;
    @Autowired
    private YfDrugApplyService yfDrugApplyService;

    /**
     * 分页查询药品请购记录
     * @param ypInPlanVo
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpInPlanPage",method = {RequestMethod.POST})
    public Map<String,Object> getYpInPlanPage(@RequestBody YpInPlanVo ypInPlanVo){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (ypInPlanVo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询发药窗口字典列表失败，参数为空");
                return resultMap;
            }
            int total = ypInPlanService.countYpInPlan(ypInPlanVo);
            List<YpInPlanVo> list = ypInPlanService.queryYpInPlanPage(ypInPlanVo);
            list.forEach(ypInPlan ->{
                Employee inpEmployee = employeeService.queryByUserCode(ypInPlan.getDrawer());
                Employee confEmployee = employeeService.queryByUserCode(ypInPlan.getConfirmId());
                YpZdGroupName ypZdGroupName = ypZdGroupNameService.queryByGroupNo(ypInPlan.getGroupYk());
                YpZdGroupName groupName = ypZdGroupNameService.queryByGroupNo(ypInPlan.getGroupNo());
                if (inpEmployee != null) {
                    ypInPlan.setInputName(inpEmployee.getEmployeeName());
                }
                if (confEmployee != null) {
                    ypInPlan.setConfirmName(confEmployee.getEmployeeName());
                }
                if (ypZdGroupName != null) {
                    ypInPlan.setGroupName(ypZdGroupName.getGroupName());
                }
                if (groupName != null) {
                    ypInPlan.setGroupNamePharmacy(groupName.getGroupName());
                }
            });
            resultMap.put("code", 0);
            resultMap.put("message", "查询药品请领记录成功");
            resultMap.put("total", total);
            resultMap.put("data", list);
            return resultMap;
        } catch (Exception e) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询药品请领记录失败");
            return resultMap;
        }
    }

    /**
     * 查询药库药品信息
     * @param searchText
     * @param groupYk
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpZdDictBase",method = {RequestMethod.GET})
    public Map<String,Object> getYpZdDictBase(@RequestParam("searchText") String searchText,@RequestParam("groupYk") String groupYk){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(groupYk)){
                resultMap.put("code", 0);
                resultMap.put("message", "查询药品成功");
                resultMap.put("data", "");
                return resultMap;
            }
            if(searchText.matches("[a-zA-Z]+")){
                searchText = searchText.toUpperCase();
            }
            List<YpZdDictBase> list = ypInPlanService.queryYpZdDictBase(searchText, groupYk);
            list.forEach(ypZdDictBase -> {
                if(StringUtils.isNotBlank(ypZdDictBase.getManuCode())){
                    String manufactoryName = ypZdManufactoryService.queryYpZdManufactoryByCode(ypZdDictBase.getManuCode());
                    ypZdDictBase.setManufactoryName(manufactoryName);//生产厂家
                }
            });
            resultMap.put("code", 0);
            resultMap.put("message", "查询药品成功");
            resultMap.put("data", list);
            return resultMap;
        } catch (Exception e) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询药品失败");
            return resultMap;
        }
    }

    /**
     * 查询药品药房库存和用量统计
     * @param code
     * @param serial
     * @param groupNo 药房编码
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getStockAmountAndDosage",method = {RequestMethod.GET})
    public Map<String,Object> getStockAmountAndDosage(@RequestParam("code") String code,@RequestParam("serial") String serial,@RequestParam("groupNo") String groupNo){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(code)){
                resultMap.put("code", -1);
                resultMap.put("message", "查询药房库存和用量统计失败,药品编码为空");
                return resultMap;
            }
            if(StringUtils.isBlank(serial)){
                resultMap.put("code", -1);
                resultMap.put("message", "查询药房库存和用量统计失败,药品规格为空");
                return resultMap;
            }
            Map<String,Object> map = new HashMap<>();
            YpBaseYf ypBaseYf = ypBaseYfService.queryYpBaseYf(code, serial, groupNo);
            map.put("stockAmount",ypBaseYf.getStockAmount());
            int sevenDayDosage = ypMzFytjService.queryDrugUsageStatistics(code,serial,groupNo,7);
            map.put("sevenDayDosage",sevenDayDosage);
            resultMap.put("code", 0);
            resultMap.put("message", "查询药房库存和用量统计成功");
            resultMap.put("data", map);
            return resultMap;
        } catch (Exception e) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询药房库存和用量统计失败");
            return resultMap;
        }
    }

    /**
     * 保存药品请领
     * @param ypInPlans 药品集合
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveDrugApply",method = {RequestMethod.POST})
    public Map<String,Object> saveDrugApply(@RequestBody List<YpInPlan> ypInPlans, HttpServletRequest httpServletRequest){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            if(ypInPlans == null && ypInPlans.size() > 0){
                resultMap.put("code", -1);
                resultMap.put("message", "保存药品请领失败,药品为空");
                return resultMap;
            }
            ypInPlans.forEach(ypInPlan -> {
                ypInPlan.setDrawer(tokenUser.getUserIdCode());
                ypInPlan.setInputId(tokenUser.getUserIdCode());
                Date date = new Date();
                ypInPlan.setPlanDate(date);
                ypInPlan.setSysDate(date);
            });
            int i = ypInPlanService.saveYpInPlan(ypInPlans);
            if(i == 0){
                resultMap.put("code", -1);
                resultMap.put("message", "保存药品请领失败");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "保存药品请领成功");
            return resultMap;
        } catch (Exception e) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存药品请领失败");
            return resultMap;
        }
    }

    /**
     * 查询请领药品详细
     * @param drawNo 请领单
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpInPlanDrug",method = {RequestMethod.GET})
    public Map<String,Object> getYpInPlanDrug(@RequestParam("drawNo") String drawNo){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(drawNo)){
                resultMap.put("code", -1);
                resultMap.put("message", "查询请领单药品失败,请领单为空");
                return resultMap;
            }
            List<YpInPlanDrugVo> ypInPlanDrugVos = ypInPlanService.queryYpInPlanDrug(drawNo);
            ypInPlanDrugVos.forEach(ypInPlanDrugVo -> {
                YpBaseYf ypBaseYf = ypBaseYfService.queryYpBaseYf(ypInPlanDrugVo.getChargeCode(), ypInPlanDrugVo.getSerial(), ypInPlanDrugVo.getGroupNo());
                ypInPlanDrugVo.setPharmacyStockAmount(ypBaseYf.getStockAmount());
                Double s = ypBaseService.queryStockAmount(ypInPlanDrugVo.getChargeCode(), ypInPlanDrugVo.getSerial(), ypInPlanDrugVo.getGroupYk());
                ypInPlanDrugVo.setStockAmount(s);
                int sevenDayDosage = ypMzFytjService.queryDrugUsageStatistics(ypInPlanDrugVo.getChargeCode(), ypInPlanDrugVo.getSerial(), ypInPlanDrugVo.getGroupNo(),7);
                ypInPlanDrugVo.setSevenDayDosage(sevenDayDosage);
                YpZdDict ypZdDict = ypZdDictService.queryYpZdDictByCodeAndSerial(ypInPlanDrugVo.getChargeCode(), ypInPlanDrugVo.getSerial());
                ypInPlanDrugVo.setName(ypZdDict.getName());
                ypInPlanDrugVo.setPackRetprice(ypZdDict.getPackRetprice());
                ypInPlanDrugVo.setSpecification(ypZdDict.getSpecification());
                String manufactoryName = ypZdManufactoryService.queryYpZdManufactoryByCode(ypZdDict.getManuCode());
                ypInPlanDrugVo.setManufactoryName(manufactoryName);//生产厂家
            });
            resultMap.put("code", 0);
            resultMap.put("message", "查询请领单药品成功");
            resultMap.put("data", ypInPlanDrugVos);
            return resultMap;
        } catch (Exception e) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询请领单药品失败");
            return resultMap;
        }
    }

    /**
     * 查询药品集上下限
     * @param searchText
     * @param groupNo
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpBaseYfBoundPage", method = {RequestMethod.GET})
    public Map<String, Object> getYpBaseYfBoundPage(@RequestParam("searchText") String searchText,@RequestParam("groupNo") String groupNo,PageBean pageBean) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(groupNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询上下限失败,药房编码为空");
                return resultMap;
            }
            if(searchText.matches("[a-zA-Z]+")){
                searchText = searchText.toUpperCase();
            }
            List<YpBaseYfBoundVo> ypBaseYfBoundVos = yfDrugApplyService.queryYpBaseYfBoundPage(searchText, groupNo, pageBean);
            ypBaseYfBoundVos.forEach(ypBaseYfBoundVo -> {
                if(StringUtils.isNotBlank(ypBaseYfBoundVo.getManuCode())){
                    ypBaseYfBoundVo.setManufactoryName(ypZdManufactoryService.queryYpZdManufactoryByCode(ypBaseYfBoundVo.getManuCode()));
                }
            });
            resultMap.put("code", 0);
            resultMap.put("message", "查询上下限成功");
            resultMap.put("data", ypBaseYfBoundVos);
            resultMap.put("total", yfDrugApplyService.countYpBaseYfBound(searchText, groupNo));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询上下限失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询上下限失败");
            return resultMap;
        }
    }

    /**
     * 修改药品上下限
     * @param ypBaseYf
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/modifMaxMiniStockAmount", method = {RequestMethod.POST})
    public Map<String, Object> modifMaxMiniStockAmount(@RequestBody YpBaseYf ypBaseYf) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (ypBaseYf == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "修改上下限失败,参数为空");
                return resultMap;
            }
            int i = ypBaseYfService.modifyYpBaseYf(ypBaseYf);
            resultMap.put("code", 0);
            resultMap.put("message", "修改上下限成功");
            if(i == 0){
                resultMap.put("code", -1);
                resultMap.put("message", "修改上下限失败");
            }
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改上下限失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "修改上下限失败");
            return resultMap;
        }
    }
}
