package cn.hnthyy.thmz.controller.yf;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.service.his.YpBaseYfService;
import cn.hnthyy.thmz.service.his.yf.YfDivideService;
import cn.hnthyy.thmz.vo.YpBaseYfVo;
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;

/**
 * @description:药品分装、组合
 * @author: WANGJIALIANG
 * @time: 2021/8/27 16:35
 */
@RestController
@Slf4j
public class YfDivideController {

    @Autowired
    private YpBaseYfService ypBaseYfService;
    @Autowired
    private YfDivideService yfDivideService;

    /**
     * 模糊查询药房药品信息
     * @param searchText
     * @param groupNo
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpBaseYfLike",method = {RequestMethod.GET})
    public Map<String,Object> getYpBaseYfLike(@RequestParam("searchText") String searchText, @RequestParam("groupNo") String groupNo){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(groupNo)){
                resultMap.put("code", 0);
                resultMap.put("message", "查询药品成功");
                resultMap.put("data", "");
                return resultMap;
            }
            if(searchText.matches("[a-zA-Z]+")){
                searchText = searchText.toUpperCase();
            }
            List<YpBaseYfVo> list = ypBaseYfService.queryYpBaseYfLike(searchText, groupNo);
            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;
        }
    }

    /**
     * 根据编码查询药房药品信息
     * @param chargeCode
     * @param groupNo
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpByChargeCodeAndGroupNo",method = {RequestMethod.GET})
    public Map<String,Object> getYpByChargeCodeAndGroupNo(@RequestParam("chargeCode") String chargeCode, @RequestParam("groupNo") String groupNo){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(groupNo)){
                resultMap.put("code", -1);
                resultMap.put("message", "查询药品失败，");
                return resultMap;
            }
            if(StringUtils.isBlank(groupNo)){
                resultMap.put("code", -1);
                resultMap.put("message", "查询药品失败，");
                return resultMap;
            }
            List<YpBaseYfVo> list = ypBaseYfService.queryYpByChargeCodeAndGroupNo(chargeCode, groupNo);
            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;
        }
    }

    /**
     * 药品分装
     * @param ypBaseYfVos
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveYfDivide",method = {RequestMethod.POST})
    public Map<String,Object> saveYfDivide(@RequestBody List<YpBaseYfVo> ypBaseYfVos, HttpServletRequest httpServletRequest){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            if(ypBaseYfVos.size() == 0 ){
                resultMap.put("code", -1);
                resultMap.put("message", "药品分装失败，参数为空");
                return resultMap;
            }
            yfDivideService.saveYfDivide(ypBaseYfVos,tokenUser.getUserIdCode());
            resultMap.put("code", 0);
            resultMap.put("message", "药品分装成功");
            return resultMap;
        } catch (Exception e) {
            resultMap.put("code", -1);
            resultMap.put("message", "药品分装失败");
            return resultMap;
        }
    }
}
