| package com.zy.asrs.wms.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.core.conditions.query.LambdaQueryWrapper; | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.baomidou.mybatisplus.core.metadata.IPage; | 
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
| import com.zy.asrs.common.wms.entity.LocDetl; | 
| import com.zy.asrs.common.wms.entity.Mat; | 
| import com.zy.asrs.common.wms.entity.Order; | 
| import com.zy.asrs.common.wms.service.LocDetlService; | 
| import com.zy.asrs.common.wms.service.MatService; | 
| import com.zy.asrs.framework.annotations.ManagerAuth; | 
| import com.zy.asrs.framework.common.BaseRes; | 
| import com.zy.asrs.framework.common.Cools; | 
| import com.zy.asrs.framework.common.R; | 
| import com.zy.asrs.framework.domain.KeyValueVo; | 
| import com.zy.asrs.framework.common.DateUtils; | 
| import com.zy.asrs.common.web.BaseController; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.web.bind.annotation.*; | 
|   | 
| import javax.servlet.http.HttpServletResponse; | 
| import java.io.IOException; | 
| import java.net.URLEncoder; | 
| import java.util.*; | 
|   | 
| @RestController | 
| public class LocDetlController extends BaseController { | 
|   | 
|     @Autowired | 
|     private LocDetlService locDetlService; | 
|     @Autowired | 
|     private MatService matService; | 
|   | 
|     @RequestMapping(value = "/locDetl/forLocNoAndBarcode/auth") | 
|     @ManagerAuth | 
|     public R forLocNoAndBarcode(@RequestParam String locNo,@RequestParam String barcode) { | 
|         List<LocDetl> detls = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocNo, locNo).eq(LocDetl::getZpallet, barcode).eq(LocDetl::getHostId, getHostId())); | 
|         if (Cools.isEmpty(detls)){ | 
|             return R.error("未查询到库存数据"); | 
|         } | 
|         return R.ok(detls); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetl/forlocNo/auth/v1") | 
|     @ManagerAuth | 
|     public R forlocNoV1(@RequestParam String locNo) { | 
|         LambdaQueryWrapper<LocDetl> locDetlLambdaQueryWrapper = new LambdaQueryWrapper<LocDetl>() | 
|                 .eq(LocDetl::getLocNo, locNo) | 
|                 .eq(LocDetl::getHostId, getHostId()); | 
|         List<LocDetl> detls = locDetlService.list(locDetlLambdaQueryWrapper); | 
|         if (Cools.isEmpty(detls)){ | 
|             return R.error("未查询到库存数据"); | 
|         } | 
|         return R.ok(detls); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetl/forlocNo/auth") | 
|     @ManagerAuth | 
|     public R forlocNo(@RequestParam String orderNo,@RequestParam String locNo,@RequestParam String batch,@RequestParam String matnr) { | 
|         LambdaQueryWrapper<LocDetl> locDetlLambdaQueryWrapper = new LambdaQueryWrapper<LocDetl>() | 
|                 .eq(LocDetl::getLocNo, locNo) | 
|                 .eq(LocDetl::getHostId, getHostId()) | 
|                 .eq(LocDetl::getMatnr, matnr); | 
|         if (!Cools.isEmpty(batch)){ | 
|             locDetlLambdaQueryWrapper.eq(LocDetl::getBatch,batch); | 
|         } | 
|         List<LocDetl> detls = locDetlService.list(locDetlLambdaQueryWrapper); | 
|         if (Cools.isEmpty(detls)){ | 
|             return R.error("未查询到库存数据"); | 
|         } | 
|         return R.ok(detls); | 
|     } | 
|   | 
| //    @RequestMapping(value = "/locDetl/{id}/auth") | 
| //    @ManagerAuth | 
| //    public R get(@PathVariable("id") String id) { | 
| // | 
| //        return R.ok(locDetlService.getById(String.valueOf(id))); | 
| //    } | 
|   | 
|     @RequestMapping(value = "/locDetl/export/auth") | 
|     @ManagerAuth(memo = "库位明细导出") | 
|     public synchronized R export(@RequestBody JSONObject param){ | 
|         List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); | 
|         QueryWrapper<LocDetl> wrapper = new QueryWrapper<>(); | 
|         Map<String, Object> 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<LocDetl> list = locDetlService.list(wrapper); | 
|         return R.ok(exportSupport(list, fields)); | 
|     } | 
|   | 
|     private <T> void convert(Map<String, Object> map, QueryWrapper<T> wrapper){ | 
|         for (Map.Entry<String, Object> entry : map.entrySet()){ | 
|             if(Cools.eq(entry.getKey(),"curr") | 
|                     || Cools.eq(entry.getKey(),"limit") | 
|                     || Cools.eq(entry.getKey(),"unreason") | 
|                     || Cools.eq(entry.getKey(),"orderByField") | 
|                     || Cools.eq(entry.getKey(),"orderByType") | 
|                     || Cools.eq(entry.getKey(),"row") | 
|                     || Cools.eq(entry.getKey(),"condition")){ | 
|                 continue; | 
|             } | 
|   | 
|             if(Cools.isEmpty(entry.getValue())){ | 
|                 continue; | 
|             } | 
|   | 
|             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/page/auth") | 
|     @ManagerAuth | 
|     public R page(@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(required = false) String timeRange, | 
|                   @RequestParam Map<String, Object> param) { | 
|         QueryWrapper<LocDetl> wrapper = new QueryWrapper<>(); | 
|         Object barcode = null; | 
|         if (!Cools.isEmpty(param.get("barcode"))) { | 
|             barcode = param.get("barcode"); | 
|         } | 
|         param.remove("zpallet"); | 
|         param.remove("barcode"); | 
|   | 
|         if (!Cools.isEmpty(param.get("decrees"))) { | 
|             ArrayList<String> mats = new ArrayList<>(); | 
|             List<Mat> decrees = matService.list(new LambdaQueryWrapper<Mat>().like(Mat::getDecrees, param.get("decrees"))); | 
|             if (decrees.isEmpty()) { | 
|                 mats.add("-1"); | 
|             }else { | 
|                 for (Mat mat : decrees) { | 
|                     mats.add(mat.getMatnr()); | 
|                 } | 
|             } | 
|   | 
|             wrapper.in("matnr", mats); | 
|             param.remove("decrees"); | 
|         } | 
|   | 
|         convert(param,wrapper); | 
|         wrapper.eq("host_id", getHostId()); | 
| //        if (!Cools.isEmpty(condition)) { | 
| //            wrapper.like(LocDetl::getLocNo, condition); | 
| //        } | 
|         if (!Cools.isEmpty(timeRange)) { | 
|             String[] range = timeRange.split(RANGE_TIME_LINK); | 
|             wrapper.ge("appe_time", DateUtils.convert(range[0])); | 
|             wrapper.le("appe_time", DateUtils.convert(range[1])); | 
|         } | 
|         if (!Cools.isEmpty(param.get("locNo"))) { | 
|             wrapper.eq("loc_no", param.get("locNo")); | 
|         } | 
|   | 
|         if (!Cools.isEmpty(barcode)) { | 
|             wrapper.eq("zpallet", barcode); | 
|         } | 
|   | 
|         if (!Cools.isEmpty(orderByField)){ | 
|             wrapper.orderBy(true, "asc".equals(orderByType), humpToLine(orderByField)); | 
|         } | 
|   | 
|   | 
|   | 
|         param.remove("locNo"); | 
|         param.remove("owner"); | 
|         param.remove("matnr"); | 
|         allLike(LocDetl.class, param.keySet(), wrapper, condition); | 
|         return R.ok(locDetlService.page(new Page<>(curr, limit), wrapper)); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetl/auth") | 
|     @ManagerAuth | 
|     public R stockOutList(@RequestParam(value = "locNos[]") List<String> locNos){ | 
|         if (!locNos.isEmpty()) { | 
|             List<LocDetl> res = new ArrayList<>(); | 
|             for (String locNo : new HashSet<>(locNos)) { | 
|                 List<LocDetl> locDetls = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocNo, locNo)); | 
|                 if (!locDetls.isEmpty()) { | 
|                     res.addAll(locDetls); | 
|                 } | 
|             } | 
|             return R.ok().add(res); | 
|         } | 
|         return R.parse(BaseRes.EMPTY); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetl/add/auth") | 
|     @ManagerAuth | 
|     public R add(LocDetl locDetl) { | 
|         locDetl.setHostId(getHostId()); | 
|         locDetlService.save(locDetl); | 
|         return R.ok(); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetl/update/auth") | 
|     @ManagerAuth | 
|     public R update(LocDetl locDetl){ | 
|         if (Cools.isEmpty(locDetl) || null==locDetl.getLocNo()){ | 
|             return R.error(); | 
|         } | 
|         locDetlService.updateById(locDetl); | 
|         return R.ok(); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetl/delete/auth") | 
|     @ManagerAuth | 
|     public R delete(@RequestParam(value="ids[]") Long[] ids){ | 
|          for (Long id : ids){ | 
|             locDetlService.removeById(id); | 
|         } | 
|         return R.ok(); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetlQuery/auth") | 
|     @ManagerAuth | 
|     public R query(String condition) { | 
|         LambdaQueryWrapper<LocDetl> wrapper = new LambdaQueryWrapper<>(); | 
|         wrapper.eq(LocDetl::getHostId, getHostId()); | 
|         wrapper.like(LocDetl::getLocNo, condition); | 
|         Page<LocDetl> page = locDetlService.page(new Page<>(0, 10), wrapper); | 
|         List<Map<String, Object>> result = new ArrayList<>(); | 
|         for (LocDetl locDetl : page.getRecords()){ | 
|             Map<String, Object> map = new HashMap<>(); | 
|             map.put("id", locDetl.getLocNo()); | 
|             map.put("value", locDetl.getLocNo()); | 
|             result.add(map); | 
|         } | 
|         return R.ok(result); | 
|     } | 
|   | 
|     @RequestMapping("/locDetl/all/get/kv") | 
|     @ManagerAuth | 
|     public R getDataKV(@RequestParam(required = false) String condition) { | 
|         List<KeyValueVo> vos = new ArrayList<>(); | 
|         LambdaQueryWrapper<LocDetl> wrapper = new LambdaQueryWrapper<>(); | 
|         wrapper.eq(LocDetl::getHostId, getHostId()); | 
|         if (!Cools.isEmpty(condition)) { | 
|             wrapper.like(LocDetl::getLocNo, condition); | 
|         } | 
|         locDetlService.page(new Page<>(1, 30), wrapper).getRecords().forEach(item -> vos.add(new KeyValueVo(String.valueOf(item.getLocNo()), Long.parseLong(item.getLocNo())))); | 
|         return R.ok().add(vos); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetl/statis/auth") | 
|     @ManagerAuth | 
|     public R statis(@RequestParam(defaultValue = "1")Integer curr, | 
|                     @RequestParam(defaultValue = "10")Integer limit, | 
|                     @RequestParam Map<String, Object> param) { | 
|         IPage<LocDetl> stockStatis = locDetlService.getStockStatis(curr, limit, param); | 
|         for (LocDetl locDetl : stockStatis.getRecords()) { | 
|             Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>() | 
|                     .eq(Mat::getMatnr, locDetl.getMatnr()) | 
|                     .eq(Mat::getHostId, getHostId())); | 
|             if (mat != null) { | 
|                 locDetl.sync(mat); | 
|             } | 
|         } | 
|         return R.ok().add(stockStatis); | 
|     } | 
|   | 
|     /** | 
|      * 获取库存总数 | 
|      * @return | 
|      */ | 
|     @RequestMapping("/locDetl/count") | 
|     public R getAllCount(){ | 
|         Integer sum = locDetlService.sum(); | 
|         return R.ok(sum); | 
|     } | 
|   | 
|     @RequestMapping(value = "/stock/out/list/auth") | 
|     @ManagerAuth | 
|     public R stockOutList(@RequestParam(defaultValue = "1")Integer curr, | 
|                           @RequestParam(defaultValue = "10")Integer limit, | 
|                           @RequestParam Map<String, Object> 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"); | 
|             } | 
|         } | 
|         param.put("hostId", getHostId()); | 
|         return R.ok(locDetlService.getStockOut(curr, limit, param)); | 
|     } | 
|   | 
|     @RequestMapping(value = "/locDetl/statis/export") | 
|     public void statisExport(HttpServletResponse response) throws IOException { | 
|         List<LocDetl> excel = locDetlService.getStockStatisExcel(); | 
|         for (LocDetl locDetl : excel) { | 
|             Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, 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); | 
|     } | 
|   | 
| } |