MobileShipmentController.java 10.9 KB
package com.huaheng.mobile.shipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.general.domain.ReceiptDomain;
import com.huaheng.api.general.domain.ShipmentDomain;
import com.huaheng.common.exception.BusinessException;
import com.huaheng.common.utils.StringUtils;
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.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.mobile.invenory.TaskIds;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.receiptType.domain.ReceiptType;
import com.huaheng.pc.config.shipmentType.domain.ShipmentType;
import com.huaheng.pc.config.shipmentType.service.ShipmentTypeService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
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.task.taskHeader.domain.ShipmentTaskCreateModel;
import com.huaheng.pc.task.taskHeader.service.ShipmentTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author mahua
 * @ClassName MobileShipmentController
 * @projectName wms_xinyi
 * @description: TODO
 * @date 2020/2/1115:29
 */
@RestController
@RequestMapping("/mobile/shipment")
public class MobileShipmentController extends BaseController {
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    private ShipmentTaskService shipmentTaskService;
    @Autowired
    private ShipmentTypeService shipmentTypeService;

    /**
     * 自动组盘
     * @param shipmentCode
     * @return
     */
    @PostMapping("/autoCombination")
    @ResponseBody
    public AjaxResult autoCombination(@RequestBody  Map<String, String> param){
        String shipmentCode = param.get("shipmentCode");
        AjaxResult ajaxResult = shipmentContainerHeaderService.autoCombination(shipmentCode);
        return ajaxResult;
    }

    /**
     * 查询出库单主列表
     */
    @Log(title = "出库-出库单", operating="查看出库主单", action = BusinessType.GRANT)
    @PostMapping("/list")
    public AjaxResult list(@RequestBody @ApiParam(value = "出库单号-referCode、货主编码-companyCode、出库单类型-shipmentType") Map<String, String> param) {
        if (StringUtils.isEmpty(param.get("companyCode"))) {
            return AjaxResult.error("上游系统单号为空");
        } else if (StringUtils.isEmpty(param.get("shipmentType"))) {
            return AjaxResult.error("类型为空");
        } else if (StringUtils.isEmpty(param.get("referCode"))) {
            return AjaxResult.error("上游系统单号为空");
        }
        LambdaQueryWrapper<ShipmentHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentHeader::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(ShipmentHeader::getDeleted,false)
                .in(ShipmentHeader::getCompanyCode, param.get("companyCode"))
                .eq(ShipmentHeader::getShipmentType,param.get("shipmentType"))
                .eq(ShipmentHeader::getReferCode, param.get("referCode"))
                .orderByDesc(ShipmentHeader::getId);
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(lambdaQueryWrapper);

        if (shipmentHeader == null) {
            return AjaxResult.error("没有找到出库单");
        }
        LambdaQueryWrapper<ShipmentDetail> detailLambdaQueryWrapper = Wrappers.lambdaQuery();
        detailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
        Shipment shipment = new Shipment();
        shipment.setShipmentHeader(shipmentHeader);
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(detailLambdaQueryWrapper);
        shipment.setShipmentDetailList(shipmentDetailList);
        return AjaxResult.success(shipment);
    }


    /**
     * 根据出库单号生成出库任务
     */
    @PostMapping("/createShipmentTask")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createShipmentTask(@RequestBody List<TaskIds> taskDetails){
        List<Integer> ids = new ArrayList<>();
        for (TaskIds task : taskDetails) {
            ids.add(task.getTaskId());
        }
        List<Integer> listWithoutDuplicates = ids.stream().distinct().collect(Collectors.toList());
        ShipmentTaskCreateModel shipmentTask = new ShipmentTaskCreateModel();
        List<Integer> taskIds = new ArrayList<>();
        for (Integer id : listWithoutDuplicates) {
            shipmentTask.setShipmentContainerHeaderIds(id);
            AjaxResult ajaxResult = shipmentTaskService.createTaskFromShipmentContainers(shipmentTask);
            if(ajaxResult.hasErr()){
                return ajaxResult;
            }
            Integer taskId = (Integer)ajaxResult.getData();
            taskIds.add(taskId);
        }
        return AjaxResult.success(taskIds);
    }

