package com.yh.saas.plugin.yiliangyiyun.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsea.svc.base.base.entity.CommonRole;
import com.winsea.svc.base.base.entity.CommonStaff;
import com.winsea.svc.base.base.entity.CommonStaffRole;
import com.winsea.svc.base.base.service.ICommonRoleResourceService;
import com.winsea.svc.base.base.service.ICommonStaffRoleService;
import com.winsea.svc.base.base.service.ICommonStaffService;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.winsea.svc.base.workflow.entity.Workflow;
import com.winsea.svc.notice.entity.NoticeTaskInfo;
import com.winsea.svc.notice.entity.base.BaseNotice;
import com.winsea.svc.notice.service.INoticeService;
import com.winsea.svc.notice.service.INoticeTaskService;
import com.yh.saas.common.support.util.IdGenerator;
//import com.yh.saas.common.support.util.StringUtils;
import com.yh.saas.plugin.base.service.ICommonBillOperateHisService;
import com.yh.saas.plugin.yiliangyiyun.constant.NumberConstant;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.entity.view.WarehouseNumView;
import com.yh.saas.plugin.yiliangyiyun.exception.YException;
import com.yh.saas.plugin.yiliangyiyun.exception.YExceptionEnum;
import com.yh.saas.plugin.yiliangyiyun.mapper.InOutWarehouseTaskMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IContractManagementInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.IInOutWarehouseTaskService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.service.INewWorkflowService;
import com.yh.saas.plugin.yiliangyiyun.service.IWarehouseInOutInfoService;
import com.yh.saas.toolkit.workflow.service.IWorkflowService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 出入库任务 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-06-22
 */
@Service
public class InOutWarehouseTaskServiceImpl extends ServiceImpl<InOutWarehouseTaskMapper, InOutWarehouseTask> implements IInOutWarehouseTaskService {
    @Autowired
    private IInOutWarehouseTaskService inOutWarehouseTaskService;
    @Autowired
    private ICommonBillOperateHisService billOperateHisService;
    @Autowired
    private INewWorkflowService workflowService;
    @Autowired
    private IWarehouseInOutInfoService warehouseInOutInfoService;
    @Autowired
    private IContractManagementInfoService contractManagementInfoService;
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private INoticeTaskService noticeTaskService;
    @Autowired
    private ICommonStaffService staffService;
    @Autowired
    private ICommonStaffRoleService commonStaffRoleService;
    @Autowired
    private ICommonRoleResourceService roleResourceService;

