ProductionLineServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package com.qlm.service.impl;
  2. import com.jfinal.kit.StrKit;
  3. import com.jfinal.plugin.activerecord.Db;
  4. import com.jfinal.plugin.activerecord.Page;
  5. import com.jfinal.plugin.activerecord.Record;
  6. import com.qlm.common.ApiResponse;
  7. import com.qlm.common.PageResult;
  8. import com.qlm.dto.ProductionLineDto;
  9. import com.qlm.service.IProductionLineService;
  10. import com.qlm.tools.WxUtil;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. /**
  16. * 产线服务实现类
  17. */
  18. public class ProductionLineServiceImpl implements IProductionLineService {
  19. private static final Logger logger = LoggerFactory.getLogger(ProductionLineServiceImpl.class);
  20. @Override
  21. public ApiResponse saveProductionLine(ProductionLineDto productionLineDto) {
  22. try {
  23. // 数据验证
  24. if (StrKit.isBlank(productionLineDto.getLineCode())) {
  25. return ApiResponse.error("产线编号不能为空");
  26. }
  27. if (StrKit.isBlank(productionLineDto.getLineName())) {
  28. return ApiResponse.error("产线名称不能为空");
  29. }
  30. if (productionLineDto.getFactoryId() == null) {
  31. return ApiResponse.error("所属工厂不能为空");
  32. }
  33. if (productionLineDto.getWorkshopId() == null) {
  34. return ApiResponse.error("所属车间不能为空");
  35. }
  36. // 检查产线编号是否已存在
  37. Record existRecord = Db.findFirst("select id from t_jz_device where device_no = ?", productionLineDto.getLineCode());
  38. if (existRecord != null) {
  39. return ApiResponse.error("产线编号已存在");
  40. }
  41. // 保存数据
  42. Record record = new Record();
  43. record.set("device_no", productionLineDto.getLineCode());
  44. record.set("desc", productionLineDto.getLineName());
  45. record.set("factory_id", productionLineDto.getFactoryId());
  46. record.set("workshop_id", productionLineDto.getWorkshopId());
  47. record.set("contact_name", productionLineDto.getContactName());
  48. record.set("contact_phone", productionLineDto.getContactPhone());
  49. record.set("account_id", productionLineDto.getAccountId());
  50. record.set("status", productionLineDto.getStatus() != null ? productionLineDto.getStatus() : 1);
  51. record.set("operator", productionLineDto.getOperator());
  52. boolean result = Db.save("t_jz_device", record);
  53. if (result) {
  54. return ApiResponse.success("保存成功");
  55. } else {
  56. return ApiResponse.error("保存失败");
  57. }
  58. } catch (Exception e) {
  59. logger.error("保存产线信息异常:", e);
  60. return ApiResponse.error("系统异常:" + e.getMessage());
  61. }
  62. }
  63. @Override
  64. public ApiResponse updateProductionLine(ProductionLineDto productionLineDto) {
  65. try {
  66. // 数据验证
  67. if (productionLineDto.getId() == null) {
  68. return ApiResponse.error("产线ID不能为空");
  69. }
  70. if (StrKit.isBlank(productionLineDto.getLineCode())) {
  71. return ApiResponse.error("产线编号不能为空");
  72. }
  73. if (StrKit.isBlank(productionLineDto.getLineName())) {
  74. return ApiResponse.error("产线名称不能为空");
  75. }
  76. if (productionLineDto.getFactoryId() == null) {
  77. return ApiResponse.error("所属工厂不能为空");
  78. }
  79. if (productionLineDto.getWorkshopId() == null) {
  80. return ApiResponse.error("所属车间不能为空");
  81. }
  82. // 检查产线编号是否已存在(排除当前记录)
  83. Record existRecord = Db.findFirst("select id from t_jz_device where device_no = ? and id != ?",
  84. productionLineDto.getLineCode(), productionLineDto.getId());
  85. if (existRecord != null) {
  86. return ApiResponse.error("产线编号已存在");
  87. }
  88. // 更新数据
  89. Record record = new Record();
  90. record.set("id", productionLineDto.getId());
  91. record.set("device_no", productionLineDto.getLineCode());
  92. record.set("desc", productionLineDto.getLineName());
  93. record.set("factory_id", productionLineDto.getFactoryId());
  94. record.set("workshop_id", productionLineDto.getWorkshopId());
  95. record.set("contact_name", productionLineDto.getContactName());
  96. record.set("contact_phone", productionLineDto.getContactPhone());
  97. record.set("account_id", productionLineDto.getAccountId());
  98. record.set("status", productionLineDto.getStatus());
  99. record.set("operator", productionLineDto.getOperator());
  100. boolean result = Db.update("t_jz_device", record);
  101. if (result) {
  102. return ApiResponse.success("更新成功");
  103. } else {
  104. return ApiResponse.error("更新失败");
  105. }
  106. } catch (Exception e) {
  107. logger.error("更新产线信息异常:", e);
  108. return ApiResponse.error("系统异常:" + e.getMessage());
  109. }
  110. }
  111. @Override
  112. public ApiResponse deleteProductionLine(Long id) {
  113. try {
  114. if (id == null) {
  115. return ApiResponse.error("产线ID不能为空");
  116. }
  117. // 检查是否存在关联数据(如果有)
  118. // TODO: 如果有其他表关联到产线表,需要先检查并处理
  119. boolean result = Db.deleteById("t_jz_device", id);
  120. if (result) {
  121. return ApiResponse.success("删除成功");
  122. } else {
  123. return ApiResponse.error("删除失败");
  124. }
  125. } catch (Exception e) {
  126. logger.error("删除产线信息异常:", e);
  127. return ApiResponse.error("系统异常:" + e.getMessage());
  128. }
  129. }
  130. @Override
  131. public ApiResponse getProductionLineById(Long id) {
  132. try {
  133. if (id == null) {
  134. return ApiResponse.error("产线ID不能为空");
  135. }
  136. // 关联查询工厂和车间名称
  137. String sql = "select pl.*, f.factory_name, w.workshop_name from t_jz_device pl " +
  138. "left join t_factory f on pl.factory_id = f.id " +
  139. "left join t_workshop w on pl.workshop_id = w.id " +
  140. "where pl.id = ?";
  141. Record record = Db.findFirst(sql, id);
  142. if (record == null) {
  143. return ApiResponse.error("未找到该产线信息");
  144. }
  145. ProductionLineDto productionLineDto = convertRecordToDto(record);
  146. return ApiResponse.success(productionLineDto);
  147. } catch (Exception e) {
  148. logger.error("获取产线信息异常:", e);
  149. return ApiResponse.error("系统异常:" + e.getMessage());
  150. }
  151. }
  152. @Override
  153. public ApiResponse toggleProductionLineStatus(Long id, Integer status) {
  154. try {
  155. if (id == null) {
  156. return ApiResponse.error("产线ID不能为空");
  157. }
  158. if (status == null || (status != 0 && status != 1)) {
  159. return ApiResponse.error("状态值无效,只能是0或1");
  160. }
  161. Record record = new Record();
  162. record.set("id", id);
  163. record.set("status", status);
  164. record.set("updated_time", WxUtil.getNowTime());
  165. boolean result = Db.update("t_jz_device", record);
  166. if (result) {
  167. return ApiResponse.success("状态更新成功");
  168. } else {
  169. return ApiResponse.error("状态更新失败");
  170. }
  171. } catch (Exception e) {
  172. logger.error("切换产线状态异常:", e);
  173. return ApiResponse.error("系统异常:" + e.getMessage());
  174. }
  175. }
  176. @Override
  177. public PageResult<ProductionLineDto> listProductionLine(int pageNumber, int pageSize, String lineName, String lineCode, Long factoryId, Long workshopId) {
  178. try {
  179. // 构建查询条件
  180. StringBuilder sqlWhere = new StringBuilder(" from t_jz_device pl " +
  181. "left join t_factory f on pl.factory_id = f.id " +
  182. "left join t_workshop w on pl.workshop_id = w.id " +
  183. "where 1=1 ");
  184. List<Object> params = new ArrayList<>();
  185. if (StrKit.notBlank(lineName)) {
  186. sqlWhere.append(" and pl.desc like ? ");
  187. params.add("%" + lineName + "%");
  188. }
  189. if (StrKit.notBlank(lineCode)) {
  190. sqlWhere.append(" and pl.device_no like ? ");
  191. params.add("%" + lineCode + "%");
  192. }
  193. if (factoryId != null) {
  194. sqlWhere.append(" and pl.factory_id = ? ");
  195. params.add(factoryId);
  196. }
  197. if (workshopId != null) {
  198. sqlWhere.append(" and pl.workshop_id = ? ");
  199. params.add(workshopId);
  200. }
  201. sqlWhere.append(" order by pl.updated_time desc ");
  202. // 执行分页查询
  203. Page<Record> recordPage = Db.paginate(pageNumber, pageSize,
  204. "select pl.*, f.factory_name, w.workshop_name ",
  205. sqlWhere.toString(),
  206. params.toArray());
  207. if (recordPage == null) {
  208. return new PageResult<>(0, pageNumber, pageSize, new ArrayList<>());
  209. }
  210. // 转换为Dto对象
  211. List<ProductionLineDto> productionLineDtoList = new ArrayList<>();
  212. for (Record record : recordPage.getList()) {
  213. productionLineDtoList.add(convertRecordToDto(record));
  214. }
  215. // 创建新的Page对象,包含Dto列表
  216. return new PageResult<>(recordPage.getTotalRow(),
  217. recordPage.getPageNumber(),
  218. recordPage.getPageSize(),
  219. productionLineDtoList);
  220. } catch (Exception e) {
  221. logger.error("查询产线列表异常:", e);
  222. return new PageResult<>(0, pageNumber, pageSize, new ArrayList<>());
  223. }
  224. }
  225. @Override
  226. public ApiResponse getAllFactories(String keyword) {
  227. try {
  228. StringBuilder sql = new StringBuilder("select id, factory_name from t_factory where status = 1 ");
  229. List<Object> params = new ArrayList<>();
  230. if (StrKit.notBlank(keyword)) {
  231. sql.append(" and factory_name like ? ");
  232. params.add("%" + keyword + "%");
  233. }
  234. sql.append(" order by factory_name asc ");
  235. List<Record> records = Db.find(sql.toString(), params.toArray());
  236. return ApiResponse.success(records);
  237. } catch (Exception e) {
  238. logger.error("获取工厂列表异常:", e);
  239. return ApiResponse.error("系统异常:" + e.getMessage());
  240. }
  241. }
  242. @Override
  243. public ApiResponse getWorkshopsByFactoryId(Long factoryId, String keyword) {
  244. try {
  245. StringBuilder sql = new StringBuilder("select id, workshop_name from t_workshop where status = 1 ");
  246. List<Object> params = new ArrayList<>();
  247. if (factoryId != null) {
  248. sql.append(" and factory_id = ? ");
  249. params.add(factoryId);
  250. }
  251. if (StrKit.notBlank(keyword)) {
  252. sql.append(" and workshop_name like ? ");
  253. params.add("%" + keyword + "%");
  254. }
  255. sql.append(" order by workshop_name asc ");
  256. List<Record> records = Db.find(sql.toString(), params.toArray());
  257. return ApiResponse.success(records);
  258. } catch (Exception e) {
  259. logger.error("获取车间列表异常:", e);
  260. return ApiResponse.error("系统异常:" + e.getMessage());
  261. }
  262. }
  263. /**
  264. * 将Record转换为ProductionLineDto的工具方法
  265. * @param record
  266. * @return
  267. */
  268. private ProductionLineDto convertRecordToDto(Record record) {
  269. ProductionLineDto dto = new ProductionLineDto();
  270. dto.setId(WxUtil.getInt("id",record).longValue());
  271. dto.setLineCode(record.getStr("device_no"));
  272. dto.setLineName(record.getStr("desc"));
  273. dto.setFactoryId(WxUtil.getInt("factory_id", record).longValue());
  274. dto.setFactoryName(record.getStr("factory_name"));
  275. dto.setWorkshopId(WxUtil.getInt("workshop_id", record).longValue());
  276. dto.setWorkshopName(record.getStr("workshop_name"));
  277. dto.setContactName(record.getStr("contact_name"));
  278. dto.setContactPhone(record.getStr("contact_phone"));
  279. dto.setAccountId(WxUtil.getStr("account_id", record));
  280. dto.setOperDetail(record.getStr("oper_detail"));
  281. dto.setStatus(record.getInt("status"));
  282. dto.setCreatedTime(record.getDate("created_time"));
  283. dto.setUpdatedTime(record.getDate("updated_time"));
  284. dto.setOperator(record.getStr("operator"));
  285. return dto;
  286. }
  287. }