MaterialController.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. package com.jsh.erp.controller;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.jsh.erp.base.BaseController;
  5. import com.jsh.erp.base.TableDataInfo;
  6. import com.jsh.erp.datasource.entities.Material;
  7. import com.jsh.erp.datasource.entities.MaterialExtend;
  8. import com.jsh.erp.datasource.entities.MaterialVo4Unit;
  9. import com.jsh.erp.datasource.entities.Unit;
  10. import com.jsh.erp.service.*;
  11. import com.jsh.erp.utils.*;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.web.bind.annotation.*;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import javax.annotation.Resource;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. import java.math.BigDecimal;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;
  28. import static com.jsh.erp.utils.ResponseJsonUtil.returnStr;
  29. /**
  30. * @author ji|sheng|hua jshERP
  31. */
  32. @RestController
  33. @RequestMapping(value = "/material")
  34. @Api(tags = {"商品管理"})
  35. public class MaterialController extends BaseController {
  36. private Logger logger = LoggerFactory.getLogger(MaterialController.class);
  37. @Resource
  38. private MaterialService materialService;
  39. @Resource
  40. private DepotItemService depotItemService;
  41. @Resource
  42. private SystemConfigService systemConfigService;
  43. @Resource
  44. private UnitService unitService;
  45. @Resource
  46. private DepotService depotService;
  47. @Resource
  48. private RoleService roleService;
  49. @Resource
  50. private UserService userService;
  51. @Value(value="${file.uploadType}")
  52. private Long fileUploadType;
  53. @GetMapping(value = "/info")
  54. @ApiOperation(value = "根据id获取信息")
  55. public String getList(@RequestParam("id") Long id,
  56. HttpServletRequest request) throws Exception {
  57. Material material = materialService.getMaterial(id);
  58. Map<String, Object> objectMap = new HashMap<>();
  59. if(material != null) {
  60. objectMap.put("info", material);
  61. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  62. } else {
  63. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  64. }
  65. }
  66. @GetMapping(value = "/list")
  67. @ApiOperation(value = "获取信息列表")
  68. public TableDataInfo getList(@RequestParam(value = Constants.SEARCH, required = false) String search,
  69. HttpServletRequest request)throws Exception {
  70. String categoryId = StringUtil.getInfo(search, "categoryId");
  71. String materialParam = StringUtil.getInfo(search, "materialParam");
  72. String standard = StringUtil.getInfo(search, "standard");
  73. String model = StringUtil.getInfo(search, "model");
  74. String color = StringUtil.getInfo(search, "color");
  75. String brand = StringUtil.getInfo(search, "brand");
  76. String mfrs = StringUtil.getInfo(search, "mfrs");
  77. String materialOther = StringUtil.getInfo(search, "materialOther");
  78. String weight = StringUtil.getInfo(search, "weight");
  79. String expiryNum = StringUtil.getInfo(search, "expiryNum");
  80. String enableSerialNumber = StringUtil.getInfo(search, "enableSerialNumber");
  81. String enableBatchNumber = StringUtil.getInfo(search, "enableBatchNumber");
  82. String position = StringUtil.getInfo(search, "position");
  83. String enabled = StringUtil.getInfo(search, "enabled");
  84. String remark = StringUtil.getInfo(search, "remark");
  85. String mpList = StringUtil.getInfo(search, "mpList");
  86. List<MaterialVo4Unit> list = materialService.select(materialParam, standard, model, color, brand, mfrs, materialOther, weight, expiryNum,
  87. enableSerialNumber, enableBatchNumber, position, enabled, remark, categoryId, mpList);
  88. return getDataTable(list);
  89. }
  90. @PostMapping(value = "/add")
  91. @ApiOperation(value = "新增")
  92. public String addResource(@RequestBody JSONObject obj, HttpServletRequest request)throws Exception {
  93. Map<String, Object> objectMap = new HashMap<>();
  94. int insert = materialService.insertMaterial(obj, request);
  95. return returnStr(objectMap, insert);
  96. }
  97. @PutMapping(value = "/update")
  98. @ApiOperation(value = "修改")
  99. public String updateResource(@RequestBody JSONObject obj, HttpServletRequest request)throws Exception {
  100. Map<String, Object> objectMap = new HashMap<>();
  101. int update = materialService.updateMaterial(obj, request);
  102. return returnStr(objectMap, update);
  103. }
  104. @DeleteMapping(value = "/delete")
  105. @ApiOperation(value = "删除")
  106. public String deleteResource(@RequestParam("id") Long id, HttpServletRequest request)throws Exception {
  107. Map<String, Object> objectMap = new HashMap<>();
  108. int delete = materialService.deleteMaterial(id, request);
  109. return returnStr(objectMap, delete);
  110. }
  111. @DeleteMapping(value = "/deleteBatch")
  112. @ApiOperation(value = "批量删除")
  113. public String batchDeleteResource(@RequestParam("ids") String ids, HttpServletRequest request)throws Exception {
  114. Map<String, Object> objectMap = new HashMap<>();
  115. int delete = materialService.batchDeleteMaterial(ids, request);
  116. return returnStr(objectMap, delete);
  117. }
  118. @GetMapping(value = "/checkIsNameExist")
  119. @ApiOperation(value = "检查名称是否存在")
  120. public String checkIsNameExist(@RequestParam Long id, @RequestParam(value ="name", required = false) String name,
  121. HttpServletRequest request)throws Exception {
  122. Map<String, Object> objectMap = new HashMap<>();
  123. int exist = materialService.checkIsNameExist(id, name);
  124. if(exist > 0) {
  125. objectMap.put("status", true);
  126. } else {
  127. objectMap.put("status", false);
  128. }
  129. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  130. }
  131. /**
  132. * 检查商品是否存在
  133. * @param id
  134. * @param name
  135. * @param model
  136. * @param color
  137. * @param standard
  138. * @param mfrs
  139. * @param otherField1
  140. * @param otherField2
  141. * @param otherField3
  142. * @param unit
  143. * @param unitId
  144. * @param request
  145. * @return
  146. * @throws Exception
  147. */
  148. @GetMapping(value = "/checkIsExist")
  149. @ApiOperation(value = "检查商品是否存在")
  150. public String checkIsExist(@RequestParam("id") Long id, @RequestParam("name") String name,
  151. @RequestParam("model") String model, @RequestParam("color") String color,
  152. @RequestParam("standard") String standard, @RequestParam("mfrs") String mfrs,
  153. @RequestParam("otherField1") String otherField1, @RequestParam("otherField2") String otherField2,
  154. @RequestParam("otherField3") String otherField3, @RequestParam("unit") String unit,@RequestParam("unitId") Long unitId,
  155. HttpServletRequest request)throws Exception {
  156. Map<String, Object> objectMap = new HashMap<String, Object>();
  157. int exist = materialService.checkIsExist(id, name, StringUtil.toNull(model), StringUtil.toNull(color),
  158. StringUtil.toNull(standard), StringUtil.toNull(mfrs), StringUtil.toNull(otherField1),
  159. StringUtil.toNull(otherField2), StringUtil.toNull(otherField3), StringUtil.toNull(unit), unitId);
  160. if(exist > 0) {
  161. objectMap.put("status", true);
  162. } else {
  163. objectMap.put("status", false);
  164. }
  165. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  166. }
  167. /**
  168. * 批量设置状态-启用或者禁用
  169. * @param jsonObject
  170. * @param request
  171. * @return
  172. * @throws Exception
  173. */
  174. @PostMapping(value = "/batchSetStatus")
  175. @ApiOperation(value = "批量设置状态-启用或者禁用")
  176. public String batchSetStatus(@RequestBody JSONObject jsonObject,
  177. HttpServletRequest request)throws Exception {
  178. Boolean status = jsonObject.getBoolean("status");
  179. String ids = jsonObject.getString("ids");
  180. Map<String, Object> objectMap = new HashMap<>();
  181. int res = materialService.batchSetStatus(status, ids);
  182. if(res > 0) {
  183. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  184. } else {
  185. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  186. }
  187. }
  188. /**
  189. * 根据id来查询商品名称
  190. * @param id
  191. * @param request
  192. * @return
  193. */
  194. @GetMapping(value = "/findById")
  195. @ApiOperation(value = "根据id来查询商品名称")
  196. public BaseResponseInfo findById(@RequestParam("id") Long id, HttpServletRequest request) throws Exception{
  197. BaseResponseInfo res = new BaseResponseInfo();
  198. try {
  199. List<MaterialVo4Unit> list = materialService.findById(id);
  200. res.code = 200;
  201. res.data = list;
  202. } catch(Exception e){
  203. logger.error(e.getMessage(), e);
  204. res.code = 500;
  205. res.data = "获取数据失败";
  206. }
  207. return res;
  208. }
  209. /**
  210. * 根据meId来查询商品名称
  211. * @param meId
  212. * @param request
  213. * @return
  214. */
  215. @GetMapping(value = "/findByIdWithBarCode")
  216. @ApiOperation(value = "根据meId来查询商品名称")
  217. public BaseResponseInfo findByIdWithBarCode(@RequestParam("meId") Long meId,
  218. @RequestParam("mpList") String mpList,
  219. HttpServletRequest request) throws Exception{
  220. BaseResponseInfo res = new BaseResponseInfo();
  221. try {
  222. String[] mpArr = mpList.split(",");
  223. MaterialVo4Unit mu = new MaterialVo4Unit();
  224. List<MaterialVo4Unit> list = materialService.findByIdWithBarCode(meId);
  225. if(list!=null && list.size()>0) {
  226. mu = list.get(0);
  227. mu.setMaterialOther(materialService.getMaterialOtherByParam(mpArr, mu));
  228. }
  229. res.code = 200;
  230. res.data = mu;
  231. } catch(Exception e){
  232. logger.error(e.getMessage(), e);
  233. res.code = 500;
  234. res.data = "获取数据失败";
  235. }
  236. return res;
  237. }
  238. /**
  239. * 根据关键词查找商品信息-条码、名称、规格、型号
  240. * @param q
  241. * @param request
  242. * @return
  243. */
  244. @GetMapping(value = "/getMaterialByParam")
  245. @ApiOperation(value = "根据关键词查找商品信息")
  246. public BaseResponseInfo getMaterialByParam(@RequestParam("q") String q,
  247. HttpServletRequest request) throws Exception{
  248. BaseResponseInfo res = new BaseResponseInfo();
  249. try {
  250. JSONArray arr = materialService.getMaterialByParam(q);
  251. res.code = 200;
  252. res.data = arr;
  253. } catch (Exception e) {
  254. logger.error(e.getMessage(), e);
  255. res.code = 500;
  256. res.data = "获取数据失败";
  257. }
  258. return res;
  259. }
  260. /**
  261. * 查找商品信息-下拉框
  262. * @param mpList
  263. * @param request
  264. * @return
  265. */
  266. @GetMapping(value = "/findBySelect")
  267. @ApiOperation(value = "查找商品信息")
  268. public JSONObject findBySelect(@RequestParam(value = "categoryId", required = false) Long categoryId,
  269. @RequestParam(value = "q", required = false) String q,
  270. @RequestParam(value = "standardOrModel", required = false) String standardOrModel,
  271. @RequestParam(value = "mpList", required = false) String mpList,
  272. @RequestParam(value = "depotId", required = false) Long depotId,
  273. @RequestParam(value = "color", required = false) String color,
  274. @RequestParam(value = "brand", required = false) String brand,
  275. @RequestParam(value = "mfrs", required = false) String mfrs,
  276. @RequestParam(value = "enableSerialNumber", required = false) String enableSerialNumber,
  277. @RequestParam(value = "enableBatchNumber", required = false) String enableBatchNumber,
  278. @RequestParam("page") Integer currentPage,
  279. @RequestParam("rows") Integer pageSize,
  280. HttpServletRequest request) throws Exception{
  281. JSONObject object = new JSONObject();
  282. try {
  283. String[] mpArr = new String[]{};
  284. if(StringUtil.isNotEmpty(mpList)){
  285. mpArr= mpList.split(",");
  286. }
  287. List<MaterialVo4Unit> dataList = materialService.findBySelectWithBarCode(categoryId, q, StringUtil.toNull(standardOrModel),
  288. StringUtil.toNull(color), StringUtil.toNull(brand), StringUtil.toNull(mfrs), enableSerialNumber, enableBatchNumber,
  289. (currentPage-1)*pageSize, pageSize,depotId);
  290. int total = materialService.findBySelectWithBarCodeCount(categoryId, q, StringUtil.toNull(standardOrModel),
  291. StringUtil.toNull(color), StringUtil.toNull(brand), StringUtil.toNull(mfrs), enableSerialNumber, enableBatchNumber,depotId);
  292. object.put("total", total);
  293. JSONArray dataArray = new JSONArray();
  294. //存放数据json数组
  295. if (null != dataList) {
  296. for (MaterialVo4Unit material : dataList) {
  297. JSONObject item = new JSONObject();
  298. item.put("id", material.getMeId()); //商品扩展表的id
  299. item.put("mid", material.getId()); //商品扩展表的id
  300. String ratioStr = ""; //比例
  301. Unit unit = new Unit();
  302. if (material.getUnitId() == null) {
  303. ratioStr = "";
  304. } else {
  305. unit = unitService.getUnit(material.getUnitId());
  306. //拼接副单位的比例
  307. String commodityUnit = material.getCommodityUnit();
  308. if(commodityUnit.equals(unit.getBasicUnit())) {
  309. ratioStr = "[基本]";
  310. }
  311. if(commodityUnit.equals(unit.getOtherUnit()) && unit.getRatio()!=null) {
  312. ratioStr = "[" + unit.getRatio().stripTrailingZeros().toPlainString() + unit.getBasicUnit() + "]";
  313. }
  314. if(commodityUnit.equals(unit.getOtherUnitTwo()) && unit.getRatioTwo()!=null) {
  315. ratioStr = "[" + unit.getRatioTwo().stripTrailingZeros().toPlainString() + unit.getBasicUnit() + "]";
  316. }
  317. if(commodityUnit.equals(unit.getOtherUnitThree()) && unit.getRatioThree()!=null) {
  318. ratioStr = "[" + unit.getRatioThree().stripTrailingZeros().toPlainString() + unit.getBasicUnit() + "]";
  319. }
  320. }
  321. item.put("barCode", material.getBarCode());
  322. item.put("name", material.getName());
  323. item.put("mnemonic", material.getMnemonic());
  324. item.put("categoryName", material.getCategoryName());
  325. item.put("standard", material.getStandard());
  326. item.put("model", material.getModel());
  327. item.put("color", material.getColor());
  328. item.put("brand", material.getBrand());
  329. //item.put("mfrs", material.getMfrs());
  330. item.put("unit", material.getCommodityUnit() + ratioStr);
  331. item.put("sku", material.getSku());
  332. item.put("enableSerialNumber", material.getEnableSerialNumber());
  333. item.put("enableBatchNumber", material.getEnableBatchNumber());
  334. item.put("productionDate",material.getProductionDate());
  335. item.put("expiryNum",material.getExpiryNum());
  336. item.put("batchNumber",material.getBatchNumber());
  337. item.put("position",material.getPosition());
  338. item.put("supplierId",material.getSupplierId());
  339. item.put("supplierName",material.getSupplierName());
  340. item.put("depotId",material.getDepotId());
  341. item.put("depotName",material.getDepotName());
  342. item.put("unitId",material.getUnitId());
  343. BigDecimal stock;
  344. if(StringUtil.isNotEmpty(material.getSku())){
  345. stock = depotItemService.getSkuStockByParam(depotId,material.getMeId(),null,null);
  346. } else {
  347. stock = depotItemService.getCurrentStockByParam(depotId, material.getId());
  348. if (material.getUnitId()!=null){
  349. String commodityUnit = material.getCommodityUnit();
  350. stock = unitService.parseStockByUnit(stock, unit, commodityUnit);
  351. }
  352. }
  353. item.put("stock", stock);
  354. item.put("expand", materialService.getMaterialOtherByParam(mpArr, material));
  355. item.put("imgName", material.getImgName());
  356. if(fileUploadType == 2) {
  357. item.put("imgSmall", "small");
  358. item.put("imgLarge", "large");
  359. } else {
  360. item.put("imgSmall", "");
  361. item.put("imgLarge", "");
  362. }
  363. dataArray.add(item);
  364. }
  365. }
  366. object.put("rows", dataArray);
  367. } catch (Exception e) {
  368. logger.error(e.getMessage(), e);
  369. }
  370. return object;
  371. }
  372. /**
  373. * 根据商品id查找商品信息
  374. * @param meId
  375. * @param request
  376. * @return
  377. * @throws Exception
  378. */
  379. @GetMapping(value = "/getMaterialByMeId")
  380. @ApiOperation(value = "根据商品id查找商品信息")
  381. public JSONObject getMaterialByMeId(@RequestParam(value = "meId", required = false) Long meId,
  382. @RequestParam("mpList") String mpList,
  383. HttpServletRequest request) throws Exception{
  384. JSONObject item = new JSONObject();
  385. try {
  386. String[] mpArr = mpList.split(",");
  387. List<MaterialVo4Unit> materialList = materialService.getMaterialByMeId(meId);
  388. if(materialList!=null && materialList.size()!=1) {
  389. return item;
  390. } else if(materialList.size() == 1) {
  391. MaterialVo4Unit material = materialList.get(0);
  392. item.put("Id", material.getMeId()); //商品扩展表的id
  393. String ratio; //比例
  394. if (material.getUnitId() == null || material.getUnitId().equals("")) {
  395. ratio = "";
  396. } else {
  397. ratio = material.getUnitName();
  398. ratio = ratio.substring(ratio.indexOf("("));
  399. }
  400. //名称/型号/扩展信息/包装
  401. String MaterialName = "";
  402. MaterialName = MaterialName + material.getmBarCode() + "_" + material.getName()
  403. + ((material.getStandard() == null || material.getStandard().equals("")) ? "" : "(" + material.getStandard() + ")");
  404. String expand = materialService.getMaterialOtherByParam(mpArr, material); //扩展信息
  405. MaterialName = MaterialName + expand + ((material.getUnit() == null || material.getUnit().equals("")) ? "" : "(" + material.getUnit() + ")") + ratio;
  406. item.put("MaterialName", MaterialName);
  407. item.put("name", material.getName());
  408. item.put("expand", expand);
  409. item.put("model", material.getModel());
  410. item.put("standard", material.getStandard());
  411. item.put("unit", material.getUnit() + ratio);
  412. }
  413. } catch (Exception e) {
  414. logger.error(e.getMessage(), e);
  415. }
  416. return item;
  417. }
  418. /**
  419. * 生成excel表格
  420. * @param categoryId
  421. * @param materialParam
  422. * @param color
  423. * @param weight
  424. * @param expiryNum
  425. * @param enabled
  426. * @param enableSerialNumber
  427. * @param enableBatchNumber
  428. * @param remark
  429. * @param mpList
  430. * @param request
  431. * @param response
  432. */
  433. @GetMapping(value = "/exportExcel")
  434. @ApiOperation(value = "生成excel表格")
  435. public void exportExcel(@RequestParam(value = "categoryId", required = false) String categoryId,
  436. @RequestParam(value = "materialParam", required = false) String materialParam,
  437. @RequestParam(value = "color", required = false) String color,
  438. @RequestParam(value = "materialOther", required = false) String materialOther,
  439. @RequestParam(value = "weight", required = false) String weight,
  440. @RequestParam(value = "expiryNum", required = false) String expiryNum,
  441. @RequestParam(value = "enabled", required = false) String enabled,
  442. @RequestParam(value = "enableSerialNumber", required = false) String enableSerialNumber,
  443. @RequestParam(value = "enableBatchNumber", required = false) String enableBatchNumber,
  444. @RequestParam(value = "remark", required = false) String remark,
  445. @RequestParam(value = "mpList", required = false) String mpList,
  446. HttpServletRequest request, HttpServletResponse response) {
  447. try {
  448. materialService.exportExcel(StringUtil.toNull(categoryId), StringUtil.toNull(materialParam), StringUtil.toNull(color),
  449. StringUtil.toNull(materialOther), StringUtil.toNull(weight),
  450. StringUtil.toNull(expiryNum), StringUtil.toNull(enabled), StringUtil.toNull(enableSerialNumber),
  451. StringUtil.toNull(enableBatchNumber), StringUtil.toNull(remark), response);
  452. } catch (Exception e) {
  453. logger.error(e.getMessage(), e);
  454. }
  455. }
  456. /**
  457. * excel表格导入产品(含初始库存)
  458. * @param file
  459. * @param request
  460. * @param response
  461. * @return
  462. */
  463. @PostMapping(value = "/importExcel")
  464. @ApiOperation(value = "excel表格导入产品")
  465. public BaseResponseInfo importExcel(MultipartFile file,
  466. HttpServletRequest request, HttpServletResponse response) throws Exception{
  467. BaseResponseInfo res = new BaseResponseInfo();
  468. try {
  469. res = materialService.importExcelTwo(file, request);
  470. } catch (Exception e) {
  471. logger.error(e.getMessage(), e);
  472. }
  473. return res;
  474. }
  475. /**
  476. * 获取商品序列号
  477. * @param q
  478. * @param currentPage
  479. * @param pageSize
  480. * @param request
  481. * @param response
  482. * @return
  483. * @throws Exception
  484. */
  485. @GetMapping(value = "/getMaterialEnableSerialNumberList")
  486. @ApiOperation(value = "获取商品序列号")
  487. public JSONObject getMaterialEnableSerialNumberList(
  488. @RequestParam(value = "q", required = false) String q,
  489. @RequestParam("page") Integer currentPage,
  490. @RequestParam("rows") Integer pageSize,
  491. HttpServletRequest request,
  492. HttpServletResponse response)throws Exception {
  493. JSONObject object= new JSONObject();
  494. try {
  495. List<MaterialVo4Unit> list = materialService.getMaterialEnableSerialNumberList(q, (currentPage-1)*pageSize, pageSize);
  496. Long count = materialService.getMaterialEnableSerialNumberCount(q);
  497. object.put("rows", list);
  498. object.put("total", count);
  499. } catch (Exception e) {
  500. logger.error(e.getMessage(), e);
  501. }
  502. return object;
  503. }
  504. /**
  505. * 获取最大条码
  506. * @return
  507. * @throws Exception
  508. */
  509. @GetMapping(value = "/getMaxBarCode")
  510. @ApiOperation(value = "获取最大条码")
  511. public BaseResponseInfo getMaxBarCode() throws Exception {
  512. BaseResponseInfo res = new BaseResponseInfo();
  513. Map<String, Object> map = new HashMap<String, Object>();
  514. String barCode = materialService.getMaxBarCode();
  515. map.put("barCode", barCode);
  516. res.code = 200;
  517. res.data = map;
  518. return res;
  519. }
  520. /**
  521. * 商品名称模糊匹配
  522. * @return
  523. * @throws Exception
  524. */
  525. @GetMapping(value = "/getMaterialNameList")
  526. @ApiOperation(value = "商品名称模糊匹配")
  527. public JSONArray getMaterialNameList() throws Exception {
  528. JSONArray arr = new JSONArray();
  529. try {
  530. List<String> list = materialService.getMaterialNameList();
  531. for (String s : list) {
  532. JSONObject item = new JSONObject();
  533. item.put("value", s);
  534. item.put("text", s);
  535. arr.add(item);
  536. }
  537. } catch (Exception e) {
  538. logger.error(e.getMessage(), e);
  539. }
  540. return arr;
  541. }
  542. /**
  543. * 根据条码查询商品信息
  544. * @return
  545. * @throws Exception
  546. */
  547. @GetMapping(value = "/getMaterialByBarCode")
  548. @ApiOperation(value = "根据条码查询商品信息")
  549. public BaseResponseInfo getMaterialByBarCode(@RequestParam("barCode") String barCode,
  550. @RequestParam(value = "organId", required = false) Long organId,
  551. @RequestParam(value = "depotId", required = false) Long depotId,
  552. @RequestParam("mpList") String mpList,
  553. @RequestParam(required = false, value = "prefixNo") String prefixNo,
  554. HttpServletRequest request) throws Exception {
  555. BaseResponseInfo res = new BaseResponseInfo();
  556. try {
  557. Long userId = userService.getUserId(request);
  558. String priceLimit = userService.getRoleTypeByUserId(userId).getPriceLimit();
  559. String[] mpArr = mpList.split(",");
  560. //支持序列号查询,先根据序列号查询条码,如果查不到就直接查条码
  561. MaterialExtend materialExtend = materialService.getMaterialExtendBySerialNumber(barCode);
  562. if(materialExtend!=null && StringUtil.isNotEmpty(materialExtend.getBarCode())) {
  563. barCode = materialExtend.getBarCode();
  564. }
  565. List<MaterialVo4Unit> list = materialService.getMaterialByBarCode(barCode);
  566. if(list!=null && list.size()>0) {
  567. for(MaterialVo4Unit mvo: list) {
  568. mvo.setMaterialOther(materialService.getMaterialOtherByParam(mpArr, mvo));
  569. if ("LSCK".equals(prefixNo) || "LSTH".equals(prefixNo)) {
  570. //零售价
  571. mvo.setBillPrice(mvo.getCommodityDecimal());
  572. } else if ("CGDD".equals(prefixNo) || "CGRK".equals(prefixNo) || "CGTH".equals(prefixNo)) {
  573. //采购价
  574. mvo.setBillPrice(mvo.getPurchaseDecimal());
  575. } else if("QTRK".equals(prefixNo) || "DBCK".equals(prefixNo) || "ZZD".equals(prefixNo) || "CXD".equals(prefixNo)
  576. || "PDLR".equals(prefixNo) || "PDFP".equals(prefixNo)) {
  577. //采购价-给录入界面按权限屏蔽
  578. mvo.setBillPrice(roleService.parseBillPriceByLimit(mvo.getPurchaseDecimal(), "buy", priceLimit, request));
  579. } if ("XSDD".equals(prefixNo) || "XSCK".equals(prefixNo) || "XSTH".equals(prefixNo) || "QTCK".equals(prefixNo)) {
  580. //销售价
  581. if(organId == null) {
  582. mvo.setBillPrice(mvo.getWholesaleDecimal());
  583. } else {
  584. //查询最后一单的销售价,实现不同的客户不同的销售价
  585. BigDecimal lastUnitPrice = depotItemService.getLastUnitPriceByParam(organId, mvo.getMeId(), prefixNo);
  586. mvo.setBillPrice(lastUnitPrice!=null? lastUnitPrice : mvo.getWholesaleDecimal());
  587. }
  588. //销售价-给录入界面按权限屏蔽价格
  589. if("QTCK".equals(prefixNo)) {
  590. mvo.setBillPrice(roleService.parseBillPriceByLimit(mvo.getWholesaleDecimal(), "sale", priceLimit, request));
  591. }
  592. }
  593. //仓库id
  594. if (depotId == null) {
  595. JSONArray depotArr = depotService.findDepotByCurrentUser();
  596. for (Object obj : depotArr) {
  597. JSONObject depotObj = JSONObject.parseObject(obj.toString());
  598. if (depotObj.get("isDefault") != null) {
  599. Boolean isDefault = depotObj.getBoolean("isDefault");
  600. if (isDefault) {
  601. Long id = depotObj.getLong("id");
  602. if (!"CGDD".equals(prefixNo) && !"XSDD".equals(prefixNo)) {
  603. //除订单之外的单据才有仓库
  604. mvo.setDepotId(id);
  605. }
  606. getStockByMaterialInfo(mvo);
  607. }
  608. }
  609. }
  610. } else {
  611. mvo.setDepotId(depotId);
  612. getStockByMaterialInfo(mvo);
  613. }
  614. }
  615. }
  616. res.code = 200;
  617. res.data = list;
  618. } catch(Exception e){
  619. logger.error(e.getMessage(), e);
  620. res.code = 500;
  621. res.data = "获取数据失败";
  622. }
  623. return res;
  624. }
  625. /**
  626. * 根据商品信息获取库存,进行赋值
  627. * @param mvo
  628. * @throws Exception
  629. */
  630. private void getStockByMaterialInfo(MaterialVo4Unit mvo) throws Exception {
  631. BigDecimal stock;
  632. if (StringUtil.isNotEmpty(mvo.getSku())) {
  633. stock = depotItemService.getSkuStockByParam(mvo.getDepotId(), mvo.getMeId(), null, null);
  634. } else {
  635. stock = depotItemService.getCurrentStockByParam(mvo.getDepotId(), mvo.getId());
  636. if (mvo.getUnitId() != null) {
  637. Unit unit = unitService.getUnit(mvo.getUnitId());
  638. String commodityUnit = mvo.getCommodityUnit();
  639. stock = unitService.parseStockByUnit(stock, unit, commodityUnit);
  640. }
  641. }
  642. mvo.setStock(stock);
  643. }
  644. /**
  645. * 商品库存查询
  646. * @param currentPage
  647. * @param pageSize
  648. * @param depotIds
  649. * @param categoryId
  650. * @param materialParam
  651. * @param zeroStock
  652. * @param column
  653. * @param order
  654. * @param request
  655. * @return
  656. * @throws Exception
  657. */
  658. @GetMapping(value = "/getListWithStock")
  659. @ApiOperation(value = "商品库存查询")
  660. public BaseResponseInfo getListWithStock(@RequestParam("currentPage") Integer currentPage,
  661. @RequestParam("pageSize") Integer pageSize,
  662. @RequestParam(value = "depotIds", required = false) String depotIds,
  663. @RequestParam(value = "categoryId", required = false) Long categoryId,
  664. @RequestParam(value = "position", required = false) String position,
  665. @RequestParam("materialParam") String materialParam,
  666. @RequestParam("zeroStock") Integer zeroStock,
  667. @RequestParam(value = "column", required = false, defaultValue = "createTime") String column,
  668. @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
  669. HttpServletRequest request)throws Exception {
  670. BaseResponseInfo res = new BaseResponseInfo();
  671. Map<String, Object> map = new HashMap<>();
  672. try {
  673. List<Long> idList = new ArrayList<>();
  674. List<Long> depotList = new ArrayList<>();
  675. if(categoryId != null){
  676. idList = materialService.getListByParentId(categoryId);
  677. }
  678. if(StringUtil.isNotEmpty(depotIds)) {
  679. depotList = StringUtil.strToLongList(depotIds);
  680. } else {
  681. //未选择仓库时默认为当前用户有权限的仓库
  682. JSONArray depotArr = depotService.findDepotByCurrentUser();
  683. for(Object obj: depotArr) {
  684. JSONObject object = JSONObject.parseObject(obj.toString());
  685. depotList.add(object.getLong("id"));
  686. }
  687. }
  688. Boolean moveAvgPriceFlag = systemConfigService.getMoveAvgPriceFlag();
  689. List<MaterialVo4Unit> dataList = materialService.getListWithStock(depotList, idList, StringUtil.toNull(position), StringUtil.toNull(materialParam),
  690. moveAvgPriceFlag, zeroStock, StringUtil.safeSqlParse(column), StringUtil.safeSqlParse(order), (currentPage-1)*pageSize, pageSize);
  691. int total = materialService.getListWithStockCount(depotList, idList, StringUtil.toNull(position), StringUtil.toNull(materialParam), zeroStock);
  692. MaterialVo4Unit materialVo4Unit= materialService.getTotalStockAndPrice(depotList, idList, StringUtil.toNull(position), StringUtil.toNull(materialParam));
  693. map.put("total", total);
  694. map.put("currentStock", materialVo4Unit.getCurrentStock()!=null?materialVo4Unit.getCurrentStock():BigDecimal.ZERO);
  695. if(moveAvgPriceFlag) {
  696. map.put("currentStockPrice", materialVo4Unit.getCurrentStockMovePrice()!=null?materialVo4Unit.getCurrentStockMovePrice():BigDecimal.ZERO);
  697. } else {
  698. map.put("currentStockPrice", materialVo4Unit.getCurrentStockPrice()!=null?materialVo4Unit.getCurrentStockPrice():BigDecimal.ZERO);
  699. }
  700. map.put("currentWeight", materialVo4Unit.getCurrentWeight()!=null?materialVo4Unit.getCurrentWeight():BigDecimal.ZERO);
  701. map.put("rows", dataList);
  702. res.code = 200;
  703. res.data = map;
  704. } catch(Exception e){
  705. logger.error(e.getMessage(), e);
  706. res.code = 500;
  707. res.data = "获取数据失败";
  708. }
  709. return res;
  710. }
  711. /**
  712. * 批量设置商品当前的实时库存(按每个仓库)
  713. * @param jsonObject
  714. * @param request
  715. * @return
  716. * @throws Exception
  717. */
  718. @PostMapping(value = "/batchSetMaterialCurrentStock")
  719. @ApiOperation(value = "批量设置商品当前的实时库存(按每个仓库)")
  720. public String batchSetMaterialCurrentStock(@RequestBody JSONObject jsonObject,
  721. HttpServletRequest request)throws Exception {
  722. String ids = jsonObject.getString("ids");
  723. Map<String, Object> objectMap = new HashMap<>();
  724. int res = materialService.batchSetMaterialCurrentStock(ids);
  725. if(res > 0) {
  726. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  727. } else {
  728. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  729. }
  730. }
  731. /**
  732. * 批量设置商品当前的成本价
  733. * @param jsonObject
  734. * @param request
  735. * @return
  736. * @throws Exception
  737. */
  738. @PostMapping(value = "/batchSetMaterialCurrentUnitPrice")
  739. @ApiOperation(value = "批量设置商品当前的成本价")
  740. public String batchSetMaterialCurrentUnitPrice(@RequestBody JSONObject jsonObject,
  741. HttpServletRequest request)throws Exception {
  742. String ids = jsonObject.getString("ids");
  743. Map<String, Object> objectMap = new HashMap<>();
  744. int res = materialService.batchSetMaterialCurrentUnitPrice(ids);
  745. if(res > 0) {
  746. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  747. } else {
  748. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  749. }
  750. }
  751. /**
  752. * 批量更新商品信息
  753. * @param jsonObject
  754. * @param request
  755. * @return
  756. * @throws Exception
  757. */
  758. @PostMapping(value = "/batchUpdate")
  759. @ApiOperation(value = "批量更新商品信息")
  760. public String batchUpdate(@RequestBody JSONObject jsonObject,
  761. HttpServletRequest request)throws Exception {
  762. Map<String, Object> objectMap = new HashMap<>();
  763. int res = materialService.batchUpdate(jsonObject);
  764. if(res > 0) {
  765. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  766. } else {
  767. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  768. }
  769. }
  770. /**
  771. * 转换名称为拼音
  772. * @param jsonObject
  773. */
  774. @PostMapping(value = "/changeNameToPinYin")
  775. @ApiOperation(value = "转换名称为拼音")
  776. public BaseResponseInfo changeNameToPinYin(@RequestBody JSONObject jsonObject)throws Exception {
  777. BaseResponseInfo res = new BaseResponseInfo();
  778. try {
  779. String name = jsonObject.getString("name");
  780. res.code = 200;
  781. res.data = PinYinUtil.getFirstLettersLo(name);
  782. } catch(Exception e){
  783. logger.error(e.getMessage(), e);
  784. res.code = 500;
  785. res.data = "获取数据失败";
  786. }
  787. return res;
  788. }
  789. /**
  790. * 根据批次号查询商品信息
  791. * @return
  792. * @throws Exception
  793. */
  794. @GetMapping(value = "/getMaterialByBatchNumber")
  795. @ApiOperation(value = "根据批次号查询商品信息")
  796. public BaseResponseInfo getMaterialByBatchNumber(@RequestParam("batchNumber") String batchNumber,
  797. @RequestParam(value = "organId", required = false) Long organId,
  798. @RequestParam(value = "depotId", required = false) Long depotId,
  799. @RequestParam("mpList") String mpList,
  800. @RequestParam(required = false, value = "prefixNo") String prefixNo,
  801. HttpServletRequest request) throws Exception {
  802. BaseResponseInfo res = new BaseResponseInfo();
  803. try {
  804. Long userId = userService.getUserId(request);
  805. String priceLimit = userService.getRoleTypeByUserId(userId).getPriceLimit();
  806. String[] mpArr = mpList.split(",");
  807. //支持序列号查询,先根据序列号查询条码,如果查不到就直接查条码
  808. // MaterialExtend materialExtend = materialService.getMaterialExtendBySerialNumber(barCode);
  809. // if(materialExtend!=null && StringUtil.isNotEmpty(materialExtend.getBarCode())) {
  810. // barCode = materialExtend.getBarCode();
  811. // }
  812. List<MaterialVo4Unit> list = materialService.getMaterialByBatchNumber(batchNumber);
  813. if(list!=null && list.size()>0) {
  814. for(MaterialVo4Unit mvo: list) {
  815. mvo.setMaterialOther(materialService.getMaterialOtherByParam(mpArr, mvo));
  816. if ("LSCK".equals(prefixNo) || "LSTH".equals(prefixNo)) {
  817. //零售价
  818. mvo.setBillPrice(mvo.getCommodityDecimal());
  819. } else if ("CGDD".equals(prefixNo) || "CGRK".equals(prefixNo) || "CGTH".equals(prefixNo)) {
  820. //采购价
  821. mvo.setBillPrice(mvo.getPurchaseDecimal());
  822. } else if("QTRK".equals(prefixNo) || "DBCK".equals(prefixNo) || "ZZD".equals(prefixNo) || "CXD".equals(prefixNo)
  823. || "PDLR".equals(prefixNo) || "PDFP".equals(prefixNo)) {
  824. //采购价-给录入界面按权限屏蔽
  825. mvo.setBillPrice(roleService.parseBillPriceByLimit(mvo.getPurchaseDecimal(), "buy", priceLimit, request));
  826. } if ("XSDD".equals(prefixNo) || "XSCK".equals(prefixNo) || "XSTH".equals(prefixNo) || "QTCK".equals(prefixNo)) {
  827. //销售价
  828. if(organId == null) {
  829. mvo.setBillPrice(mvo.getWholesaleDecimal());
  830. } else {
  831. //查询最后一单的销售价,实现不同的客户不同的销售价
  832. BigDecimal lastUnitPrice = depotItemService.getLastUnitPriceByParam(organId, mvo.getMeId(), prefixNo);
  833. mvo.setBillPrice(lastUnitPrice!=null? lastUnitPrice : mvo.getWholesaleDecimal());
  834. }
  835. //销售价-给录入界面按权限屏蔽价格
  836. if("QTCK".equals(prefixNo)) {
  837. mvo.setBillPrice(roleService.parseBillPriceByLimit(mvo.getWholesaleDecimal(), "sale", priceLimit, request));
  838. }
  839. }
  840. //仓库id
  841. // if (depotId == null) {
  842. // JSONArray depotArr = depotService.findDepotByCurrentUser();
  843. // for (Object obj : depotArr) {
  844. // JSONObject depotObj = JSONObject.parseObject(obj.toString());
  845. // if (depotObj.get("isDefault") != null) {
  846. // Boolean isDefault = depotObj.getBoolean("isDefault");
  847. // if (isDefault) {
  848. // Long id = depotObj.getLong("id");
  849. // if (!"CGDD".equals(prefixNo) && !"XSDD".equals(prefixNo)) {
  850. // //除订单之外的单据才有仓库
  851. // mvo.setDepotId(id);
  852. // }
  853. // getStockByMaterialInfo(mvo);
  854. // }
  855. // }
  856. // }
  857. // } else {
  858. // mvo.setDepotId(depotId);
  859. // getStockByMaterialInfo(mvo);
  860. // }
  861. }
  862. }
  863. res.code = 200;
  864. res.data = list;
  865. } catch(Exception e){
  866. logger.error(e.getMessage(), e);
  867. res.code = 500;
  868. res.data = "获取数据失败";
  869. }
  870. return res;
  871. }
  872. @GetMapping(value = "/getMaterialById")
  873. @ApiOperation(value = "根据商品id查询商品及子表信息")
  874. public BaseResponseInfo getMaterialById(@RequestParam("mid") Long materialId,
  875. HttpServletRequest request)throws Exception {
  876. BaseResponseInfo res = new BaseResponseInfo();
  877. res.code = 200;
  878. res.data = materialService.getMaterialById(materialId);
  879. return res;
  880. }
  881. @GetMapping(value = "/getMaterialWarn")
  882. @ApiOperation(value = "获取商品提醒")
  883. public BaseResponseInfo getMaterialWarn()throws Exception {
  884. BaseResponseInfo res = new BaseResponseInfo();
  885. res.code = 200;
  886. res.data = materialService.getMaterialWarn();
  887. return res;
  888. }
  889. /**
  890. * 商品信息全选获取批次号
  891. */
  892. @GetMapping(value = "/findBatchNumbersBySelect")
  893. @ApiOperation(value = "商品选择全选获取商品批次号")
  894. public BaseResponseInfo findBatchNumberSBySelect(@RequestParam(value = "categoryId", required = false) Long categoryId,
  895. @RequestParam(value = "q", required = false) String q,
  896. @RequestParam(value = "standardOrModel", required = false) String standardOrModel,
  897. @RequestParam(value = "mpList", required = false) String mpList,
  898. @RequestParam(value = "depotId", required = false) Long depotId,
  899. @RequestParam(value = "color", required = false) String color,
  900. @RequestParam(value = "brand", required = false) String brand,
  901. @RequestParam(value = "mfrs", required = false) String mfrs,
  902. @RequestParam(value = "enableSerialNumber", required = false) String enableSerialNumber,
  903. @RequestParam(value = "enableBatchNumber", required = false) String enableBatchNumber,
  904. HttpServletRequest request){
  905. BaseResponseInfo res = new BaseResponseInfo();
  906. try {
  907. List<MaterialVo4Unit> dataList = materialService.findBySelectWithBarCode(categoryId, q, StringUtil.toNull(standardOrModel),
  908. StringUtil.toNull(color), StringUtil.toNull(brand), StringUtil.toNull(mfrs), enableSerialNumber, enableBatchNumber,
  909. null, null,depotId);
  910. StringBuffer str = new StringBuffer();
  911. //存放数据json数组
  912. if (null != dataList) {
  913. for (MaterialVo4Unit material : dataList) {
  914. str.append("," + material.getBatchNumber());
  915. }
  916. }
  917. res.code = 200;
  918. res.data = str.deleteCharAt(0);
  919. } catch (Exception e) {
  920. logger.error(e.getMessage(), e);
  921. res.code = 500;
  922. }
  923. return res;
  924. }
  925. }