MaterielService.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package co.dc.aptiv.service;
  2. import co.dc.aptiv.AptivApp;
  3. import co.dc.commons.basedao.CommonBaseDao;
  4. import co.dc.commons.utils.DateTimeUtil;
  5. import co.dc.aptiv.pojo.MaterielPojo;
  6. import org.apache.commons.dbutils.QueryRunner;
  7. import org.apache.commons.dbutils.handlers.BeanHandler;
  8. import org.apache.commons.dbutils.handlers.BeanListHandler;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.util.StringUtils;
  13. import javax.annotation.Resource;
  14. import java.sql.SQLException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. /**
  18. * @describe: 物料
  19. * @author: liuxinglong
  20. * @version: 2018/4/17 15:21
  21. */
  22. @Service("materielService")
  23. public class MaterielService {
  24. private static final Log log = LogFactory.getLog(MaterielService.class);
  25. @Resource(name = "commonBaseDao")
  26. private CommonBaseDao baseDao;
  27. @Resource
  28. private QueryRunner queryRunner;
  29. /*
  30. * 1,增加=saveSku
  31. * 2,删除=delbyid
  32. * 3,修改=updateSku
  33. * 4,查询=findbyid
  34. * 5,查询列表=listAll
  35. * 6,查询是否有sku已经存在=isExistByproductCode
  36. */
  37. /**
  38. * 增加物料
  39. *
  40. * @return
  41. */
  42. public long saveMateriel(MaterielPojo materielPojo) {
  43. if (materielPojo != null) {
  44. log.info("添加一个物料 dpn=" + materielPojo.getDpn() + " cpn=" + materielPojo.getCpn() + " type=" + materielPojo.getType());
  45. return baseDao.save(materielPojo);
  46. } else {
  47. return 0;
  48. }
  49. }
  50. /**
  51. * 根据ID删除物料
  52. *
  53. * @param id
  54. * @return
  55. */
  56. public int deleteById(long id) {
  57. log.info("删除一个物料 id=" + id);
  58. if (id != 0) {
  59. return baseDao.del(MaterielPojo.class, id);
  60. } else {
  61. return 0;
  62. }
  63. }
  64. /**
  65. * 根据dpn删除物料
  66. *
  67. * @param dpn
  68. * @return
  69. */
  70. public int deleteByDpn(String dpn) {
  71. log.info("根据dpn删除物料 dpn=" + dpn);
  72. if (!StringUtils.isEmpty(dpn)) {
  73. return baseDao.del(MaterielPojo.class, "dpn", dpn);
  74. } else {
  75. return 0;
  76. }
  77. }
  78. /**
  79. * 更新物料序号
  80. *
  81. * @param id
  82. * @return
  83. */
  84. public int updateMaterielpojoMaxNum(long id) {
  85. log.info("更新物料序号 id=" + id);
  86. MaterielPojo materielPojo = AptivApp.materielService.findById(id);
  87. materielPojo.setMaxNum(materielPojo.getMaxNum() + 1);
  88. materielPojo.setModifyTime(DateTimeUtil.getDateTimeString());
  89. return baseDao.update(materielPojo);
  90. }
  91. /**
  92. * 更新物料
  93. *
  94. * @param materielPojo
  95. * @return
  96. */
  97. public int updateMaterielpojo(MaterielPojo materielPojo) {
  98. log.info("更新物料序号 物料名称=" + materielPojo.getDpn());
  99. return baseDao.update(materielPojo);
  100. }
  101. /**
  102. * 查询单个物料
  103. *
  104. * @param id
  105. * @return
  106. */
  107. public MaterielPojo findById(Long id) {
  108. MaterielPojo materielPojo = baseDao.findById(MaterielPojo.class, id);
  109. if (materielPojo != null) {
  110. return materielPojo;
  111. } else {
  112. return new MaterielPojo();
  113. }
  114. }
  115. /**
  116. * 根据dpn查物料
  117. *
  118. * @param dpn
  119. * @return
  120. */
  121. public MaterielPojo findByDPN(String dpn) {
  122. MaterielPojo materielPojo = baseDao.findByParam(MaterielPojo.class, "dpn", dpn);
  123. if (materielPojo != null) {
  124. return materielPojo;
  125. }
  126. return null;
  127. }
  128. /**
  129. * 根据cpn查物料
  130. *
  131. * @param dpn
  132. * @param cpn
  133. * @return
  134. */
  135. public MaterielPojo findByCPN(String dpn, String cpn) {
  136. try {
  137. MaterielPojo materielPojo = baseDao.query("SELECT * FROM materiel WHERE delFlag = 1 AND (dpn = ? OR cpn = ?)", new BeanHandler<MaterielPojo>(MaterielPojo.class), dpn, cpn);
  138. if (materielPojo != null) {
  139. return materielPojo;
  140. }
  141. } catch (SQLException e) {
  142. e.printStackTrace();
  143. }
  144. return null;
  145. }
  146. /**
  147. * 查询所有物料数据
  148. *
  149. * @return
  150. */
  151. public List<MaterielPojo> listAll() {
  152. log.info("查询所有物料数据。");
  153. List<MaterielPojo> materielPojos = baseDao.listByParam(MaterielPojo.class, "1", "1", "id asc");
  154. if (materielPojos != null) {
  155. return materielPojos;
  156. } else {
  157. return new ArrayList<MaterielPojo>();
  158. }
  159. }
  160. /**
  161. * 查询是否有sku已经存在=isExistByproductCode
  162. *
  163. * @return
  164. */
  165. public boolean isExistByproductCode(String productCode, long skuId) {
  166. log.info("查询是否有sku已经存在。");
  167. List<MaterielPojo> materielPojos = baseDao.listByParam(MaterielPojo.class, "productCode", productCode);
  168. boolean reBoolean = false;
  169. if (materielPojos != null && materielPojos.size() > 0) {
  170. if (skuId == 0) {
  171. reBoolean = true;
  172. } else {
  173. for (MaterielPojo materielPojo : materielPojos) {
  174. if (materielPojo.getId() != skuId) {
  175. reBoolean = true;
  176. break;
  177. }
  178. }
  179. }
  180. } else {
  181. reBoolean = false;
  182. }
  183. return reBoolean;
  184. }
  185. /**
  186. * 判断参数是不是符合要求
  187. *
  188. * @param paramName 要查询的字段名
  189. * @param paramValue 要查询的参数
  190. * @param MaxLength 参数的最大长度
  191. * @param id 0 添加 >0修改
  192. * @return
  193. */
  194. public int isExistByParam(String paramName, String paramValue, int MaxLength, long id) {
  195. String sql = "SELECT * FROM materiel WHERE delFlag = 0 AND ? = ?";
  196. if (id > 0) {
  197. sql += " AND id != " + id;
  198. }
  199. try {
  200. List<MaterielPojo> materielPojos = baseDao.query(sql, new BeanListHandler<MaterielPojo>(MaterielPojo.class), paramName, paramValue);
  201. if (materielPojos != null && materielPojos.size() > 0) {
  202. return 1;
  203. }
  204. if (MaxLength != paramValue.length()) {
  205. return 2;
  206. }
  207. } catch (SQLException e) {
  208. e.printStackTrace();
  209. }
  210. return 0;
  211. }
  212. }