TaskDetailServiceImpl.java 19.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
package com.huaheng.pc.task.taskDetail.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.FilterConfigDetail.domain.FilterConfigDetail;
import com.huaheng.pc.config.FilterConfigDetail.service.FilterConfigDetailService;
import com.huaheng.pc.config.cycleCountPreference.domain.CycleCountPreference;
import com.huaheng.pc.config.cycleCountPreference.service.CycleCountPreferenceService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.receiptPreference.domain.ReceiptPreference;
import com.huaheng.pc.config.receiptPreference.service.ReceiptPreferenceService;
import com.huaheng.pc.config.shipmentPreference.domain.ShipmentPreference;
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.system.user.service.IUserService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.mapper.TaskDetailMapper;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class TaskDetailServiceImpl extends ServiceImpl<TaskDetailMapper, TaskDetail> implements TaskDetailService {


    @Resource
    private CycleCountDetailService cycleCountDetailService;
    @Resource
    private CycleCountPreferenceService cycleCountPreferenceService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ReceiptPreferenceService receiptPreferenceService;
    @Resource
    private ShipmentPreferenceService shipmentPreferenceService;
    @Resource
    private FilterConfigDetailService filterConfigDetailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private IUserService userService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private MaterialService materialService;


    /**
     * 盘点任务首选项
     *
     * @param taskDetailList
     * @return
     */
    @Transactional
    @Override
    public List<TaskDetail> preferenceRealize(List<TaskDetail> taskDetailList) {
        /*查出盘点明细,找出盘点首选项,根据首选项显示字段*/
        List<TaskDetail> taskDetails = new ArrayList<>();

        for (TaskDetail item : taskDetailList) {
            //盘点明细
            CycleCountDetail cycleCountDetailtemp = new CycleCountDetail();
            cycleCountDetailtemp.setCycleCountHeadCode(item.getBillCode());
            cycleCountDetailtemp.setId(item.getBillDetailId());
            LambdaQueryWrapper<CycleCountDetail> detailLambdaQueryWrapper = Wrappers.lambdaQuery(cycleCountDetailtemp);
            CycleCountDetail cycleCountDetail = cycleCountDetailService.getOne(detailLambdaQueryWrapper);
            if (cycleCountDetail == null) {
                throw new SecurityException("没有找到子任务对应的盘点明细!");
            }
            //找盘点首选项
            CycleCountPreference cycleCountPreferenceTemp = new CycleCountPreference();
            cycleCountPreferenceTemp.setCode(cycleCountDetail.getPreferenceCode());
            cycleCountPreferenceTemp.setWarehouseCode(cycleCountDetail.getWarehouseCode());
            LambdaQueryWrapper<CycleCountPreference> preferenceLambdaQueryWrapper = Wrappers.lambdaQuery(cycleCountPreferenceTemp);
            CycleCountPreference cycleCountPreference = cycleCountPreferenceService.getOne(preferenceLambdaQueryWrapper);
            if (cycleCountPreference == null) {
                throw new SecurityException("没有找到盘点首选项!");
            }
            //根据首选项判断数据
            if (cycleCountPreference.getPromptLocation() == false) {
                //库位
                item.setFromLocation("");
                item.setToLocation("");
            }
            if (cycleCountPreference.getPromptLpn() == false) {
                //容器
                item.setContainerCode("");
            }
            if (cycleCountPreference.getPromptItem() == false) {
                //物料
                item.setMaterialCode("");
                item.setMaterialName("");
                item.setMaterialUnit("");
                item.setMaterialSpec("");
            }
            if (cycleCountPreference.getPromptQuantity() == false) {
                //系统数量
                item.setQty(null);
            }
            taskDetails.add(item);
        }
        return taskDetails;
    }

    /**
     * 任务位置拣货单打印
     * 根据任务头表id查询任务明细,根据上架或拣货货规则排序
     *
     * @param ids 头表id数组
     * @return
     */
    @Override
    public List<TaskDetail> findByTaskId(Integer[] ids) {
        Integer taskType = taskHeaderService.getById(ids[0]).getTaskType();
        String filterConfigCode = "";
        if (taskType.equals(QuantityConstant.TASK_TYPE_WHOLERECEIPT) || taskType.equals(QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT)) {
            ReceiptPreference receiptPreference = receiptPreferenceService.list().get(0);
            filterConfigCode = receiptPreference.getListingRules();
        }
        if (taskType.equals(QuantityConstant.TASK_TYPE_WHOLESHIPMENT) || taskType.equals(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT)) {
            ShipmentPreference shipmentPreference = shipmentPreferenceService.list().get(0);
            filterConfigCode = shipmentPreference.getShipmentPickingRule();
        }
        FilterConfigDetail filterConfigDetail = new FilterConfigDetail();
        if (StringUtils.isNotEmpty(filterConfigCode)) {
            LambdaQueryWrapper<FilterConfigDetail> detailQueryWrapper = Wrappers.lambdaQuery();
            detailQueryWrapper.eq(FilterConfigDetail::getCode, filterConfigCode);
            filterConfigDetail = filterConfigDetailService.getOne(detailQueryWrapper);
        }
        //Integer数组转object数组
        Object[] objects = new Object[ids.length];
        try {
            for (int i = 0; i < ids.length; i++) {
                Object a = (ids[i]);
                objects[i] = a;
            }
        } catch (Exception e) {
            throw new ServiceException(e.toString());
        }
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
                .in(TaskDetail::getTaskId, objects)
                .last(StringUtils.isNotEmpty(filterConfigDetail.getStatement()), filterConfigDetail.getStatement());
        return this.list(queryWrapper);
    }

    /**
     * 根据任务头表id查询任务明细
     *
     * @param id
     * @return
     */
    @Override
    public List<TaskDetail> findByTaskId(Integer id) {
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getTaskId, id);
        return this.list(queryWrapper);
    }

    @Override
    public void createTaskDetailByReceipt(TaskHeader task, ReceiptContainerDetail item) {
        TaskDetail taskDetail = new TaskDetail();
        taskDetail.setTaskId(task.getId());
        taskDetail.setTaskType(task.getTaskType());
        taskDetail.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_RECEIPT);
        taskDetail.setWarehouseCode(task.getWarehouseCode());
        taskDetail.setAllocationId(item.getId());
        taskDetail.setCompanyCode(task.getCompanyCode());
        taskDetail.setMaterialCode(item.getMaterialCode());
        taskDetail.setMaterialName(item.getMaterialName());
        taskDetail.setMaterialSpec(item.getMaterialSpec());
        taskDetail.setMaterialUnit(item.getMaterialUnit());
        taskDetail.setInventorySts(item.getInventorySts());
        taskDetail.setBillCode(item.getReceiptCode());
        taskDetail.setBillDetailId(item.getReceiptDetailId());
        taskDetail.setQty(item.getQty());
        taskDetail.setContainerCode(task.getContainerCode());
        taskDetail.setFromLocation(task.getFromLocation());
        taskDetail.setGoodsShelfNo(task.getGoodsShelfNo());
        if (task.getToLocation() != null) {
            taskDetail.setToLocation(task.getToLocation());
        }
        String receiptCode = item.getReceiptCode();
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(new LambdaQueryWrapper<ReceiptHeader>()
                .eq(ReceiptHeader::getCode, receiptCode)
                .eq(ReceiptHeader::getWarehouseCode, item.getWarehouseCode())
        );
        taskDetail.setReferenceCode(receiptHeader.getReferCode());

        taskDetail.setBatch(item.getBatch());
        taskDetail.setProjectNo(item.getProjectNo());
        taskDetail.setColor(item.getColor());
        if (!this.save(taskDetail)) {
            throw new ServiceException("生成任务明细失败");
        }
    }

    @Override
    public List<TaskDetail> selectListEntityUndone(TaskDetail taskDetail) {
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(taskDetail.getContainerCode()), TaskDetail::getContainerCode, taskDetail.getContainerCode());
        queryWrapper.le(StringUtils.isNotNull(taskDetail.getStatus()), TaskDetail::getStatus, taskDetail.getStatus());
        queryWrapper.eq(StringUtils.isNotNull(taskDetail.getNumber()), TaskDetail::getNumber, taskDetail.getNumber());
        queryWrapper.eq(StringUtils.isNotEmpty(taskDetail.getWarehouseCode()), TaskDetail::getWarehouseCode, taskDetail.getWarehouseCode());
        queryWrapper.ne(TaskDetail::getTaskType, QuantityConstant.TASK_TYPE_EMPTYSHIPMENT);
        return this.list(queryWrapper);
    }

    @Override
    public List<TaskDetail> selectListByGoodshelf(TaskDetail taskDetail) {
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(taskDetail.getContainerCode()), TaskDetail::getContainerCode, taskDetail.getContainerCode());
        queryWrapper.eq(StringUtils.isNotNull(taskDetail.getStatus()), TaskDetail::getStatus, taskDetail.getStatus());
        queryWrapper.eq(StringUtils.isNotNull(taskDetail.getNumber()), TaskDetail::getNumber, taskDetail.getNumber());
        queryWrapper.eq(StringUtils.isNotEmpty(taskDetail.getWarehouseCode()), TaskDetail::getWarehouseCode, taskDetail.getWarehouseCode());
        queryWrapper.lt(TaskDetail::getStatus, 100);
        return this.list(queryWrapper);
    }

    @Override
    public void updateByInventory(TaskDetail taskDetail) {
        InventoryDetail inventory = new InventoryDetail();
        inventory.setContainerCode(taskDetail.getContainerCode());
        try {
            inventory.setCompanyCode(taskDetail.getCompanyCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
        inventory.setWarehouseCode(taskDetail.getWarehouseCode());
        inventory.setMaterialCode(taskDetail.getMaterialCode());
        List<InventoryDetail> inventories = inventoryDetailService.selectListEntityByEqual(inventory);
        BigDecimal number = inventoryDetailService.addQty(inventories);
        taskDetail.setWarehouseQty(number);
        if (QuantityConstant.TASK_TYPE_WHOLESHIPMENT == taskDetail.getTaskType() || QuantityConstant.TASK_TYPE_SORTINGSHIPMENT == taskDetail.getTaskType()) {
            if (number.compareTo(taskDetail.getQty()) > 0) {
                taskDetail.setRemainder((number.subtract(taskDetail.getQty())));
            } else {
                taskDetail.setRemainder(new BigDecimal(0));
            }
        } else if (QuantityConstant.TASK_TYPE_WHOLERECEIPT == taskDetail.getTaskType() || QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT == taskDetail.getTaskType()) {
            taskDetail.setRemainder((number.add(taskDetail.getQty())));
        } else {
            taskDetail.setRemainder((number.add(taskDetail.getQty())));
        }

        Material material = materialService.getMaterialByCode(taskDetail.getMaterialCode());
        if (material != null) {
            taskDetail.setMaterialOldCode(material.getUserDef1());
            taskDetail.setMaterialSpec(material.getSpec());
            taskDetail.setMaterialName(material.getName());
        }
        taskDetail.setLastUpdatedBy(ShiroUtils.getUserName());
        inventoryDetailService.selectUwarehouse(taskDetail);
    }

    @Override
    public void updateTask(TaskDetail taskDetail, BigDecimal number) {
        if (taskDetail.getTaskType().equals("s")) {
            //修改出库子单据,修改出库组盘明细
            ShipmentDetail shipmentDetail = shipmentDetailService.getById(taskDetail.getBillDetailId());
            if (shipmentDetail == null) {
                throw new ServiceException("找不到子单");
            }
            shipmentDetail.setStatus(200);
            shipmentDetail.setQtyCompleted(shipmentDetail.getQtyCompleted().subtract(taskDetail.getQty()).add(number));
            if (!shipmentDetailService.updateById(shipmentDetail)) {
                throw new ServiceException("修改子单据失败");
            }
            ;

            ShipmentHeader shipmentHeader = shipmentHeaderService.getById(taskDetail.getBillId());
            if (shipmentHeader == null) {
                throw new ServiceException("找不到主单");
            }
            if (shipmentHeader.getLastStatus() > 200) {
                shipmentHeader.setLastStatus(200);
                if (!shipmentHeaderService.updateById(shipmentHeader)) {
                    throw new ServiceException("修改主单据失败");
                }
            }
            //修改出库组盘明细
            ShipmentContainerDetail shipmentContainerDetail = shipmentContainerDetailService.getById(taskDetail.getAllocationId());
            if (shipmentContainerDetail == null) {
                throw new ServiceException("找不到组盘明细");
            }
            shipmentContainerDetail.setQty(number);
            if (!shipmentContainerDetailService.updateById(shipmentContainerDetail)) {
                throw new ServiceException("修改组盘明细失败");
            }
            ;
        } else {
            //修改入库子单据,修改入库组盘明细
            ReceiptDetail receiptDetail = receiptDetailService.getById(taskDetail.getBillDetailId());
            if (receiptDetail == null) {
                throw new ServiceException("找不到子单");
            }
            receiptDetail.setStatus((short) 200);
            receiptDetail.setTaskQty(receiptDetail.getTaskQty().subtract(taskDetail.getQty()).add(number));
            if (!receiptDetailService.updateById(receiptDetail)) {
                throw new ServiceException("修改子单据失败");
            }
            ;

            ReceiptHeader receiptHeader = receiptHeaderService.getById(taskDetail.getBillId());
            if (receiptHeader == null) {
                throw new ServiceException("找不到主单");
            }
            if (receiptHeader.getLastStatus() > 200) {
                receiptHeader.setLastStatus(200);
                if (!receiptHeaderService.updateById(receiptHeader)) {
                    throw new ServiceException("修改主单据失败");
                }
            }
            //修改出库组盘明细
            ReceiptContainerDetail receiptContainerDetail = receiptContainerDetailService.getById(taskDetail.getAllocationId());
            if (receiptContainerDetail == null) {
                throw new ServiceException("找不到组盘明细");
            }
            receiptContainerDetail.setQty(number);
            if (!receiptContainerDetailService.updateById(receiptContainerDetail)) {
                throw new ServiceException("修改组盘明细失败");
            }
            ;
        }
        //修改子任务
        taskDetail.setQty(number);
        if (!this.updateById(taskDetail)) {
            throw new ServiceException("修改子任务失败");
        }
    }

    public List<TaskDetail> selectByIdList(List<Integer> ids) {
        if (ids == null) {
            return null;
        }
        String[] idArray = ids.toArray(new String[ids.size()]);
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(TaskDetail::getId, idArray);
        return this.list(queryWrapper);
    }

    @Override
    public AjaxResult updateTaskDetail(String ids, String code) {
        if (StringUtils.isEmpty(ids) || StringUtils.isEmpty(code)) {
            return AjaxResult.error("容器编码不能为空!");
        }
        List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
        if (idList.isEmpty()) {
            return AjaxResult.error("任务详情id不能为空值!");
        }
        List<TaskDetail> taskDetails = this.selectByIdList(idList);
        if (taskDetails.isEmpty()) {
            return AjaxResult.error("未查询到任务详情!");
        }
        taskDetails.forEach(item -> {
            item.setContainerCode(code);
            item.setLastUpdatedBy(ShiroUtils.getLoginName());
        });
        this.updateContainerCode(taskDetails);
        return AjaxResult.success("容器修改成功!");
    }

    public int updateContainerCode(List<TaskDetail> list) {
        if (list.isEmpty()) {
            return 0;
        }
        for (int a = 0; a < list.size(); a++) {
            LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(TaskDetail::getId, list.get(a).getId());
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setLastUpdatedBy(list.get(a).getLastUpdatedBy());
            this.update(taskDetail, queryWrapper);
        }
        return 1;
    }

}