    /**
     * 出入库任务新增 编辑
     *
     * @param inOutWarehouseTask
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertOrUpdateInOutTask(InOutWarehouseTask inOutWarehouseTask) {
        // 根据传参id判断是否是新增
        //  空是新增
        if (StringUtils.isEmpty(inOutWarehouseTask.getId())) {
            // 主键id
            inOutWarehouseTask.setId(IdGenerator.generateUUID());
            inOutWarehouseTask.setTaskStatusKey(StatusEnum.TASK_REVIEWED.getFlag());
            inOutWarehouseTask.setTaskStatus(StatusEnum.TASK_REVIEWED.getName());
            inOutWarehouseTask.setEstablishDate(new Date());
            //出库任务，入库任务，赋值关联id
            if ("1".equals(inOutWarehouseTask.getTaskTypeKey()) || "2".equals(inOutWarehouseTask.getTaskTypeKey())) {
                inOutWarehouseTask.setRelevanceId(inOutWarehouseTask.getId());
            }
            //新增移库出库任务时，获取发货库和出库量
            else if ("3".equals(inOutWarehouseTask.getTaskTypeKey()) && "1".equals(inOutWarehouseTask.getInOutFlag())) {
                inOutWarehouseTask.setSendWarehouse(inOutWarehouseTask.getWarehouseName());
                inOutWarehouseTask.setDeliveryVolume(inOutWarehouseTask.getWeight());
            }
            //新增移库入库任务时，获取收货库和入库量
            else if ("3".equals(inOutWarehouseTask.getTaskTypeKey()) && "2".equals(inOutWarehouseTask.getInOutFlag())) {
                inOutWarehouseTask.setReceiveWarehouse(inOutWarehouseTask.getWarehouseName());
                inOutWarehouseTask.setStorageVolume(inOutWarehouseTask.getWeight());
            }
            // 操作主表数据
            this.insert(inOutWarehouseTask);

            boolean isStartWorkflow = StringUtils.isBlank(inOutWarehouseTask.getWorkflowId());
            // 不是退回的单子
            if (isStartWorkflow) {
                Workflow workflow = workflowService
                        .findLatestWorkflowByBusinessCode("INOUTTASK-TASK-APPROVE");
                // 没配置审核流程，直接结束并处理信息
                if (workflow == null) {
                    throw new YException(YExceptionEnum.INTERVER_WORKFLOW_ERROR);
                }
                // 开启审核流
                else {

                    // 设置状态 已提交审核
                    inOutWarehouseTask.setWorkflowId(workflow.getId());
                    this.updateById(inOutWarehouseTask);
                    workflowService.startInstance(workflow.getId(), inOutWarehouseTask.getId());
                }
            }
            // 退回的单子 再启用
            else {

                this.updateById(inOutWarehouseTask);
                workflowService.activateInstance(inOutWarehouseTask.getWorkflowId(), inOutWarehouseTask.getId());
            }
        }
        //  有id是修改
        else {
            //编辑把状态改成待审核
            inOutWarehouseTask.setTaskStatusKey(StatusEnum.TASK_REVIEWED.getFlag());
            inOutWarehouseTask.setTaskStatus(StatusEnum.TASK_REVIEWED.getName());
            // 操作主表数据
            this.updateById(inOutWarehouseTask);
            boolean isStartWorkflow = StringUtils.isBlank(inOutWarehouseTask.getWorkflowId());
            // 不是退回的单子
            if (isStartWorkflow) {
                Workflow workflow = workflowService
                        .findLatestWorkflowByBusinessCode("INOUTTASK-TASK-APPROVE");
                // 没配置审核流程，直接结束并处理信息
                if (workflow == null) {
                    throw new YException(YExceptionEnum.INTERVER_WORKFLOW_ERROR);
                }
                // 开启审核流
                else {

                    // 设置状态 已提交审核
                    inOutWarehouseTask.setWorkflowId(workflow.getId());
                    this.updateById(inOutWarehouseTask);
                    workflowService.startInstance(workflow.getId(), inOutWarehouseTask.getId());
                }
            }
            // 退回的单子 再启用
            else {

                this.updateById(inOutWarehouseTask);
                workflowService.activateInstance(inOutWarehouseTask.getWorkflowId(), inOutWarehouseTask.getId());
            }
            List<String> roleIdList = new ArrayList<>();
            roleIdList = roleResourceService.getBindRoleIdsByResourceIds(AuthSecurityUtils.getCurrentUserInfo().getCompId(),
                    Lists.newArrayList("warehouseInfoTask-Audit"));
            // 先删除任务
            noticeService.removeNotice(BaseNotice.NoticeStatus.TASK, "INOUTTASK-TASK-APPROVE",
                    inOutWarehouseTask.getId(), null);
            String staffName = "XXX";
            CommonStaff commitUserInfo = staffService.getInfo(AuthSecurityUtils.getCurrentUserId());
            if (commitUserInfo != null) {
                staffName = commitUserInfo.getMajorRole() != null
                        ? commitUserInfo.getMajorRole().getRoleName() + " " + commitUserInfo.getStaffName()
                        : commitUserInfo.getStaffName();
            }
            List<CommonStaffRole> commonStaffRoles = commonStaffRoleService.findStaff(roleIdList);
            List<String> staffIds = commonStaffRoles.stream().map(CommonStaffRole::getStaffId).collect(Collectors.toList());
            String taskContent = "";
            // 向下一步操作人发送任务通知
            taskContent =  staffName + "提交的出入库任务需要您审核，请及时处理。";
            NoticeTaskInfo taskInfo = new NoticeTaskInfo();
            taskInfo.setCompId(commitUserInfo.getCompId());
            taskInfo.setAcceptType(BaseNotice.AcceptStatus.STAFF);
            taskInfo.setBusinessId(inOutWarehouseTask.getId());
            taskInfo.setBusinessType("WAREHOUSE");
            taskInfo.setMessageContent(taskContent);
            taskInfo.setBusinessCode("INOUTTASK-TASK-APPROVE");
//            taskInfo.setNoticeTaskStatus(NoticeTaskInfo.NoticeTaskStatus.RETURN);
            noticeTaskService.createNoticeTask(staffIds,
                    null, taskInfo);
        }
        return inOutWarehouseTask.getId();
    }

    /**
     * 判断移库编号
     *
     * @param moveTaskNo
     * @return
     */
    @Override
    public boolean selectTaskNo(String moveTaskNo) {
        // 根据出入出入库编号查询数据
        InOutWarehouseTask inOutWarehouseTask = this.selectOne(new EntityWrapper<InOutWarehouseTask>().eq(InOutWarehouseTask.QueryFiles.MOVE_TASK_NO, moveTaskNo)
                .eq(InOutWarehouseTask.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        //如果没有可以使用
        if (inOutWarehouseTask == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 出入库任务列表
     *
     * @param inOutWarehouseTask
     * @return
     */
    @Override
    public Page<InOutWarehouseTask> selectInOutWarehouseTask(InOutWarehouseTask inOutWarehouseTask) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (inOutWarehouseTask.getCurrentPage() - 1)
                * inOutWarehouseTask.getPageSize());
        //公司id
        pageView.put("compId", inOutWarehouseTask.getCompId());
        pageView.put("searchKeyWord", inOutWarehouseTask.getSearchKeyWord());
        pageView.put("searchType", inOutWarehouseTask.getSearchType());
        pageView.put("inOutType", inOutWarehouseTask.getInOutType());
        pageView.put("pageSize", inOutWarehouseTask.getPageSize());
        pageView.put("currentPage", inOutWarehouseTask.getCurrentPage());
        // 查询出入库任务总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<InOutWarehouseTask> dataList = baseMapper.getListByCondition(pageView);
        if (CollectionUtils.isNotEmpty(dataList)) {
//            dataList.forEach(inOutWarehouseTask1 -> {
            for (InOutWarehouseTask inOutWarehouseTask1 : dataList) {
                String taskId = "";
                // 只有待审核状态才有taskId
                if (StringUtils.isNotBlank(inOutWarehouseTask1.getWorkflowId())) {
                    JSONObject jsonObject = workflowService.getActiveTask(Lists.newArrayList(inOutWarehouseTask1.getWorkflowId()), inOutWarehouseTask1.getId());
                    taskId = jsonObject.getString("taskId");
                    inOutWarehouseTask1.setTaskId(taskId);
                }
            }
        }
        Page<InOutWarehouseTask> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(inOutWarehouseTask.getCurrentPage());
        page.setSize(inOutWarehouseTask.getPageSize());
        return page;
    }

    /**
     * 查看出入库任务
     *
     * @param relevanceId
     * @return
     */
    @Override
    public List<InOutWarehouseTask> getInOutWarehouseTask(String relevanceId) {
        List<InOutWarehouseTask> inOutWarehouseTaskList = inOutWarehouseTaskService.selectList(new EntityWrapper<InOutWarehouseTask>()
                .eq(InOutWarehouseTask.QueryFiles.relevanceId, relevanceId).eq(InOutWarehouseTask.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0)
        .orderBy("in_out_flag"));
        if (CollectionUtils.isNotEmpty(inOutWarehouseTaskList)) {
            for (InOutWarehouseTask inOutWarehouseTask1 : inOutWarehouseTaskList) {
                String taskId = "";
                // 只有待审核状态才有taskId
                if (StringUtils.isNotBlank(inOutWarehouseTask1.getWorkflowId())) {
                    JSONObject jsonObject = workflowService.getActiveTask(Lists.newArrayList(inOutWarehouseTask1.getWorkflowId()), inOutWarehouseTask1.getId());
                    taskId = jsonObject.getString("taskId");
                    inOutWarehouseTask1.setTaskId(taskId);
                }
            }
        }

        return inOutWarehouseTaskList;
    }

    /**
     * 删除出入库任务
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInOutWarehouseTask(String id, String relevanceId) {
        //查询出入库任务
        InOutWarehouseTask inOutWarehouseTask = this.selectById(id);
        if (inOutWarehouseTask != null) {
            //出库任务，入库任务,移库先删入库
            if ("1".equals(inOutWarehouseTask.getTaskTypeKey()) || "2".equals(inOutWarehouseTask.getTaskTypeKey()) ||
                    ("3".equals(inOutWarehouseTask.getTaskTypeKey()) && "2".equals(inOutWarehouseTask.getInOutFlag()))) {
                inOutWarehouseTaskService.deleteById(inOutWarehouseTask.getId());
            }
            //移库先删出库，退库并出库
            else if (("3".equals(inOutWarehouseTask.getTaskTypeKey()) && "1".equals(inOutWarehouseTask.getInOutFlag())) || "4".equals(inOutWarehouseTask.getTaskTypeKey())) {
                List<InOutWarehouseTask> inOutWarehouseTaskList = inOutWarehouseTaskService.selectList(new EntityWrapper<InOutWarehouseTask>()
                        .eq(InOutWarehouseTask.QueryFiles.relevanceId, relevanceId).eq(InOutWarehouseTask.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
                if (!CollectionUtils.isEmpty(inOutWarehouseTaskList)) {
                    for (InOutWarehouseTask inOutWarehouseTask1 : inOutWarehouseTaskList) {
                        inOutWarehouseTaskService.deleteById(inOutWarehouseTask1.getId());
                    }
                }
            }
        }
    }

    /**
     * 更改状态
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editStatus(String id) {
        //查询出入库任务
        InOutWarehouseTask inOutWarehouseTask = this.selectById(id);
        //更改状态
        if (inOutWarehouseTask != null) {
            if (StatusEnum.TASK_DO.getFlag().equals(inOutWarehouseTask.getTaskStatusKey())) {
                inOutWarehouseTask.setTaskStatusKey(StatusEnum.TASK_COMPLETED.getFlag());
                inOutWarehouseTask.setTaskStatus(StatusEnum.TASK_COMPLETED.getName());
            } else {
                inOutWarehouseTask.setTaskStatusKey(StatusEnum.TASK_DO.getFlag());
                inOutWarehouseTask.setTaskStatus(StatusEnum.TASK_DO.getName());
            }
            //更改运输任务信息
            this.updateById(inOutWarehouseTask);
            // 插入操作历史
            String staffName = this.billOperateHisService.getStaffAndName();
            // 插入操作历史
            this.billOperateHisService.saveBillOperateHis(inOutWarehouseTask.getId(), NumberConstant.CONSTANT_TRAN, staffName, null,
                    inOutWarehouseTask.getTaskStatus(), null, "");
            return "OK";
        }
        return "NG";
    }

    /**
     * 移库任务列表
     *
     * @param inOutWarehouseTask
     * @return
     */
    @Override
    public Page<InOutWarehouseTask> selectInOutWarehouseTaskRelocation(InOutWarehouseTask inOutWarehouseTask) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (inOutWarehouseTask.getCurrentPage() - 1)
                * inOutWarehouseTask.getPageSize());
        //公司id
        pageView.put("compId", inOutWarehouseTask.getCompId());
        pageView.put("searchKeyWord", inOutWarehouseTask.getSearchKeyWord());
        pageView.put("searchType", inOutWarehouseTask.getSearchType());
        pageView.put("pageSize", inOutWarehouseTask.getPageSize());
        pageView.put("currentPage", inOutWarehouseTask.getCurrentPage());

        List<InOutWarehouseTask> dataList = this.baseMapper.getMoveListByCondition(pageView);

        Integer dataCount = this.baseMapper.getMoveCountByCondition(pageView);


        if (!CollectionUtils.isEmpty(dataList)) {
            for (InOutWarehouseTask inOutWarehouseTaska : dataList) {
                //定义入库
                Float inNetWeight = 0.0f;
                // 查询入库数量
                List<WarehouseInOutInfo> warehouseInOutInfoAllList = warehouseInOutInfoService.selectList(new EntityWrapper<WarehouseInOutInfo>()
                        .eq("contract_no", inOutWarehouseTaska.getMoveTaskNo()).eq("in_out_flag", "2")
                        .eq("status_flag", "3"));
                if (!CollectionUtils.isEmpty(warehouseInOutInfoAllList)) {
                    Float netWeight = 0f;
                    for (WarehouseInOutInfo warehouse : warehouseInOutInfoAllList) {
                        //潮粮累计纯重，干粮累计净重
                        netWeight = netWeight + (warehouse.getPureWeight()!=null?warehouse.getPureWeight():Float.valueOf(String.valueOf(warehouse.getNetWeight())));
                    }
                    inNetWeight = inNetWeight + netWeight;
                    inOutWarehouseTaska.setStorageVolume(inNetWeight);
                }
                //定义出库
                Float outNetWeight = 0.0f;
                // 查询出库数量
                List<WarehouseInOutInfo> warehouseInOutInfoAllList1 = warehouseInOutInfoService.selectList(new EntityWrapper<WarehouseInOutInfo>()
                        .eq("status_flag", "3").eq("contract_no", inOutWarehouseTaska.getMoveTaskNo()).eq("in_out_flag", "1"));
                if (!CollectionUtils.isEmpty(warehouseInOutInfoAllList1)) {
                    Float netWeight = 0f;
                    for (WarehouseInOutInfo warehouse : warehouseInOutInfoAllList1) {
                        //潮粮累计纯重，干粮累计净重
                        netWeight = netWeight + (warehouse.getPureWeight()!=null?warehouse.getPureWeight():Float.valueOf(String.valueOf(warehouse.getNetWeight())));
                    }
                    outNetWeight = outNetWeight + netWeight;
                    inOutWarehouseTaska.setDeliveryVolume(outNetWeight);
                }
                //定义累计退库量
                Float stockReturnCount = 0.0f;
                // 查询退库量
                List<WarehouseInOutInfo> warehouseInOutInfoAllList2 = warehouseInOutInfoService.selectList(new EntityWrapper<WarehouseInOutInfo>()
                        .eq("status_flag", "3").eq("contract_no", inOutWarehouseTaska.getMoveTaskNo()).eq("in_out_type", "退库"));
                if (!CollectionUtils.isEmpty(warehouseInOutInfoAllList2)) {
                    Float netWeight = 0f;
                    for (WarehouseInOutInfo warehouse : warehouseInOutInfoAllList2) {
                        //潮粮累计纯重，干粮累计净重
                        netWeight = netWeight + (warehouse.getPureWeight()!=null?warehouse.getPureWeight():Float.valueOf(String.valueOf(warehouse.getNetWeight())));
                    }
                    stockReturnCount = stockReturnCount + netWeight;
                    inOutWarehouseTaska.setStockReturnCount(stockReturnCount);
                }
//                //定义累计退库量
//                String stockReturnCount = "0";
//
//                //查询退库量
//                List<InOutWarehouseTask> inOutWarehouseTaskList = inOutWarehouseTaskService.selectList(new EntityWrapper<InOutWarehouseTask>()
//                        .eq("contract_no", inOutWarehouseTaska.getMoveTaskNo())
//                        .eq("in_out_type_key", "6")
//                        .eq("delete_flag", "0"));
//                if (!CollectionUtils.isEmpty(inOutWarehouseTaskList)) {
//                    for (InOutWarehouseTask inoutwarehouse : inOutWarehouseTaskList) {
//                        if (StringUtils.isEmpty(stockReturnCount)) {
//                            stockReturnCount = "0";
//                        }
//                        stockReturnCount = String.valueOf(Float.valueOf(stockReturnCount) + inoutwarehouse.getWeight());
//                    }
//                }
//                inOutWarehouseTaska.setInOutWarehouseTaskList(inOutWarehouseTaskList);
//                inOutWarehouseTaska.setStockReturnCount(stockReturnCount);
            }
        }

        Page<InOutWarehouseTask> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(inOutWarehouseTask.getCurrentPage());
        page.setSize(inOutWarehouseTask.getPageSize());
        return page;
    }

