MobileBatchReceiptController.java 15.6 KB
package com.huaheng.mobile.receipt;

import com.alibaba.fastjson.JSONException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
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.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Enzo Cotter
 * @date 2019/12/15
 */
@CrossOrigin
@RestController
@RequestMapping("/mobile/receipt/batch")
@Api(tags = {"MobileBatchReceiptController"}, description = "移动端收货")
public class MobileBatchReceiptController {

    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private ContainerService containerService;
    @Resource
    private MaterialService materialService;
    @Resource
    private LocationService locationService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private CompanyService companyService;


    @PostMapping("/scanContainer")
    @ApiOperation("移动端入库扫描容器")
    @Log(title = "移动端入库扫描容器", action = BusinessType.OTHER)
    public AjaxResult scanContainer(@RequestBody @ApiParam(value = "容器号") Map<String, String> param) throws Exception {
        if (param.get("code") == null || param.get("code").trim().length() < 1) {
            throw new JSONException("容器号(code)不能为空");
        }

        ReceiptContainerView receiptContainerView = new ReceiptContainerView();
        receiptContainerView.setReceiptContainerCode(param.get("code"));
        receiptContainerView.setTaskType((short) 100);
        AjaxResult result = receiptContainerHeaderService.checkContainer(receiptContainerView);
        LambdaQueryWrapper<Container> containerLambdaQueryWrapper = Wrappers.lambdaQuery();
        containerLambdaQueryWrapper.eq(Container::getCode, param.get("code"));
        Container container = containerService.getOne(containerLambdaQueryWrapper);
        if (container == null) {
            return AjaxResult.error("托盘编码错误");
        }
        if (!container.getStatus().equals("empty")) {
            return AjaxResult.error("托盘状态不为空");
        }
        if (!container.getLocationCode().equals("")) {
            return AjaxResult.error("");
        }
        return result;
    }

    @PostMapping("/save")
    @ApiOperation("移动端收货保存")
    @Log(title = "移动端收货保存", action = BusinessType.OTHER)
    public AjaxResult save(@RequestBody @ApiParam(value = "收货单") List<ReceiptContainerView> record) throws Exception {
        AjaxResult result = receiptContainerHeaderService.batchSave(record);
        return result;
    }

    @PostMapping("/getMaterial")
    @ApiOperation("移动端获取物料信息")
    @Log(title = "移动端获取物料信息", action = BusinessType.OTHER)
    public AjaxResult getMaterial(@RequestBody @ApiParam(value = "物料号") Map<String, String> param) throws Exception {
        if (param.get("code") == null || param.get("code").trim().length() < 1) {
            throw new JSONException("容器号(code)不能为空");
        }
        Material material = materialService.findAllByCode(param.get("code"));
        if (material == null) {
            return AjaxResult.error("没有该物料");
        }
        MaterialInfo materialInfo = new MaterialInfo();
        materialInfo.setMaterialCode(material.getCode());
        materialInfo.setMaterialName(material.getName());
        materialInfo.setType(material.getSpec());

        return AjaxResult.success(materialInfo);
    }

    @PostMapping("/getLatestMaterial")
    @ApiOperation("移动端获取物料信息")
    @Log(title = "移动端获取物料信息", action = BusinessType.OTHER)
    public AjaxResult getLatestMaterial(@RequestBody @ApiParam(value = "物料号") Map<String, String> param) throws Exception {
        List<Material> materials = materialService.findLatest();
        if (materials == null) {
            return AjaxResult.error("没有找到物料");
        }

        return AjaxResult.success(materials);
    }

    @PostMapping("/getLatestReceipt")
    @ApiOperation("移动端获取入库单")
    @Log(title = "移动端获取入库单", action = BusinessType.OTHER)
    public AjaxResult getLatestReceipt(@RequestBody @ApiParam(value = "物料号") Map<String, String> param) throws Exception {
        List<ReceiptHeader> receiptHeaders = receiptHeaderService.getLatestReceipt();
        if (receiptHeaders == null) {
            return AjaxResult.error("没有找到入库单");
        }
        return AjaxResult.success(receiptHeaders);
    }



