KuaidiHeaderService.java 20.2 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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
package com.huaheng.pc.shipment.kuaidiHeader.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.DataUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.file.FileUploadUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.config.HuaHengConfig;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.DictService;
import com.huaheng.pc.config.KDCertification.domain.KDCertification;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.token.domain.Token;
import com.huaheng.pc.shipment.kuaidiDetail.domain.KuaidiDetail;
import com.huaheng.pc.shipment.kuaidiDetail.service.KuaidiDetailService;
import com.huaheng.pc.shipment.kuaidiHeader.domain.KuaidiHeader;
import com.huaheng.pc.shipment.kuaidiHeader.mapper.KuaidiHeaderMapper;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.u8.domain.ICSShipmentHeader;
import com.huaheng.pc.u8.service.ICSShipmentHeaderService;
import com.huaheng.pc.u8.service.IcsKuaidiService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 【请填写功能名称】 服务层实现
 * 
 * @author huaheng
 * @date 2022-09-07
 */
@Service
public class KuaidiHeaderService extends ServiceImpl<KuaidiHeaderMapper, KuaidiHeader>  {

    @Resource
    private IDictDataService dictDataService;
    @Resource
    private IcsKuaidiService icsKuaidiService;
    @Resource
    private KuaidiDetailService kuaidiDetailService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ICSShipmentHeaderService icsShipmentHeaderService;
    @Resource
    private KuaidiHeaderDataService kuaidiHeaderDataService;
    @Resource
    private CompanyService companyService;


    public AjaxResult<Boolean> saveHeader(KuaidiHeader kuaidiHeader)  {
        kuaidiHeader.setType("CO");
        if(dictDataService.checkConfig("shipmentType", kuaidiHeader.getType()) == false)
        {
            return  AjaxResult.error("没有对应的出库单类型");
        }
        String code = kuaidiHeaderDataService.createCode(kuaidiHeader.getType());
        kuaidiHeader.setId(null);
        kuaidiHeader.setLastUpdated(null);
        kuaidiHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        kuaidiHeader.setCreated(null);
        kuaidiHeader.setCreatedBy(ShiroUtils.getLoginName());
        kuaidiHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        kuaidiHeader.setCode(code);
        kuaidiHeader.setDeleted(false);
        boolean result = this.save(kuaidiHeader);
        return  AjaxResult.toAjax(result);
    }




    /**
     * 从U8获取信息
     * 1、检查出库单号或者发货单号是否为空,如果为空则报错
     * 2、如果是出库单获取信息,剔除在快递单中已存在的出库单
     * 3、如果是发货单,分组,剔除在快递单中已存在的出库单
     *
     */
    @Transactional
    public AjaxResult saveU8Kuaidi(KuaidiHeader kuaidiHeader) {
        //1、检查出库单号或者发货单号是否为空,如果为空则报错
        if(StringUtils.isEmpty(kuaidiHeader.getOrderBill()) &&
                StringUtils.isEmpty(kuaidiHeader.getSourceCode())){
            return AjaxResult.error("U8出库单号和发货单号为空,请填下U8结束出库单号或发货单单号");
        }
        //2、如果是出库单获取信息,剔除在快递单中已存在的出库单
        if(!StringUtils.isEmpty(kuaidiHeader.getOrderBill())&& StringUtils.isEmpty(kuaidiHeader.getFromCode())){
           return  saveu8KuaiOne(kuaidiHeader);

        }
        //批量获取出库单单据
        if(StringUtils.isNotEmpty(kuaidiHeader.getOrderBill()) && StringUtils.isNotEmpty(kuaidiHeader.getFromCode())) {
            return saveu8KuaiTwo(kuaidiHeader);
        }
        //3、如果是发货单,分组,剔除在快递单中已存在的出库单
        if(StringUtils.isEmpty(kuaidiHeader.getOrderBill()) &&!StringUtils.isEmpty(kuaidiHeader.getSourceCode())) {
            return saveu8KuaiThree(kuaidiHeader);
        }
        return AjaxResult.success("成功");
    }

