DepotService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package com.jsh.erp.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.jsh.erp.constants.BusinessConstants;
  5. import com.jsh.erp.constants.ExceptionConstants;
  6. import com.jsh.erp.datasource.entities.*;
  7. import com.jsh.erp.datasource.mappers.*;
  8. import com.jsh.erp.exception.BusinessRunTimeException;
  9. import com.jsh.erp.exception.JshException;
  10. import com.jsh.erp.utils.PageUtils;
  11. import com.jsh.erp.utils.StringUtil;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.web.context.request.RequestContextHolder;
  17. import org.springframework.web.context.request.ServletRequestAttributes;
  18. import javax.annotation.Resource;
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.List;
  23. @Service
  24. public class DepotService {
  25. private Logger logger = LoggerFactory.getLogger(DepotService.class);
  26. @Resource
  27. private DepotMapper depotMapper;
  28. @Resource
  29. private DepotMapperEx depotMapperEx;
  30. @Resource
  31. private UserService userService;
  32. @Resource
  33. private SystemConfigService systemConfigService;
  34. @Resource
  35. private UserBusinessService userBusinessService;
  36. @Resource
  37. private LogService logService;
  38. @Resource
  39. private DepotItemMapperEx depotItemMapperEx;
  40. @Resource
  41. private MaterialInitialStockMapperEx materialInitialStockMapperEx;
  42. @Resource
  43. private MaterialCurrentStockMapperEx materialCurrentStockMapperEx;
  44. public Depot getDepot(long id)throws Exception {
  45. Depot result=null;
  46. try{
  47. result=depotMapper.selectByPrimaryKey(id);
  48. }catch(Exception e){
  49. JshException.readFail(logger, e);
  50. }
  51. return result;
  52. }
  53. public List<Depot> getDepotListByIds(String ids)throws Exception {
  54. List<Long> idList = StringUtil.strToLongList(ids);
  55. DepotExample example = new DepotExample();
  56. example.createCriteria().andIdIn(idList);
  57. return depotMapper.selectByExample(example);
  58. }
  59. public List<Depot> getDepot()throws Exception {
  60. DepotExample example = new DepotExample();
  61. example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  62. List<Depot> list=null;
  63. try{
  64. list=depotMapper.selectByExample(example);
  65. }catch(Exception e){
  66. JshException.readFail(logger, e);
  67. }
  68. return list;
  69. }
  70. public List<Depot> getAllList()throws Exception {
  71. DepotExample example = new DepotExample();
  72. example.createCriteria().andEnabledEqualTo(true).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  73. example.setOrderByClause("sort asc, id desc");
  74. List<Depot> list=null;
  75. try{
  76. list=depotMapper.selectByExample(example);
  77. }catch(Exception e){
  78. JshException.readFail(logger, e);
  79. }
  80. return list;
  81. }
  82. public List<DepotEx> select(String name, Integer type, String remark)throws Exception {
  83. List<DepotEx> list=null;
  84. try{
  85. PageUtils.startPage();
  86. list=depotMapperEx.selectByConditionDepot(name, type, remark);
  87. }catch(Exception e){
  88. JshException.readFail(logger, e);
  89. }
  90. return list;
  91. }
  92. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  93. public int insertDepot(JSONObject obj, HttpServletRequest request)throws Exception {
  94. Depot depot = JSONObject.parseObject(obj.toJSONString(), Depot.class);
  95. int result=0;
  96. try{
  97. depot.setType(0);
  98. List<Depot> depotList = getDepot();
  99. if(depotList.size() == 0) {
  100. depot.setIsDefault(true);
  101. } else {
  102. depot.setIsDefault(false);
  103. }
  104. depot.setEnabled(true);
  105. result=depotMapper.insertSelective(depot);
  106. //新增仓库时给当前用户自动授权
  107. Long userId = userService.getUserId(request);
  108. Long depotId = getIdByName(depot.getName());
  109. String ubKey = "[" + depotId + "]";
  110. List<UserBusiness> ubList = userBusinessService.getBasicData(userId.toString(), "UserDepot");
  111. if(ubList ==null || ubList.size() == 0) {
  112. JSONObject ubObj = new JSONObject();
  113. ubObj.put("type", "UserDepot");
  114. ubObj.put("keyId", userId);
  115. ubObj.put("value", ubKey);
  116. userBusinessService.insertUserBusiness(ubObj, request);
  117. } else {
  118. UserBusiness ubInfo = ubList.get(0);
  119. JSONObject ubObj = new JSONObject();
  120. ubObj.put("id", ubInfo.getId());
  121. ubObj.put("type", ubInfo.getType());
  122. ubObj.put("keyId", ubInfo.getKeyId());
  123. ubObj.put("value", ubInfo.getValue() + ubKey);
  124. userBusinessService.updateUserBusiness(ubObj, request);
  125. }
  126. logService.insertLog("仓库",
  127. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(depot.getName()).toString(), request);
  128. }catch(Exception e){
  129. JshException.writeFail(logger, e);
  130. }
  131. return result;
  132. }
  133. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  134. public int updateDepot(JSONObject obj, HttpServletRequest request) throws Exception{
  135. Depot depot = JSONObject.parseObject(obj.toJSONString(), Depot.class);
  136. int result=0;
  137. try{
  138. result= depotMapper.updateByPrimaryKeySelective(depot);
  139. logService.insertLog("仓库",
  140. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(depot.getName()).toString(), request);
  141. }catch(Exception e){
  142. JshException.writeFail(logger, e);
  143. }
  144. return result;
  145. }
  146. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  147. public int deleteDepot(Long id, HttpServletRequest request)throws Exception {
  148. return batchDeleteDepotByIds(id.toString());
  149. }
  150. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  151. public int batchDeleteDepot(String ids, HttpServletRequest request) throws Exception{
  152. return batchDeleteDepotByIds(ids);
  153. }
  154. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  155. public int batchDeleteDepotByIds(String ids)throws Exception {
  156. int result=0;
  157. String [] idArray=ids.split(",");
  158. //校验单据子表 jsh_depot_item
  159. List<DepotItem> depotItemList = depotItemMapperEx.getDepotItemListListByDepotIds(idArray);
  160. if(depotItemList!=null&&depotItemList.size()>0){
  161. logger.error("异常码[{}],异常提示[{}],参数,DepotIds[{}]",
  162. ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,ExceptionConstants.DELETE_FORCE_CONFIRM_MSG,ids);
  163. throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
  164. ExceptionConstants.DELETE_FORCE_CONFIRM_MSG);
  165. }
  166. try{
  167. //记录日志
  168. StringBuffer sb = new StringBuffer();
  169. sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
  170. List<Depot> list = getDepotListByIds(ids);
  171. for(Depot depot: list){
  172. sb.append("[").append(depot.getName()).append("]");
  173. }
  174. User userInfo=userService.getCurrentUser();
  175. //校验通过执行删除操作
  176. //删除仓库关联的商品的初始库存
  177. materialInitialStockMapperEx.batchDeleteByDepots(idArray);
  178. //删除仓库关联的商品的当前库存
  179. materialCurrentStockMapperEx.batchDeleteByDepots(idArray);
  180. //删除仓库
  181. result = depotMapperEx.batchDeleteDepotByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
  182. //记录日志
  183. logService.insertLog("仓库", sb.toString(),
  184. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  185. } catch (Exception e) {
  186. JshException.writeFail(logger, e);
  187. }
  188. return result;
  189. }
  190. public int checkIsNameExist(Long id, String name)throws Exception {
  191. DepotExample example = new DepotExample();
  192. example.createCriteria().andIdNotEqualTo(id).andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  193. List<Depot> list=null;
  194. try{
  195. list= depotMapper.selectByExample(example);
  196. }catch(Exception e){
  197. JshException.readFail(logger, e);
  198. }
  199. return list==null?0:list.size();
  200. }
  201. public List<Depot> findUserDepot()throws Exception{
  202. DepotExample example = new DepotExample();
  203. example.createCriteria().andTypeEqualTo(0).andEnabledEqualTo(true)
  204. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  205. example.setOrderByClause("sort asc, id desc");
  206. List<Depot> list=null;
  207. try{
  208. list= depotMapper.selectByExample(example);
  209. }catch(Exception e){
  210. JshException.readFail(logger, e);
  211. }
  212. return list;
  213. }
  214. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  215. public int updateIsDefault(Long depotId) throws Exception{
  216. int result=0;
  217. try{
  218. //全部取消默认
  219. Depot allDepot = new Depot();
  220. allDepot.setIsDefault(false);
  221. DepotExample allExample = new DepotExample();
  222. allExample.createCriteria();
  223. depotMapper.updateByExampleSelective(allDepot, allExample);
  224. //给指定仓库设为默认
  225. Depot depot = new Depot();
  226. depot.setIsDefault(true);
  227. DepotExample example = new DepotExample();
  228. example.createCriteria().andIdEqualTo(depotId);
  229. depotMapper.updateByExampleSelective(depot, example);
  230. logService.insertLog("仓库",BusinessConstants.LOG_OPERATION_TYPE_EDIT+depotId,
  231. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  232. result = 1;
  233. }catch(Exception e){
  234. JshException.writeFail(logger, e);
  235. }
  236. return result;
  237. }
  238. /**
  239. * 根据名称获取id
  240. * @param name
  241. */
  242. public Long getIdByName(String name){
  243. Long id = 0L;
  244. DepotExample example = new DepotExample();
  245. example.createCriteria().andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  246. List<Depot> list = depotMapper.selectByExample(example);
  247. if(list!=null && list.size()>0) {
  248. id = list.get(0).getId();
  249. }
  250. return id;
  251. }
  252. /**
  253. * 解析仓库列表
  254. * @param depotId 仓库id
  255. * @return 仓库id为空,返回当前用户有权限的仓库,不为空返回仓库id的集合
  256. */
  257. public List<Long> parseDepotList(Long depotId) throws Exception {
  258. List<Long> depotList = new ArrayList<>();
  259. if(depotId !=null) {
  260. depotList.add(depotId);
  261. } else {
  262. //未选择仓库时默认为当前用户有权限的仓库
  263. JSONArray depotArr = findDepotByCurrentUser();
  264. for(Object obj: depotArr) {
  265. JSONObject object = JSONObject.parseObject(obj.toString());
  266. depotList.add(object.getLong("id"));
  267. }
  268. }
  269. return depotList;
  270. }
  271. public JSONArray findDepotByCurrentUser() throws Exception {
  272. JSONArray arr = new JSONArray();
  273. String type = "UserDepot";
  274. Long userId = userService.getCurrentUser().getId();
  275. List<Depot> dataList = findUserDepot();
  276. //开始拼接json数据
  277. if (null != dataList) {
  278. boolean depotFlag = systemConfigService.getDepotFlag();
  279. if(depotFlag) {
  280. List<UserBusiness> list = userBusinessService.getBasicData(userId.toString(), type);
  281. if(list!=null && list.size()>0) {
  282. String depotStr = list.get(0).getValue();
  283. if(StringUtil.isNotEmpty(depotStr)){
  284. depotStr = depotStr.replaceAll("\\[", "").replaceAll("]", ",");
  285. String[] depotArr = depotStr.split(",");
  286. for (Depot depot : dataList) {
  287. for(String depotId: depotArr) {
  288. if(depot.getId() == Long.parseLong(depotId)){
  289. JSONObject item = new JSONObject();
  290. item.put("id", depot.getId());
  291. item.put("depotName", depot.getName());
  292. item.put("isDefault", depot.getIsDefault());
  293. arr.add(item);
  294. }
  295. }
  296. }
  297. }
  298. }
  299. } else {
  300. for (Depot depot : dataList) {
  301. JSONObject item = new JSONObject();
  302. item.put("id", depot.getId());
  303. item.put("depotName", depot.getName());
  304. item.put("isDefault", depot.getIsDefault());
  305. arr.add(item);
  306. }
  307. }
  308. }
  309. return arr;
  310. }
  311. /**
  312. * 当前用户有权限使用的仓库列表的id,用逗号隔开
  313. * @return
  314. * @throws Exception
  315. */
  316. public String findDepotStrByCurrentUser() throws Exception {
  317. JSONArray arr = findDepotByCurrentUser();
  318. StringBuffer sb = new StringBuffer();
  319. for(Object object: arr) {
  320. JSONObject obj = (JSONObject)object;
  321. sb.append(obj.getLong("id")).append(",");
  322. }
  323. String depotStr = sb.toString();
  324. if(StringUtil.isNotEmpty(depotStr)){
  325. depotStr = depotStr.substring(0, depotStr.length()-1);
  326. }
  327. return depotStr;
  328. }
  329. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  330. public int batchSetStatus(Boolean status, String ids)throws Exception {
  331. logService.insertLog("仓库",
  332. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ENABLED).toString(),
  333. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  334. List<Long> depotIds = StringUtil.strToLongList(ids);
  335. Depot depot = new Depot();
  336. depot.setEnabled(status);
  337. DepotExample example = new DepotExample();
  338. example.createCriteria().andIdIn(depotIds);
  339. int result=0;
  340. try{
  341. result = depotMapper.updateByExampleSelective(depot, example);
  342. }catch(Exception e){
  343. JshException.writeFail(logger, e);
  344. }
  345. return result;
  346. }
  347. }