package com.zy.asrs.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.baomidou.mybatisplus.plugins.Page; import com.core.annotations.ManagerAuth; import com.core.common.BaseRes; import com.core.common.Cools; import com.core.common.DateUtils; import com.core.common.R; import com.zy.asrs.entity.BasCrnDepthRule; import com.zy.asrs.entity.param.BasCrnDepthRuleRuntimePreviewParam; import com.zy.asrs.entity.param.BasCrnDepthRuleTemplateParam; import com.zy.asrs.service.BasCrnDepthRuleService; import com.zy.common.service.CommonService; import com.zy.common.web.BaseController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController public class BasCrnDepthRuleController extends BaseController { @Autowired private BasCrnDepthRuleService basCrnDepthRuleService; @Autowired private CommonService commonService; /** * 查询单条堆垛机深浅规则。 */ @RequestMapping(value = "/basCrnDepthRule/{id}/auth") @ManagerAuth public R get(@PathVariable("id") Long id) { return R.ok(basCrnDepthRuleService.selectById(id)); } /** * 分页查询堆垛机深浅规则列表。 */ @RequestMapping(value = "/basCrnDepthRule/list/auth") @ManagerAuth public R list(@RequestParam(defaultValue = "1") Integer curr, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = false) String orderByField, @RequestParam(required = false) String orderByType, @RequestParam(required = false) String condition, @RequestParam Map param) { EntityWrapper wrapper = new EntityWrapper(); excludeTrash(param); convert(param, wrapper); allLike(BasCrnDepthRule.class, param.keySet(), wrapper, condition); if (!Cools.isEmpty(orderByField)) { wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType)); } else { wrapper.orderBy("whs_type", true).orderBy("crn_no", true); } return R.ok(basCrnDepthRuleService.selectPage(new Page(curr, limit), wrapper)); } /** * 把前端查询参数转换成 MyBatis Plus 的模糊/时间范围条件。 */ private void convert(Map map, EntityWrapper wrapper) { for (Map.Entry entry : map.entrySet()) { String val = String.valueOf(entry.getValue()); if (val.contains(RANGE_TIME_LINK)) { String[] dates = val.split(RANGE_TIME_LINK); wrapper.ge(entry.getKey(), DateUtils.convert(dates[0])); wrapper.le(entry.getKey(), DateUtils.convert(dates[1])); } else { wrapper.like(entry.getKey(), val); } } } /** * 新增一条堆垛机深浅规则。 */ @RequestMapping(value = "/basCrnDepthRule/add/auth") @ManagerAuth(memo = "堆垛机深浅规则添加") public R add(BasCrnDepthRule basCrnDepthRule) { basCrnDepthRuleService.validateRule(basCrnDepthRule); Date now = new Date(); basCrnDepthRule.setCreateBy(getUserId()); basCrnDepthRule.setCreateTime(now); basCrnDepthRule.setUpdateBy(getUserId()); basCrnDepthRule.setUpdateTime(now); basCrnDepthRuleService.insert(basCrnDepthRule); return R.ok("保存成功"); } /** * 修改已有堆垛机深浅规则,并保留创建审计字段。 */ @RequestMapping(value = "/basCrnDepthRule/update/auth") @ManagerAuth(memo = "堆垛机深浅规则修改") public R update(BasCrnDepthRule basCrnDepthRule) { if (Cools.isEmpty(basCrnDepthRule) || basCrnDepthRule.getId() == null) { return R.error(); } basCrnDepthRuleService.validateRule(basCrnDepthRule); BasCrnDepthRule dbRule = basCrnDepthRuleService.selectById(basCrnDepthRule.getId()); if (dbRule == null) { return R.error("规则不存在"); } basCrnDepthRule.setCreateBy(dbRule.getCreateBy()); basCrnDepthRule.setCreateTime(dbRule.getCreateTime()); basCrnDepthRule.setUpdateBy(getUserId()); basCrnDepthRule.setUpdateTime(new Date()); basCrnDepthRuleService.updateById(basCrnDepthRule); return R.ok("修改完成"); } /** * 批量删除堆垛机深浅规则。 */ @RequestMapping(value = "/basCrnDepthRule/delete/auth") @ManagerAuth(memo = "堆垛机深浅规则删除") public R delete(@RequestParam String param) { List list = JSONArray.parseArray(param, BasCrnDepthRule.class); if (Cools.isEmpty(list)) { return R.error(); } for (BasCrnDepthRule entity : list) { if (entity != null && entity.getId() != null) { basCrnDepthRuleService.deleteById(entity.getId()); } } return R.ok(); } /** * 导出当前查询条件命中的规则数据。 */ @RequestMapping(value = "/basCrnDepthRule/export/auth") @ManagerAuth(memo = "堆垛机深浅规则导出") public R export(@RequestBody JSONObject param) { List fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); EntityWrapper wrapper = new EntityWrapper(); Map map = excludeTrash(param.getJSONObject("basCrnDepthRule")); convert(map, wrapper); List list = basCrnDepthRuleService.selectList(wrapper); return R.ok(exportSupport(list, fields)); } /** * 按模板预览某仓库范围内的默认深浅规则。 */ @RequestMapping(value = "/basCrnDepthRule/templatePreview/auth") @ManagerAuth(memo = "堆垛机深浅规则模板预览") public R templatePreview(BasCrnDepthRuleTemplateParam param) { return R.ok(basCrnDepthRuleService.previewTemplate(param)); } /** * 按模板批量生成或覆盖某仓库的堆垛机深浅规则。 */ @RequestMapping(value = "/basCrnDepthRule/templateGenerate/auth") @ManagerAuth(memo = "堆垛机深浅规则模板生成") public R templateGenerate(BasCrnDepthRuleTemplateParam param) { basCrnDepthRuleService.saveTemplate(param, getUserId()); return R.ok("模板生成完成"); } /** * 预览某站点当前的库区、设备和深浅排参与顺序。 */ @RequestMapping(value = "/basCrnDepthRule/runtimePreview/auth") @ManagerAuth(memo = "堆垛机深浅规则运行预览") public R runtimePreview(BasCrnDepthRuleRuntimePreviewParam param) { return R.ok(commonService.previewRun2Allocation(param)); } /** * 为前端自动完成提供规则主键查询。 */ @RequestMapping(value = "/basCrnDepthRuleQuery/auth") @ManagerAuth public R query(String condition) { EntityWrapper wrapper = new EntityWrapper(); wrapper.like("crn_no", condition); Page page = basCrnDepthRuleService.selectPage(new Page(0, 10), wrapper); List> result = new ArrayList>(); for (BasCrnDepthRule rule : page.getRecords()) { Map map = new HashMap(); map.put("id", rule.getId()); map.put("value", rule.getWhsType() + "-" + rule.getCrnNo()); result.add(map); } return R.ok(result); } /** * 检查某个字段值是否已存在,用于前端重复校验。 */ @RequestMapping(value = "/basCrnDepthRule/check/column/auth") @ManagerAuth public R query(@RequestBody JSONObject param) { Wrapper wrapper = new EntityWrapper() .eq(humpToLine(String.valueOf(param.get("key"))), param.get("val")); if (null != basCrnDepthRuleService.selectOne(wrapper)) { return R.parse(BaseRes.REPEAT).add(getComment(BasCrnDepthRule.class, String.valueOf(param.get("key")))); } return R.ok(); } }