package com.boman.system.common; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.boman.common.core.utils.StringUtils; import com.boman.common.core.utils.collection.CollectionUtils; import com.boman.common.redis.RedisKey; import com.boman.common.redis.service.RedisService; import com.boman.gen.domain.GenTable; import com.boman.gen.domain.GenTableColumn; import com.boman.system.service.impl.BaseDeleteService; import com.boman.system.service.impl.BaseSaveService; import com.boman.system.utils.IdUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull; import static com.boman.common.core.utils.obj.ObjectUtils.requiredNonNull; import static com.boman.system.common.FormDataConstant.*; /** * @author shiqian * @description * @date 2021年03月22日 09:51 **/ @Component public class TableServiceCmdService { @Autowired private BaseDeleteService deleteService; @Autowired private BaseSaveService saveService; @Autowired private RedisService redisService; private static final Logger LOGGER = LoggerFactory.getLogger(TableServiceCmdService.class); private BaseTableDTO packTableDTO(BaseTableSaveDTO baseTableSaveDTO) { BaseTableDTO baseTableDTO = new BaseTableDTO(); baseTableDTO.setFixedData(baseTableSaveDTO.getFixedData()); baseTableDTO.setObjId(baseTableSaveDTO.getObjId()); baseTableDTO.setTable(baseTableSaveDTO.getTable()); return baseTableDTO; } public final ValueHolder objectSave(BaseTableSaveDTO baseTableSaveDTO) { BaseTableDTO baseTableDTO = packTableDTO(baseTableSaveDTO); TableServiceContext context = TableServiceContext.createFrom(baseTableDTO); // BaseSaveService baseSaveService = SpringUtils.getBean(BaseSaveService.class); // 拿到pkName和maxId List columns = context.getTable().getColumns(); String pkName = IdUtils.getPkName(columns); StringUtils.requireNonNull(pkName); Long maxId = IdUtils.getMaxId(baseTableDTO.getTable(), pkName); RowResult rowResult = saveService.insertRow(context.getRealTableName(), pkName, maxId, context.getRows().get(0)); if (RowResult.checkSuccess(rowResult)) { LOGGER.info("保存成功,封装到数据库的数据为: {}", JSON.toJSONString(rowResult.getData())); } else { LOGGER.error("保存失败,保持的原始数据为: {}", JSON.toJSONString(baseTableSaveDTO)); } return ValueHolder.ok(rowResult); } /** * 功能描述: 通用删除接口 (真的删除) * * @param dto 前台传过来的dto * @return com.boman.system.common.ValueHolder */ public ValueHolder objectDelete(BaseTableSaveDTO dto) { requireNonNull(dto.getTable()); Long[] idArr = CollectionUtils.listToArray(dto.getIdList()); requiredNonNull(idArr); // 拿到pkName GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + requireNonNull(dto.getTable())); requireNonNull(genTable); String pkName = IdUtils.getPkName(genTable.getColumns()); RowResult rowResult = deleteService.objectDelete(idArr, dto.getTable(), requireNonNull(pkName)); LOGGER.info(rowResult.getMessage() + ", id: {}", Arrays.toString(idArr)); return ValueHolder.ok(rowResult); } /** * 功能描述: 通用删除接口 (真的删除) * * @param dto 前台传过来的dto * @return com.boman.system.common.ValueHolder */ public ValueHolder objectLogicDelete(BaseTableSaveDTO dto) { requireNonNull(dto.getTable()); Long[] idArr = CollectionUtils.listToArray(dto.getIdList()); requiredNonNull(idArr); // 拿到pkName GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + requireNonNull(dto.getTable())); requireNonNull(genTable); String pkName = IdUtils.getPkName(genTable.getColumns()); JSONObject jsonObject= new JSONObject(); jsonObject.put(dto.getLogicDelName(), dto.getLogicDelValue()); RowResult rowResult = deleteService.objectLogicDelete(idArr, dto.getTable(), requireNonNull(pkName), jsonObject); LOGGER.info(rowResult.getMessage() + ", id: {}", Arrays.toString(idArr)); return ValueHolder.ok(rowResult); } /** * 功能描述: 获取单表单数据 * * @param condition condition * @return com.boman.system.common.ValueHolder */ public ValueHolder getObject(BaseTableSaveDTO dto) { requireNonNull(dto.getTable()); // 拿到每个字段对应的查询类型,是 = 或者 like 或者 > 或者 < GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + requireNonNull(dto.getTable())); requireNonNull(genTable); JSONObject fixedData = dto.getFixedData(); requireNonNull(fixedData); // 查询条件 JSONObject condition = fixedData.getJSONObject(CONDITION); // 需要返回到前台的列 JSONArray showData = fixedData.getJSONArray(SHOW_DATA); return ValueHolder.ok(""); } }