package thyyxxk.webserver.service.executeItem;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.executeItem.YzChangeDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.executeItem.YzChangeData;
import thyyxxk.webserver.entity.executeItem.YzChangeParam;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.CommonUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 医嘱变更
 * @Author:lihong
 * @Date: 2023/11/6
 */
@Service
@Slf4j
public class YzChangeService {
    @Autowired
    private YzChangeDao dao;

    /**
     * @description:查询医嘱变更单数据
     * @author: lihong
     * @date: 2023/11/6 8:56
     * @param: param
     * @return: java.util.List<thyyxxk.webserver.entity.datamodify.YzActOrder>
     **/
    public List<YzChangeData> queryYzChange(YzChangeParam param) {
        List<YzChangeData>  result = new ArrayList<>();
        dealYzChangeParam(param);
        List<YzChangeData> list = dao.listYzActOrder(param);
        if (CollUtil.isNotEmpty(list)) {
            result.addAll(list);
        }
        if(YzChangeParam.ChangeClassEnum.All.getCode().equals(param.getChangeClass())){
            List<YzChangeData> list2 = dao.listYzEraseOrder(param);
            if (CollUtil.isNotEmpty(list2)) {
                result.addAll(list2);
            }
        }
        if(CollUtil.isNotEmpty(result)){
            CommonUtil.BeanTrim(result);
            List<YzChangeData> collect = result.stream().sorted(Comparator.comparing(o-> Convert.toInt(o.getBedNo(),0))).collect(Collectors.toList());
            return collect;
        }
        return result;
    }

    private void dealYzChangeParam(YzChangeParam param) {
        if(CollUtil.newArrayList(YzChangeParam.ChangeClassEnum.CQ_KF.getCode(),YzChangeParam.ChangeClassEnum.LS_KF.getCode()).contains(param.getChangeClass())){
            param.setTypeClass("1");
        }
        if(CollUtil.newArrayList(YzChangeParam.ChangeClassEnum.CQ_SY.getCode(),YzChangeParam.ChangeClassEnum.LS_SY.getCode()).contains(param.getChangeClass())){
            param.setTypeClass("2");
        }

        if(CollUtil.newArrayList(YzChangeParam.ChangeClassEnum.CQ_SY.getCode(),YzChangeParam.ChangeClassEnum.CQ.getCode(),YzChangeParam.ChangeClassEnum.CQ_KF.getCode(), YzChangeParam.ChangeClassEnum.CQ_FKF.getCode()).contains(param.getChangeClass())){
            param.setFrequCode(" != 'ONCE' ");
        }

        if(CollUtil.newArrayList(YzChangeParam.ChangeClassEnum.LS.getCode(),YzChangeParam.ChangeClassEnum.LS_SY.getCode(), YzChangeParam.ChangeClassEnum.LS_KF.getCode()).contains(param.getChangeClass())){
            param.setFrequCode(" = 'ONCE' ");
        }
    }

    /**
     * @description:确认变更医嘱
     * @author: lihong
     * @date: 2023/11/6 9:00
     * @param: param
     * @return: thyyxxk.webserver.entity.ResultVo<java.lang.String>
     **/
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<String> saveYzChange(YzChangeParam param) {
        AssertUtil.isnotBlank(param.getList(),"确认变更医嘱数据不能为空");
        Map<String, List<YzChangeData>> groupMap = param.getList().stream().collect(Collectors.groupingBy(o -> o.getOrderFlag()));
        for(String key : groupMap.keySet()){
            List<BigDecimal> actOrderNoList = groupMap.get(key).stream().map(o -> o.getActOrderNo()).collect(Collectors.toList());
            String sql = CollUtil.join(actOrderNoList,",");
            if(key.equals("0")){
                dao.updatePrintChange("yz_act_order", TokenUtil.getInstance().getTokenUserId(), sql);
            }else {
                dao.updatePrintChange("yz_erase_order", TokenUtil.getInstance().getTokenUserId(), sql);
            }
        }
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
    }
}
