ItemDownloadApiService.java 4.84 KB
package com.huaheng.api.SSP.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.SSP.domain.SSPmaterialModel;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//物料信息上传
@Service
@Transactional
public class ItemDownloadApiService {

    @Autowired
    MaterialService materialService;

    @Autowired
    private AddressService addressService;


    public String getQGUrl(String containerCode){
        Address address =new Address();
        String number=containerCode.substring(0,1);
        address.setNumber(number);
        address.setParam("QG");
        address.setWarehouseCode(ShiroUtils.getWarehouseCode());
        String url=addressService.selectEntity(address).getUrl();
        return url;
    }

    //private  static String address="http://172.16.30.238:4322/api/SSP/";

    public AjaxResult Idls(String url){
        String ur=url.substring(0,25);
        //Address address =new Address();
        //address.setUrl(ur);
        //String warehouseCode=addressService.selectWarehouse(address).getWarehouseCode();
        String warehouseCode=addressService.list(new LambdaQueryWrapper<Address>().like(Address::getUrl,ur)).get(0).getWarehouseCode();
        if (StringUtils.isEmpty(warehouseCode)){
            return AjaxResult.success("该地址没找到仓库");
        }
        //Material material = new Material();
        //material.setWarehouseCode(warehouseCode);
        //material.setZoneCode("QG");
        //material.setUserDef2("0");
        //Material materials = materialService.selectFirstEntity(material);
        List<Material> qgList = materialService.list(new LambdaQueryWrapper<Material>().eq(Material::getWarehouseCode,warehouseCode)
                .eq(Material::getZoneCode, "QG").last("limit 1"));

        if (qgList.size() == 0){
            return AjaxResult.success("系统中没有钱柜的物料!!或钱柜的物流已全部发完!!");
        }
        Material qg = qgList.get(0);
        try {
            SSPmaterialModel smm = new SSPmaterialModel();
            smm.setSpecification(qg.getSpec());//规格
            smm.setCubeCode(qg.getZoneCode());
            smm.setCompanyCode(qg.getCompanyCode());
            smm.setItemCode(qg.getCode());
            smm.setItemName(qg.getName());
            smm.setItemBarcode(qg.getCode());
            smm.setWarehouseCode(qg.getWarehouseCode());
            smm.setItemUnit(qg.getUnit());//单位
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
            smm.setcDatetime(df.format(new Date()));

            String JsonParam = JSON.toJSONString(smm);
            String result = HttpUtils.bodypost(url, JsonParam,"CS0001");
            AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);

            Material upm = new Material();
            upm.setId(qg.getId());
            upm.setUserDef2("1");
            materialService.updateById(upm);
            return ajaxResult;
        }catch (Exception e){
            throw new ServiceException("物料信息同步上传失败");
        }
    }

    public AjaxResult sendMaterial(Material material,String containerCode){
        SSPmaterialModel smm = new SSPmaterialModel();
        smm.setSpecification(material.getSpec());
        smm.setCubeCode(material.getZoneCode());
        smm.setCompanyCode(material.getCompanyCode());
        smm.setItemCode(material.getCode());
        smm.setItemName(material.getName());
        smm.setItemBarcode(material.getCode());
        smm.setWarehouseCode(material.getWarehouseCode());
        smm.setItemUnit(material.getUnit());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
        smm.setcDatetime(df.format(new Date()));
        String url=this.getQGUrl(containerCode)+"ItemDownload";
        String JsonParam = JSON.toJSONString(smm);
        String result = HttpUtils.bodypost(url, JsonParam,"CS0001");
        if(StringUtils.isEmpty(result)){
            throw new ServiceException("接口地址错误或服务器连接不到或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        return ajaxResult;
    }


}