package com.glxp.mipsdl.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glxp.mipsdl.client.ClientProcessor;
import com.glxp.mipsdl.dao.auth.AuthWarehouseDao;
import com.glxp.mipsdl.dao.basic.BasicUdirelDao;
import com.glxp.mipsdl.entity.auth.AuthWarehouseEntity;
import com.glxp.mipsdl.entity.basic.BasicUdirelEntity;
import com.glxp.mipsdl.req.base.UdiwmsQueryUdiInvProductsRequest;
import com.glxp.mipsdl.req.base.forthird.ForBasicProductsSearchFilter;
import com.glxp.mipsdl.req.base.forthird.ForInvOutScanCodeFilter;
import com.glxp.mipsdl.req.base.forthird.ForInvProductsFilter;
import com.glxp.mipsdl.res.BaseResponse;
import com.glxp.mipsdl.service.auth.AuthWarehouseService;
import com.glxp.mipsdl.util.ResultVOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 南靖县医院--提供给第三方系统调用的接口
 */
@Slf4j
@RestController
public class ExNjxyyController {


    @Resource
    BasicUdirelDao basicUdirelDao;
    @Resource
    AuthWarehouseDao authWarehouseDao;

    /**
     * B001 查询指定科室高值/普耗库存
     *
     * @param forInvProductsFilter
     * @return
     */
    @PostMapping("/ex/udiwms/inv/products/filter")
    public BaseResponse invProductsFilter(@RequestBody ForInvProductsFilter forInvProductsFilter) {
        if (ObjectUtil.isNull(forInvProductsFilter)) {
            return ResultVOUtils.error(500, "查询条件不能为空");
        }
        if (StrUtil.isEmpty(forInvProductsFilter.getUdiCode()) &&
                StrUtil.isEmpty(forInvProductsFilter.getNameCode()) &&
                StrUtil.isEmpty(forInvProductsFilter.getCpmctymc()) &&
                StrUtil.isEmpty(forInvProductsFilter.getBatchNo()) &&
                StrUtil.isEmpty(forInvProductsFilter.getInvCode()) &&
                StrUtil.isEmpty(forInvProductsFilter.getSupName())) {
            return ResultVOUtils.error(500, "查询条件不能为空！");
        }
        if (forInvProductsFilter.getPage() == null) {
            forInvProductsFilter.setPage(1);
            forInvProductsFilter.setLimit(20);
        }
        ForInvProductsFilter newRequest = new ForInvProductsFilter();

        if (StrUtil.isEmpty(forInvProductsFilter.getInvCode()))
            return ResultVOUtils.error(500, "科室编码不能为空！");
        AuthWarehouseEntity authWarehouseEntity = authWarehouseDao.selectOne(new QueryWrapper<AuthWarehouseEntity>().eq("thirdId", forInvProductsFilter.getInvCode()).last("limit 1"));
        if (authWarehouseEntity == null)
            return ResultVOUtils.error(500, "科室编码未对照！");
        else
            forInvProductsFilter.setInvCode(authWarehouseEntity.getCode());

        if (StrUtil.isNotEmpty(forInvProductsFilter.getUdiCode())) {
            newRequest.setPage(forInvProductsFilter.getPage());
            newRequest.setLimit(forInvProductsFilter.getLimit());
            newRequest.setInvCode(forInvProductsFilter.getInvCode());
            newRequest.setUdiCode(forInvProductsFilter.getUdiCode());
            return ClientProcessor.getHttpClient().queryUdiHighInvProductsFilter(newRequest);
        } else {
            if (StrUtil.isEmpty(forInvProductsFilter.getXmbm())) {
                return ResultVOUtils.error(500, "项目编码不能为空！");
            }
            List<BasicUdirelEntity> basicUdirelEntityList = basicUdirelDao.selectList(new QueryWrapper<BasicUdirelEntity>().eq("mainId", forInvProductsFilter.getXmbm()));
            if (CollUtil.isNotEmpty(basicUdirelEntityList)) {
                newRequest.setRelIdFk(basicUdirelEntityList.get(0).getId() + "");
            } else {
                return ResultVOUtils.error(500, "项目编码未对照！");
            }
            newRequest.setPage(forInvProductsFilter.getPage());
            newRequest.setLimit(forInvProductsFilter.getLimit());
            newRequest.setBatchNo(forInvProductsFilter.getBatchNo());
            newRequest.setCpmctymc(forInvProductsFilter.getCpmctymc());
            newRequest.setInvCode(forInvProductsFilter.getInvCode());
            newRequest.setNameCode(forInvProductsFilter.getNameCode());
            newRequest.setSupName(forInvProductsFilter.getSupName());
//            newRequest.setUdiCode(forInvProductsFilter.getUdiCode());
            return ClientProcessor.getHttpClient().queryUdiInvProductsFilter(newRequest);
        }


    }


    /**
     * B002 查询耗材字典
     *
     * @param forBasicProductsSearchFilter
     * @return
     */
    @PostMapping("/ex/udiwms/basic/products/search")
    public BaseResponse basicProductsSearch(@RequestBody ForBasicProductsSearchFilter forBasicProductsSearchFilter) {
        if (ObjectUtil.isNull(forBasicProductsSearchFilter)) {
            return ResultVOUtils.error(500, "查询条件不能为空");
        }
        if (StrUtil.isEmpty(forBasicProductsSearchFilter.getUdiCode()) &&
                StrUtil.isEmpty(forBasicProductsSearchFilter.getNameCode()) &&
                StrUtil.isEmpty(forBasicProductsSearchFilter.getCpmctymc()) &&
                StrUtil.isEmpty(forBasicProductsSearchFilter.getMainId()) &&
                StrUtil.isEmpty(forBasicProductsSearchFilter.getGgxh())) {
            return ResultVOUtils.error(500, "查询条件不能为空！");
        }
        if (forBasicProductsSearchFilter.getPage() == null) {
            forBasicProductsSearchFilter.setPage(1);
            forBasicProductsSearchFilter.setLimit(20);
        }
        return ClientProcessor.getHttpClient().queryUdiBasicProductsSearch(forBasicProductsSearchFilter);
    }

    /**
     * B003 高值耗材实时出库/退库
     *
     * @param forInvOutScanCodeFilter
     * @return
     */
    @PostMapping("/ex/udiwms/inv/out/scanCode")
    public BaseResponse invOutScanCode(@RequestBody ForInvOutScanCodeFilter forInvOutScanCodeFilter) {
        return ClientProcessor.getHttpClient().queryUdiInvOutScanCode(forInvOutScanCodeFilter);
    }


}
