MaterialController.java 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  335. // String date = material.getProductionDate() == null ? null : sdf.format(material.getProductionDate());
  336. item.put("productionDate",material.getProductionDate());
  337. item.put("expiryNum",material.getExpiryNum());
  338. item.put("batchNumber",material.getBatchNumber());
  339. item.put("position",material.getPosition());
  340. item.put("supplierId",material.getSupplierId());
  341. item.put("supplierName",material.getSupplierName());
  342. item.put("depotId",material.getDepotId());
  343. item.put("depotName",material.getDepotName());
  344. item.put("unitId",material.getUnitId());
  345. BigDecimal stock;
  346. if(StringUtil.isNotEmpty(material.getSku())){
  347. stock = depotItemService.getSkuStockByParam(depotId,material.getMeId(),null,null);
  348. } else {
  349. stock = depotItemService.getCurrentStockByParam(depotId, material.getId());
  350. if (material.getUnitId()!=null){
  351. String commodityUnit = material.getCommodityUnit();
  352. stock = unitService.parseStockByUnit(stock, unit, commodityUnit);
  353. }
  354. }
  355. item.put("stock", stock);
  356. item.put("expand", materialService.getMaterialOtherByParam(mpArr, material));
  357. item.put("imgName", material.getImgName());
  358. if(fileUploadType == 2) {
  359. item.put("imgSmall", "small");
  360. item.put("imgLarge", "large");
  361. } else {
  362. item.put("imgSmall", "");
  363. item.put("imgLarge", "");
  364. }
  365. dataArray.add(item);
  366. }
  367. }
  368. object.put("rows", dataArray);
  369. } catch (Exception e) {
  370. logger.error(e.getMessage(), e);
  371. }
  372. return object;
  373. }
  374. /**
  375. * 根据商品id查找商品信息
  376. * @param meId
  377. * @param request
  378. * @return
  379. * @throws Exception
  380. */
  381. @GetMapping(value = "/getMaterialByMeId")
  382. @ApiOperation(value = "根据商品id查找商品信息")
  383. public JSONObject getMaterialByMeId(@RequestParam(value = "meId", required = false) Long meId,
  384. @RequestParam("mpList") String mpList,
  385. HttpServletRequest request) throws Exception{
  386. JSONObject item = new JSONObject();
  387. try {
  388. String[] mpArr = mpList.split(",");
  389. List<MaterialVo4Unit> materialList = materialService.getMaterialByMeId(meId);
  390. if(materialList!=null && materialList.size()!=1) {
  391. return item;
  392. } else if(materialList.size() == 1) {
  393. MaterialVo4Unit material = materialList.get(0);
  394. item.put("Id", material.getMeId()); //商品扩展表的id
  395. String ratio; //比例
  396. if (material.getUnitId() == null || material.getUnitId().equals("")) {
  397. ratio = "";
  398. } else {
  399. ratio = material.getUnitName();
  400. ratio = ratio.substring(ratio.indexOf("("));
  401. }
  402. //名称/型号/扩展信息/包装
  403. String MaterialName = "";
  404. MaterialName = MaterialName + material.getmBarCode() + "_" + material.getName()
  405. + ((material.getStandard() == null || material.getStandard().equals("")) ? "" : "(" + material.getStandard() + ")");
  406. String expand = materialService.getMaterialOtherByParam(mpArr, material); //扩展信息
  407. MaterialName = MaterialName + expand + ((material.getUnit() == null || material.getUnit().equals("")) ? "" : "(" + material.getUnit() + ")") + ratio;
  408. item.put("MaterialName", MaterialName);
  409. item.put("name", material.getName());
  410. item.put("expand", expand);
  411. item.put("model", material.getModel());
  412. item.put("standard", material.getStandard());
  413. item.put("unit", material.getUnit() + ratio);
  414. }
  415. } catch (Exception e) {
  416. logger.error(e.getMessage(), e);
  417. }
  418. return item;
  419. }
  420. /**
  421. * 生成excel表格
  422. * @param categoryId
  423. * @param materialParam
  424. * @param color
  425. * @param weight
  426. * @param expiryNum
  427. * @param enabled
  428. * @param enableSerialNumber
  429. * @param enableBatchNumber
  430. * @param remark
  431. * @param mpList
  432. * @param request
  433. * @param response
  434. */
  435. @GetMapping(value = "/exportExcel")
  436. @ApiOperation(value = "生成excel表格")
  437. public void exportExcel(@RequestParam(value = "categoryId", required = false) String categoryId,
  438. @RequestParam(value = "materialParam", required = false) String materialParam,
  439. @RequestParam(value = "color", required = false) String color,
  440. @RequestParam(value = "materialOther", required = false) String materialOther,
  441. @RequestParam(value = "weight", required = false) String weight,
  442. @RequestParam(value = "expiryNum", required = false) String expiryNum,
  443. @RequestParam(value = "enabled", required = false) String enabled,
  444. @RequestParam(value = "enableSerialNumber", required = false) String enableSerialNumber,
  445. @RequestParam(value = "enableBatchNumber", required = false) String enableBatchNumber,
  446. @RequestParam(value = "remark", required = false) String remark,
  447. @RequestParam(value = "mpList", required = false) String mpList,
  448. HttpServletRequest request, HttpServletResponse response) {
  449. try {
  450. materialService.exportExcel(StringUtil.toNull(categoryId), StringUtil.toNull(materialParam), StringUtil.toNull(color),
  451. StringUtil.toNull(materialOther), StringUtil.toNull(weight),
  452. StringUtil.toNull(expiryNum), StringUtil.toNull(enabled), StringUtil.toNull(enableSerialNumber),
  453. StringUtil.toNull(enableBatchNumber), StringUtil.toNull(remark), response);
  454. } catch (Exception e) {
  455. logger.error(e.getMessage(), e);
  456. }
  457. }
  458. /**
  459. * excel表格导入产品(含初始库存)
  460. * @param file
  461. * @param request
  462. * @param response
  463. * @return
  464. */
  465. @PostMapping(value = "/importExcel")
  466. @ApiOperation(value = "excel表格导入产品")
  467. public BaseResponseInfo importExcel(MultipartFile file,
  468. HttpServletRequest request, HttpServletResponse response) throws Exception{
  469. BaseResponseInfo res = new BaseResponseInfo();
  470. try {
  471. res = materialService.importExcel(file, request);
  472. } catch (Exception e) {
  473. logger.error(e.getMessage(), e);
  474. }
  475. return res;
  476. }
  477. /**
  478. * 获取商品序列号
  479. * @param q
  480. * @param currentPage
  481. * @param pageSize
  482. * @param request
  483. * @param response
  484. * @return
  485. * @throws Exception
  486. */
  487. @GetMapping(value = "/getMaterialEnableSerialNumberList")
  488. @ApiOperation(value = "获取商品序列号")
  489. public JSONObject getMaterialEnableSerialNumberList(
  490. @RequestParam(value = "q", required = false) String q,
  491. @RequestParam("page") Integer currentPage,
  492. @RequestParam("rows") Integer pageSize,
  493. HttpServletRequest request,
  494. HttpServletResponse response)throws Exception {
  495. JSONObject object= new JSONObject();
  496. try {
  497. List<MaterialVo4Unit> list = materialService.getMaterialEnableSerialNumberList(q, (currentPage-1)*pageSize, pageSize);
  498. Long count = materialService.getMaterialEnableSerialNumberCount(q);
  499. object.put("rows", list);
  500. object.put("total", count);
  501. } catch (Exception e) {
  502. logger.error(e.getMessage(), e);
  503. }
  504. return object;
  505. }
  506. /**
  507. * 获取最大条码
  508. * @return
  509. * @throws Exception
  510. */
  511. @GetMapping(value = "/getMaxBarCode")
  512. @ApiOperation(value = "获取最大条码")
  513. public BaseResponseInfo getMaxBarCode() throws Exception {
  514. BaseResponseInfo res = new BaseResponseInfo();
  515. Map<String, Object> map = new HashMap<String, Object>();
  516. String barCode = materialService.getMaxBarCode();
  517. map.put("barCode", barCode);
  518. res.code = 200;
  519. res.data = map;
  520. return res;
  521. }
  522. /**
  523. * 商品名称模糊匹配
  524. * @return
  525. * @throws Exception
  526. */
  527. @GetMapping(value = "/getMaterialNameList")
  528. @ApiOperation(value = "商品名称模糊匹配")
  529. public JSONArray getMaterialNameList() throws Exception {
  530. JSONArray arr = new JSONArray();
  531. try {
  532. List<String> list = materialService.getMaterialNameList();
  533. for (String s : list) {
  534. JSONObject item = new JSONObject();
  535. item.put("value", s);
  536. item.put("text", s);
  537. arr.add(item);
  538. }
  539. } catch (Exception e) {
  540. logger.error(e.getMessage(), e);
  541. }
  542. return arr;
  543. }
  544. /**
  545. * 根据条码查询商品信息
  546. * @return
  547. * @throws Exception
  548. */
  549. @GetMapping(value = "/getMaterialByBarCode")
  550. @ApiOperation(value = "根据条码查询商品信息")
  551. public BaseResponseInfo getMaterialByBarCode(@RequestParam("barCode") String barCode,
  552. @RequestParam(value = "organId", required = false) Long organId,
  553. @RequestParam(value = "depotId", required = false) Long depotId,
  554. @RequestParam("mpList") String mpList,
  555. @RequestParam(required = false, value = "prefixNo") String prefixNo,
  556. HttpServletRequest request) throws Exception {
  557. BaseResponseInfo res = new BaseResponseInfo();
  558. try {
  559. Long userId = userService.getUserId(request);
  560. String priceLimit = userService.getRoleTypeByUserId(userId).getPriceLimit();
  561. String[] mpArr = mpList.split(",");
  562. //支持序列号查询,先根据序列号查询条码,如果查不到就直接查条码
  563. MaterialExtend materialExtend = materialService.getMaterialExtendBySerialNumber(barCode);
  564. if(materialExtend!=null && StringUtil.isNotEmpty(materialExtend.getBarCode())) {
  565. barCode = materialExtend.getBarCode();
  566. }
  567. List<MaterialVo4Unit> list = materialService.getMaterialByBarCode(barCode);
  568. if(list!=null && list.size()>0) {
  569. for(MaterialVo4Unit mvo: list) {
  570. mvo.setMaterialOther(materialService.getMaterialOtherByParam(mpArr, mvo));
  571. if ("LSCK".equals(prefixNo) || "LSTH".equals(prefixNo)) {
  572. //零售价
  573. mvo.setBillPrice(mvo.getCommodityDecimal());
  574. } else if ("CGDD".equals(prefixNo) || "CGRK".equals(prefixNo) || "CGTH".equals(prefixNo)) {
  575. //采购价
  576. mvo.setBillPrice(mvo.getPurchaseDecimal());
  577. } else if("QTRK".equals(prefixNo) || "DBCK".equals(prefixNo) || "ZZD".equals(prefixNo) || "CXD".equals(prefixNo)
  578. || "PDLR".equals(prefixNo) || "PDFP".equals(prefixNo)) {
  579. //采购价-给录入界面按权限屏蔽
  580. mvo.setBillPrice(roleService.parseBillPriceByLimit(mvo.getPurchaseDecimal(), "buy", priceLimit, request));
  581. } if ("XSDD".equals(prefixNo) || "XSCK".equals(prefixNo) || "XSTH".equals(prefixNo) || "QTCK".equals(prefixNo)) {
  582. //销售价
  583. if(organId == null) {
  584. mvo.setBillPrice(mvo.getWholesaleDecimal());
  585. } else {
  586. //查询最后一单的销售价,实现不同的客户不同的销售价
  587. BigDecimal lastUnitPrice = depotItemService.getLastUnitPriceByParam(organId, mvo.getMeId(), prefixNo);
  588. mvo.setBillPrice(lastUnitPrice!=null? lastUnitPrice : mvo.getWholesaleDecimal());
  589. }
  590. //销售价-给录入界面按权限屏蔽价格
  591. if("QTCK".equals(prefixNo)) {
  592. mvo.setBillPrice(roleService.parseBillPriceByLimit(mvo.getWholesaleDecimal(), "sale", priceLimit, request));
  593. }
  594. }
  595. //仓库id
  596. if (depotId == null) {
  597. JSONArray depotArr = depotService.findDepotByCurrentUser();
  598. for (Object obj : depotArr) {
  599. JSONObject depotObj = JSONObject.parseObject(obj.toString());
  600. if (depotObj.get("isDefault") != null) {
  601. Boolean isDefault = depotObj.getBoolean("isDefault");
  602. if (isDefault) {
  603. Long id = depotObj.getLong("id");
  604. if (!"CGDD".equals(prefixNo) && !"XSDD".equals(prefixNo)) {
  605. //除订单之外的单据才有仓库
  606. mvo.setDepotId(id);
  607. }
  608. getStockByMaterialInfo(mvo);
  609. }
  610. }
  611. }
  612. } else {
  613. mvo.setDepotId(depotId);
  614. getStockByMaterialInfo(mvo);
  615. }
  616. }
  617. }
  618. res.code = 200;
  619. res.data = list;
  620. } catch(Exception e){
  621. logger.error(e.getMessage(), e);
  622. res.code = 500;
  623. res.data = "获取数据失败";
  624. }
  625. return res;
  626. }
  627. /**
  628. * 根据商品信息获取库存,进行赋值
  629. * @param mvo
  630. * @throws Exception
  631. */
  632. private void getStockByMaterialInfo(MaterialVo4Unit mvo) throws Exception {
  633. BigDecimal stock;
  634. if (StringUtil.isNotEmpty(mvo.getSku())) {
  635. stock = depotItemService.getSkuStockByParam(mvo.getDepotId(), mvo.getMeId(), null, null);
  636. } else {
  637. stock = depotItemService.getCurrentStockByParam(mvo.getDepotId(), mvo.getId());
  638. if (mvo.getUnitId() != null) {
  639. Unit unit = unitService.getUnit(mvo.getUnitId());
  640. String commodityUnit = mvo.getCommodityUnit();
  641. stock = unitService.parseStockByUnit(stock, unit, commodityUnit);
  642. }
  643. }
  644. mvo.setStock(stock);
  645. }
  646. /**
  647. * 商品库存查询
  648. * @param currentPage
  649. * @param pageSize
  650. * @param depotIds
  651. * @param categoryId
  652. * @param materialParam
  653. * @param zeroStock
  654. * @param column
  655. * @param order
  656. * @param request
  657. * @return
  658. * @throws Exception
  659. */
  660. @GetMapping(value = "/getListWithStock")
  661. @ApiOperation(value = "商品库存查询")
  662. public BaseResponseInfo getListWithStock(@RequestParam("currentPage") Integer currentPage,
  663. @RequestParam("pageSize") Integer pageSize,
  664. @RequestParam(value = "depotIds", required = false) String depotIds,
  665. @RequestParam(value = "categoryId", required = false) Long categoryId,
  666. @RequestParam(value = "position", required = false) String position,
  667. @RequestParam("materialParam") String materialParam,
  668. @RequestParam("zeroStock") Integer zeroStock,
  669. @RequestParam(value = "column", required = false, defaultValue = "createTime") String column,
  670. @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
  671. HttpServletRequest request)throws Exception {
  672. BaseResponseInfo res = new BaseResponseInfo();
  673. Map<String, Object> map = new HashMap<>();
  674. try {
  675. List<Long> idList = new ArrayList<>();
  676. List<Long> depotList = new ArrayList<>();
  677. if(categoryId != null){
  678. idList = materialService.getListByParentId(categoryId);
  679. }
  680. if(StringUtil.isNotEmpty(depotIds)) {
  681. depotList = StringUtil.strToLongList(depotIds);
  682. } else {
  683. //未选择仓库时默认为当前用户有权限的仓库
  684. JSONArray depotArr = depotService.findDepotByCurrentUser();
  685. for(Object obj: depotArr) {
  686. JSONObject object = JSONObject.parseObject(obj.toString());
  687. depotList.add(object.getLong("id"));
  688. }
  689. }
  690. Boolean moveAvgPriceFlag = systemConfigService.getMoveAvgPriceFlag();
  691. List<MaterialVo4Unit> dataList = materialService.getListWithStock(depotList, idList, StringUtil.toNull(position), StringUtil.toNull(materialParam),
  692. moveAvgPriceFlag, zeroStock, StringUtil.safeSqlParse(column), StringUtil.safeSqlParse(order), (currentPage-1)*pageSize, pageSize);
  693. int total = materialService.getListWithStockCount(depotList, idList, StringUtil.toNull(position), StringUtil.toNull(materialParam), zeroStock);
  694. MaterialVo4Unit materialVo4Unit= materialService.getTotalStockAndPrice(depotList, idList, StringUtil.toNull(position), StringUtil.toNull(materialParam));
  695. map.put("total", total);
  696. map.put("currentStock", materialVo4Unit.getCurrentStock()!=null?materialVo4Unit.getCurrentStock():BigDecimal.ZERO);
  697. if(moveAvgPriceFlag) {
  698. map.put("currentStockPrice", materialVo4Unit.getCurrentStockMovePrice()!=null?materialVo4Unit.getCurrentStockMovePrice():BigDecimal.ZERO);
  699. } else {
  700. map.put("currentStockPrice", materialVo4Unit.getCurrentStockPrice()!=null?materialVo4Unit.getCurrentStockPrice():BigDecimal.ZERO);
  701. }
  702. map.put("currentWeight", materialVo4Unit.getCurrentWeight()!=null?materialVo4Unit.getCurrentWeight():BigDecimal.ZERO);
  703. map.put("rows", dataList);
  704. res.code = 200;
  705. res.data = map;
  706. } catch(Exception e){
  707. logger.error(e.getMessage(), e);
  708. res.code = 500;
  709. res.data = "获取数据失败";
  710. }
  711. return res;
  712. }
  713. /**
  714. * 批量设置商品当前的实时库存(按每个仓库)
  715. * @param jsonObject
  716. * @param request
  717. * @return
  718. * @throws Exception
  719. */
  720. @PostMapping(value = "/batchSetMaterialCurrentStock")
  721. @ApiOperation(value = "批量设置商品当前的实时库存(按每个仓库)")
  722. public String batchSetMaterialCurrentStock(@RequestBody JSONObject jsonObject,
  723. HttpServletRequest request)throws Exception {
  724. String ids = jsonObject.getString("ids");
  725. Map<String, Object> objectMap = new HashMap<>();
  726. int res = materialService.batchSetMaterialCurrentStock(ids);
  727. if(res > 0) {
  728. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  729. } else {
  730. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  731. }
  732. }
  733. /**
  734. * 批量设置商品当前的成本价
  735. * @param jsonObject
  736. * @param request
  737. * @return
  738. * @throws Exception
  739. */
  740. @PostMapping(value = "/batchSetMaterialCurrentUnitPrice")
  741. @ApiOperation(value = "批量设置商品当前的成本价")
  742. public String batchSetMaterialCurrentUnitPrice(@RequestBody JSONObject jsonObject,
  743. HttpServletRequest request)throws Exception {
  744. String ids = jsonObject.getString("ids");
  745. Map<String, Object> objectMap = new HashMap<>();
  746. int res = materialService.batchSetMaterialCurrentUnitPrice(ids);
  747. if(res > 0) {
  748. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  749. } else {
  750. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  751. }
  752. }
  753. /**
  754. * 批量更新商品信息
  755. * @param jsonObject
  756. * @param request
  757. * @return
  758. * @throws Exception
  759. */
  760. @PostMapping(value = "/batchUpdate")
  761. @ApiOperation(value = "批量更新商品信息")
  762. public String batchUpdate(@RequestBody JSONObject jsonObject,
  763. HttpServletRequest request)throws Exception {
  764. Map<String, Object> objectMap = new HashMap<>();
  765. int res = materialService.batchUpdate(jsonObject);
  766. if(res > 0) {
  767. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  768. } else {
  769. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  770. }
  771. }
  772. /**
  773. * 转换名称为拼音
  774. * @param jsonObject
  775. */
  776. @PostMapping(value = "/changeNameToPinYin")
  777. @ApiOperation(value = "转换名称为拼音")
  778. public BaseResponseInfo changeNameToPinYin(@RequestBody JSONObject jsonObject)throws Exception {
  779. BaseResponseInfo res = new BaseResponseInfo();
  780. try {
  781. String name = jsonObject.getString("name");
  782. res.code = 200;
  783. res.data = PinYinUtil.getFirstLettersLo(name);
  784. } catch(Exception e){
  785. logger.error(e.getMessage(), e);
  786. res.code = 500;
  787. res.data = "获取数据失败";
  788. }
  789. return res;
  790. }
  791. /**
  792. * 根据批次号查询商品信息
  793. * @return
  794. * @throws Exception
  795. */
  796. @GetMapping(value = "/getMaterialByBatchNumber")
  797. @ApiOperation(value = "根据批次号查询商品信息")
  798. public BaseResponseInfo getMaterialByBatchNumber(@RequestParam("batchNumber") String batchNumber,
  799. @RequestParam(value = "organId", required = false) Long organId,
  800. @RequestParam(value = "depotId", required = false) Long depotId,
  801. @RequestParam("mpList") String mpList,
  802. @RequestParam(required = false, value = "prefixNo") String prefixNo,
  803. HttpServletRequest request) throws Exception {
  804. BaseResponseInfo res = new BaseResponseInfo();
  805. try {
  806. Long userId = userService.getUserId(request);
  807. String priceLimit = userService.getRoleTypeByUserId(userId).getPriceLimit();
  808. String[] mpArr = mpList.split(",");
  809. //支持序列号查询,先根据序列号查询条码,如果查不到就直接查条码
  810. // MaterialExtend materialExtend = materialService.getMaterialExtendBySerialNumber(barCode);
  811. // if(materialExtend!=null && StringUtil.isNotEmpty(materialExtend.getBarCode())) {
  812. // barCode = materialExtend.getBarCode();
  813. // }
  814. List<MaterialVo4Unit> list = materialService.getMaterialByBatchNumber(batchNumber);
  815. if(list!=null && list.size()>0) {
  816. for(MaterialVo4Unit mvo: list) {
  817. mvo.setMaterialOther(materialService.getMaterialOtherByParam(mpArr, mvo));
  818. if ("LSCK".equals(prefixNo) || "LSTH".equals(prefixNo)) {
  819. //零售价
  820. mvo.setBillPrice(mvo.getCommodityDecimal());
  821. } else if ("CGDD".equals(prefixNo) || "CGRK".equals(prefixNo) || "CGTH".equals(prefixNo)) {
  822. //采购价
  823. mvo.setBillPrice(mvo.getPurchaseDecimal());
  824. } else if("QTRK".equals(prefixNo) || "DBCK".equals(prefixNo) || "ZZD".equals(prefixNo) || "CXD".equals(prefixNo)
  825. || "PDLR".equals(prefixNo) || "PDFP".equals(prefixNo)) {
  826. //采购价-给录入界面按权限屏蔽
  827. mvo.setBillPrice(roleService.parseBillPriceByLimit(mvo.getPurchaseDecimal(), "buy", priceLimit, request));
  828. } if ("XSDD".equals(prefixNo) || "XSCK".equals(prefixNo) || "XSTH".equals(prefixNo) || "QTCK".equals(prefixNo)) {
  829. //销售价
  830. if(organId == null) {
  831. mvo.setBillPrice(mvo.getWholesaleDecimal());
  832. } else {
  833. //查询最后一单的销售价,实现不同的客户不同的销售价
  834. BigDecimal lastUnitPrice = depotItemService.getLastUnitPriceByParam(organId, mvo.getMeId(), prefixNo);
  835. mvo.setBillPrice(lastUnitPrice!=null? lastUnitPrice : mvo.getWholesaleDecimal());
  836. }
  837. //销售价-给录入界面按权限屏蔽价格
  838. if("QTCK".equals(prefixNo)) {
  839. mvo.setBillPrice(roleService.parseBillPriceByLimit(mvo.getWholesaleDecimal(), "sale", priceLimit, request));
  840. }
  841. }
  842. //仓库id
  843. // if (depotId == null) {
  844. // JSONArray depotArr = depotService.findDepotByCurrentUser();
  845. // for (Object obj : depotArr) {
  846. // JSONObject depotObj = JSONObject.parseObject(obj.toString());
  847. // if (depotObj.get("isDefault") != null) {
  848. // Boolean isDefault = depotObj.getBoolean("isDefault");
  849. // if (isDefault) {
  850. // Long id = depotObj.getLong("id");
  851. // if (!"CGDD".equals(prefixNo) && !"XSDD".equals(prefixNo)) {
  852. // //除订单之外的单据才有仓库
  853. // mvo.setDepotId(id);
  854. // }
  855. // getStockByMaterialInfo(mvo);
  856. // }
  857. // }
  858. // }
  859. // } else {
  860. // mvo.setDepotId(depotId);
  861. // getStockByMaterialInfo(mvo);
  862. // }
  863. }
  864. }
  865. res.code = 200;
  866. res.data = list;
  867. } catch(Exception e){
  868. logger.error(e.getMessage(), e);
  869. res.code = 500;
  870. res.data = "获取数据失败";
  871. }
  872. return res;
  873. }
  874. @GetMapping(value = "/getMaterialById")
  875. @ApiOperation(value = "根据商品id查询商品及子表信息")
  876. public BaseResponseInfo getMaterialById(@RequestParam("mid") Long materialId,
  877. HttpServletRequest request)throws Exception {
  878. BaseResponseInfo res = new BaseResponseInfo();
  879. res.code = 200;
  880. res.data = materialService.getMaterialById(materialId);
  881. return res;
  882. }
  883. }