package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.YpZdGroupName;
import cn.hnthyy.thmz.entity.his.mz.MzDrugWin;
import cn.hnthyy.thmz.pageDto.MzDrugWinPageDto;
import cn.hnthyy.thmz.service.his.YpZdGroupNameService;
import cn.hnthyy.thmz.service.his.mz.MzDrugWinService;
import cn.hnthyy.thmz.vo.MzDrugWinVo;
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.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MzDrugWinController {
    @Autowired
    private MzDrugWinService mzDrugWinService;
    @Autowired
    private YpZdGroupNameService ypZdGroupNameService;

    /**
     * 分页查询发药窗口字典列表
     *
     * @param mzDrugWinPageDto 发药窗口查询条件
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/listDrugWin", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> listDrugWin(@RequestBody MzDrugWinPageDto mzDrugWinPageDto) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (mzDrugWinPageDto == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询发药窗口字典列表失败，参数为空");
                return resultMap;
            }
            MzDrugWin mzDrugWin = mzDrugWinPageDto.getMzDrugWin();
            if (mzDrugWin == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询发药窗口字典列表失败，参数为空");
                return resultMap;
            }
            if (StringUtils.isBlank(mzDrugWinPageDto.getOrderByCase())) {
                mzDrugWinPageDto.setOrderByCase("winNo");
            }
            log.info("查询发药窗口字典，参数信息");
            int total = mzDrugWinService.countMzDrugWin(mzDrugWin);
            List<MzDrugWinVo> list = mzDrugWinService.queryMzDrugWinPage(mzDrugWinPageDto);
            for (MzDrugWinVo mzDrugWinVo : list) {//查询药房
                YpZdGroupName ypZdGroupName = ypZdGroupNameService.queryByGroupNo(mzDrugWinVo.getGroupNo());
                if (ypZdGroupName != null) {
                    mzDrugWinVo.setGroupName(ypZdGroupName.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;
        }
    }

    /**
     * 保存发药窗口字典
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveDrugWin", method = {RequestMethod.POST})
    public Map<String, Object> saveDrugWin(@RequestBody MzDrugWinVo mzDrugWinVo, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (mzDrugWinVo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(mzDrugWinVo.getWinNo())) {
                resultMap.put("code", -1);
                resultMap.put("message", "窗口编号不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(mzDrugWinVo.getWinName())) {
                resultMap.put("code", -1);
                resultMap.put("message", "发药窗口名称不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(mzDrugWinVo.getWinCate())) {
                resultMap.put("code", -1);
                resultMap.put("message", "窗口状态不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(mzDrugWinVo.getGroupNo())) {
                resultMap.put("code", -1);
                resultMap.put("message", "药房不能为空");
                return resultMap;
            }
            if (null == mzDrugWinVo.getPatientNum()) {
                mzDrugWinVo.setPatientNum(0);
            }
            MzDrugWin mzDrugWin = mzDrugWinService.queryMzDrugWinByGroupNoAndWinNo(mzDrugWinVo.getGroupNo(), mzDrugWinVo.getWinNo());
            if (mzDrugWin != null && mzDrugWinVo.getIsEdit() != 1) {
                resultMap.put("code", -1);
                resultMap.put("message", "该发药窗口已存在，请勿重复添加（找到该窗口，修改即可）");
                return resultMap;
            }
            mzDrugWinService.saveMzDrugWin(mzDrugWinVo);
            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;
        }
    }


    /**
     * 删除发药窗口
     *
     * @param groupNo
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/removeByGroupNoWinNo", method = {RequestMethod.GET})
    public Map<String, Object> removeByGroupNoWinNo(@RequestParam("groupNo") String groupNo, @RequestParam("winNo") String winNo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            mzDrugWinService.removeByGroupNoWinNo(groupNo, winNo);
            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;
        }
    }

}
