package zy.cloud.wms.common.service.erp; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.common.Cools; import com.core.common.DateUtils; import com.core.exception.CoolException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import zy.cloud.wms.common.service.erp.entity.GetBasisResult; import zy.cloud.wms.common.service.erp.entity.GetOrderResult; import zy.cloud.wms.manager.entity.Cstmr; import zy.cloud.wms.manager.entity.CustOrder; import zy.cloud.wms.manager.entity.Mat; import zy.cloud.wms.manager.entity.Tag; import zy.cloud.wms.manager.mapper.TagMapper; import zy.cloud.wms.manager.service.*; import zy.cloud.wms.manager.utils.HttpHandler; import zy.cloud.wms.manager.utils.NodeUtils; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * erp任务控制器 * Created by vincent on 2020/11/27 */ @Slf4j @Component @RestController public class ErpScheduler { public static final String URI = "http://8.133.182.21:8080/api"; public static final String GET_ORDERS = "cM/basis/getOrders"; public static final String GET_BASIS = "cM/basis/getBasis"; public static final String UPLOAD_STOCK = "cM/basis/uploadStock"; @Autowired private CustOrderService custOrderService; @Autowired private MatService matService; @Autowired private CstmrService cstmrService; @Autowired private LocDetlService locDetlService; @Autowired private TagService tagService; @Autowired private TagMapper tagMapper; /** * 商品下载 */ // @Scheduled(cron = "0/5 * * * * ? ") @GetMapping("/test1") // @PostConstruct public void getBasis1Execute(){ try { Map param = new HashMap<>(); param.put("rec", 0); param.put("Flag", 1); String response = new HttpHandler.Builder() .setUri(URI) .setPath(GET_BASIS) .setJson(JSON.toJSONString(param)) .build() .doPost(); if (!Cools.isEmpty(response)) { log.info(response); Date now = new Date(); Result result = JSON.parseObject(response, Result.class); if (result.getCode() != 1) { return; } List list = JSON.parseArray(result.getData(), GetBasisResult.class); Tag first = null; for (GetBasisResult data : list) { Mat mat = matService.selectByMatnr(data.getUserCode()); String modifyDate = data.getModifyDate(); Date updateTime = null; if (!Cools.isEmpty(modifyDate)) { updateTime = DateUtils.convert(data.getModifyDate()); } // 分类 Tag second = tagService.selectOne(new EntityWrapper().eq("name", data.getYjfl()).eq("level", 2)); if (second == null) { if (first == null) { first = tagService.selectOne(new EntityWrapper().eq("level", 1)); if (first == null) { throw new CoolException("全部分类出错"); } } NodeUtils nodeUtils = new NodeUtils(); nodeUtils.executePath(first.getId()); second = new Tag( null, // 编号 data.getYjfl(), // 名称 first.getId(), // 父级 first.getName(), // 父级名称 nodeUtils.path.toString(), // 关联路径 nodeUtils.pathName.toString(), // 关联路径名 0, // 类型 null, // 负责人 null, // 图片 null, // 简要描述 null, // 数量 2, // 等级 null, // 排序 1, // 状态 now, // 添加时间 null, // 添加人员 now, // 修改时间 null, // 修改人员 null // 备注 ); if (tagMapper.insert(second) == 0) { throw new CoolException("插入分类失败"); } } Tag third = tagService.selectOne(new EntityWrapper().eq("level", 3).eq("name", data.getEjfl()).eq("parent_name", data.getYjfl())); if (third == null) { NodeUtils nodeUtils = new NodeUtils(); nodeUtils.executePath(second.getId()); third = new Tag( null, // 编号 data.getEjfl(), // 名称 second.getId(), // 父级 second.getName(), // 父级名称 nodeUtils.path.toString(), // 关联路径 nodeUtils.pathName.toString(), // 关联路径名 0, // 类型 null, // 负责人 null, // 图片 null, // 简要描述 null, // 数量 3, // 等级 null, // 排序 1, // 状态 now, // 添加时间 null, // 添加人员 now, // 修改时间 null, // 修改人员 null // 备注 ); if (tagMapper.insert(third) == 0) { throw new CoolException("插入分类失败"); } } if (null == mat) { mat = new Mat( String.valueOf(data.getRec()), // 编号 null, // 所属区域 third.getId(), // 所属归类 data.getUserCode(), // 商品编号 data.getFullName(), // 商品名称 null, // 名称 data.getStandard(), // 规格 null, // 型号 null, // 批号 data.getUnit3(), // 单位 data.getBarcode(), // 条码 null, // 单据类型 null, // 单据编号 null, // 客户名称 null, // 品项数 null, // 库存余量 null, // 重量 1, // null, // 添加人员 now, // 添加时间 null, // 修改人员 updateTime, // 修改时间 null // 备注 ); matService.insert(mat); } else { mat.setUuid(String.valueOf(data.getRec())); mat.setMatnr(data.getUserCode()); mat.setMaktx(data.getFullName()); mat.setSpecs(data.getStandard()); mat.setUpdateTime(updateTime); mat.setBarcode(data.getBarcode()); mat.setUnit(data.getUnit3()); matService.updateById(mat); } log.info("====>> 更新物料:{}", data.getUserCode()); } } } catch (Exception e) { e.printStackTrace(); } } /** * 客户下载 */ // @Scheduled(cron = "0/5 * * * * ? ") // @PostConstruct public void getBasis2Execute(){ try { Map param = new HashMap<>(); param.put("rec", 0); param.put("Flag", 2); String response = new HttpHandler.Builder() .setUri(URI) .setPath(GET_BASIS) .setJson(JSON.toJSONString(param)) .build() .doPost(); if (!Cools.isEmpty(response)) { log.info(response); Date now = new Date(); Result result = JSON.parseObject(response, Result.class); if (result.getCode() != 1) { return; } List list = JSON.parseArray(result.getData(), GetBasisResult.class); for (GetBasisResult data : list) { Cstmr cstmr = cstmrService.selectOne(new EntityWrapper().eq("uuid", data.getUserCode())); String modifyDate = data.getModifyDate(); Date updateTime = null; if (!Cools.isEmpty(modifyDate)) { updateTime = DateUtils.convert(data.getModifyDate()); } if (null == cstmr) { cstmr = new Cstmr( data.getUserCode(), // 客户编号[非空] data.getFullName(), // 客户名称 null, // 联系人 null, // 联系电话 null, // 联系地址 1, // 状态 null, // 添加人员 now, // 添加时间 null, // 修改人员 updateTime, // 修改时间 String.valueOf(data.getRec()) // 备注 ); cstmrService.insert(cstmr); } else { cstmr.setUuid(data.getUserCode()); cstmr.setName(data.getFullName()); cstmr.setUpdateTime(updateTime); cstmr.setMemo(String.valueOf(data.getRec())); cstmrService.updateById(cstmr); } log.info("====>> 更新客户资料:{}", data.getUserCode()); } } } catch (Exception e) { e.printStackTrace(); } } /** * 持久化销售订单 */ // @Scheduled(cron = "0/3 * * * * ? ") @Transactional public void getOrdersExecute(){ try { Map json = new HashMap<>(); json.put("vchType", 41); String response = new HttpHandler.Builder() .setUri(URI) .setPath(GET_ORDERS) .setJson(JSON.toJSONString(json)) .build() .doPost(); if (!Cools.isEmpty(response)) { log.info(response); Date now = new Date(); Result result = JSON.parseObject(response, Result.class); if (result.getCode() != 1) { return; } List list = JSON.parseArray(result.getData(), GetOrderResult.class); if (!Cools.isEmpty(list)) { boolean complete = true; for (GetOrderResult data : list) { if (custOrderService.selectCount(new EntityWrapper().eq("number", data.getNumber()).ne("bType_id", data.getBTypeID())) > 0) { throw new CoolException("抱歉单号已存在,请修改单号:" + data.getNumber()); } CustOrder custOrder = new CustOrder( data.getNumber(), // 销售单号 data.getBillDate(), // 单据日期[非空] data.getBTypeID(), // 客户编号 data.getETypeID(), // 经手人编号[非空] data.getUserCode(), // 商品编号 data.getQty(), // 商品数量 data.getPrice(), // 商品单价 data.getComment(), // 商品备注[非空] 0, // 状态 now, // 添加时间 now, // 修改时间 null // 备注 ); boolean insert = custOrderService.insert(custOrder); if (!insert) { complete = false; log.error("保存销售订单失败!"); } } if (complete) { List collect = list.stream().map(GetOrderResult::getNumber).distinct().collect(Collectors.toList()); for (String number : collect) { custOrderService.updateStatus(number, 1); } } } } } catch (IOException e) { e.printStackTrace(); } } /** * 更新库存 */ // @Scheduled(cron = "0/3 * * * * ? ") public void uploadStockExecute(){ try { // locDetlService Map param = new HashMap<>(); param.put("rec", 0); param.put("Flag", 1); String response = new HttpHandler.Builder() .setUri(URI) .setPath(GET_BASIS) .setJson(JSON.toJSONString(param)) .build() .doPost(); if (!Cools.isEmpty(response)) { log.info(response); Date now = new Date(); Result result = JSON.parseObject(response, Result.class); if (result.getCode() != 1) { return; } List list = JSON.parseArray(result.getData(), GetBasisResult.class); for (GetBasisResult data : list) { Mat mat = matService.selectByMatnr(data.getUserCode()); String modifyDate = data.getModifyDate(); Date updateTime = null; if (!Cools.isEmpty(modifyDate)) { updateTime = DateUtils.convert(data.getModifyDate()); } if (null == mat) { mat = new Mat( String.valueOf(data.getRec()), // 编号 null, // 所属区域 null, // 所属归类 data.getUserCode(), // 商品编号 data.getFullName(), // 商品名称 null, // 名称 null, // 规格 null, // 型号 null, // 批号 null, // 单位 null, // 条码 null, // 单据类型 null, // 单据编号 null, // 客户名称 null, // 品项数 null, // 库存余量 null, // 重量 1, // null, // 添加人员 now, // 添加时间 null, // 修改人员 updateTime, // 修改时间 null // 备注 ); matService.insert(mat); } else { mat.setUuid(String.valueOf(data.getRec())); mat.setMatnr(data.getUserCode()); mat.setMaktx(data.getFullName()); mat.setUpdateTime(updateTime); matService.updateById(mat); } log.info("====>> 更新物料:{}", data.getUserCode()); } } } catch (Exception e) { e.printStackTrace(); } } }