KuaidiHeaderDataService.java 12.5 KB
package com.huaheng.pc.shipment.kuaidiHeader.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.KDCertification.domain.KDCertification;
import com.huaheng.pc.config.KDCertification.service.KDCertificationService;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.token.service.ITokensService;
import com.huaheng.pc.shipment.kuaidiHeader.domain.KuaidiHeader;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.u8.domain.ICSShipmentHeader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;


/**
 * 【请填写功能名称】 服务层实现
 *
 * @author huaheng
 * @date 2022-09-07
 */
@Service
public class KuaidiHeaderDataService {

    @Resource
    private KuaidiHeaderService kuaidiHeaderService;
    @Resource
    private ITokensService tokens;
    @Resource
    private KuaidiSendService kuaidiSendService;
    @Resource
    private KDCertificationService kdCertificationService;


    public String createCode(String shipmentType) {
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String maxCode = null;
        KuaidiHeader kuaidiHeader1 = new KuaidiHeader();
        kuaidiHeader1.setType(shipmentType);
        KuaidiHeader kuaidiHeader = getByOrderBill(kuaidiHeader1);
        if (kuaidiHeader != null) {
            maxCode = kuaidiHeader.getCode();
        }
        if (maxCode != null && maxCode.length() > 13 && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now))) {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
            code = shipmentType + df.format(now) + String.format("%05d", Count + 1);
        } else {
            code = shipmentType + df.format(now) + "00001";
        }
        return code;
    }


    public KuaidiHeader getByOrderBill(KuaidiHeader kuaidiHeader) {
        LambdaQueryWrapper<KuaidiHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(KuaidiHeader::getDeleted, false);
        lambdaQueryWrapper.eq(KuaidiHeader::getWarehouseCode, kuaidiHeader.getWarehouseCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getOrderBill()), KuaidiHeader::getOrderBill, kuaidiHeader.getOrderBill());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(kuaidiHeader.getType()), KuaidiHeader::getType, kuaidiHeader.getType());
        lambdaQueryWrapper.orderByDesc(KuaidiHeader::getId);
        lambdaQueryWrapper.last("limit 1");
        return kuaidiHeaderService.getOne(lambdaQueryWrapper);
    }

    /**
     * 根据出库单
     *
     * @param shipmentHeader
     * @return
     */
    public KuaidiHeader getModelByShipment(ShipmentHeader shipmentHeader) {
        KuaidiHeader kuaidiHeader = new KuaidiHeader();
        kuaidiHeader.setRecName(shipmentHeader.getRecName());
        kuaidiHeader.setRecMobile(shipmentHeader.getRecMobile());
        kuaidiHeader.setRecAddr(shipmentHeader.getRecAddr());
        kuaidiHeader.setRecCompany(shipmentHeader.getRecCompany());
        kuaidiHeader.setSendName(shipmentHeader.getSendName());
        kuaidiHeader.setSendMobile(shipmentHeader.getSendMobile());
        kuaidiHeader.setSendAddr(shipmentHeader.getSendAddr());
        kuaidiHeader.setSendCompany(shipmentHeader.getSendCompany());
        kuaidiHeader.setUWarehouseCode(shipmentHeader.getUWarehouseCode());
        kuaidiHeader.setCode(shipmentHeader.getCode());
//        kuaidiHeader.setWarehouseId(shipmentHeader.getWarehouseId());
        kuaidiHeader.setWarehouseCode(shipmentHeader.getWarehouseCode());
//        kuaidiHeader.setCompanyId(shipmentHeader.getCompanyId());
        kuaidiHeader.setCompanyCode(shipmentHeader.getCompanyCode());
        kuaidiHeader.setSourceCode(shipmentHeader.getSourceCode());
        kuaidiHeader.setType(shipmentHeader.getShipmentType());
        kuaidiHeader.setRemark(shipmentHeader.getRemark());
        kuaidiHeader.setCreated(new Date());
        kuaidiHeader.setCDepName(shipmentHeader.getCDepName());
        kuaidiHeader.setCreatedBy(shipmentHeader.getCreatedBy());
        kuaidiHeader.setEnable(shipmentHeader.getEnable());
        kuaidiHeader.setDeleted(shipmentHeader.getDeleted());
        return kuaidiHeader;
    }

    public KuaidiHeader getModelByICSShipment(ICSShipmentHeader icsShipmentHeader) {

        KuaidiHeader kuaidiHeader = new KuaidiHeader();

        kuaidiHeader.setType("CO");
        String code = this.createCode(kuaidiHeader.getType());

        kuaidiHeader.setRecName(icsShipmentHeader.getRecName());
        kuaidiHeader.setRecMobile(icsShipmentHeader.getRecMobile());
        kuaidiHeader.setRecAddr(icsShipmentHeader.getRecAddr());
        kuaidiHeader.setRecCompany(icsShipmentHeader.getRecCompany());
        kuaidiHeader.setSendName(icsShipmentHeader.getSendName());
        kuaidiHeader.setSendMobile(icsShipmentHeader.getSendMobile());
        kuaidiHeader.setSendAddr(icsShipmentHeader.getSendAddr());
        kuaidiHeader.setSendCompany(icsShipmentHeader.getSendCompany());
        kuaidiHeader.setCode(code);
        kuaidiHeader.setWarehouseId(1);
        kuaidiHeader.setWarehouseCode("CS0001");
        kuaidiHeader.setSourceCode(icsShipmentHeader.getCDLCode());
        kuaidiHeader.setCreated(new Date());
        kuaidiHeader.setCreatedBy(icsShipmentHeader.getCMaker());
        kuaidiHeader.setCDepName(icsShipmentHeader.getCDepName());
        kuaidiHeader.setEnable(0);
        kuaidiHeader.setDeleted(false);
        return kuaidiHeader;
    }

    public void checkImport(KuaidiHeader map) {
        if (StringUtils.isNull(map.getSendName())) {
            throw new ServiceException("发件人信息为空");
        }
        if (StringUtils.isNull(map.getSendMobile())) {
            throw new ServiceException("发件人号码信息为空");
        }
        if (StringUtils.isNull(map.getSendAddr())) {
            throw new ServiceException("发件人地址信息为空");
        }
        if (StringUtils.isNull(map.getSendCompany())) {
            throw new ServiceException("发件人公司信息为空");
        }
        if (StringUtils.isNull(map.getRecName())) {
            throw new ServiceException("收件人信息为空");
        }
        if (StringUtils.isNull(map.getRecMobile())) {
            throw new ServiceException("收件人号码信息为空");
        }
        if (StringUtils.isNull(map.getRecAddr())) {
            throw new ServiceException("收件人地址信息为空");
        }
        if (StringUtils.isNull(map.getRecCompany())) {
            throw new ServiceException("收件人公司信息为空");
        }
        if (StringUtils.isNull(map.getCompanyId())) {
            throw new ServiceException("公司ID信息为空");
        }
        if (StringUtils.isNull(map.getCDepName())) {
            throw new ServiceException("部门信息为空");
        }
        if (StringUtils.isNull(map.getSourceCode())) {
            throw new ServiceException("发货单号为空");
        }
        if (StringUtils.isNull(map.getOrderBill())) {
            throw new ServiceException("出库单号为空");
        }
    }

    public KuaidiHeader createHeaderByImport(KuaidiHeader kuaidiHeader, KuaidiHeader map, Company company) {
//        BeanUtils.copyBeanProp(user, obj);
        map.setType(kuaidiHeader.getType());
        map.setCompanyCode(company.getCode());
       /* kuaidiHeader.setRecName(map.get("收件人").toString());
        kuaidiHeader.setRecMobile(map.get("收件人号码").toString());
        kuaidiHeader.setRecAddr(map.get("收件人地址").toString());
        kuaidiHeader.setRecCompany(map.get("收件人公司").toString());
        kuaidiHeader.setSendName(map.get("发件人").toString());
        kuaidiHeader.setSendMobile(map.get("发件人号码").toString());
        kuaidiHeader.setSendAddr(map.get("发件人地址").toString());
        kuaidiHeader.setSendCompany(map.get("发件人公司").toString());
        kuaidiHeader.setSourceCode(map.get("发货单号").toString());
        kuaidiHeader.setOrderBill(map.get("出库单号").toString());
        kuaidiHeader.setCompanyId(DataUtils.getInteger(map.get("公司ID")));
        kuaidiHeader.setCompanyCode(company.getCode());
        kuaidiHeader.setCDepName(map.get("部门").toString());
        if(StringUtils.isNotNull(map.get("备注"))) {
            kuaidiHeader.setRemark(map.get("备注").toString());
        }*/
        return map;
    }


    public AjaxResult sendKD(KuaidiHeader kuaidiHeader) {
        //查出销售单的快递信息
        KuaidiHeader condition = kuaidiHeaderService.getById(kuaidiHeader.getId());
        if (condition == null) {
            return AjaxResult.error("快递单无法查到,是否被删除");
        }
        String orderNum = null;
        if (StringUtils.isEmpty(condition.getKuaidiNum())) {
            orderNum = condition.getCode() + condition.getId() + "1";
            condition.setKuaidiNum(orderNum);
        } else {
            orderNum = condition.getKuaidiNum() + "1";
            condition.setKuaidiNum(orderNum);
        }
        condition.setKuaidiCom(kuaidiHeader.getKuaidiCom());
        //整合快递信息
        Map<String, String> data = new TreeMap<>();
        data.put("recName", condition.getRecName());
        data.put("recMobile", condition.getRecMobile());
        data.put("recAddr", condition.getRecAddr());
        data.put("recCompany", condition.getRecCompany());
        data.put("sendName", condition.getSendName());
        data.put("sendMobile", condition.getSendMobile());
        data.put("sendAddr", condition.getSendAddr());
        data.put("sendCompany", condition.getSendCompany());
        data.put("kuaidiCom", condition.getKuaidiCom());
        data.put("orderNum", orderNum);
        data.put("cargo", "机械配件");
        String comment = "";
        if (StringUtils.isNotEmpty(condition.getSourceCode())) {
            comment = "发货单:" + condition.getSourceCode() + ";";
        }
        if (StringUtils.isNotEmpty(condition.getOrderBill())) {
            comment = comment + "出货单:" + condition.getOrderBill();
        }

        data.put("comment", comment);
        String jsonObject = JSONObject.toJSONString(data);

        /*Token token =new Token();
        token.setType("kd");
        String accessToken = tokens.getTokenByParam(token).getAccesstoken();*/
//        KDCertification kdCertification = new KDCertification();
//        String accessToken = kdCertificationService.getOne(kdCertification).getAccessToken();
        LambdaQueryWrapper<KDCertification> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(KDCertification::getWarehouseCode, condition.getWarehouseCode());
        KDCertification kdCertification = kdCertificationService.getOne(wrapper);
        try {
            String message = kuaidiSendService.send(kdCertification, jsonObject);
            System.out.println(message);
            JSONObject object = JSONObject.parseObject(message);
            String code = object.getString("status");
            if (!"200".equals(code)) {
                return AjaxResult.error(object.getString("message"));
            }
        } catch (Exception e) {
            throw new ServiceException("快递信息send异常");
        }
        kuaidiHeaderService.updateById(condition);
        return AjaxResult.success("成功");
    }

    //快递打印
    public AjaxResult printKD(String kuaidiNum, String warehouseCode) {
        if (StringUtils.isEmpty(kuaidiNum)) {
            return AjaxResult.error("没有导入快递信息");
        }
        //整合快递信息
        String message = null;
        KDCertification kdCertification = new KDCertification();
        kdCertification.setWarehouseCode(warehouseCode);
        LambdaQueryWrapper<KDCertification> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(KDCertification::getWarehouseCode, warehouseCode);
        kdCertification = kdCertificationService.getOne(wrapper);
        try {
            message = kuaidiSendService.quickPrintByClass(kdCertification, kuaidiNum);
            System.out.println(message);
        } catch (Exception e) {
            throw new ServiceException("快递信息send异常");
        }
        return AjaxResult.success(message);
    }


}