    @PostMapping("/findShipment")
    @ApiOperation("移动端查询出库单")
    @Log(title = "移动端查询出库单", action = BusinessType.OTHER)
    public AjaxResult findShipment(@RequestBody @ApiParam(value = "物料号") Map<String, String> param){
        String shipmentCode = param.get("shipmentCode");
        String companyCode = param.get("companyCode");
        if (StringUtils.isNull(shipmentCode)){
            return AjaxResult.error("上游系统关联单号为空");
        } else if (StringUtils.isNull(companyCode)){
            return AjaxResult.error("公司编码为空");
        }

        /* 查询入库单,如果数据库中不存在,则调用ERP接口拉取单据,成功后再次查询返回结果*/
        LambdaQueryWrapper<ShipmentHeader> shipmentLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentLambdaQueryWrapper.eq(ShipmentHeader::getCode, shipmentCode)
                .eq(ShipmentHeader::getCompanyCode, companyCode);
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(shipmentLambdaQueryWrapper);
        if(shipmentHeader == null) {
            return AjaxResult.error("没有找到出库单");
        }
        ShipmentDomain shipment = new ShipmentDomain();
        shipment.setShipmentHeader(shipmentHeader);

        LambdaQueryWrapper<ShipmentDetail> shipmentDetailQueryWrapper = Wrappers.lambdaQuery();
        shipmentDetailQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailQueryWrapper);
        shipment.setShipmentDetails(shipmentDetailList);
        return AjaxResult.success(shipment);
    }

    @PostMapping("/searchShipment")
    @ApiOperation("移动端查询出库单")
    @Log(title = "移动端查询出库单", action = BusinessType.OTHER)
    public AjaxResult searchShipment(@RequestBody @ApiParam(value = "物料号") Map<String, String> param){
        String companyCode = param.get("companyCode");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = df.format(new Date());
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -7);
        Date first = c.getTime();
        String start = df.format(first);//前一天
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderQueryWrapper = Wrappers.lambdaQuery();
        shipmentHeaderQueryWrapper.eq(ShipmentHeader::getCompanyCode, companyCode)
                .eq(ShipmentHeader::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .le(ShipmentHeader::getCreated, now)
                .gt(ShipmentHeader::getCreated, start)
                .orderByDesc(ShipmentHeader::getCreated);
        List<ShipmentHeader> receiptHeaderList = shipmentHeaderService.list(shipmentHeaderQueryWrapper);
        return AjaxResult.success(receiptHeaderList);
    }

    @PostMapping("/searchShipmentInCondition")
    @ApiOperation("移动端查询出库单")
    @Log(title = "移动端查询出库单", action = BusinessType.OTHER)
    public AjaxResult searchShipmentInCondition(@RequestBody @ApiParam(value = "物料号") Map<String, String> param){
        String companyCode = param.get("companyCode");
        String code = param.get("code");
        String shipmentType = param.get("shipmentType");
        String lastStatus = param.get("lastStatus");
        String startTime = param.get("startTime");
        String endTime = param.get("endTime");
        LambdaQueryWrapper<ShipmentHeader> shipmentQueryWrapper = Wrappers.lambdaQuery();
        shipmentQueryWrapper.eq(ShipmentHeader::getCompanyCode, companyCode)
                .eq(ShipmentHeader::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .like(StringUtils.isNotEmpty(code), ShipmentHeader::getCode, code)
                .eq(StringUtils.isNotEmpty(shipmentType), ShipmentHeader::getShipmentType, shipmentType)
                .eq(StringUtils.isNotEmpty(lastStatus), ShipmentHeader::getLastStatus, lastStatus)
                .gt(StringUtils.isNotEmpty(startTime), ShipmentHeader::getCreated, startTime)
                .le(StringUtils.isNotEmpty(endTime), ShipmentHeader::getCreated, endTime)
                .orderByDesc(ShipmentHeader::getCreated);
        List<ShipmentHeader>  shipmentHeaderList = shipmentHeaderService.list(shipmentQueryWrapper);
        return AjaxResult.success(shipmentHeaderList);
    }

    @PostMapping("/getShipmentType")
    @ApiOperation("移动端查询入库类型")
    @Log(title = "移动端查询字典信息", action = BusinessType.OTHER)
    public AjaxResult getShipmentType(@RequestBody @ApiParam(value = "物料号") Map<String, String> param){
        LambdaQueryWrapper<ShipmentType> shipmentTypeLambda = Wrappers.lambdaQuery();
        List<ShipmentType> shipmentTypes = shipmentTypeService.list(shipmentTypeLambda);
        return AjaxResult.success(shipmentTypes);
    }
}