package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.YpZdDict;
import cn.hnthyy.thmz.entity.his.zd.MzZdYpYsh;
import cn.hnthyy.thmz.service.his.YpZdDictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class YpZdDictController {
    @Autowired
    private YpZdDictService ypZdDictService;

    /**
     * 根据药品编码和规格查询药品信息
     * @param code 药品编码
     * @param serial 药品规格
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpZdDictByCodeAndSerial", method = {RequestMethod.GET})
    public Map<String, Object> getYpZdDictByCodeAndSerial(@RequestParam("code") String code, @RequestParam("serial") String serial) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(code)) {
                resultMap.put("code", -1);
                resultMap.put("message", "药品编码不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(serial)) {
                resultMap.put("code", -1);
                resultMap.put("message", "药品拆零规格不能为空");
                return resultMap;
            }
            YpZdDict ypZdDict= ypZdDictService.queryYpZdDictByCodeAndSerial(code,serial);
            if (ypZdDict ==null) {
                resultMap.put("code", -1);
                resultMap.put("message", "未查询到符合条件的药品");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("data", ypZdDict);
            resultMap.put("message", "查询药品成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询药品失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询药品失败");
            return resultMap;
        }
    }
}
