MaterialController.java 37 KB

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