MesController.java 22.1 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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
package com.huaheng.api.mes.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.mes.domain.*;
import com.huaheng.api.mes.service.SearchInventoryService;
import com.huaheng.common.constant.HttpConstant;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.restful.RestUtil;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.receiptType.domain.ReceiptType;
import com.huaheng.pc.config.receiptType.service.ReceiptTypeService;
import com.huaheng.pc.config.shipmentType.domain.ShipmentType;
import com.huaheng.pc.config.shipmentType.service.ShipmentTypeService;
import com.huaheng.pc.inventory.adjustDetail.domain.AdjustDetail;
import com.huaheng.pc.inventory.adjustDetail.service.AdjustDetailService;
import com.huaheng.pc.inventory.adjustHeader.domain.AdjustHeader;
import com.huaheng.pc.inventory.adjustHeader.service.AdjustHeaderService;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.cycleCountHeader.domain.CycleCountHeader;
import com.huaheng.pc.inventory.cycleCountHeader.service.CycleCountHeaderService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptDetailHistory.domain.ReceiptDetailHistory;
import com.huaheng.pc.receipt.receiptDetailHistory.service.ReceiptDetailHistoryService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.mapper.ReceiptHeaderMapper;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.receipt.receiptHeaderHistory.domain.ReceiptHeaderHistory;
import com.huaheng.pc.receipt.receiptHeaderHistory.service.ReceiptHeaderHistoryService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentDetailHistory.domain.ShipmentDetailHistory;
import com.huaheng.pc.shipment.shipmentDetailHistory.service.ShipmentDetailHistoryService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.mapper.ShipmentHeaderMapper;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.shipment.shipmentHeaderHistory.domain.ShipmentHeaderHistory;
import com.huaheng.pc.shipment.shipmentHeaderHistory.service.ShipmentHeaderHistoryService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/mes/v2")
public class MesController extends BaseController {

    @Resource
    private SearchInventoryService searchInventoryService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptHeaderHistoryService receiptheaderhistoryService;
    @Resource
    private ReceiptDetailHistoryService receiptDetailHistoryService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptHeaderMapper receiptHeaderMapper;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private ShipmentHeaderMapper shipmentHeaderMapper;
    @Resource
    private ShipmentHeaderHistoryService shipmentHeaderHistoryService;
    @Resource
    private ShipmentDetailHistoryService shipmentDetailHistoryService;
    @Resource
    private ReceiptTypeService receiptTypeService;
    @Autowired
    private MaterialService materialService;
    @Resource
    private ShipmentTypeService shipmentTypeService;
    @Resource
    private CycleCountHeaderService cycleCountHeaderService;
    @Resource
    private CycleCountDetailService cycleCountDetailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private AdjustDetailService adjustDetailService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private AddressService addressService;

    /**
     * 查询库存
     */
    @PostMapping("/searchInventory")
    @ApiLogger(from = "mes", apiName = "查询库存")
    @ResponseBody
    public AjaxResult searchInventory(@RequestBody SearchInventoryDomain domain) {
        List<SearchInventoryVo> vo = searchInventoryService.searchInventory(domain.getMaterialCode(), domain.getMaterialName());
        return AjaxResult.success(vo);
    }

