MaterialAttributeService.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.datasource.entities.MaterialAttribute;
  6. import com.jsh.erp.datasource.entities.MaterialAttributeExample;
  7. import com.jsh.erp.datasource.mappers.MaterialAttributeMapper;
  8. import com.jsh.erp.datasource.mappers.MaterialAttributeMapperEx;
  9. import com.jsh.erp.exception.BusinessRunTimeException;
  10. import com.jsh.erp.exception.JshException;
  11. import com.jsh.erp.utils.PageUtils;
  12. import com.jsh.erp.utils.StringUtil;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import javax.annotation.Resource;
  18. import javax.servlet.http.HttpServletRequest;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. @Service
  22. public class MaterialAttributeService {
  23. private Logger logger = LoggerFactory.getLogger(MaterialAttributeService.class);
  24. @Resource
  25. private LogService logService;
  26. @Resource
  27. private MaterialAttributeMapper materialAttributeMapper;
  28. @Resource
  29. private MaterialAttributeMapperEx materialAttributeMapperEx;
  30. public MaterialAttribute getMaterialAttribute(long id)throws Exception {
  31. MaterialAttribute result=null;
  32. try{
  33. result=materialAttributeMapper.selectByPrimaryKey(id);
  34. }catch(Exception e){
  35. JshException.readFail(logger, e);
  36. }
  37. return result;
  38. }
  39. public List<MaterialAttribute> getMaterialAttribute() throws Exception{
  40. MaterialAttributeExample example = new MaterialAttributeExample();
  41. example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  42. example.setOrderByClause("id desc");
  43. List<MaterialAttribute> list=null;
  44. try{
  45. list=materialAttributeMapper.selectByExample(example);
  46. }catch(Exception e){
  47. JshException.readFail(logger, e);
  48. }
  49. return list;
  50. }
  51. public List<MaterialAttribute> select(String attributeName) throws Exception{
  52. List<MaterialAttribute> list = new ArrayList<>();
  53. try{
  54. PageUtils.startPage();
  55. list = materialAttributeMapperEx.selectByConditionMaterialAttribute(attributeName);
  56. }catch(Exception e){
  57. JshException.readFail(logger, e);
  58. }
  59. return list;
  60. }
  61. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  62. public int insertMaterialAttribute(JSONObject obj, HttpServletRequest request)throws Exception {
  63. MaterialAttribute m = JSONObject.parseObject(obj.toJSONString(), MaterialAttribute.class);
  64. try{
  65. materialAttributeMapper.insertSelective(m);
  66. logService.insertLog("商品属性",
  67. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(m.getAttributeName()).toString(), request);
  68. return 1;
  69. }
  70. catch (BusinessRunTimeException ex) {
  71. throw new BusinessRunTimeException(ex.getCode(), ex.getMessage());
  72. }
  73. catch(Exception e){
  74. JshException.writeFail(logger, e);
  75. return 0;
  76. }
  77. }
  78. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  79. public int updateMaterialAttribute(JSONObject obj, HttpServletRequest request) throws Exception{
  80. MaterialAttribute materialAttribute = JSONObject.parseObject(obj.toJSONString(), MaterialAttribute.class);
  81. try{
  82. materialAttributeMapper.updateByPrimaryKeySelective(materialAttribute);
  83. logService.insertLog("商品属性",
  84. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(materialAttribute.getAttributeName()).toString(), request);
  85. return 1;
  86. }catch(Exception e){
  87. JshException.writeFail(logger, e);
  88. return 0;
  89. }
  90. }
  91. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  92. public int deleteMaterialAttribute(Long id, HttpServletRequest request)throws Exception {
  93. return batchDeleteMaterialAttributeByIds(id.toString());
  94. }
  95. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  96. public int batchDeleteMaterialAttribute(String ids, HttpServletRequest request)throws Exception {
  97. return batchDeleteMaterialAttributeByIds(ids);
  98. }
  99. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  100. public int batchDeleteMaterialAttributeByIds(String ids) throws Exception{
  101. String [] idArray=ids.split(",");
  102. try{
  103. return materialAttributeMapperEx.batchDeleteMaterialAttributeByIds(idArray);
  104. }catch(Exception e){
  105. JshException.writeFail(logger, e);
  106. return 0;
  107. }
  108. }
  109. public int checkIsNameExist(Long id, String name)throws Exception {
  110. MaterialAttributeExample example = new MaterialAttributeExample();
  111. example.createCriteria().andIdNotEqualTo(id).andAttributeNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  112. List<MaterialAttribute> list =null;
  113. try{
  114. list = materialAttributeMapper.selectByExample(example);
  115. }catch(Exception e){
  116. JshException.readFail(logger, e);
  117. }
  118. return list==null?0:list.size();
  119. }
  120. public JSONArray getValueArrById(Long id) {
  121. JSONArray valueArr = new JSONArray();
  122. MaterialAttribute ma = getInfoById(id);
  123. if(ma!=null) {
  124. String value = ma.getAttributeValue();
  125. if(StringUtil.isNotEmpty(value)){
  126. String[] arr = value.split("\\|");
  127. for(String v: arr) {
  128. JSONObject item = new JSONObject();
  129. item.put("value",v);
  130. item.put("name",v);
  131. valueArr.add(item);
  132. }
  133. }
  134. }
  135. return valueArr;
  136. }
  137. public MaterialAttribute getInfoById(Long id) {
  138. MaterialAttributeExample example = new MaterialAttributeExample();
  139. example.createCriteria().andIdEqualTo(id).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  140. List<MaterialAttribute> list = materialAttributeMapper.selectByExample(example);
  141. if(list!=null && list.size()>0) {
  142. return list.get(0);
  143. } else {
  144. return null;
  145. }
  146. }
  147. }