package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.YzActOrder;
import cn.hnthyy.thmz.entity.his.YzTemperature;
import cn.hnthyy.thmz.service.his.yz.YzActOrderService;
import cn.hnthyy.thmz.service.his.yz.YzTemperatureService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * his系统数据修复小工具
 */
@Slf4j
@RestController
public class DataUtilController {
    @Autowired
    private YzActOrderService yzActOrderService;

    @Autowired
    private YzTemperatureService yzTemperatureService;

    /**
     * 修改病人医嘱时间
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/setYzActOrderDate", method = {RequestMethod.POST})
    public Map<String, Object> setYzActOrderDate(@RequestBody YzActOrder yzActOrder) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (yzActOrder==null){
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(yzActOrder.getActOrderNos())){
                resultMap.put("code", -1);
                resultMap.put("message", "医嘱号数组不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(yzActOrder.getInpatientNo())){
                resultMap.put("code", -1);
                resultMap.put("message", "住院号不能为空");
                return resultMap;
            }
            String actOrderNos=yzActOrder.getActOrderNos().replaceAll("，",",");
            if(StringUtils.isBlank(actOrderNos)){
                resultMap.put("code", -1);
                resultMap.put("message", "医嘱号数组不能为空");
                return resultMap;
            }
            String [] arr=actOrderNos.split(",");
            if(arr==null || arr.length==0){
                resultMap.put("code", -1);
                resultMap.put("message", "医嘱号数组不能为空");
                return resultMap;
            }
            List<Float> actOrderNoList = new ArrayList<>();
            for (int i=0;i<arr.length;i++){
                Float actOrderNo = Float.valueOf(arr[i]);
                actOrderNoList.add(actOrderNo);
            }
            yzActOrder.setActOrderNoList(actOrderNoList);
            if(yzActOrder.getOrderTime()==null
                    && yzActOrder.getEnterTime()==null
                    && yzActOrder.getStartTime()==null
                    && yzActOrder.getEndTime()==null
                    && yzActOrder.getConfirmTime()==null
                    && yzActOrder.getStatusTime()==null
                    && yzActOrder.getPerformanceTime()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "您没有修改任何内容");
                return resultMap;
            }
            yzActOrderService.modifyYzActOrderTime(yzActOrder);
            resultMap.put("code", 0);
            resultMap.put("message", "修改病人医嘱时间成功");
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error("修改病人医嘱时间失败,错误信息{}",e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "修改病人医嘱时间失败");
            return resultMap;
        }
    }


    /**
     * 调整护理记录单病室
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/setTemperatureWard", method = {RequestMethod.POST})
    public Map<String, Object> setTemperatureWard(@RequestBody YzTemperature yzTemperature) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (yzTemperature==null){
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(yzTemperature.getWard())){
                resultMap.put("code", -1);
                resultMap.put("message", "病室不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(yzTemperature.getInpatientNo())){
                resultMap.put("code", -1);
                resultMap.put("message", "住院号不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(yzTemperature.getDetailNos())){
                resultMap.put("code", -1);
                resultMap.put("message", "护理记录单不能为空");
                return resultMap;
            }
            String detailNos=yzTemperature.getDetailNos().replaceAll("，",",");
            if(StringUtils.isBlank(detailNos)){
                resultMap.put("code", -1);
                resultMap.put("message", "护理记录单不能为空");
                return resultMap;
            }
            String [] arr=detailNos.split(",");
            if(arr==null || arr.length==0){
                resultMap.put("code", -1);
                resultMap.put("message", "护理记录单不能为空");
                return resultMap;
            }
            List<Integer> detailNoList = new ArrayList<>();
            for (int i=0;i<arr.length;i++){
                Integer detailNo = Integer.valueOf(arr[i]);
                detailNoList.add(detailNo);
            }
            yzTemperature.setDetailNoList(detailNoList);
            yzTemperatureService.modifyYzTemperature(yzTemperature);
            resultMap.put("code", 0);
            resultMap.put("message", "调整护理记录单病室成功");
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error("调整护理记录单病室失败,错误信息{}",e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "调整护理记录单病室失败");
            return resultMap;
        }
    }
}
