package com.zy.asrs.controller; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; 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.LocDetl; import com.zy.asrs.entity.Mat; import com.zy.asrs.entity.param.AbnormalLocDetlParam; import com.zy.asrs.mapper.LocDetlMapper; import com.zy.asrs.service.LocDetlService; import com.zy.asrs.service.MatService; import com.zy.common.web.BaseController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import javax.xml.crypto.Data; import java.io.IOException; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; @RestController public class LocDetlController extends BaseController { @Autowired private LocDetlService locDetlService; @Autowired private LocDetlMapper locDetlMapper; @Autowired private MatService matService; @RequestMapping(value = "/locDetl/update") public R update1() { if (!locDetlService.updateLocNo("0402805", "0402804")) { return R.error(); } return R.ok(); } @RequestMapping(value = "/locDetl/{id}/auth") @ManagerAuth public R get(@PathVariable("id") String id) { return R.ok(locDetlService.selectById(String.valueOf(id))); } @RequestMapping(value = "/locDetl/auth") @ManagerAuth public R stockOutList(@RequestParam(value = "locNos[]") List locNos) { if (!locNos.isEmpty()) { List res = new ArrayList<>(); for (String locNo : new HashSet<>(locNos)) { List locDetls = locDetlService.selectList(new EntityWrapper().eq("loc_no", locNo)); if (!locDetls.isEmpty()) { res.addAll(locDetls); } } return R.ok().add(res); } return R.parse(BaseRes.EMPTY); } @RequestMapping(value = "/stock/out/list/auth") @ManagerAuth public R stockOutList(@RequestParam(defaultValue = "1") Integer curr, @RequestParam(defaultValue = "10") Integer limit, @RequestParam Map param) { if (!Cools.isEmpty(param.get("modi_time"))) { String val = String.valueOf(param.get("modi_time")); if (val.contains(RANGE_TIME_LINK)) { String[] dates = val.split(RANGE_TIME_LINK); param.put("startTime", DateUtils.convert(dates[0])); param.put("endTime", DateUtils.convert(dates[1])); param.remove("modi_time"); } } return R.ok(locDetlService.getStockOut(toPage(curr, limit, param, LocDetl.class))); } @RequestMapping(value = "/locDetl/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, @RequestParam(required = false) Boolean unreason) { if (!Cools.isEmpty(unreason) && unreason) { return R.ok(locDetlService.selectPage(new Page<>(curr, limit), new EntityWrapper() .where(" DATALENGTH( batch ) != 11 or\n" + " batch LIKE '%[a-z]%'"))); } param.remove("unreason"); String row = ""; EntityWrapper wrapper = new EntityWrapper<>(); if (param.get("row") != null) { String chooseRow = (String) param.get("row"); if (chooseRow.length() == 1) { row = "0" + chooseRow; param.remove("row"); } else { row = chooseRow; param.remove("row"); } } excludeTrash(param); convert(param, wrapper); allLike(LocDetl.class, param.keySet(), wrapper, condition); if (!Cools.isEmpty(orderByField)) { wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType)); } if (!row.equals("")) { wrapper.and() .where("loc_no like '" + row + "%'"); } return R.ok(locDetlService.selectPage(new Page<>(curr, limit), wrapper)); } 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 { if (entry.getKey().equals("locNo")) { wrapper.eq("loc_no", String.valueOf(entry.getValue())); } else { wrapper.like(entry.getKey(), String.valueOf(entry.getValue())); } } } } @RequestMapping(value = "/locDetl/add/auth") @ManagerAuth(memo = "库位明细添加") public R add(LocDetl locDetl) { locDetl.setModiUser(getUserId()); locDetl.setModiTime(new Date()); locDetl.setAppeUser(getUserId()); locDetl.setAppeTime(new Date()); locDetlService.insert(locDetl); return R.ok(); } @RequestMapping(value = "/locDetl/update/auth") @ManagerAuth(memo = "库位明细修改") public R update(LocDetl locDetl) { if (Cools.isEmpty(locDetl) || null == locDetl.getMatnr()) { return R.error(); } locDetl.setModiUser(getUserId()); locDetl.setModiTime(new Date()); locDetlService.updateById(locDetl); return R.ok(); } @RequestMapping(value = "/locDetl/delete/auth") @ManagerAuth(memo = "库位明细删除") public R delete(@RequestParam String param) { List list = JSONArray.parseArray(param, LocDetl.class); if (Cools.isEmpty(list)) { return R.error(); } for (LocDetl entity : list) { locDetlService.delete(new EntityWrapper<>(entity)); } return R.ok(); } @RequestMapping(value = "/locDetl/export/auth") @ManagerAuth(memo = "库位明细导出") public synchronized R export(@RequestBody JSONObject param) { List fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); EntityWrapper wrapper = new EntityWrapper<>(); Map map = excludeTrash(param.getJSONObject("locDetl")); String row = ""; if (map.get("row") != null) { String chooseRow = (String) map.get("row"); if (chooseRow.length() == 1) { row = "0" + chooseRow; map.remove("row"); } else { row = chooseRow; map.remove("row"); } } convert(map, wrapper); if (!row.equals("")) { wrapper.and() .where("loc_no like '" + row + "%'"); } List list = locDetlService.selectList(wrapper); return R.ok(exportSupport(list, fields)); } @RequestMapping(value = "/locDetlQuery/auth") @ManagerAuth public R query(String condition) { EntityWrapper wrapper = new EntityWrapper<>(); wrapper.like("matnr", condition); Page page = locDetlService.selectPage(new Page<>(0, 10), wrapper); List> result = new ArrayList<>(); for (LocDetl locDetl : page.getRecords()) { Map map = new HashMap<>(); map.put("id", locDetl.getMatnr()); map.put("value", locDetl.getMatnr()); result.add(map); } return R.ok(result); } @RequestMapping(value = "/locDetl/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 != locDetlService.selectOne(wrapper)) { return R.parse(BaseRes.REPEAT).add(getComment(LocDetl.class, String.valueOf(param.get("key")))); } return R.ok(); } @RequestMapping(value = "/locDetl/statis/auth") @ManagerAuth public R statis(@RequestParam(defaultValue = "1") Integer curr, @RequestParam(defaultValue = "10") Integer limit, @RequestParam Map param) { Page stockStatis = locDetlService.getStockStatis(toPage(curr, limit, param, LocDetl.class)); for (LocDetl locDetl : stockStatis.getRecords()) { Mat mat = matService.selectByMatnr(locDetl.getMatnr()); if (mat != null) { locDetl.sync(mat); } } return R.ok().add(stockStatis); } @Autowired private LocDetlMapper LocDetlMapper; @RequestMapping(value = "/locDetl/statis/export") // @ManagerAuth public void statisExport(HttpServletResponse response) throws IOException { List excel = LocDetlMapper.getStockStatisExcel(); for (LocDetl locDetl : excel) { Mat mat = matService.selectByMatnr(locDetl.getMatnr()); if (mat != null) { locDetl.sync(mat); } } response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("库存明细统计报表", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), LocDetl.class) .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) .sheet("表1") .doWrite(excel); } /** * 获取库存总数 * * @return */ @RequestMapping("/locDetl/count") public R getAllCount() { Integer sum = locDetlService.sum(); return R.ok(sum); } /**********************************************************异常库存相关controller**********************************************************/ @RequestMapping(value = "/locDetl/grouplist/auth") @ManagerAuth public R groupList(@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) { // List result = new ArrayList<>(); List result = new ArrayList<>(); // excludeTrash(param); param.put("curr", curr); param.put("limit", limit); Page groupLocDetl = locDetlService.getStockStatis2(toPage(1, 10000, param, LocDetl.class)); // List groupLocDetl = locDetlService.selectGroupPage(param, curr, limit); for (LocDetl locDetl : groupLocDetl.getRecords()) { AbnormalLocDetlParam abnormalLocDetlParam = new AbnormalLocDetlParam(); Mat mat = matService.selectOne(new EntityWrapper() .eq("matnr", locDetl.getMatnr())); if (Cools.isEmpty(mat)) { continue; } if (!Cools.isEmpty(mat.getStoreMax()) || !Cools.isEmpty(mat.getStoreMin())) { abnormalLocDetlParam.setStoreMax(mat.getStoreMax()); abnormalLocDetlParam.setStoreMaxDate(mat.getStoreMaxDate()); abnormalLocDetlParam.setStoreMin(mat.getStoreMin()); abnormalLocDetlParam.setAnfme(locDetl.getAnfme()); abnormalLocDetlParam.setMaktx(mat.getMaktx()); abnormalLocDetlParam.setMatnr(mat.getMatnr()); abnormalLocDetlParam.setSpecs(mat.getSpecs()); abnormalLocDetlParam.setBatch(locDetl.getBatch()); // SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd"); // Date maxDate = simple.parse(locDetl.getBatch()); // long time = maxDate.getTime(); // Date now = new Date(); // long time1 = now.getTime(); // abnormalLocDetlParam.setNowTime((int) ((time1 - time) / (1000 * 60 * 60 * 24))); if (!Cools.isEmpty(mat.getStoreMax()) && locDetl.getAnfme() > mat.getStoreMax()) { result.add(abnormalLocDetlParam); continue; } else if (!Cools.isEmpty(mat.getStoreMin()) && locDetl.getAnfme() < mat.getStoreMin()) { result.add(abnormalLocDetlParam); continue; } } else if (!Cools.isEmpty(mat.getStoreMaxDate())) { if (Cools.isEmpty(locDetl.getBatch())) { continue; } else if (locDetl.getBatch().length() != 8) { continue; } try { SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd"); Date maxDate = simple.parse(locDetl.getBatch()); Calendar calendar = new GregorianCalendar(); calendar.setTime(maxDate); // 把日期往后增加一天,整数 往后推,负数往前移动 calendar.add(Calendar.DATE, mat.getStoreMaxDate()); // 这个时间就是日期往后推一天的结果 maxDate = calendar.getTime(); Date now = new Date(); abnormalLocDetlParam.setStoreMax(mat.getStoreMax()); abnormalLocDetlParam.setStoreMaxDate(mat.getStoreMaxDate()); abnormalLocDetlParam.setStoreMin(mat.getStoreMin()); abnormalLocDetlParam.setAnfme(locDetl.getAnfme()); abnormalLocDetlParam.setMaktx(mat.getMaktx()); abnormalLocDetlParam.setMatnr(mat.getMatnr()); abnormalLocDetlParam.setSpecs(mat.getSpecs()); abnormalLocDetlParam.setBatch(locDetl.getBatch()); if (now.compareTo(maxDate) > 0) { result.add(abnormalLocDetlParam); } } catch (Exception e) { continue; } } } Page locDetlPage = new Page<>(); locDetlPage.setRecords(paging(result, curr, limit)); locDetlPage.setTotal(result.size()); locDetlPage.setCurrent(curr); locDetlPage.setSize(limit); return R.ok(locDetlPage); } private List paging(List result, Integer curr, Integer limit) { List pageRecord = new ArrayList(); int minIndex = (curr - 1) * limit; int maxIndex = (curr * limit) - 1; if (result.size() < maxIndex) { maxIndex = result.size(); } for (int i = minIndex; i < maxIndex; i++) { pageRecord.add(result.get(i)); } return pageRecord; } }