package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.JyZdItem;
import cn.hnthyy.thmz.entity.his.zd.MzZdYpYsh;
import cn.hnthyy.thmz.entity.his.zd.ZdChargeItem;
import cn.hnthyy.thmz.service.his.mz.JcJyItemChargeService;
import cn.hnthyy.thmz.service.his.zd.MzZdYpYshService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MzZdYpYshController {
    @Autowired
    private MzZdYpYshService mzZdYpYshService;
    @Autowired
    private JcJyItemChargeService jcJyItemChargeService;

    /**
     * 查询药品成功
     *
     * @param commonParams 药品通用查询参数
     * @param groupNo      药房编码
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpListByCommonParams", method = {RequestMethod.GET})
    public Map<String, Object> getYpListByCommonParams(@RequestParam("commonParams") String commonParams, @RequestParam("groupNo") String groupNo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(groupNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "药房编码不能为空");
                return resultMap;
            }
            List<MzZdYpYsh> mzZdYpYshes = mzZdYpYshService.queryMzZdYpYshByCommonParamsAndGroupNo(commonParams, groupNo);
            if (mzZdYpYshes == null || mzZdYpYshes.size() == 0) {
                resultMap.put("code", -1);
                resultMap.put("message", "未查询到符合条件的药品");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("data", mzZdYpYshes);
            resultMap.put("message", "查询药品成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询药品失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询药品失败");
            return resultMap;
        }
    }


    /**
     * 查询诊疗项目与医技
     *
     * @param commonParams 药品通用查询参数
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getJcJyItemByCommonParams", method = {RequestMethod.GET})
    public Map<String, Object> getJcJyItemByCommonParams(@RequestParam("commonParams") String commonParams) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            //检查与检验
            List<JyZdItem> jyZdItemList = jcJyItemChargeService.queryJcJyItemByCommonParams(commonParams);
//            //诊疗
//            List<JyZdItem> zlList = jcJyItemChargeService.queryZlByCommonParams(commonParams);
//            if (jyZdItemList == null || jyZdItemList.size() == 0) {
//                jyZdItemList=zlList;
//            }else {
//                jyZdItemList.addAll(zlList);
//            }
            if (jyZdItemList == null || jyZdItemList.size() == 0) {
                resultMap.put("code", -1);
                resultMap.put("message", "未查询到符合条件的诊疗与医技");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("data", jyZdItemList);
            resultMap.put("message", "查询诊疗项目与医技成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询诊疗项目与医技失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询诊疗项目与医技失败");
            return resultMap;
        }
    }



    /**
     * 查询诊疗与医技项目明细
     *
     * @param code 诊疗与医技项目明细
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getJcJyItemChargeByCode", method = {RequestMethod.GET})
    public Map<String, Object> getJcJyItemChargeByCode(@RequestParam("code") String code) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(code)){
                resultMap.put("code", -1);
                resultMap.put("message", "项目编码不能为空");
                return resultMap;
            }
            List<ZdChargeItem> zdChargeItemList = jcJyItemChargeService.queryJcJyItemChargeByCode(code);
            if (zdChargeItemList == null || zdChargeItemList.size() == 0) {
                resultMap.put("code", -1);
                resultMap.put("message", "未查询到符合条件的诊疗与医技项目明细");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("data", zdChargeItemList);
            BigDecimal totalAmount =BigDecimal.ZERO;
            for (ZdChargeItem zd:zdChargeItemList){
                totalAmount=totalAmount.add(zd.getTotalAmount()==null?BigDecimal.ZERO:zd.getTotalAmount());
            }
            List<JyZdItem> jyZdItemList = jcJyItemChargeService.queryJcJyItemByCommonParams(code);
            if(jyZdItemList!=null && jyZdItemList.size()>0){
                //设置套餐名称
                resultMap.put("name", jyZdItemList.get(0).getName());
            }
            resultMap.put("totalAmount", totalAmount);
            resultMap.put("message", "查询诊疗与医技项目明细成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询诊疗与医技项目明细失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询诊疗与医技项目明细失败");
            return resultMap;
        }
    }
}
