package cn.hnthyy.thmz.controller.yf;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.YpInDetlYf;
import cn.hnthyy.thmz.entity.his.YpZdDict;
import cn.hnthyy.thmz.entity.his.YpZdManufactory;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.service.his.YpInDetlYfService;
import cn.hnthyy.thmz.service.his.YpZdDictService;
import cn.hnthyy.thmz.service.his.YpZdManufactoryService;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:入库记录查询
 * @author: WANGJIALIANG
 * @time: 2021/8/10 11:01
 */
@RestController
@Slf4j
public class YfIncomingRecordsController {

    @Autowired
    private YpZdDictService ypZdDictService;
    @Autowired
    private YpZdManufactoryService ypZdManufactoryService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;
    @Autowired
    private YpInDetlYfService ypInDetlYfService;

    /**
     * 查询药品信息
     * @param searchTextDrug
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYp",method = {RequestMethod.GET})
    public Map<String,Object> getYp(@RequestParam("searchTextDrug") String searchTextDrug){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(searchTextDrug)){
                resultMap.put("code", 0);
                resultMap.put("message", "查询药品成功");
                resultMap.put("data", "");
                return resultMap;
            }
            if(searchTextDrug.matches("[a-zA-Z]+")){
                searchTextDrug = searchTextDrug.toUpperCase();
            }
            List<YpZdDict> list = ypZdDictService.queryYpBySearchText(searchTextDrug);
            list.forEach(ypZdDict -> {
                ypZdDict.setManufactoryName(ypZdManufactoryService.queryYpZdManufactoryByCode(ypZdDict.getManuCode()));
            });
            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 = "/getYpZdManufactoryBySearchText", method = {RequestMethod.GET})
    public Map<String, Object> getYpZdManufactoryBySearchText(@RequestParam("searchText") String searchText) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(searchText)) {
                resultMap.put("code", 0);
                resultMap.put("message", "查询成功");
                return resultMap;
            }
            if(searchText.matches("[a-zA-Z]+")){
                searchText = searchText.toUpperCase();
            }
            List<YpZdManufactory> ypZdManufactories = ypZdManufactoryService.queryYpZdManufactoryBySearchText(searchText);
            resultMap.put("code", 0);
            resultMap.put("message", "查询制造厂商成功");
            resultMap.put("data", ypZdManufactories);
            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 = "/getEmployeeBySearchText", method = {RequestMethod.GET})
    public Map<String, Object> getEmployeeBySearchText(@RequestParam("searchText") String searchText) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(searchText)) {
                resultMap.put("code", 0);
                resultMap.put("message", "查询成功");
                return resultMap;
            }
            if(searchText.matches("[a-zA-Z]+")){
                searchText = searchText.toUpperCase();
            }
            List<Employee> employees = employeeService.queryEmployeeBySearchText(searchText);
            employees.forEach(employee -> {
                employee.setDeptName(zdUnitCodeService.queryDeptNameByIdInCache(employee.getDeptCode()));
            });
            resultMap.put("code", 0);
            resultMap.put("message", "查询医生信息成功");
            resultMap.put("data", employees);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询医生信息失败,错误信息：【" + e.getMessage() + "】");
            log.error("查询医生信息失败,错误信息：[]", e.getMessage());
            return resultMap;
        }
    }

    /**
     * 模糊查询药品入库记录
     * @param ypInDetlYf
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpInDetlYf", method = {RequestMethod.POST})
    public Map<String, Object> getYpInDetlYf(YpInDetlYf ypInDetlYf, Integer page, Integer rows) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (ypInDetlYf == null) {
                resultMap.put("code", 0);
                resultMap.put("message", "查询药品入库记录失败");
                return resultMap;
            }
            if(StringUtils.isBlank(ypInDetlYf.getChargeCode())){
                ypInDetlYf.setChargeCode(null);
            }
            if(StringUtils.isBlank(ypInDetlYf.getSerial())){
                ypInDetlYf.setSerial(null);
            }
            if(StringUtils.isBlank(ypInDetlYf.getManuCode())){
                ypInDetlYf.setManuCode(null);
            }
            if(StringUtils.isBlank(ypInDetlYf.getInputId())){
                ypInDetlYf.setInputId(null);
            }
            if(StringUtils.isBlank(ypInDetlYf.getPlanNo())){
                ypInDetlYf.setPlanNo(null);
            }
            if(StringUtils.isBlank(ypInDetlYf.getInDocuNo())){
                ypInDetlYf.setInDocuNo(null);
            }
            if(StringUtils.isBlank(ypInDetlYf.getInType())){
                ypInDetlYf.setInType(null);
            }
            List<YpInDetlYf> ypInDetlYfs = ypInDetlYfService.queryYpInDetlYfPage(ypInDetlYf,page,rows);
            int count = ypInDetlYfService.countYpInDetlYf(ypInDetlYf);
            ypInDetlYfs.forEach(yp -> {
                YpZdDict ypZdDict = ypZdDictService.queryYpZdDictByCodeAndSerial(yp.getChargeCode(), yp.getSerial());
                yp.setName(ypZdDict.getName());
                yp.setSpecification(ypZdDict.getSpecification());
                yp.setManufactoryName(ypZdManufactoryService.queryYpZdManufactoryByCode(yp.getManuCode()));
                Employee employee = employeeService.queryByUserCode(yp.getInputId());
                yp.setInputName(employee.getEmployeeName());
                switch (yp.getInType()){
                    case "0":
                        yp.setInTypeName("入库");
                        break;
                    case "1":
                        yp.setInTypeName("退货");
                        break;
                    case "2":
                        yp.setInTypeName("赠药");
                        break;
                    case "3":
                        yp.setInTypeName("紧急入库");
                        break;
                    case "9":
                        yp.setInTypeName("拆零");
                        break;
                }
            });
            resultMap.put("code", 0);
            resultMap.put("total", Math.ceil((double)count/rows));
            resultMap.put("page", page);
            resultMap.put("rows", count);
            resultMap.put("message", "查询药品入库记录成功");
            resultMap.put("data", ypInDetlYfs);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询药品入库记录失败,错误信息：【" + e.getMessage() + "】");
            log.error("查询药品入库记录失败,错误信息：[]", e.getMessage());
            return resultMap;
        }
    }
}