    /**
     * 查看移库任务
     *
     * @param moveTaskNo
     * @return
     */
    @Override
    public List<InOutWarehouseTask> getInOutWarehouseTaskRelocation(String moveTaskNo) {
        List<InOutWarehouseTask> inOutWarehouseTaskList = inOutWarehouseTaskService.selectList(new EntityWrapper<InOutWarehouseTask>()
                .eq("move_task_no", moveTaskNo));
        if (!CollectionUtils.isEmpty(inOutWarehouseTaskList)) {
            for (InOutWarehouseTask inOutWarehouseTask : inOutWarehouseTaskList) {
                //定义累计退库量
                Float stockReturnCount = 0f;
                //查询退库量
                List<InOutWarehouseTask> inOutWarehouseTaskList1 = inOutWarehouseTaskService.selectList(new EntityWrapper<InOutWarehouseTask>()
                        .eq("in_out_type_key", "6")
                        .eq("contract_no", inOutWarehouseTask.getMoveTaskNo())
                        .eq("delete_flag", "0"));
                if (!CollectionUtils.isEmpty(inOutWarehouseTaskList1)) {
                    for (InOutWarehouseTask inoutwarehouse : inOutWarehouseTaskList1) {
                        stockReturnCount = stockReturnCount + inoutwarehouse.getWeight();
                    }
                }
                inOutWarehouseTask.setInOutWarehouseTaskList(inOutWarehouseTaskList1);
                inOutWarehouseTask.setStockReturnCount(stockReturnCount);
            }
        }
        return inOutWarehouseTaskList;
    }