    //如果是出库单获取信息,剔除在快递单中已存在的出库单
    public AjaxResult saveu8KuaiOne(KuaidiHeader kuaidiHeader){
        KuaidiHeader kuaidiHeader1=new KuaidiHeader();
        kuaidiHeader1.setOrderBill(kuaidiHeader.getOrderBill());
        KuaidiHeader condition=kuaidiHeaderDataService.getByOrderBill(kuaidiHeader1);
        if(condition!=null){
            return AjaxResult.success("该出库单的快递信息已存在,请在搜索栏搜索");
        }
        kuaidiHeader.setSourceCode("");
        //(2)获取子单列表
        List<Map<String,String>> list = icsKuaidiService.getKDMeseeage(kuaidiHeader);
        List<KuaidiDetail> kuaidiDetails = new ArrayList<>();
        for (Map<String, String> map : list) {
            KuaidiDetail kuaidiDetail = this.getEntityFromU8(map);
            kuaidiDetails.add(kuaidiDetail);
        }
        //(3)根据快递子单反推快递主单,通过发货单号搜索wms出库单和中间表
        Map<String,Object> map= this.checkHeader(kuaidiDetails.get(0).getSourceCode());
        Boolean flag = ((Boolean)map.get("flag")).booleanValue();
        if(flag == false){
            return AjaxResult.error("快递信息不存在");
        }
        KuaidiHeader header = JSONObject.parseObject(JSON.toJSONString(map.get("header")),KuaidiHeader.class);
        header.setOrderBill(kuaidiDetails.get(0).getOrderBill());
        header.setUserDef1(kuaidiDetails.get(0).getUserDef1());
        this.save(header);
        for (KuaidiDetail detail : kuaidiDetails) {
            detail.setShipmentId(header.getId());
            detail.setShipmentCode(header.getCode());
            kuaidiDetailService.save(detail);
        }
        return AjaxResult.success("成功");
    }
    //批量获取出库单单据
    public AjaxResult saveu8KuaiTwo(KuaidiHeader kuaidiHeader){
        //1、获取所有出库单信息
        List<Map<String, String>> mapList= icsKuaidiService.getKDMeseeage(kuaidiHeader);
        //2、发货单分组,查看是否符合快递条件
        Set<String> hashSet= new HashSet();
        for (Map<String, String> item : mapList) {
            hashSet.add(item.get("subconsignmentcode"));
        }
        //循环判断
        for (String s : hashSet) {
            Map<String, Object> hashmap= this.checkHeader(s);
            Boolean flag= ((Boolean) hashmap.get("flag")).booleanValue();
            //不符合条件的剔除
            if (flag == false) {
                continue;
            }
            KuaidiHeader header= JSONObject.parseObject(JSON.toJSONString(hashmap.get("header")), KuaidiHeader.class);
            List<KuaidiDetail> kuaidiDetails= new ArrayList<>();
            for (Map<String, String> item : mapList) {
                //获取不存在快递单数据里的单据
                if (item.get("subconsignmentcode").equals(s)) {
                    KuaidiHeader condition = new KuaidiHeader();
                    condition.setOrderBill(item.get("code"));
                    condition= kuaidiHeaderDataService.getByOrderBill(condition);
                    if (condition == null) {
                        KuaidiDetail  kuaidiDetail= this.getEntityFromU8(item);
                        kuaidiDetails.add(kuaidiDetail);
                    }
                }
            }
            if (kuaidiDetails.isEmpty()) {
                continue;
            }
            Set<String> stringSet= new HashSet<>();
            //再把子单以出库单分组
            for (KuaidiDetail kuaidiDetail : kuaidiDetails) {
                stringSet.add(kuaidiDetail.getOrderBill());
            }
            for (String s1 : stringSet) {
                header.setId(null);
                header.setOrderBill(s1);
                header.setUserDef1(kuaidiDetails.get(0).getUserDef1());
                this.save(header);
                for (KuaidiDetail detail : kuaidiDetails) {
                    if (detail.getOrderBill().equals(s1)) {
                        detail.setShipmentId(header.getId());
                        detail.setShipmentCode(header.getCode());
                        kuaidiDetailService.save(detail);
                    }
                }
            }
        }
        return AjaxResult.success("成功");
    }
    //3、如果是发货单,分组,剔除在快递单中已存在的出库单
    public AjaxResult saveu8KuaiThree(KuaidiHeader kuaidiHeader){
        //(1)检验发货单号的发货信息是否可以快递
        Map<String,Object> hashmap= this.checkHeader(kuaidiHeader.getSourceCode());
        Boolean flag = ((Boolean)hashmap.get("flag")).booleanValue();
        if(flag == false){
            return AjaxResult.error("快递信息不存在");
        }
        KuaidiHeader header = JSONObject.parseObject(JSON.toJSONString(hashmap.get("header")),KuaidiHeader.class);
        //(2) 分组,以出库单号为分组基准
        List<Map<String, String>> mapList = icsKuaidiService.getKDMeseeage(kuaidiHeader);
        Set<String> hashSet = new HashSet();
        for (Map<String, String> item : mapList) {
            hashSet.add(item.get("code"));
        }
        int number = 0;
        for (String item : hashSet) {
            List<KuaidiDetail> kuaidiDetails = new ArrayList<>();

            //(3)剔除在快递单中已存在的出库单
            KuaidiHeader condition = new KuaidiHeader();
            condition.setOrderBill(item);
            condition = kuaidiHeaderDataService.getByOrderBill(condition);
            if(condition ==null){
                for (Map<String, String> map : mapList) {
                    if(map.get("code").equals(item)){
                        KuaidiDetail kuaidiDetail = this.getEntityFromU8(map);
                        kuaidiDetails.add(kuaidiDetail);
                    }
                }
            }
            if(kuaidiDetails.isEmpty()){
                number++;
                continue;
            }
            //(4)生成主单和子单
            header.setId(null);
            header.setOrderBill(item);
            header.setUserDef1(kuaidiDetails.get(0).getUserDef1());
            this.save(header);
            for (KuaidiDetail detail : kuaidiDetails) {
                detail.setShipmentId(header.getId());
                detail.setShipmentCode(header.getCode());
                kuaidiDetailService.save(detail);
            }
        }
        if(number == hashSet.size()){
            return AjaxResult.success("该发货单号的快递信息都存在,请在搜索栏以发货单号搜索");
        }
        return AjaxResult.success("成功");
    }



