package cn.hnthyy.thmz.controller.yf;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.pageDto.PageBean;
import cn.hnthyy.thmz.service.his.YpBaseYfService;
import cn.hnthyy.thmz.service.his.YpZdManufactoryService;
import cn.hnthyy.thmz.service.his.yf.YfInventoryListService;
import cn.hnthyy.thmz.vo.YpZdBaseYfVo;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:药房药品库存清单
 * @author: WANGJIALIANG
 * @time: 2021/8/16 15:38
 */
@RestController
@Slf4j
public class YfInventoryListController {

    @Autowired
    private YfInventoryListService yfInventoryListService;
    @Autowired
    private YpZdManufactoryService ypZdManufactoryService;
    @Autowired
    private  YpBaseYfService ypBaseYfService;

    /**
     * 分页查询药房库存清单
     * @param searchText
     * @param pageBean
     * @param groupNo
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpZdBaseYfPage",method = {RequestMethod.GET})
    public Map<String, Object> getYpZdBaseYfPage(@RequestParam("classCode") String classCode, @RequestParam("searchText") String searchText,
                                                 @RequestParam("groupNo") String groupNo,@RequestParam("isMultiple") Integer isMultiple,PageBean pageBean) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(!StringUtils.isBlank(searchText) && searchText.matches("[a-zA-Z]+")){
                searchText = searchText.toUpperCase();
            }
            if(StringUtils.isBlank(classCode)){
                classCode = null;
            }else{
                String reCode = new StringBuffer(classCode).reverse().toString();
                int i = Integer.parseInt(reCode);
                if (i < 100) {
                    classCode = classCode.substring(0, 2) + "%";//查询二级节点
                } else if (i < 10000) {
                    classCode = classCode.substring(0, 4) + "%";//查询三级节点
                }
            }
            List<YpZdBaseYfVo> ypZdBaseYfVos = new ArrayList<>();
            int total = 0;
            if(isMultiple == 0){
                ypZdBaseYfVos = yfInventoryListService.queryYpZdBaseYfPage(groupNo, classCode, searchText, pageBean);
                total = yfInventoryListService.countYpZdBaseYf(groupNo, classCode, searchText);
            }else if(isMultiple == 1){//多药房
                ypZdBaseYfVos = yfInventoryListService.queryYpZdBaseYfMultiplePage(classCode, searchText, pageBean);
                total = yfInventoryListService.countYpZdBaseYfMultiple(classCode, searchText);
            }
            ypZdBaseYfVos.forEach(ypZdBaseYfVo -> {
                String manufactoryName = ypZdManufactoryService.queryYpZdManufactoryByCode(ypZdBaseYfVo.getManuCode());
                ypZdBaseYfVo.setManufactoryName(manufactoryName);
            });
            resultMap.put("code", 0);
            resultMap.put("data", ypZdBaseYfVos);
            resultMap.put("total", total);
            resultMap.put("message", "查询药房库存清单成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询药品库存清单失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询药品库存清单失败,错误原因【" + e.getMessage() + "】");
            return resultMap;
        }
    }

    /**
     * 平虚库存
     * @param ypZdBaseYfVos
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/resetVirtual", method = {RequestMethod.POST})
    public Map<String, Object> resetVirtual(@RequestBody List<YpZdBaseYfVo> ypZdBaseYfVos) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (ypZdBaseYfVos == null && ypZdBaseYfVos.size() < 1) {
                resultMap.put("code", -1);
                resultMap.put("message", "平虚库存失败,参数为空");
                return resultMap;
            }
            for (YpZdBaseYfVo ypZdBaseYfVo:ypZdBaseYfVos) {
                int i = ypBaseYfService.modifyResetVirtual(ypZdBaseYfVo.getCode(), ypZdBaseYfVo.getSerial(), ypZdBaseYfVo.getGroupNo());
                if(i == 0){
                    resultMap.put("code", -1);
                    resultMap.put("message", "平虚库存失败");
                    return resultMap;
                }
            }
            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;
        }
    }
}