    /**
     * 入库单
     */
    @PostMapping("/receipt")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    @ApiLogger(from = "mes", apiName = "入库单")
    public AjaxResult receipt(@RequestBody ReceiptDomain domain) {
        ReceiptHeader receiptHeader = new ReceiptHeader();
        List<ReceiptDetail> list = new ArrayList<>();
        List<ReceiptDetails> receiptDetails = domain.getReceiptDetails();
        //入库单类型表是否有此MES单据类型
        List<ReceiptType> lst = receiptTypeService.list();
        if (lst.stream().noneMatch(w -> String.valueOf(w.getName()).equals(domain.getReceiptType()))) {
            return AjaxResult.error("没有对应的入库单类型");
        }
        //入库单头表是否已存在此MES单据单号   唯一校验
        List<ReceiptHeader> headerList = receiptHeaderService.list();
        if (headerList.stream().anyMatch(w -> String.valueOf(w.getMesCode()).equals(domain.getReceiptHeader()))) {
            return AjaxResult.error("MES单据单号已存在");
        }
        List<ReceiptDetail> detailList = receiptDetailService.list();
        List<Material> material = materialService.list();
        BigDecimal totalQty = BigDecimal.ZERO;
        String warehouseCode = null;
        for (ReceiptDetails details : receiptDetails) {
            //出库单详情的物料批次为空
            if (details.getMaterialBatch() == null) {
                return AjaxResult.error("物料批次为空,新增失败");
            }
            //出库单详情的仓库编码为空
//            if (details.getWarehouseCode() == null) {
//                return AjaxResult.error("仓库编码为空,新增失败");
//            }
            //出库单详情的条码数量为空
            if (details.getTotalQty() == null) {
                return AjaxResult.error("条码数量为空,新增失败");
            }
            //出库单详情的物料名称为空
            if (details.getMaterialName() == null) {
                return AjaxResult.error("物料名称为空,新增失败");
            }
            //出库单详情的物料条码为空
            if (details.getMaterialID() == null) {
                return AjaxResult.error("物料条码为空,新增失败");
            }
            //物料表是否有此MES单据明细中的物料编码
            if (material.stream().noneMatch(w -> String.valueOf(w.getCode()).equals(details.getMaterialCode())) || details.getMaterialCode() == null) {
                return AjaxResult.error("没有对应的物料");
            }
            //入库单详情表是否已存在此MES单据明细中的物料条码     唯一校验
            if (detailList.stream().anyMatch(w -> String.valueOf(w.getMaterialID()).equals(details.getMaterialID()))) {
                return AjaxResult.error("物料条码已存在");
            }
            totalQty = totalQty.add(details.getTotalQty());
//            warehouseCode = details.getWarehouseCode();
        }
        //mes传中文入库任务类型,由name接收,code存
        LambdaQueryWrapper<ReceiptType> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ReceiptType::getName,domain.getReceiptType());
        ReceiptType receiptType = receiptTypeService.getOne(wrapper);
        String code = receiptHeaderService.createCode(receiptType.getCode());
        if (StringUtils.isEmpty(code)) {
            throw new ServiceException("根据单据类型" + domain.getReceiptType() + "生成单号失败");
        }
        receiptHeader.setMesCode(domain.getReceiptHeader());
        receiptHeader.setCode(code);
        receiptHeader.setReceiptType(receiptType.getCode());
        receiptHeader.setCompanyCode(QuantityConstant.COMPANYCODE);
//        receiptHeader.setWarehouseCode(warehouseCode);
        receiptHeader.setWarehouseCode("CS0001");
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setTotalQty(totalQty);
        receiptHeader.setTotalLines(receiptDetails.size());
        receiptHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
        receiptHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
        if (receiptHeaderService.save(receiptHeader)) {
            ReceiptHeader header = receiptHeaderMapper.getByOrderHeader(domain.getReceiptHeader());
            for (ReceiptDetails details : receiptDetails) {
                ReceiptDetail receiptDetail = new ReceiptDetail();
                receiptDetail.setReceiptId(header.getId());
                receiptDetail.setReceiptCode(header.getCode());
//                receiptDetail.setWarehouseCode(details.getWarehouseCode());
                receiptDetail.setWarehouseCode("CS0001");
                receiptDetail.setCompanyCode(QuantityConstant.COMPANYCODE);
                receiptDetail.setMaterialCode(details.getMaterialCode());
                receiptDetail.setMaterialName(details.getMaterialName());
                receiptDetail.setBatch(details.getMaterialBatch());
                receiptDetail.setMesCode(header.getMesCode());
                receiptDetail.setMaterialID(details.getMaterialID());
                receiptDetail.setQty(details.getTotalQty());
                receiptDetail.setInventorySts(QuantityConstant.GOOD);
                receiptDetail.setCreatedBy(QuantityConstant.PLATFORM_MES);
                receiptDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
                list.add(receiptDetail);
            }
        } else {
            return AjaxResult.error("新增入库单头表失败");
        }
        return AjaxResult.success(receiptDetailService.saveBatch(list));
    }

    /**
     * 出库单
     */
    @PostMapping("/shipment")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    @ApiLogger(from = "mes", apiName = "出库单")
    public AjaxResult shipment(@RequestBody ShipmentDomain domain) {
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        List<ShipmentDetail> list = new ArrayList<>();
        List<ShipmentDetails> shipmentDetails = domain.getShipmentDetails();
        //出库单类型表是否有此MES单据类型
        List<ShipmentType> lst = shipmentTypeService.list();
        if (lst.stream().noneMatch(w -> String.valueOf(w.getName()).equals(domain.getShipmentType()))) {
            return AjaxResult.error("没有对应的出库单类型");
        }
        //出库单头表是否已存在此MES单据单号   唯一校验
        List<ShipmentHeader> headerList = shipmentHeaderService.list();
        if (headerList.stream().anyMatch(w -> String.valueOf(w.getMesCode()).equals(domain.getShipmentHeader()))) {
            return AjaxResult.error("MES单据单号已存在");
        }
        String warehouseCode = null;
        BigDecimal qty = BigDecimal.ZERO;
        List<Material> material = materialService.list();
        for (ShipmentDetails details : shipmentDetails) {
            //物料表是否有此MES单据明细中的物料编码
            if (details.getMaterialCode() == null || material.stream().noneMatch(w -> String.valueOf(w.getCode()).equals(details.getMaterialCode()))) {
                return AjaxResult.error("没有对应的物料");
            }
            //出库单详情的仓库编码为空
//            if (details.getWarehouseCode() == null) {
//                return AjaxResult.error("仓库编码为空,新增失败");
//            }
            //出库单详情的条码数量为空
            if (details.getTotalQty() == null) {
                return AjaxResult.error("条码数量为空,新增失败");
            }
            //出库单详情的物料名称为空
            if (details.getMaterialName() == null) {
                return AjaxResult.error("物料名称为空,新增失败");
            }
            qty = qty.add(details.getTotalQty());
//            warehouseCode = details.getWarehouseCode();
        }
        //mes传中文入库任务类型,由name接收,code存
        LambdaQueryWrapper<ShipmentType> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ShipmentType::getName,domain.getShipmentType());
        ShipmentType shipmentType = shipmentTypeService.getOne(wrapper);
        String code = shipmentHeaderService.createCode(shipmentType.getCode());
        shipmentHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        shipmentHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        shipmentHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
        shipmentHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
        shipmentHeader.setCompanyCode(QuantityConstant.COMPANYCODE);
        shipmentHeader.setShipmentType(shipmentType.getCode());
        shipmentHeader.setMesCode(domain.getShipmentHeader());
