package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.JsonUtil;
import cn.hnthyy.thmz.Utils.MacAddressUtil;
import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.thmz.DispensingWindows;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.service.thmz.DispensingWindowsService;
import com.auth0.jwt.interfaces.DecodedJWT;
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 javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class DispensingWindowsController {
    @Autowired
    private DispensingWindowsService dispensingWindowsService;

    /**
     * 保存窗口号,药房，打印机
     * @param dispensingWindows
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveDispensingWindows", method = {RequestMethod.POST})
    public Map<String, Object> saveDispensingWindows(@RequestBody DispensingWindows dispensingWindows, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        if(dispensingWindows==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，参数为空");
            return resultMap;
        }
        if(dispensingWindows.getWindowsNo()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，窗口号为空");
            return resultMap;
        }
        if(dispensingWindows.getMzCfPrintIndex()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，门诊处方打印机为空");
            return resultMap;
        }
        if(dispensingWindows.getEkCfPrintIndex()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，儿科处方打印机为空");
            return resultMap;
        }
        if(dispensingWindows.getJzCfPrintIndex()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，急诊处方打印机为空");
            return resultMap;
        }
        if(dispensingWindows.getZsdPrintIndex()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，注射单打印机为空");
            return resultMap;
        }
        if(dispensingWindows.getBqPrintIndex()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，标签打印机为空");
            return resultMap;
        }
        if(dispensingWindows.getFskPrintIndex()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，发射科发药清单打印机为空");
            return resultMap;
        }
        if(dispensingWindows.getGroupNo()==null){
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败，药房为空");
            return resultMap;
        }
        String token = TokenUtil.getToken(httpServletRequest);
        if (StringUtils.isBlank(token)) {
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败,用户Token不存在");
            return resultMap;
        }
        DecodedJWT decodedJWT=TokenUtil.parseJWT(token);
        User tokenUser=(User)JsonUtil.jsontoObject(decodedJWT.getSubject(),User.class);
        try {
            dispensingWindows.setUserIdCode(tokenUser.getUserIdCode());
            dispensingWindows.setCreateDate(new Date());
            DispensingWindows fyWindows = dispensingWindowsService.queryLastDispensingWindowsByUserIdCode(tokenUser.getUserIdCode(), null);
            if(fyWindows!=null){
                dispensingWindows.setId(fyWindows.getId());
            }
            int num =dispensingWindowsService.saveDispensingWindows(dispensingWindows);
            if(num==1){
                resultMap.put("code", 0);
                resultMap.put("message", "保存发药员设置成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败");
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "保存发药员设置失败,错误信息：【"+e.getMessage()+"】");
            log.error("保存发药员设置失败,错误信息：[]",e.getMessage());
            return resultMap;
        }
    }


    /**
     * 查询用户的最后一个在使用中的发药员设置
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getLastDispensingWindowsByCurrentUser", method = {RequestMethod.GET})
    public Map<String, Object> getLastDispensingWindowsByCurrentUser(HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        String token = TokenUtil.getToken(httpServletRequest);
        if (StringUtils.isBlank(token)) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询发药员设置失败,用户Token不存在");
            return resultMap;
        }
        try {
            String ipAddress=MacAddressUtil.getIpAddr(httpServletRequest);
            DecodedJWT decodedJWT=TokenUtil.parseJWT(token);
            User tokenUser=(User)JsonUtil.jsontoObject(decodedJWT.getSubject(),User.class);
            DispensingWindows dispensingWindows=dispensingWindowsService.queryLastDispensingWindowsByUserIdCode(tokenUser.getUserIdCode(),ipAddress);
            if(dispensingWindows!=null){
                resultMap.put("code", 0);
                resultMap.put("message", "查询发药员设置成功");
                resultMap.put("data",dispensingWindows);
                return resultMap;
            }
            resultMap.put("code", -2);
            resultMap.put("message", "查询发药员设置失败");
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询发药员设置失败,错误信息：【"+e.getMessage()+"】");
            log.error("查询发药员设置失败,错误信息：[]",e.getMessage());
            return resultMap;
        }
    }

}
