package com.zy.asrs.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.core.annotations.ManagerAuth; import com.core.common.BaseRes; import com.core.common.Cools; import com.core.common.R; import com.zy.asrs.entity.BasCrnp; import com.zy.asrs.entity.dto.BasCrnpMonitorDto; import com.zy.asrs.entity.dto.WcsCrnDto; import com.zy.asrs.entity.dto.WcsCrnSyncResult; import com.zy.asrs.service.BasCrnpService; import com.zy.asrs.service.WcsCrnSyncService; import com.zy.asrs.utils.CrnUtils; import com.zy.common.web.BaseController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController public class BasCrnpController extends BaseController { @Autowired private BasCrnpService basCrnpService; @Autowired private WcsCrnSyncService wcsCrnSyncService; @Autowired private CrnUtils crnUtils; @RequestMapping(value = "/basCrnp/{id}/auth") @ManagerAuth public R get(@PathVariable("id") Integer id) { return R.ok(basCrnpService.getById(id)); } @RequestMapping(value = "/basCrnp/list/auth") @ManagerAuth public R list(@RequestParam(defaultValue = "1") Integer curr, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = false) Integer crnNo, @RequestParam(required = false) String condition) { QueryWrapper wrapper = new QueryWrapper<>(); if (crnNo != null) { wrapper.eq("crn_no", crnNo); } if (!Cools.isEmpty(condition)) { wrapper.and(inner -> inner.like("crn_no", condition).or().like("memo", condition)); } wrapper.orderBy(true, true, "crn_no"); Page page = basCrnpService.page(new Page<>(curr, limit), wrapper); List records = new ArrayList<>(); for (BasCrnp basCrnp : page.getRecords()) { records.add(toMonitorDto(basCrnp)); } Map data = new HashMap<>(); data.put("total", page.getTotal()); data.put("records", records); data.put("lastSyncTime", crnUtils.getLastSyncTime()); data.put("syncError", crnUtils.getLastSyncError()); return R.ok(data); } @RequestMapping(value = "/basCrnp/add/auth", method = RequestMethod.POST) @ManagerAuth public R add(BasCrnp basCrnp) { if (basCrnp == null || basCrnp.getCrnNo() == null) { return R.error("堆垛机编号不能为空"); } if (basCrnpService.count(new QueryWrapper().eq("crn_no", basCrnp.getCrnNo())) > 0) { return R.parse(BaseRes.REPEAT).add("堆垛机编号"); } Date now = new Date(); basCrnp.setStatus(basCrnp.getStatus() == null ? 1 : basCrnp.getStatus()); basCrnp.setCreateBy(getUserId()); basCrnp.setCreateTime(now); basCrnp.setUpdateBy(getUserId()); basCrnp.setUpdateTime(now); basCrnpService.save(basCrnp); return R.ok(); } @RequestMapping(value = "/basCrnp/update/auth", method = RequestMethod.POST) @ManagerAuth public R update(BasCrnp basCrnp) { if (basCrnp == null || basCrnp.getCrnNo() == null) { return R.error("堆垛机编号不能为空"); } BasCrnp old = basCrnpService.getById(basCrnp.getCrnNo()); if (old == null) { return R.error("堆垛机配置不存在"); } old.setStatus(basCrnp.getStatus() == null ? old.getStatus() : basCrnp.getStatus()); old.setMemo(basCrnp.getMemo()); old.setUpdateBy(getUserId()); old.setUpdateTime(new Date()); basCrnpService.updateById(old); return R.ok(); } @RequestMapping(value = "/basCrnp/delete/auth", method = RequestMethod.POST) @ManagerAuth public R delete(@RequestParam(value = "ids[]") Integer[] ids) { if (ids == null || ids.length == 0) { return R.error("请选择要删除的数据"); } basCrnpService.removeByIds(Arrays.asList(ids)); for (Integer id : ids) { crnUtils.crnMap.remove(id); } return R.ok(); } @RequestMapping(value = "/basCrnp/check/column/auth", method = RequestMethod.POST) @ManagerAuth public R checkColumn(@RequestBody JSONObject param) { String key = param == null ? null : param.getString("key"); Object val = param == null ? null : param.get("val"); if (!"crnNo".equals(key) || Cools.isEmpty(val)) { return R.ok(); } BasCrnp basCrnp = basCrnpService.getOne(new QueryWrapper().eq("crn_no", val)); if (basCrnp != null) { return R.parse(BaseRes.REPEAT).add(getComment(BasCrnp.class, key)); } return R.ok(); } @RequestMapping(value = "/basCrnp/refresh/auth", method = RequestMethod.POST) @ManagerAuth public R refresh() { WcsCrnSyncResult result = wcsCrnSyncService.sync(); if (!result.isSuccess()) { return R.error(result.getMessage()); } return R.ok(result); } private BasCrnpMonitorDto toMonitorDto(BasCrnp basCrnp) { BasCrnpMonitorDto dto = new BasCrnpMonitorDto(); dto.setCrnNo(basCrnp.getCrnNo()); dto.setLocalStatus(basCrnp.getStatus()); dto.setLocalStatusDesc(basCrnp.getStatus$()); dto.setMemo(basCrnp.getMemo()); WcsCrnDto runtime = crnUtils.crnMap.get(basCrnp.getCrnNo()); if (runtime == null) { dto.setOnline(0); dto.setOnlineDesc("离线"); dto.setMode(null); dto.setModeDesc("离线"); dto.setDeviceStatus(null); dto.setDeviceStatusDesc("离线"); dto.setTaskNo(null); dto.setAlarm(null); dto.setLastSyncTime(crnUtils.getLastSyncTime()); dto.setSyncError(crnUtils.getLastSyncError()); return dto; } dto.setOnline(runtime.getOnline()); dto.setOnlineDesc(runtime.getOnline() != null && runtime.getOnline() == 1 ? "在线" : "离线"); dto.setMode(runtime.getMode()); dto.setModeDesc(runtime.getModeDesc()); dto.setDeviceStatus(runtime.getStatus()); dto.setDeviceStatusDesc(runtime.getStatusDesc()); dto.setTaskNo(runtime.getTaskNo()); dto.setAlarm(runtime.getAlarm()); dto.setLastSyncTime(runtime.getLastSyncTime()); dto.setSyncError(runtime.getSyncError()); return dto; } }