    /**
     * 出入库任务审核 编辑
     *
     * @param inOutWarehouseTask
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String checkOrUpdateInOutTask(InOutWarehouseTask inOutWarehouseTask) {
        this.updateById(inOutWarehouseTask);
        return inOutWarehouseTask.getId();
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @Override
    public InOutWarehouseTask getTask(String id) {
        InOutWarehouseTask inOutWarehouseTask=this.selectById(id);
        return inOutWarehouseTask;
    }

    @Override
    public Page<InOutWarehouseTask> selectWarehouseTask(InOutWarehouseTask inOutWarehouseTask) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (inOutWarehouseTask.getCurrentPage() - 1)
                * inOutWarehouseTask.getPageSize());
        //公司id
        pageView.put("compId", inOutWarehouseTask.getCompId());
        pageView.put("searchKeyWord", inOutWarehouseTask.getSearchKeyWord());
        pageView.put("searchType", inOutWarehouseTask.getSearchType());
        pageView.put("pageSize", inOutWarehouseTask.getPageSize());
        pageView.put("currentPage", inOutWarehouseTask.getCurrentPage());
        Integer dataCount=0;
        List<InOutWarehouseTask> dataList=new ArrayList<>();
        String[] strings = inOutWarehouseTask.getStringList().split(",");
        for (int i=0;i<strings.length;i++){
            pageView.put("warehouseName",strings[i]);
            // 查询出入库任务总数
            dataCount =dataCount+baseMapper.getCountByCondition(pageView);
            dataList.addAll(baseMapper.getListByCondition(pageView));
        }
        Page<InOutWarehouseTask> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(inOutWarehouseTask.getCurrentPage());
        page.setSize(inOutWarehouseTask.getPageSize());
        return page;
    }

    /**
     * 查出入库任务编号
     * @param inOutWarehouseTask
     * @return
     */
    @Override
    public List<InOutWarehouseTask> selectInOutWarehouseNo(InOutWarehouseTask inOutWarehouseTask) {
        List<InOutWarehouseTask> inOutWarehouseTaskList=new ArrayList<>();
        //出库执行中的编号
        if ("1".equals(inOutWarehouseTask.getFlag())) {
            List<InOutWarehouseTask> inOutWarehouseTaskList1 = this.selectList(new EntityWrapper<InOutWarehouseTask>()
                    .eq("comp_id", AuthSecurityUtils.getCurrentUserInfo().getCompId())
                    .eq("task_status_key", "5")
                    .eq("warehouse_name", inOutWarehouseTask.getWarehouseName())
                    .ne("in_out_type","采购入库")
                    .ne("in_out_type","退库")
                    .ne("in_out_type","移库入库")
                    .orderBy("update_date", false));
            inOutWarehouseTaskList=inOutWarehouseTaskList1;
        }
        //入库
        else {
            List<InOutWarehouseTask> inOutWarehouseTaskList1 = this.selectList(new EntityWrapper<InOutWarehouseTask>()
                    .eq("comp_id", AuthSecurityUtils.getCurrentUserInfo().getCompId())
                    .eq("task_status_key", "5")
                    .eq("warehouse_name", inOutWarehouseTask.getWarehouseName())
                    .ne("in_out_type","销售出库")
                    .ne("in_out_type","移库出库")
                    .orderBy("update_date", false));
            inOutWarehouseTaskList=inOutWarehouseTaskList1;
        }
        return inOutWarehouseTaskList;
    }

