package zy.cloud.wms.common.service.task; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.common.Arith; import com.core.common.Cools; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RestController; import zy.cloud.wms.common.model.MatnrDto; import zy.cloud.wms.common.model.MatnrDto0; import zy.cloud.wms.common.service.erp.ErpScheduler; import zy.cloud.wms.common.service.erp.Result; import zy.cloud.wms.common.service.erp.entity.GetBasisResult; import zy.cloud.wms.manager.entity.DiffVal; import zy.cloud.wms.manager.service.DiffValService; import zy.cloud.wms.manager.utils.HttpHandler; import java.util.*; /** * Created by vincent on 2021/11/23 */ @Slf4j @Component public class DiffValScheduler { @Autowired private DiffValService diffValService; @Autowired private JdbcTemplate jdbcTemplate; @Transactional @Scheduled(cron = "0 */10 * * * ?") public void run() { List erpQtyList = null; Date now = new Date(); try { Map param = new HashMap<>(); param.put("PUserCode", "N"); param.put("KUserCode", "N"); String response = new HttpHandler.Builder() // .setUri("http://123.60.34.127:6220/api") .setUri("http://shhtapi.vipgrasp.com:6220/api") .setPath(ErpScheduler.GET_STOCK) .setJson(JSON.toJSONString(param)) .build() .doPost(); if (!Cools.isEmpty(response)) { log.info(response); Result result = JSON.parseObject(response, Result.class); if (result.getCode() != 1) { return; } erpQtyList = JSON.parseArray(result.getData(), MatnrDto0.class); } } catch (Exception e) { e.printStackTrace(); } if (Cools.isEmpty(erpQtyList)) { log.error("获取EPR库存失败"); return; } List> maps = jdbcTemplate.queryForList("select sum(a.anfme) as count, a.matnr from (\n" + "\tselect\n" + "\tsum(anfme) as anfme,\n" + "\tmatnr\n" + "\tfrom asr_loc_detl\n" + "\tgroup by matnr\n" + "\tunion all\n" + "\tselect\n" + "\tsum(anfme) as anfme,\n" + "\tmatnr\n" + "\tfrom man_loc_detl\n" + "\tgroup by matnr\n" + ") as a\n" + "where 1=1\n" + "group by a.matnr"); List stockQtyList = new ArrayList<>(); for (Map map : maps) { stockQtyList.add(Cools.conver(map, MatnrDto.class)); } for (MatnrDto item : stockQtyList) { DiffVal diffVal = diffValService.selectById(item.getMatnr()); if (diffVal == null) { diffVal = new DiffVal( item.getMatnr(), // 物料编号[非空] item.getCount(), // WMS库存 0.0D, // ERP库存 item.getCount(), // 差异值 now, // 更新时间 null // 备注 ); diffValService.insert(diffVal); } else { diffVal.setStockQty(item.getCount()); double erpQty = diffVal.getErpQty(); diffVal.setVal(Arith.subtract(2, diffVal.getStockQty(), erpQty)); diffVal.setUpdateTime(now); diffValService.updateById(diffVal); } } for (MatnrDto0 item : erpQtyList) { DiffVal diffVal = diffValService.selectById(item.getPUserCode()); if (diffVal == null) { diffVal = new DiffVal( item.getPUserCode(), // 物料编号[非空] 0.0D, // WMS库存 item.getQty(), // ERP库存 -item.getQty(), // 差异值 now, // 更新时间 null // 备注 ); diffValService.insert(diffVal); } else { diffVal.setErpQty(item.getQty()); double stockQty = diffVal.getStockQty(); diffVal.setVal(Arith.subtract(2, stockQty, diffVal.getErpQty())); diffVal.setUpdateTime(now); diffValService.updateById(diffVal); } } } }