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

import com.alibaba.fastjson.JSONException;
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.general.container.domain.Container;
import com.huaheng.pc.general.container.mapper.ContainerMapper;
import com.huaheng.pc.general.container.service.IContainerService;
import com.huaheng.pc.general.location.domain.Location;
import com.huaheng.pc.general.location.service.ILocationService;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.receipt.receiptContainerDetail.service.IReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
import com.huaheng.pc.receipt.receiptContainerHeader.service.IReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.service.IReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.IReceiptHeaderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/mobile/receipt/batch")
@Api(tags = {"MobileBatchReceiptController"}, description = "移动端收货")
public class MobileBatchReceiptController {

    @Resource
    private IReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private IMaterialService materialService;
    @Resource
    private ILocationService locationService;
    @Resource
    private IContainerService containerService;
    @Resource
    private ContainerMapper containerMapper;
    @Resource
    private IReceiptHeaderService receiptHeaderService;
    @Resource
    private IReceiptDetailService receiptDetailService;
    @Resource
    private IReceiptContainerDetailService receiptContainerDetailService;

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

    @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);
        receiptContainerView.setZoneCode("LK");
        AjaxResult result = receiptContainerHeaderService.checkContainer(receiptContainerView);
        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)不能为空");
        if  (param.get("companyCode") == null || param.get("companyCode").trim().length() < 1)
            throw new JSONException("(companyCode)不能为空");
        if  (param.get("companyId") == null || param.get("companyId").trim().length() < 1)
            throw new JSONException("(companyId)不能为空");
        Material material = materialService.getMaterial(param.get("code"));
        if(material == null) {
            return AjaxResult.error("没有该物料");
        }
        MaterialInfo materialInfo = new MaterialInfo();
        materialInfo.setMaterialCode(material.getCode());
        materialInfo.setMaterialName(material.getName());
        materialInfo.setType(material.getSpecification());
        return AjaxResult.success(materialInfo);
    }

    @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("/quickSaveGoods")
    @ApiOperation("移动端收货保存")
    @Log(title = "移动端收货保存", action = BusinessType.OTHER)
    public AjaxResult quickSaveGoods(@RequestBody @ApiParam(value="收货单") List<ReceiptBill> receiptBills) throws Exception {
        if (receiptBills == null || receiptBills.size() <=0) {
            throw new JSONException("没有收货信息");
        }
        String receiptContainerCode = null;
        String locationCode = receiptBills.get(0).getLocationCode();
        if(locationCode != null && locationCode.contains("DM")) {
            receiptBills.get(0).setReceiptContainerCode(insertContainer());
        }
        receiptContainerCode = receiptBills.get(0).getReceiptContainerCode();
        if(receiptContainerCode == null) {
            return AjaxResult.error("请输入容器号");
        }
        Container container = new Container();
        container.setCode(receiptContainerCode);
        container.setWarehouseId(ShiroUtils.getWarehouseId());
        container = containerService.selectFirstEntity(container);
        if(!container.getStatus().equals("empty")) {
            return AjaxResult.error("托盘状态不为空");
        }
        ReceiptContainerView record = new ReceiptContainerView();
        record.setReceiptContainerCode(receiptContainerCode);
        record.setLocationCode(locationCode);
        record.setTaskType((short)100);
        record.setCompanyId(receiptBills.get(0).getCompanyId());
        record.setCompanyCode(receiptBills.get(0).getCompanyCode());
        if(locationCode != null && locationCode.contains("DM")) {
            record.setZoneCode("PK");
        } else {
            record.setZoneCode("LK");
        }
        receiptContainerHeaderService.checkContainer(record);
        receiptContainerHeaderService.mobileCheckLocationCode(record);
        int receiptHeaderId = receiptHeaderService.createTodayHeader(receiptBills.get(0).getCompanyId(), receiptBills.get(0).getCompanyCode());
        ReceiptHeader receiptHeader = receiptHeaderService.selectEntityById(receiptHeaderId);
        List<Integer> receiptDetailIds = receiptDetailService.insertTodayReceiptDetail(receiptHeaderId, receiptBills, true, receiptBills.get(0).getCompanyId(), receiptBills.get(0).getCompanyCode());
        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("快速收货成功",receiptContainerCode);
    }

    private String insertContainer() {
        Container condition = new Container();
        condition.setType("D");
        condition.setStatus("empty");
        condition.setWarehouseId(ShiroUtils.getWarehouseId());
        Container container = containerService.selectFirstEntity(condition);
        if(container == null) {
            condition.setWarehouseCode(ShiroUtils.getWarehouseCode());
            condition.setCode(getCode());
            condition.setPrintCount(0);
            condition.setCreatedBy(ShiroUtils.getLoginName());
            condition.setLastUpdatedBy(ShiroUtils.getLoginName());
            condition.setEnable(true);
            condition.setDeleted(false);
            containerService.insert(condition);
            return condition.getCode();
        }
        return container.getCode();
    }

    private String getCode() {
        String code = null;
        Date now = new Date();
        String type = "D";
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String maxCode =  containerMapper.getMaxCode(type);
        //如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
        if (maxCode != null)
        {
            Integer count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
            code = type + String.format("%05d", count + 1);
        }
        else
        {
            code = type + "00001";
        }
        return code;
    }

    @PostMapping("/replenishWarehouse")
    @ApiOperation("移动端补充入库")
    @Log(title = "移动端补充入库", action = BusinessType.OTHER)
    public AjaxResult replenishWarehouse(@RequestBody @ApiParam(value="收货单") List<ReceiptBill> receiptBills) throws Exception {
        if (receiptBills == null || receiptBills.size() <=0) {
            throw new JSONException("没有收货信息");
        }
        Location condition = new Location();
        condition.setCode(receiptBills.get(0).getLocationCode());
        condition.setWarehouseId(ShiroUtils.getWarehouseId());
        condition.setDeleted(false);
        Location location1 = locationService.selectFirstEntity(condition);
        if(location1 == null) {
            return AjaxResult.error("没有该库位");
        }
        String containerCode = location1.getContainerCode();
        String locationCode = location1.getCode();
        ReceiptContainerView receiptContainerView2 = new ReceiptContainerView();
        receiptContainerView2.setReceiptContainerCode(containerCode);
        receiptContainerView2.setTaskType((short) 200);
        receiptContainerView2.setLocationCode(locationCode);
        receiptContainerView2.setCompanyId(receiptBills.get(0).getCompanyId());
        receiptContainerView2.setCompanyCode(receiptBills.get(0).getCompanyCode());
        if(locationCode != null && locationCode.contains("DM")) {
            receiptContainerView2.setZoneCode("PK");
        } else {
            receiptContainerView2.setZoneCode("LK");
        }
        receiptContainerHeaderService.checkContainer(receiptContainerView2);
//        receiptContainerHeaderService.mobileCheckLocationCode(receiptContainerView2);
        int receiptHeaderId = receiptHeaderService.createTodayHeader(receiptBills.get(0).getCompanyId(), receiptBills.get(0).getCompanyCode());
        ReceiptHeader receiptHeader = receiptHeaderService.selectEntityById(receiptHeaderId);
        List<Integer> receiptDetailIds = receiptDetailService.insertTodayReceiptDetail(receiptHeaderId,
                receiptBills, false, receiptBills.get(0).getCompanyId(), receiptBills.get(0).getCompanyCode());
        List<ReceiptContainerView> receiptContainerViews = new ArrayList<>();
       for(int i=0; i<receiptBills.size(); i++) {
           ReceiptBill receiptBill = receiptBills.get(i);
           ReceiptContainerView receiptContainerView = new ReceiptContainerView();
           receiptContainerView.setTaskType((short) 200);
           receiptContainerView.setReceiptContainerCode(containerCode);
           receiptContainerView.setQty(receiptBill.getQty());
           receiptContainerView.setLocationCode(receiptBills.get(0).getLocationCode());
           receiptContainerView.setReceiptCode(receiptHeader.getCode());
           receiptContainerView.setReceiptDetailId(receiptDetailIds.get(i));
           receiptContainerView.setCompanyId(receiptBill.getCompanyId());
           receiptContainerView.setCompanyCode(receiptBill.getCompanyCode());
           if(locationCode != null && locationCode.contains("DM")) {
               receiptContainerView.setZoneCode("PK");
           } else {
               receiptContainerView.setZoneCode("LK");
           }
           receiptContainerViews.add(receiptContainerView);
       }
        return receiptContainerHeaderService.replenishBatchSave(receiptContainerViews);
    }

    @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)不能为空");
        Location condition = new Location();
        condition.setCode(param.get("code"));
        condition.setWarehouseId(ShiroUtils.getWarehouseId());
        condition.setDeleted(false);
        Location location = locationService.selectFirstEntity(condition);
        if(location == null) {
            return AjaxResult.error("没有该库位");
        }
        return AjaxResult.success("成功", location.getContainerCode());
    }
}