    /**
     *
     * @param compId,warehouseName
     * @return
     */
    @Override
    public InOutWarehouseTask getContractNo(String compId,String warehouseName){
        InOutWarehouseTask inOutWarehouseTask = new InOutWarehouseTask();
        List<InOutWarehouseTask> stringList = inOutWarehouseTaskService.selectList(new EntityWrapper<InOutWarehouseTask>()
                .eq("comp_id",compId)
                .eq("receive_warehouse",warehouseName)
                .eq("task_status_key","5")
                .eq("in_out_type_key","3")
                .eq("in_out_flag","2")
                .eq("delete_flag","0"));
        if(CollectionUtils.isNotEmpty(stringList)){
            List<ContractManagementInfo> contractManagementInfoList=new ArrayList<>();
            for(InOutWarehouseTask inOutWarehouseTask1 : stringList){
                ContractManagementInfo contractManagementInfo = contractManagementInfoService.selectOne(new EntityWrapper<ContractManagementInfo>()
                        .eq("contract_no",inOutWarehouseTask1.getSendWarehouse())
                        .eq("acquisition_in","1")
                        .eq("delete_flag", "0")
                );
                if (contractManagementInfo!=null){
                    contractManagementInfoList.add(contractManagementInfo);
                }
            }
            inOutWarehouseTask.setContractManagementInfoList(contractManagementInfoList);
        }
        return inOutWarehouseTask;
    }
}