    @PostMapping("/checkLocation")
    @ApiOperation("移动端验证库位")
    @Log(title = "移动端验证库位", action = BusinessType.OTHER)
    public AjaxResult checkLocation(@RequestBody @ApiParam(value = "物料号") Map<String, String> param) throws Exception {
        if (param.get("code") == null || param.get("code").trim().length() < 1) {
            throw new JSONException("容器号(code)不能为空");
        }

        boolean result = locationService.checkLocation(param.get("code"));
        if (!result) {
            return AjaxResult.error("没有该库位");
        }
        return AjaxResult.success(result);
    }

    @PostMapping("/getFreeLocation")
    @ApiOperation("移动端获得空闲库位")
    @Log(title = "移动端获得空闲库位", action = BusinessType.OTHER)
    public AjaxResult getFreeLocation(@RequestBody @ApiParam(value = "物料号") Map<String, String> param) throws Exception {
        if (param.get("materialCode") == null || param.get("materialCode").trim().length() < 1) {
            throw new JSONException("materialCode不能为空");
        }

        if (param.get("batch") == null || param.get("batch").trim().length() < 1) {
            throw new JSONException("batch不能为空");
        }
        String materialCode = param.get("materialCode");
        String batch = param.get("batch");
        boolean result = locationService.getFreeLocation(materialCode, batch);
        if (!result) {
            return AjaxResult.error("没有空闲的分区库位");
        }
        return AjaxResult.success(result);
    }