//        shipmentHeader.setWarehouseCode(warehouseCode);
        shipmentHeader.setWarehouseCode("CS0001");
        shipmentHeader.setTotalQty(qty);
        shipmentHeader.setTotalLines(shipmentDetails.size());
        shipmentHeader.setCode(code);
        if (shipmentHeaderService.save(shipmentHeader)) {
            ShipmentHeader header = shipmentHeaderMapper.getByOrderHeader(domain.getShipmentHeader());
            for (ShipmentDetails details : shipmentDetails) {
                ShipmentDetail detail = new ShipmentDetail();
                detail.setShipmentId(header.getId());
                detail.setShipmentCode(header.getCode());
                detail.setCreatedBy(QuantityConstant.PLATFORM_MES);
                detail.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
//                detail.setWarehouseCode(details.getWarehouseCode());
                detail.setWarehouseCode("CS0001");
                detail.setMesCode(domain.getShipmentHeader());
                detail.setInventorySts(QuantityConstant.GOOD);
                detail.setCompanyCode(QuantityConstant.COMPANYCODE);
                detail.setMaterialName(details.getMaterialName());
                detail.setMaterialCode(details.getMaterialCode());
                detail.setQty(details.getTotalQty());
                list.add(detail);
            }
        } else {
            return AjaxResult.error("新增入库单头表失败");
        }
        return AjaxResult.success(shipmentDetailService.saveBatch(list));
    }

    /**
     *  入库回传
     */
    @PostMapping("/backReceipt")
    @ResponseBody
    @ApiLogger(from = "mes", apiName = "入库回传")
    public AjaxResult backReceipt(String id) {
        if (StringUtils.isEmpty(id)){
            return AjaxResult.error("id不能为空");
        }
        return searchInventoryService.backReceipt(id);
    }


    /**
     *	出库回传
     */
    @PostMapping( "/backShipment")
    @ResponseBody
    @ApiLogger(from = "mes", apiName = "出库回传")
    public AjaxResult backShipment(String id){
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        return searchInventoryService.backShipment(id);
    }

    /**
     * 盘点结果
     */
    @PostMapping("/checkInventory")
    @ResponseBody
    @ApiLogger(from = "mes", apiName = "盘点结果")
    public AjaxResult checkInventory(String code) {
        if (StringUtils.isEmpty(code)){
            return AjaxResult.error("code不能为空");
        }
        return searchInventoryService.checkInventory(code);
    }

    /**
     * 单据取消
     */
    @PostMapping("/cancelOrder")
    @ResponseBody
    @ApiLogger(from = "mes", apiName = "单据取消")
    public AjaxResult cancelOrder(@RequestBody CancelOrderDomain domain) {
        if ("receipt".equals(domain.getOrderType())) {
            ReceiptHeader receiptHeader = receiptHeaderMapper.getByOrderHeader(domain.getOrderHeader());
            if (receiptHeader == null) {
                return AjaxResult.error("没有找到单据" + domain.getOrderHeader());
            }
            if (!Objects.equals(receiptHeader.getFirstStatus(), QuantityConstant.RECEIPT_HEADER_BUILD)) {
                return AjaxResult.error("非新建单据不能取消");
            }
            LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
            List<ReceiptDetail> list = receiptDetailService.list(lambdaQueryWrapper);
            List<ReceiptDetailHistory> receiptDetailHistoryList = new ArrayList<>();
            if (!receiptHeaderService.removeById(receiptHeader)) {
                throw new ServiceException("删除头表失败");
            }
            List<Integer> receiptDetailIds = new ArrayList<>();
            ReceiptHeaderHistory receiptHeaderHistory = new ReceiptHeaderHistory();
            BeanUtils.copyProperties(receiptHeader, receiptHeaderHistory);
            if (!receiptheaderhistoryService.save(receiptHeaderHistory)) {
                throw new ServiceException("新增历史头表失败");
            }
            for (ReceiptDetail receiptDetail : list) {
                receiptDetailIds.add(receiptDetail.getId());
                ReceiptDetailHistory receiptDetailHistory = new ReceiptDetailHistory();
                BeanUtils.copyProperties(receiptDetail, receiptDetailHistory);
                receiptDetailHistory.setReceiptId(receiptHeaderHistory.getId());
                receiptDetailHistoryList.add(receiptDetailHistory);
            }
            if (receiptDetailIds.size() > 0) {
                if (!receiptDetailService.removeByIds(receiptDetailIds)) {
                    throw new ServiceException("删除明细表失败");
                }
            }
            if (receiptDetailHistoryList.size() > 0) {
                if (!receiptDetailHistoryService.saveBatch(receiptDetailHistoryList)) {
                    throw new ServiceException("新增历史入库明细失败");
                }
            }
        } else if ("shipment".equals(domain.getOrderType())) {
            ShipmentHeader shipmentHeader = shipmentHeaderMapper.getByOrderHeader(domain.getOrderHeader());
            if (shipmentHeader == null) {
                return AjaxResult.error("没有找到单据" + domain.getOrderHeader());
            }
            if (!Objects.equals(shipmentHeader.getFirstStatus(), QuantityConstant.RECEIPT_HEADER_BUILD)) {
                return AjaxResult.error("非新建单据不能取消");
            }
            LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
            List<ShipmentDetail> list = shipmentDetailService.list(lambdaQueryWrapper);
            List<ShipmentDetailHistory> shipmentDetailHistories = new ArrayList<>();
            List<Integer> shipmentDetailIds = new ArrayList<>();

            if (!shipmentHeaderService.removeById(shipmentHeader)) {
                throw new ServiceException("删除头表失败");
            }

            ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
            BeanUtils.copyProperties(shipmentHeader, shipmentHeaderHistory);

            if (!shipmentHeaderHistoryService.save(shipmentHeaderHistory)) {
                throw new ServiceException("新增历史头表失败");
            }
            for (ShipmentDetail shipmentDetail : list) {
                shipmentDetailIds.add(shipmentDetail.getId());
                ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
                BeanUtils.copyProperties(shipmentDetail, shipmentDetailHistory);
                shipmentDetailHistory.setShipmentId(shipmentHeaderHistory.getId());
                shipmentDetailHistories.add(shipmentDetailHistory);
            }
            if (shipmentDetailIds.size() > 0) {
                if (!shipmentDetailService.removeByIds(shipmentDetailIds)) {
                    throw new ServiceException("删除明细表失败");
                }
            }
            if (shipmentDetailHistories.size() > 0) {
                if (!shipmentDetailHistoryService.saveBatch(shipmentDetailHistories)) {
                    throw new ServiceException("新增历史出库明细失败");
                }
            }
        } else {
            return AjaxResult.error("没有找到此类型的单据" + domain.getOrderType());
        }
        return AjaxResult.success("取消成功");
    }
}