package cn.hnthyy.thmz.controller.yf;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.*;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.mapper.his.YpBaseYfMapper;
import cn.hnthyy.thmz.service.his.*;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.vo.YpOutDetlVo;
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/3 10:57
 */
@Slf4j
@RestController
public class YfWarehouseController {
    @Autowired
    private YpOutDetlService ypOutDetlService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private YpZdGroupNameService ypZdGroupNameService;
    @Autowired
    private YpZdDictService ypZdDictService;
    @Autowired
    private YpBaseService ypBaseService;
    @Autowired
    private YpInDetlService ypInDetlService;
    @Autowired
    private YpBaseYfService ypBaseYfService;
    @Autowired
    private YpZdManufactoryService ypZdManufactoryService;
    @Autowired
    private YpBaseYfMapper ypBaseYfMapper;

    /**
     * 查询药库出库单
     * @param ypOutDetl
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpOutDetl",method = {RequestMethod.POST})
    public Map<String,Object> getYpOutDetl(@RequestBody YpOutDetl ypOutDetl){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (ypOutDetl == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询入库单列表失败，参数为空");
                return resultMap;
            }
            int total = ypOutDetlService.countYpOutDetl(ypOutDetl);
            List<YpOutDetl> list = ypOutDetlService.queryYpOutDetlPage(ypOutDetl);
            list.forEach(y ->{
                Employee inpEmployee = employeeService.queryByUserCode(y.getDrawer());
                Employee confEmployee = employeeService.queryByUserCode(y.getKeeper());
                YpZdGroupName ypZdGroupName = ypZdGroupNameService.queryByGroupNo(y.getGroupNo());
                YpZdGroupName groupName = ypZdGroupNameService.queryByGroupNo(y.getGroupNoOut());
                if (inpEmployee != null) {
                    y.setDrawerName(inpEmployee.getEmployeeName());
                }
                if (confEmployee != null) {
                    y.setKeeperName(confEmployee.getEmployeeName());
                }
                if (ypZdGroupName != null) {
                    y.setGroupName(ypZdGroupName.getGroupName());
                }
                if (groupName != null) {
                    y.setGroupNamePharmacy(groupName.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;
        }
    }

    /**
     * 查询药库出库单详细
     * @param drawNo 入库单号
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpOutDetlDetail",method = {RequestMethod.GET})
    public Map<String,Object> getYpOutDetlDetail(@RequestParam("drawNo") String drawNo){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(drawNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询入库单详细失败，单号为空");
                return resultMap;
            }
            List<YpOutDetlVo> list = ypOutDetlService.queryYpOutDetlDetail(drawNo);
            list.forEach(y ->{
                YpZdDict ypZdDict = ypZdDictService.queryYpZdDictByCodeAndSerial(y.getChargeCode(), y.getSerial());
                y.setName(ypZdDict.getName());
                y.setRetprice(ypZdDict.getRetprice());
                y.setPackSize(ypZdDict.getPackSize());
                y.setClFlag(ypZdDict.getClFlag());
                y.setSplitPercent(0.0);
                YpBase ypBase = ypBaseService.queryYpBase(y.getChargeCode(), y.getSerial(), y.getGroupNo());
                y.setSpecification(ypBase.getSpecification());
                y.setSpecification2(ypBase.getSpecification2());
                y.setSelfFlag(ypBase.getSelfFlag());
                y.setSupriceFlag(ypBase.getSupriceFlag());
                y.setSeparateFlag(ypBase.getSeparateFlag());
                y.setDrugFlag(ypBase.getDrugFlag());
                y.setMiniUnit(ypBase.getMiniUnit());
                YpInDetl ypInDetl = ypInDetlService.queryYpInDetl(y.getChargeCode(),y.getInSeri(),y.getInDrawNo() );
                y.setManuNo(ypInDetl.getManuNo());
                y.setManufactoryName(ypZdManufactoryService.queryYpZdManufactoryByCode(ypInDetl.getManuCode()));
                y.setManuCode(ypInDetl.getManuCode());
                YpZdGroupName ypZdGroupName = ypZdGroupNameService.queryByGroupNo(y.getGroupNo());
                if (ypZdGroupName != null) {
                    y.setGroupName(ypZdGroupName.getGroupName());
                }
            });
            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 ypOutDetlVos
     * @param httpServletRequest
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveYpInDetlYf",method = {RequestMethod.POST})
    public Map<String,Object> saveYpInDetlYf(@RequestBody List<YpOutDetlVo> ypOutDetlVos, HttpServletRequest httpServletRequest){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            if (ypOutDetlVos == null || ypOutDetlVos.size()==0) {
                resultMap.put("code", -1);
                resultMap.put("message", "入库失败，入库药品为空");
                return resultMap;
            }
            int i = ypBaseYfService.saveYpBaseYf(ypOutDetlVos, tokenUser);
            if(i == 0){
                resultMap.put("code", -1);
                resultMap.put("message", "入库失败");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "入库成功");
            return resultMap;
        } catch (Exception e) {
            resultMap.put("code", -1);
            resultMap.put("message", "入库失败");
            return resultMap;
        }
    }
}