    @PostMapping("/getContainerCode")
    @ApiOperation("移动端验证库位")
    @Log(title = "移动端验证库位", action = BusinessType.OTHER)
    public AjaxResult getContainerCode(@RequestBody @ApiParam(value = "物料号") Map<String, String> param) throws Exception {
        if (param.get("code") == null || param.get("code").trim().length() < 1) {
            throw new JSONException("容器号(code)不能为空");
        }
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getCode, param.get("code"))
                .eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(Location::getDeleted, false);
        Location location = locationService.getOne(queryWrapper);
        if (location == null) {
            return AjaxResult.error("没有该库位");
        }
        return AjaxResult.success("成功", location.getContainerCode());
    }

    @PostMapping("/quickReceipt")
    @ApiOperation("移动端收货保存")
    @Log(title = "移动端收货保存", action = BusinessType.OTHER)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult quickReceipt(@RequestBody @ApiParam(value="收货单") List<ReceiptBill> receiptBills) throws Exception {
        if (receiptBills == null || receiptBills.size() <=0) {
            throw new JSONException("没有收货信息");
        }
        String containerCode = receiptBills.get(0).receiptContainerCode;
        String locationCode = receiptBills.get(0).locationCode;
        String companyCode = receiptBills.get(0).getCompanyCode();
        String receiptDetailId = receiptBills.get(0).getReceiptDetailId();

        LambdaQueryWrapper<Container> containerQueryWrapper = Wrappers.lambdaQuery();
        containerQueryWrapper.eq(Container::getCode, containerCode);
        Container container = containerService.getOne(containerQueryWrapper);
        if(container == null) {
            throw new JSONException("不存在这个托盘");
        }
        if(locationCode.equals(container.getLocationCode())) {
            /* 查询未完成的入库任务*/
            LambdaQueryWrapper<TaskHeader> taskQueryWrapper = Wrappers.lambdaQuery();
            taskQueryWrapper.eq(TaskHeader::getWarehouseCode, ShiroUtils.getWarehouseCode())
                    .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_WHOLERECEIPT,
                            QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT)
                    .lt(TaskHeader::getStatus, 100)
                    .eq(TaskHeader::getContainerCode, containerCode);
            TaskHeader taskHeader = taskHeaderService.getOne(taskQueryWrapper);
            if(taskHeader == null) {
                throw new JSONException("没有找到托盘对应的任务");
            }

            LambdaQueryWrapper<ReceiptDetail> receiptDetailQueryWrapper = Wrappers.lambdaQuery();
            receiptDetailQueryWrapper.eq(ReceiptDetail::getId, receiptDetailId);
            ReceiptDetail receiptDetail = receiptDetailService.getOne(receiptDetailQueryWrapper);
            if(receiptDetail == null) {
                throw new JSONException("没有找到入库单详情");
            }

            LambdaQueryWrapper<ReceiptHeader> receiptHeaderQueryWrapper = Wrappers.lambdaQuery();
            receiptHeaderQueryWrapper.eq(ReceiptHeader::getId, receiptDetail.getReceiptId());
            ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderQueryWrapper);
            if(receiptHeader == null) {
                throw new JSONException("没有找到入库单");
            }

            for(ReceiptBill receiptBill : receiptBills) {

                TaskDetail taskDetail = new TaskDetail();
                taskDetail.setTaskId(taskHeader.getId());
                taskDetail.setContainerCode(containerCode);
                taskDetail.setTaskType(taskHeader.getTaskType());
                taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
                taskDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
                taskDetail.setCompanyCode(companyCode);
                taskDetail.setBillCode(receiptHeader.getCode());
                taskDetail.setBillDetailId(Integer.parseInt(receiptBill.getReceiptDetailId()));
                taskDetail.setMaterialCode(receiptBill.getMaterialCode());
                taskDetail.setMaterialName(receiptBill.getMaterialName());
                taskDetail.setQty(receiptBill.getQty());
                taskDetail.setFromLocation(taskHeader.getFromLocation());
                taskDetail.setToLocation(taskHeader.getToLocation());
                taskDetail.setStatus(taskHeader.getStatus());
                taskDetail.setBatch(receiptBill.getBatch());
                taskDetail.setCompanyCode(taskHeader.getCompanyCode());
                taskDetailService.save(taskDetail);


                LambdaQueryWrapper<ReceiptDetail> receiptDetail2QueryWrapper = Wrappers.lambdaQuery();
                receiptDetail2QueryWrapper.eq(ReceiptDetail::getId, receiptBill.getReceiptDetailId());
                ReceiptDetail receiptDetail2 = receiptDetailService.getOne(receiptDetail2QueryWrapper);
                if(receiptDetail2 == null) {
                    throw new JSONException("没有找到入库单详情");
                }

                BigDecimal openQty = receiptDetail2.getOpenQty();
                BigDecimal totalQty = receiptDetail2.getTotalQty();
                BigDecimal qty = openQty.add(receiptBill.getQty());
                if(totalQty.subtract(qty).compareTo(BigDecimal.ZERO) < 0) {
                    throw new Exception("配盘数量大于表单数量");
                }

                receiptDetail2.setOpenQty(qty);
                receiptDetailService.update(receiptDetail2, receiptDetail2QueryWrapper);
            }

        } else {
            ReceiptContainerView record = new ReceiptContainerView();
            record.setReceiptContainerCode(receiptBills.get(0).getReceiptContainerCode());
            record.setLocationCode(receiptBills.get(0).getLocationCode());
            record.setTaskType((short)100);
            record.setCompanyCode(receiptBills.get(0).getCompanyCode());
            receiptContainerHeaderService.checkContainer(record);
            receiptContainerHeaderService.mobileCheckLocationCode(record);
            int receiptHeaderId = receiptHeaderService.createTodayHeader(companyCode);
            ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptHeaderId);
            List<Integer> receiptDetailIds = receiptDetailService.insertTodayReceiptDetail(receiptHeaderId, receiptBills, true, companyCode);
            if(receiptDetailIds != null && receiptDetailIds.size() > 0) {
                int containerHeaderId = receiptContainerHeaderService.insertTodayReceiptContainerHeader(receiptBills.get(0), receiptHeader.getCode());
                if(containerHeaderId > 0) {
                    List<Integer> receiptContainerDetailIds = receiptContainerDetailService.insertTodayReceiptcContainerDetail(containerHeaderId, receiptHeaderId, receiptDetailIds, receiptBills);
                    if(receiptContainerDetailIds != null && receiptContainerDetailIds.size() > 0) {

                    } else {
                        return AjaxResult.error("入库组盘失败");
                    }
                } else {
                    return AjaxResult.error("插入入库容器表单头失败");
                }
            } else {
                return AjaxResult.error("插入入库明细表单失败");
            }
        }
        return AjaxResult.success("收货成功");
    }
}