    //根据获取的U8的信息生成快递子单
    public KuaidiDetail getEntityFromU8(Map<String,String> map){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = simpleDateFormat.parse(map.get("date"));
        } catch (ParseException e) {
            throw new ServiceException("时间转换格式错误");
        }
        KuaidiDetail kuaidiDetail = new KuaidiDetail();
        kuaidiDetail.setUWarehouseCode(map.get("warehousecode"));
        kuaidiDetail.setUnit(map.get("cmassunitname"));
        kuaidiDetail.setWarehouseId(1);
        kuaidiDetail.setWarehouseCode("CS0001");
        kuaidiDetail.setSourceCode(map.get("subconsignmentcode"));
        kuaidiDetail.setMaterialCode(map.get("inventorycode"));
        kuaidiDetail.setInventoryStatus("good");
        kuaidiDetail.setQty(new BigDecimal(map.get("quantity")));
        kuaidiDetail.setCreated(date);
        kuaidiDetail.setCreatedBy(map.get("maker"));
        kuaidiDetail.setOrderBill(map.get("code"));
        kuaidiDetail.setUserDef1(map.get("auditdate"));
        return kuaidiDetail;
    }

    /**
     * 根据wms出库单或者中间表检验发货单信息
     *
     * @param code
     * @return
     */
    public Map<String,Object> checkHeader(String code) {
        //先查看是否存在此发货单的快递信息
        //wms出库单检验
        Boolean flag = true;
        LambdaQueryWrapper<ShipmentHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentHeader::getSourceCode,code);
        lambdaQueryWrapper.eq(ShipmentHeader::getKdEable,1);
        lambdaQueryWrapper.last("limit 1");
        ShipmentHeader shipmentHeader=shipmentHeaderService.getOne(lambdaQueryWrapper);
        ICSShipmentHeader icsShipmentHeader = new ICSShipmentHeader();
        if(shipmentHeader == null || StringUtils.isEmpty(shipmentHeader.getRecMobile())){
            //wms中间表检验
            icsShipmentHeader.setCDLCode(code);
            icsShipmentHeader.setCSCName("快递");
            icsShipmentHeader = icsShipmentHeaderService.selectModel(icsShipmentHeader);
            if(icsShipmentHeader == null || StringUtils.isEmpty(icsShipmentHeader.getRecMobile())){
                flag = false;
            }
        }
        KuaidiHeader header = new KuaidiHeader();
        if(shipmentHeader != null && StringUtils.isNotEmpty(shipmentHeader.getRecMobile()) ){
            header = kuaidiHeaderDataService.getModelByShipment(shipmentHeader);
        }
        if(shipmentHeader == null && icsShipmentHeader != null && StringUtils.isNotEmpty(icsShipmentHeader.getRecMobile())){
            header = kuaidiHeaderDataService.getModelByICSShipment(icsShipmentHeader);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("flag",flag);
        map.put("header",header);
        return map;
    }


    public Boolean importData(List<KuaidiHeader> maps,Boolean num) {
        //实体转换
        int number = 0;
        for (KuaidiHeader map : maps) {
            KuaidiHeader kuaidiHeader = new KuaidiHeader();
            kuaidiHeader.setType("CO");
            String code = kuaidiHeaderDataService.createCode(kuaidiHeader.getType());
            //检查数据
            kuaidiHeaderDataService.checkImport(map);
            Company company = companyService.getById(DataUtils.getInteger(map.getCompanyId()));
            if(company == null){
                throw new ServiceException("公司ID错误");
            }
            kuaidiHeader= kuaidiHeaderDataService.createHeaderByImport(kuaidiHeader,map,company);
            if(num == true) {
                if (StringUtils.isNotNull(map.getTotalWeight())) {
                    kuaidiHeader.setTotalWeight(map.getTotalWeight().toString());
                }else {
                    throw new ServiceException("重量为空");
                }
                if (StringUtils.isNotNull(map.getKuaidiCom())) {
                    kuaidiHeader.setKuaidiCom(map.getKuaidiCom().toString());
                }else {
                    throw new ServiceException("快递公司为空");
                }
                if (StringUtils.isNotNull(map.getTrackCode())) {
                    kuaidiHeader.setTrackCode(map.getTrackCode().toString());
                }else {
                    throw new ServiceException("快递单号为空");
                }
            }
            kuaidiHeader.setDeleted(num);
            kuaidiHeader.setCode(code);
            kuaidiHeader.setWarehouseId(2);
            kuaidiHeader.setWarehouseCode("KS0001");
            kuaidiHeader.setCreated(new Date());
            kuaidiHeader.setCreatedBy(ShiroUtils.getUserName());
            kuaidiHeader.setEnable(1);
            boolean tag= this.save(kuaidiHeader);
            if(!tag ){
                throw new ServiceException("存储快递信息失败");
            }
        }
        return true;
    }

    @Transactional
    public AjaxResult sendKD(KuaidiHeader kuaidiHeader) {
        //查出销售单的快递信息

        return kuaidiHeaderDataService.sendKD(kuaidiHeader);
    }
    //批量导入到快递100
    @Transactional
    public AjaxResult sendKDS(KuaidiHeader kuaidiHeader) {
        String ids = kuaidiHeader.getOrderBill();
        if (StringUtils.isEmpty(ids)) {
            return AjaxResult.error("没有选中快递数据");
        }

        for (Integer id : Convert.toIntArray(ids)) {
            KuaidiHeader header = this.getById(id);
            if(header == null){
                return  AjaxResult.error("id为"+id +"的快递信息不存在");
            }
            header.setKuaidiCom(kuaidiHeader.getKuaidiCom());
            kuaidiHeaderDataService.sendKD(header);
        }
        return AjaxResult.success("成功");
    }


    //快递打印
    public AjaxResult printKD(String kuaidiNum) {
        if(StringUtils.isEmpty(kuaidiNum)){
            return AjaxResult.error("没有导入快递信息");
        }
        return kuaidiHeaderDataService.printKD(kuaidiNum);
    }

    //快递批量打印
    public AjaxResult printKDS(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return AjaxResult.error("没有选中快递数据");
        }
        Integer[] kdIds = Convert.toIntArray(ids);
        if(kdIds.length>19){
            return AjaxResult.error("一次性批量快递打印只能在20张以内");
        }

        StringBuilder sb = new StringBuilder();
        boolean flag = false;
        String kuaidiNum = "";
        for(Integer id :kdIds){
            KuaidiHeader header = this.getById(id);
            if(header == null){
                return  AjaxResult.error("id为"+id +"的快递信息不存在");
            }
            if(StringUtils.isEmpty(header.getKuaidiNum())){
                return  AjaxResult.error("id为"+id +"的快递信息没有导入到快递100");
            }
            if(flag){
                sb.append(",");
            }else{
                flag = true;
            }
            sb.append(header.getKuaidiNum());
        }
        kuaidiNum = sb.toString();
        return kuaidiHeaderDataService.printKD(kuaidiNum);
    }
    private static String pictureUrl = "http://172.16.29.88:8016/wms/profile/wmsfile/";
    //获取图片地址
    public AjaxResult getPicture() {
        //获取图片的判断条件,要有重量,快递单号,status=0
        KuaidiHeader kuaidiHeader = this.getModelForPicture();
        if(kuaidiHeader == null){
            return AjaxResult.success("没有符合条件的快递单");
        }
        String[] codes =Convert.toStrArray(kuaidiHeader.getTrackCode());

        String filePath = HuaHengConfig.getProfile() +"wmsfile/"+ codes[0]+".jpg";
        File file = new File(filePath);
        MultipartFile multipartFile = null;
        try {
            multipartFile = FileUploadUtils.fileToMultipartFile(file);
        }catch (Exception e){
            kuaidiHeader.setStatus(2L);
            this.updateById(kuaidiHeader);
            throw new ServiceException("图片不存在");
        }
        try {
            FileUploadUtils.upload(multipartFile);
        }catch (Exception e){
            throw new ServiceException("图片上传失败");
        }

        kuaidiHeader.setStatus(1L);
        kuaidiHeader.setPicture(pictureUrl+codes[0]+".jpg");
        this.updateById(kuaidiHeader);
        return AjaxResult.success("成功");
    }
    public KuaidiHeader getModelForPicture() {
        LambdaQueryWrapper<KuaidiHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.isNotNull(KuaidiHeader::getTrackCode);
        lambdaQueryWrapper.eq(KuaidiHeader::getStatus,0L);
        lambdaQueryWrapper.last("limit 1");
        return this.getOne(lambdaQueryWrapper);
    }
}