package com.zy.asrs.controller; import com.alibaba.excel.EasyExcel; import com.alibaba.fastjson.JSON; 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.sun.org.apache.bcel.internal.generic.NEW; import com.zy.asrs.entity.LocNormal; import com.zy.asrs.entity.LocNormalLog; import com.zy.asrs.entity.LocNormalReport; import com.zy.asrs.entity.param.LocNormalParam; import com.zy.asrs.service.LocNormalLogService; import com.zy.asrs.service.LocNormalReportService; import com.zy.asrs.service.LocNormalService; import com.zy.asrs.utils.VersionUtils; import com.zy.common.service.erp.ErpService; import com.zy.common.service.erp.ErpSqlServer; import com.zy.common.utils.excel.locNomal.LocNormalExcel; import com.zy.common.utils.excel.locNomal.LocNormalExcelListener; import com.zy.common.utils.excel.matcode.MatCodeExcel; import com.zy.common.utils.excel.matcode.MatCodeExcelListener; import com.zy.common.web.BaseController; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; 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.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.math.BigDecimal; import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import static jdk.nashorn.api.scripting.ScriptUtils.convert; @Slf4j @RestController public class LocNormalController extends BaseController { @Autowired private LocNormalService locNormalService; @Autowired private LocNormalReportService locNormalReportService; @Autowired private ErpSqlServer erpSqlServer; @Autowired private LocNormalLogService locNormalLogService; @RequestMapping(value = "/locNomal/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) { excludeTrash(param); EntityWrapper wrapper = new EntityWrapper<>(); convert(param, wrapper); allLike(LocNormal.class, param.keySet(), wrapper, condition); if (!Cools.isEmpty(orderByField)) { wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType)); } else { wrapper.orderBy("appe_time", false); } if (Cools.isEmpty(param.get("state"))) { wrapper.in("state", "1,2"); } return R.ok(locNormalService.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 { wrapper.like(entry.getKey(), String.valueOf(entry.getValue())); } } } @RequestMapping(value = "/locNomal/add/auth") @ManagerAuth public R add(LocNormal locNormalList) { // 插入创建信息 locNormalList.setModiUser(getUserId()); locNormalList.setModiTime(new Date()); locNormalList.setAppeUser(getUserId()); locNormalList.setAppeTime(new Date()); // 默认新增为已入库 locNormalList.setState("1"); locNormalService.insert(locNormalList); return R.ok(); } @RequestMapping(value = "/matnr/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 != locNormalService.selectOne(wrapper)) { return R.parse(BaseRes.REPEAT).add(getComment(LocNormal.class, String.valueOf(param.get("key")))); } return R.ok(); } @RequestMapping(value = "/locNormal/update/auth") @ManagerAuth public void updateLocNormal(LocNormal param) { Long modiUser = getUserId(); Date modiTime = new Date(); // 调整库存,修改,StockCheckRecord插入盘盈、盘亏数据 LocNormal locNormalOld = locNormalService.selectOne(new EntityWrapper().eq("matnr", param.getMatnr()).and().eq("warehouse", param.getWarehouse()).and().eq("supplier", param.getSupplier())); BigDecimal erpCount = new BigDecimal(0); if (param.getAnfme() == new BigDecimal(0)) { erpCount = new BigDecimal(0).subtract(new BigDecimal(String.valueOf(locNormalOld.getAnfme()))); } else { erpCount = param.getAnfme().subtract(new BigDecimal(String.valueOf(locNormalOld.getAnfme()))); } // 调整库存,新增,StockCheckRecord插入盘盈数据 String ErpSql = "insert into StockCheckRecord(Fnumber, CheckQty, Fflag_rw, Fflag_finish) values (''{0}'', {1,number,#}, 0, 0)"; ErpSql = MessageFormat.format(ErpSql, param.getMatnr(), erpCount); erpSqlServer.update(ErpSql); locNormalService.updateLocNormal(param.getMatnr(), param.getAnfme(), modiUser, modiTime, param.getId()); // 生成平仓出入库记录 LocNormalLog locLog = new LocNormalLog(); VersionUtils.setLocNormalLog(locLog, param); locLog.setAnfme(erpCount.doubleValue()); locLog.setIoType(3); // 修改 locLog.setCreateTime(new Date()); locLog.setCreateUser(modiUser); if (!locNormalLogService.insert(locLog)) { String logStr = JSON.toJSONString(locLog); log.info("平仓修改库存记录插入失败,数据:" + logStr); } } @RequestMapping(value = "/locNormal/outLoc/auth") @ManagerAuth public void outLocNormal(LocNormal param) { Long modiUser = getUserId(); Date modiTime = new Date(); locNormalService.outLocNormal(param.getMatnr(), modiUser, modiTime, param.getId()); } @RequestMapping(value = "/locNormal/removeLoc/auth") @ManagerAuth public void removeLoc(LocNormal param) { Long modiUser = getUserId(); Date modiTime = new Date(); locNormalService.removeLocNormal(param.getMatnr(), modiUser, modiTime, param.getId()); } /* 导入 */ @RequestMapping(value = "/locNormal/import/auth") @ManagerAuth(memo = "平仓管理导入") @Transactional public R locNormalImport(MultipartFile file) throws IOException, InterruptedException { LocNormalExcelListener listener = new LocNormalExcelListener(getUserId()); EasyExcel.read(file.getInputStream(), LocNormalExcel.class, listener).sheet().doRead(); return R.ok("成功导入" + listener.getTotal() + "条物料信息"); } /* 导出 */ @RequestMapping(value = "/locNormal/export/auth") @ManagerAuth(memo = "平仓管理导出") public R export(@RequestBody JSONObject param){ List fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); EntityWrapper wrapper = new EntityWrapper(); wrapper.ne("state", "3"); Map map = excludeTrash(param.getJSONObject("exportData")); convert(map, wrapper); List list = locNormalService.selectList(wrapper); return R.ok(exportSupport(list, fields)); } /* 平仓入库 成品 */ @RequestMapping(value = "/locNormal/in") @ManagerAuth(memo = "平仓入库") @Transactional public R locNormalIn(@RequestBody LocNormalParam param) { Long userId = getUserId(); Date timeNow = new Date(); for (Integer i = 0; i < param.getNormalList().size(); i++) { param.getNormalList().get(i).setAppeUser(userId); param.getNormalList().get(i).setAppeTime(timeNow); } locNormalService.locNormalIn(param.getNormalList()); return R.ok(); } /* 平仓入库-原材料 */ @RequestMapping(value = "/locNormal/in/source") @ManagerAuth(memo = "平仓入库-原材料") @Transactional public R locNormalInSource(@RequestBody LocNormalParam param) { Long userId = getUserId(); Date timeNow = new Date(); for (Integer i = 0; i < param.getNormalList().size(); i++) { param.getNormalList().get(i).setAppeUser(userId); param.getNormalList().get(i).setAppeTime(timeNow); } locNormalService.locNormalInSource(param.getNormalList()); return R.ok(); } /* pda入库 */ @RequestMapping(value = "/locNormal/pda/in") @ManagerAuth(memo = "平仓管理pda入库") @Transactional public R locNormalPdaIn(@RequestBody LocNormalParam param) { Long userId = getUserId(); Date timeNow = new Date(); for (Integer i = 0; i < param.getNormalList().size(); i++) { param.getNormalList().get(i).setAppeUser(userId); param.getNormalList().get(i).setAppeTime(timeNow); } locNormalService.pdaLocNormalIn(param.getNormalList()); return R.ok(); } /* pda出库查询 */ @RequestMapping(value = "/locNormal/pda/out/query") @ManagerAuth(memo = "pda出库查询") @Transactional public R locNormalPdaOutQuery(String matnr, String warehouse, String billNo) { List list = new ArrayList<>(); list = locNormalService.pdaLocNormalQuery(matnr, warehouse, billNo); return R.ok(list); } @RequestMapping(value = "/locNormal/pda/out") @ManagerAuth(memo = "pda出库") @Transactional public R locNormalPdaOut(@RequestBody LocNormalParam param) { Long userId = getUserId(); Date timeNow = new Date(); List list = param.getNormalList(); for (Integer i = 0; i < list.size(); i++) { list.get(i).setModiUser(userId); list.get(i).setModiTime(timeNow); } locNormalService.pdaLocNormalOut(list); return R.ok(); } @RequestMapping(value = "/locNormal/pda/warehouseQuery") @ManagerAuth(memo = "pda根据库区查询物料清单") @Transactional public R locNormalPdaWarehouseQuery(String warehouse, String matnr) { List list = locNormalService.pdaLocNormalWarehouseQuery(warehouse, matnr); return R.ok(list); } @RequestMapping(value = "/locNormal/pda/move") @ManagerAuth(memo = "pda移库") @Transactional public R LocNormalPdaMove(@RequestBody LocNormalParam param) { Long userId = getUserId(); Date timeNow = new Date(); List list = param.getNormalList(); for (Integer i = 0; i < list.size(); i++) { list.get(i).setModiUser(userId); list.get(i).setModiTime(timeNow); } locNormalService.pdaLocNormalMove(list); return R.ok(); } @RequestMapping(value = "/locNomal/getInListByDay") @ManagerAuth(memo = "平仓日出入库查询") public R getInListByDay(@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) { excludeTrash(param); EntityWrapper wrapper = new EntityWrapper<>(); String time1 = ""; String time2 = ""; if (!Cools.isEmpty(param.get("query_date"))) { String timeRange = param.get("query_date").toString(); time1 = timeRange.substring(0, 19); time2 = timeRange.substring(21, timeRange.length()); } param.remove("query_date"); convert(param, wrapper); if (time1 != "" && time2 != "") { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dateTime1 = new Date(); Date dateTime2 = new Date(); try { dateTime1 = formatter.parse(time1); dateTime2 = formatter.parse(time2); } catch (Exception e) { e.printStackTrace(); } wrapper.ge("create_time", dateTime1).and().le("create_time", dateTime2); } List oderCol = new ArrayList<>(); oderCol.add("create_time"); wrapper.orderDesc(oderCol); allLike(LocNormalLog.class, param.keySet(), wrapper, condition); return R.ok(locNormalLogService.selectPage(new Page<>(curr, limit), wrapper)); } //excel导出 @RequestMapping("/locNomal/normalReportExport.action") @ManagerAuth(memo = "日入库明细统计导出") public R normalReportExport(@RequestBody JSONObject param){ List fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); @SuppressWarnings("unchecked") List list = JSONObject.parseArray(param.getJSONArray("exportData").toJSONString(), LocNormalLog.class); return R.ok(exportSupport(list, fields)); } }