| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- package com.qlm.service.impl;
- import com.jfinal.kit.StrKit;
- import com.jfinal.plugin.activerecord.Db;
- import com.jfinal.plugin.activerecord.Page;
- import com.jfinal.plugin.activerecord.Record;
- import com.qlm.common.ApiResponse;
- import com.qlm.common.PageResult;
- import com.qlm.dto.ProductionLineDto;
- import com.qlm.service.IProductionLineService;
- import com.qlm.tools.WxUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 产线服务实现类
- */
- public class ProductionLineServiceImpl implements IProductionLineService {
- private static final Logger logger = LoggerFactory.getLogger(ProductionLineServiceImpl.class);
- @Override
- public ApiResponse saveProductionLine(ProductionLineDto productionLineDto) {
- try {
- // 数据验证
- if (StrKit.isBlank(productionLineDto.getLineCode())) {
- return ApiResponse.error("产线编号不能为空");
- }
- if (StrKit.isBlank(productionLineDto.getLineName())) {
- return ApiResponse.error("产线名称不能为空");
- }
- if (productionLineDto.getFactoryId() == null) {
- return ApiResponse.error("所属工厂不能为空");
- }
- if (productionLineDto.getWorkshopId() == null) {
- return ApiResponse.error("所属车间不能为空");
- }
- // 检查产线编号是否已存在
- Record existRecord = Db.findFirst("select id from t_jz_device where device_no = ?", productionLineDto.getLineCode());
- if (existRecord != null) {
- return ApiResponse.error("产线编号已存在");
- }
- // 保存数据
- Record record = new Record();
- record.set("device_no", productionLineDto.getLineCode());
- record.set("desc", productionLineDto.getLineName());
- record.set("factory_id", productionLineDto.getFactoryId());
- record.set("workshop_id", productionLineDto.getWorkshopId());
- record.set("contact_name", productionLineDto.getContactName());
- record.set("contact_phone", productionLineDto.getContactPhone());
- record.set("account_id", productionLineDto.getAccountId());
- record.set("status", productionLineDto.getStatus() != null ? productionLineDto.getStatus() : 1);
- record.set("operator", productionLineDto.getOperator());
- boolean result = Db.save("t_jz_device", record);
- if (result) {
- return ApiResponse.success("保存成功");
- } else {
- return ApiResponse.error("保存失败");
- }
- } catch (Exception e) {
- logger.error("保存产线信息异常:", e);
- return ApiResponse.error("系统异常:" + e.getMessage());
- }
- }
- @Override
- public ApiResponse updateProductionLine(ProductionLineDto productionLineDto) {
- try {
- // 数据验证
- if (productionLineDto.getId() == null) {
- return ApiResponse.error("产线ID不能为空");
- }
- if (StrKit.isBlank(productionLineDto.getLineCode())) {
- return ApiResponse.error("产线编号不能为空");
- }
- if (StrKit.isBlank(productionLineDto.getLineName())) {
- return ApiResponse.error("产线名称不能为空");
- }
- if (productionLineDto.getFactoryId() == null) {
- return ApiResponse.error("所属工厂不能为空");
- }
- if (productionLineDto.getWorkshopId() == null) {
- return ApiResponse.error("所属车间不能为空");
- }
- // 检查产线编号是否已存在(排除当前记录)
- Record existRecord = Db.findFirst("select id from t_jz_device where device_no = ? and id != ?",
- productionLineDto.getLineCode(), productionLineDto.getId());
- if (existRecord != null) {
- return ApiResponse.error("产线编号已存在");
- }
- // 更新数据
- Record record = new Record();
- record.set("id", productionLineDto.getId());
- record.set("device_no", productionLineDto.getLineCode());
- record.set("desc", productionLineDto.getLineName());
- record.set("factory_id", productionLineDto.getFactoryId());
- record.set("workshop_id", productionLineDto.getWorkshopId());
- record.set("contact_name", productionLineDto.getContactName());
- record.set("contact_phone", productionLineDto.getContactPhone());
- record.set("account_id", productionLineDto.getAccountId());
- record.set("status", productionLineDto.getStatus());
- record.set("operator", productionLineDto.getOperator());
- boolean result = Db.update("t_jz_device", record);
- if (result) {
- return ApiResponse.success("更新成功");
- } else {
- return ApiResponse.error("更新失败");
- }
- } catch (Exception e) {
- logger.error("更新产线信息异常:", e);
- return ApiResponse.error("系统异常:" + e.getMessage());
- }
- }
- @Override
- public ApiResponse deleteProductionLine(Long id) {
- try {
- if (id == null) {
- return ApiResponse.error("产线ID不能为空");
- }
- // 检查是否存在关联数据(如果有)
- // TODO: 如果有其他表关联到产线表,需要先检查并处理
- boolean result = Db.deleteById("t_jz_device", id);
- if (result) {
- return ApiResponse.success("删除成功");
- } else {
- return ApiResponse.error("删除失败");
- }
- } catch (Exception e) {
- logger.error("删除产线信息异常:", e);
- return ApiResponse.error("系统异常:" + e.getMessage());
- }
- }
- @Override
- public ApiResponse getProductionLineById(Long id) {
- try {
- if (id == null) {
- return ApiResponse.error("产线ID不能为空");
- }
- // 关联查询工厂和车间名称
- String sql = "select pl.*, f.factory_name, w.workshop_name from t_jz_device pl " +
- "left join t_factory f on pl.factory_id = f.id " +
- "left join t_workshop w on pl.workshop_id = w.id " +
- "where pl.id = ?";
- Record record = Db.findFirst(sql, id);
- if (record == null) {
- return ApiResponse.error("未找到该产线信息");
- }
- ProductionLineDto productionLineDto = convertRecordToDto(record);
- return ApiResponse.success(productionLineDto);
- } catch (Exception e) {
- logger.error("获取产线信息异常:", e);
- return ApiResponse.error("系统异常:" + e.getMessage());
- }
- }
- @Override
- public ApiResponse toggleProductionLineStatus(Long id, Integer status) {
- try {
- if (id == null) {
- return ApiResponse.error("产线ID不能为空");
- }
- if (status == null || (status != 0 && status != 1)) {
- return ApiResponse.error("状态值无效,只能是0或1");
- }
- Record record = new Record();
- record.set("id", id);
- record.set("status", status);
- record.set("updated_time", WxUtil.getNowTime());
- boolean result = Db.update("t_jz_device", record);
- if (result) {
- return ApiResponse.success("状态更新成功");
- } else {
- return ApiResponse.error("状态更新失败");
- }
- } catch (Exception e) {
- logger.error("切换产线状态异常:", e);
- return ApiResponse.error("系统异常:" + e.getMessage());
- }
- }
- @Override
- public PageResult<ProductionLineDto> listProductionLine(int pageNumber, int pageSize, String lineName, String lineCode, Long factoryId, Long workshopId) {
- try {
- // 构建查询条件
- StringBuilder sqlWhere = new StringBuilder(" from t_jz_device pl " +
- "left join t_factory f on pl.factory_id = f.id " +
- "left join t_workshop w on pl.workshop_id = w.id " +
- "where 1=1 ");
- List<Object> params = new ArrayList<>();
- if (StrKit.notBlank(lineName)) {
- sqlWhere.append(" and pl.desc like ? ");
- params.add("%" + lineName + "%");
- }
- if (StrKit.notBlank(lineCode)) {
- sqlWhere.append(" and pl.device_no like ? ");
- params.add("%" + lineCode + "%");
- }
- if (factoryId != null) {
- sqlWhere.append(" and pl.factory_id = ? ");
- params.add(factoryId);
- }
- if (workshopId != null) {
- sqlWhere.append(" and pl.workshop_id = ? ");
- params.add(workshopId);
- }
- sqlWhere.append(" order by pl.updated_time desc ");
- // 执行分页查询
- Page<Record> recordPage = Db.paginate(pageNumber, pageSize,
- "select pl.*, f.factory_name, w.workshop_name ",
- sqlWhere.toString(),
- params.toArray());
- if (recordPage == null) {
- return new PageResult<>(0, pageNumber, pageSize, new ArrayList<>());
- }
- // 转换为Dto对象
- List<ProductionLineDto> productionLineDtoList = new ArrayList<>();
- for (Record record : recordPage.getList()) {
- productionLineDtoList.add(convertRecordToDto(record));
- }
- // 创建新的Page对象,包含Dto列表
- return new PageResult<>(recordPage.getTotalRow(),
- recordPage.getPageNumber(),
- recordPage.getPageSize(),
- productionLineDtoList);
- } catch (Exception e) {
- logger.error("查询产线列表异常:", e);
- return new PageResult<>(0, pageNumber, pageSize, new ArrayList<>());
- }
- }
- @Override
- public ApiResponse getAllFactories(String keyword) {
- try {
- StringBuilder sql = new StringBuilder("select id, factory_name from t_factory where status = 1 ");
- List<Object> params = new ArrayList<>();
- if (StrKit.notBlank(keyword)) {
- sql.append(" and factory_name like ? ");
- params.add("%" + keyword + "%");
- }
- sql.append(" order by factory_name asc ");
- List<Record> records = Db.find(sql.toString(), params.toArray());
- return ApiResponse.success(records);
- } catch (Exception e) {
- logger.error("获取工厂列表异常:", e);
- return ApiResponse.error("系统异常:" + e.getMessage());
- }
- }
- @Override
- public ApiResponse getWorkshopsByFactoryId(Long factoryId, String keyword) {
- try {
- StringBuilder sql = new StringBuilder("select id, workshop_name from t_workshop where status = 1 ");
- List<Object> params = new ArrayList<>();
- if (factoryId != null) {
- sql.append(" and factory_id = ? ");
- params.add(factoryId);
- }
- if (StrKit.notBlank(keyword)) {
- sql.append(" and workshop_name like ? ");
- params.add("%" + keyword + "%");
- }
- sql.append(" order by workshop_name asc ");
- List<Record> records = Db.find(sql.toString(), params.toArray());
- return ApiResponse.success(records);
- } catch (Exception e) {
- logger.error("获取车间列表异常:", e);
- return ApiResponse.error("系统异常:" + e.getMessage());
- }
- }
- /**
- * 将Record转换为ProductionLineDto的工具方法
- * @param record
- * @return
- */
- private ProductionLineDto convertRecordToDto(Record record) {
- ProductionLineDto dto = new ProductionLineDto();
- dto.setId(WxUtil.getInt("id",record).longValue());
- dto.setLineCode(record.getStr("device_no"));
- dto.setLineName(record.getStr("desc"));
- dto.setFactoryId(WxUtil.getInt("factory_id", record).longValue());
- dto.setFactoryName(record.getStr("factory_name"));
- dto.setWorkshopId(WxUtil.getInt("workshop_id", record).longValue());
- dto.setWorkshopName(record.getStr("workshop_name"));
- dto.setContactName(record.getStr("contact_name"));
- dto.setContactPhone(record.getStr("contact_phone"));
- dto.setAccountId(WxUtil.getStr("account_id", record));
- dto.setOperDetail(record.getStr("oper_detail"));
- dto.setStatus(record.getInt("status"));
- dto.setCreatedTime(record.getDate("created_time"));
- dto.setUpdatedTime(record.getDate("updated_time"));
- dto.setOperator(record.getStr("operator"));
- return dto;
- }
- }
|