package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.PyWbUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.YpZdGroupName;
import cn.hnthyy.thmz.entity.his.zd.ZdUnitCode;
import cn.hnthyy.thmz.pageDto.YpZdGroupNamePageDto;
import cn.hnthyy.thmz.service.his.YpZdGroupNameService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import cn.hnthyy.thmz.vo.YpZdGroupNameVo;
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 YpZdGroupNameController {
    @Autowired
    private YpZdGroupNameService ypZdGroupNameService;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;

    /**
     * 分页查询药房库字典列表
     *
     * @param ypZdGroupNamePageDto 药房库查询条件
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/listYpZdGroupName", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> listYpZdGroupName(@RequestBody YpZdGroupNamePageDto ypZdGroupNamePageDto) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (ypZdGroupNamePageDto == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询药房库字典列表失败，参数为空");
                return resultMap;
            }
            YpZdGroupName ypZdGroupName = ypZdGroupNamePageDto.getYpZdGroupName();
            if (ypZdGroupName == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询药房库字典列表失败，参数为空");
                return resultMap;
            }
            if (StringUtils.isBlank(ypZdGroupNamePageDto.getOrderByCase())) {
                ypZdGroupNamePageDto.setOrderByCase("groupNo");
            }
            log.info("查询药房库字典，参数信息");
            int total = ypZdGroupNameService.countYpZdGroupName(ypZdGroupName);
            List<YpZdGroupNameVo> list = ypZdGroupNameService.queryYpZdGroupNamePage(ypZdGroupNamePageDto);
            for (YpZdGroupNameVo zdGroupNameVo : list) {//查询部门名称
                ZdUnitCode zdUnitCode = zdUnitCodeService.queryByCode(zdGroupNameVo.getDeptCode());
                zdGroupNameVo.setDeptName(zdUnitCode.getName());
            }
            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 = "/queryAllGroupName", method = {RequestMethod.GET})
    public Map<String, Object> queryAllGroupName() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<YpZdGroupName> list = ypZdGroupNameService.queryAllYpZdGroupName();
            resultMap.put("code", 0);
            resultMap.put("message", "查询药房库字典成功");
            resultMap.put("data", list);
            return resultMap;
        } catch (Exception e) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询药房库字典列表失败");
            return resultMap;
        }
    }

    /**
     * 保存药房库字典
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveGroupName", method = {RequestMethod.POST})
    public Map<String, Object> saveGroupName(@RequestBody YpZdGroupNameVo ypZdGroupNameVo, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (ypZdGroupNameVo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(ypZdGroupNameVo.getGroupNo())) {
                resultMap.put("code", -1);
                resultMap.put("message", "药房编号不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(ypZdGroupNameVo.getGroupName())) {
                resultMap.put("code", -1);
                resultMap.put("message", "药房名称不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(ypZdGroupNameVo.getDeptCode())) {
                resultMap.put("code", -1);
                resultMap.put("message", "部门不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(ypZdGroupNameVo.getMzFlag())) {
                ypZdGroupNameVo.setMzFlag("0");
            }
            if (StringUtils.isBlank(ypZdGroupNameVo.getYfFlag())) {
                ypZdGroupNameVo.setYfFlag("0");
            }
            ypZdGroupNameVo.setPyCode(PyWbUtil.toBigFirstChar(ypZdGroupNameVo.getGroupName()));
            ypZdGroupNameVo.setWbCode(PyWbUtil.getWBCode(ypZdGroupNameVo.getGroupName()));
            YpZdGroupName ypZdGroupName = ypZdGroupNameService.queryByGroupNo(ypZdGroupNameVo.getGroupNo());
            if (ypZdGroupName != null && ypZdGroupNameVo.getIsEdit() != 1) {
                resultMap.put("code", -1);
                resultMap.put("message", "该药房编码已存在，请勿重复添加（找到该药房，修改即可）");
                return resultMap;
            }
            ypZdGroupNameService.saveYpZdGroupName(ypZdGroupNameVo);
            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 = "/removeByGroupNo", method = {RequestMethod.GET})
    public Map<String, Object> removeByGroupNo(@RequestParam("groupNo") String groupNo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            ypZdGroupNameService.removeByGroupNo(groupNo);
            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;
        }
    }

}
