package zy.cloud.wms.manager.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.annotations.ManagerAuth; import com.core.common.Cools; import com.core.common.R; import com.core.common.SnowflakeIdWorker; import com.core.exception.CoolException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import zy.cloud.wms.common.entity.Parameter; import zy.cloud.wms.common.model.BillDto; import zy.cloud.wms.common.service.erp.ErpService; import zy.cloud.wms.common.web.BaseController; import zy.cloud.wms.manager.entity.WrkLocSync; import zy.cloud.wms.manager.service.LocSyncService; import zy.cloud.wms.manager.service.WrkLocSyncService; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController @Slf4j public class LocSyncController extends BaseController { @Autowired private LocSyncService locSyncService; @Autowired private WrkLocSyncService wrkLocSyncService; @Autowired private ErpService erpService; @Autowired private SnowflakeIdWorker snowflakeIdWorker; @RequestMapping(value = "/locSync/queryLocSum") @ManagerAuth(memo = "获取总库存清单") public R queryLocSum() { return R.ok(locSyncService.queryLocSum()); } @RequestMapping(value = "/locSync/syncLocWrkCount") @ManagerAuth(memo = "获取库存同步上传清单数量") public R getSyncLocWrkCount() { return R.ok(wrkLocSyncService.getSyncLocWrkCount()); } @RequestMapping(value = "/locSync/insertWrklocSync") @ManagerAuth(memo = "同步上传清单数据插入") @Transactional public R insertWrklocSync(@RequestBody JSONObject param) { Date now = new Date(); Long userId = getUserId(); List list = JSONObject.parseArray(param.getJSONArray("list").toJSONString(), WrkLocSync.class); for (WrkLocSync wrkLocSync : list) { WrkLocSync sync = new WrkLocSync(); sync.setMatnr(wrkLocSync.getMatnr()); sync.setQty(wrkLocSync.getQty()); sync.setState("N"); sync.setCreateTime(now); sync.setCreateBy(userId); sync.setUpdateBy(userId); sync.setUpdateTime(now); if (!wrkLocSyncService.insert(sync)) { throw new CoolException("同步失败"); } } return R.ok(list.size()); } @RequestMapping(value = "/locSync/updateWrklocSync") @ManagerAuth(memo = "清空同步上传清单数据,并插入") public R updateWrklocSync(@RequestBody JSONObject param) { // 清空同步上传清单数据 if (!wrkLocSyncService.clearWrkLocSync()) { throw new CoolException("同步上传清单数据清空失败,请联系管理员"); } ; List list = JSONObject.parseArray(param.getJSONArray("list").toJSONString(), WrkLocSync.class); // 插入创建人员和创建时间 if (list.size() > 0) { for (WrkLocSync elist : list) { elist.setCreateBy(getUserId()); elist.setCreateTime(new Date()); } } Integer result = 0; result = wrkLocSyncService.insertWrkLocSync(list); return R.ok(result); } /* 同步上传erp[uploadBill] 单据上传 */ @Scheduled(cron = "0/2 * * * * ? ") public void execute() { if (!Cools.isEmpty(Parameter.get().getSyncSwitch()) && Parameter.get().getSyncSwitch().equals("Y")) { List orderList = new ArrayList<>(); orderList.add("state"); orderList.add("create_time"); List syncList = wrkLocSyncService.selectList(new EntityWrapper() .eq("state", "N").orderDesc(orderList)); // 同步上传清单处理,取第一条 if (!Cools.isEmpty(syncList)) { WrkLocSync executeData = syncList.get(0); // 订单号 String docNum = "PU-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15); // 订单类型 34采购单 Integer docId = 34; List dtos = new ArrayList(); BillDto res = new BillDto(); res.setMatnr(executeData.getMatnr()); res.setQty(executeData.getQty()); dtos.add(res); if (erpService.uploadBill(dtos, docId, docNum).getSuccess()) { // 成功后删除任务 if (!wrkLocSyncService.delete(new EntityWrapper().eq("matnr", executeData.getMatnr()))) { log.info("库存同步删除任务失败,任务matnr=" + executeData.getMatnr() + "时间=" + new Date()); } } else { // 失败后将任务状态state更新为N executeData.setState("X"); executeData.setUpdateBy(9527L); executeData.setCreateTime(new Date()); if(!wrkLocSyncService.update(executeData, new EntityWrapper().eq("matnr", executeData.getMatnr()))) { log.info("库存同步更新任务失败,任务matnr=" + executeData.getMatnr() + "时间=" + new Date()); } } } } } }