diff --git a/youchain-generator/src/main/java/com/youchain/domain/ExcelConfig.java b/youchain-generator/src/main/java/com/youchain/domain/ExcelConfig.java new file mode 100644 index 0000000..c80ea5a --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/domain/ExcelConfig.java @@ -0,0 +1,96 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.domain; + +import com.youchain.base.BaseEntity; +import lombok.Data; +import cn.hutool.core.bean.BeanUtil; +import io.swagger.annotations.ApiModelProperty; +import cn.hutool.core.bean.copier.CopyOptions; +import javax.persistence.*; +import javax.validation.constraints.*; +import java.sql.Timestamp; +import java.io.Serializable; + +/** +* @website https://eladmin.vip +* @description / +* @author baobl +* @date 2024-09-14 +**/ +@Entity +@Data +@Table(name="sys_excel_config") +public class ExcelConfig extends BaseEntity implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "`id`") + @ApiModelProperty(value = "主键") + private Long id; + + @Column(name = "`table_name`") + @ApiModelProperty(value = "table表名") + private String tableName; + + @Column(name = "`excel_column_name`") + @ApiModelProperty(value = "Excel列名") + private String excelColumnName; + + @Column(name = "`table_column_name`") + @ApiModelProperty(value = "table字段名") + private String tableColumnName; + @Column(name = "`dict_name`") + @ApiModelProperty(value = "关联字典") + private String dictName; + @OneToOne + @JoinColumn(name = "ec_key_id") + @ApiModelProperty(value = "关联外键") + private ExcelConfigKey ecKey; + @Column(name = "`table_column_type`") + @ApiModelProperty(value = "table字段类型") + private String tableColumnType; + + @Column(name = "`glb_type`") + @ApiModelProperty(value = "关联表类型") + private Integer glbType=0; + @Column(name = "`only_flag`") + @ApiModelProperty(value = "唯一标识") + private Boolean onlyFlag=false; + + @Column(name = "`default_value`") + @ApiModelProperty(value = "默认值") + private String defaultValue; + + @Column(name = "`work_type`") + @ApiModelProperty(value = "导入规则") + private String workType="1"; + + public void copy(ExcelConfig source){ + BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true)); + } + public Object getFormat(Object value,String columnType) { + switch (columnType){ + case "string": + value="'"+value+"'"; + break; + case "long": + value=Long.valueOf(value+""); + break; + } + return value; + } +} diff --git a/youchain-generator/src/main/java/com/youchain/domain/ExcelConfigKey.java b/youchain-generator/src/main/java/com/youchain/domain/ExcelConfigKey.java new file mode 100644 index 0000000..e44c577 --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/domain/ExcelConfigKey.java @@ -0,0 +1,61 @@ +/* + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.youchain.domain; + +import com.youchain.base.BaseEntity; +import lombok.Data; +import cn.hutool.core.bean.BeanUtil; +import io.swagger.annotations.ApiModelProperty; +import cn.hutool.core.bean.copier.CopyOptions; +import javax.persistence.*; +import javax.validation.constraints.*; +import java.sql.Timestamp; +import java.io.Serializable; + +/** + * @website https://eladmin.vip + * @description / + * @author baobl + * @date 2024-09-20 + **/ +@Entity +@Data +@Table(name="sys_excel_config_key") +public class ExcelConfigKey extends BaseEntity implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "`id`") + @ApiModelProperty(value = "主键") + private Long id; + @Column(name = "`table_name`") + @ApiModelProperty(value = "关联表") + private String tableName; + @Column(name = "`key_name`") + @ApiModelProperty(value = "映射字段") + private String keyName; + + @Column(name = "`table_column_name`") + @ApiModelProperty(value = "关联表字段名") + private String tableColumnName; + + @Column(name = "`table_column_type`") + @ApiModelProperty(value = "关联表字段类型") + private String tableColumnType; + public void copy(ExcelConfigKey source){ + BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true)); + } +} diff --git a/youchain-generator/src/main/java/com/youchain/repository/ExcelConfigKeyRepository.java b/youchain-generator/src/main/java/com/youchain/repository/ExcelConfigKeyRepository.java new file mode 100644 index 0000000..d5e788f --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/repository/ExcelConfigKeyRepository.java @@ -0,0 +1,32 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.repository; + +import com.youchain.domain.ExcelConfig; +import com.youchain.domain.ExcelConfigKey; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +/** +* @website https://eladmin.vip +* @author baobl +* @date 2024-09-14 +**/ +public interface ExcelConfigKeyRepository extends JpaRepository, JpaSpecificationExecutor { +} \ No newline at end of file diff --git a/youchain-generator/src/main/java/com/youchain/repository/ExcelConfigRepository.java b/youchain-generator/src/main/java/com/youchain/repository/ExcelConfigRepository.java new file mode 100644 index 0000000..44d740b --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/repository/ExcelConfigRepository.java @@ -0,0 +1,35 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.repository; + +import com.youchain.domain.ExcelConfig; +import com.youchain.domain.ExcelConfigKey; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +/** +* @website https://eladmin.vip +* @author baobl +* @date 2024-09-14 +**/ +public interface ExcelConfigRepository extends JpaRepository, JpaSpecificationExecutor { + @Query(value = "SELECT e FROM ExcelConfig e where e.tableName=?1",nativeQuery = false) + List findByTableName(String tableName); + List findByTableNameAndTableColumnName(String tableName, String tableColumnName); +} \ No newline at end of file diff --git a/youchain-generator/src/main/java/com/youchain/rest/ExcelConfigController.java b/youchain-generator/src/main/java/com/youchain/rest/ExcelConfigController.java new file mode 100644 index 0000000..289ba74 --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/rest/ExcelConfigController.java @@ -0,0 +1,258 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.rest; + +import cn.hutool.poi.excel.ExcelReader; +import cn.hutool.poi.excel.ExcelUtil; +import com.youchain.annotation.AnonymousAccess; +import com.youchain.config.FileProperties; +import com.youchain.domain.ExcelConfig; +import com.youchain.domain.ExcelConfigKey; +import com.youchain.exception.BadRequestException; +import com.youchain.exception.handler.ApiError; +import com.youchain.exception.handler.ApiResult; +import com.youchain.repository.ExcelConfigKeyRepository; +import com.youchain.repository.ExcelConfigRepository; +import com.youchain.service.ExcelConfigService; +import com.youchain.service.dto.ExcelConfigDto; +import com.youchain.service.dto.ExcelConfigQueryCriteria; +import com.youchain.utils.FileUtil; +import org.apache.poi.ss.formula.functions.T; +import org.springframework.data.domain.Pageable; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import io.swagger.annotations.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import javax.persistence.*; +import javax.servlet.http.HttpServletResponse; + +/** +* @website https://eladmin.vip +* @author baobl +* @date 2024-09-14 +**/ +@RestController +@RequiredArgsConstructor +@Api(tags = "excel_config管理") +@RequestMapping("/api/excelConfig") +public class ExcelConfigController { + private final FileProperties properties; + private final ExcelConfigService excelConfigService; + private final ExcelConfigKeyRepository excelConfigKeyRepository; + @PersistenceContext + private EntityManager entityManager; + private final ExcelConfigRepository excelConfigRepository; + @ApiOperation("导出数据") + @GetMapping(value = "/download") + @PreAuthorize("@el.check('excelConfig:list')") + public void exportExcelConfig(HttpServletResponse response, ExcelConfigQueryCriteria criteria) throws Exception { + excelConfigService.download(excelConfigService.queryAll(criteria), response); + } + + @GetMapping + @ApiOperation("查询excel_config") + @PreAuthorize("@el.check('excelConfig:list')") + public ResponseEntity queryExcelConfig(ExcelConfigQueryCriteria criteria, Pageable pageable){ + return new ResponseEntity<>(excelConfigService.queryAll(criteria,pageable),HttpStatus.OK); + } + + @PostMapping + @ApiOperation("新增excel_config") + @PreAuthorize("@el.check('excelConfig:add')") + public ResponseEntity createExcelConfig(@Validated @RequestBody ExcelConfigDto resources){ + return new ResponseEntity<>(excelConfigService.create(resources),HttpStatus.CREATED); + } + + @PutMapping + @ApiOperation("修改excel_config") + @PreAuthorize("@el.check('excelConfig:edit')") + public ResponseEntity updateExcelConfig(@Validated @RequestBody ExcelConfigDto resources){ + excelConfigService.create(resources); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @DeleteMapping + @ApiOperation("删除excel_config") + @PreAuthorize("@el.check('excelConfig:del')") + public ResponseEntity deleteExcelConfig(@RequestBody Long[] ids) { + excelConfigService.deleteAll(ids); + return new ResponseEntity<>(HttpStatus.OK); + } + @PostMapping(value = "/import") + @ApiOperation("导入点位") + @AnonymousAccess + @Transactional + public ResponseEntity importPoint(@RequestParam("file") MultipartFile multipartFile,@RequestParam("tableName") String tableNames) { + /*通过权限匹配对应的数据库表*/ + String tableName = getTableName(tableNames); + FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize()); + String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename()); + String type = FileUtil.getFileType(suffix); + + File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator); + +// Dept dept = UserUtils.getDept(); + // 编码、描述、库区、存储类型 + ExcelReader reader = ExcelUtil.getReader(file); + int edit_len = 0; + int new_len = 0; + int i = 0; + StringBuffer sqlBf=new StringBuffer(); + /* 获取导入模板*/ + List excelConfigList=excelConfigRepository.findByTableName(tableName); + List> readAll = reader.readAll(); + for (i = 0; i < readAll.size(); i++) { + ExcelConfig editEI=null;//0 新增 1 修改 + StringBuffer columns=new StringBuffer();/* 字段集*/ + StringBuffer values=new StringBuffer();/* 数据集*/ + StringBuffer editValues=new StringBuffer();/* 修改*/ + + Map readMap=readAll.get(i); + for (int j = 0; j < excelConfigList.size(); j++) { + ExcelConfig EI=excelConfigList.get(j); + /* 唯一判定*/ + if (EI.getOnlyFlag()==true){ + if (readMap.get(EI.getExcelColumnName())==null){ + throw new BadRequestException(EI.getExcelColumnName()+"本字段不能为空"); + }else { + Object value=readMap.get(EI.getExcelColumnName()); + + String hql ="select * from "+tableName+" where "+EI.getTableColumnName()+"="+ EI.getFormat(value,EI.getTableColumnType()); +// System.out.println(hql); + Query query = entityManager.createNativeQuery(hql); + List qL= query.getResultList(); + if (qL.size()<1){ + //新增 + }else { + //修改 + editEI=EI; + } + } + } + + if (readMap.get(EI.getExcelColumnName())!=null||(EI.getDefaultValue()!=null&&EI.getDefaultValue().length()>0)){ + /* 获取导入值*/ + Object value; + if (readMap.get(EI.getExcelColumnName())!=null){ + value = readMap.get(EI.getExcelColumnName()); + }else { + value=EI.getDefaultValue(); + } + /*添加到sql字段集*/ + if (EI.getGlbType()==1){ + /* 外键关联*/ + ExcelConfigKey excelConfigKey=EI.getEcKey(); + if (excelConfigKey!=null){ + /*获取外键id*/ + String hql ="select "+excelConfigKey.getKeyName()+" from "+excelConfigKey.getTableName()+" where "+excelConfigKey.getTableColumnName()+"="+ EI.getFormat(value,excelConfigKey.getTableColumnType()); +// System.out.println(hql); + Query query = entityManager.createNativeQuery(hql); + List qL= query.getResultList(); + if (qL.size()>0){ + value=qL.get(0); + }else { + value=null; + } + } + }else + if (EI.getGlbType()==2){ + /*关联字典*/ + String hql =" select dd.`value` from sys_dict_detail dd " + +" left join sys_dict d on dd.dict_id=d.dict_id " + +" where d.`name`='"+EI.getDictName()+"' and dd.label='"+value+"' "; + Query query = entityManager.createNativeQuery(hql); + List qL= query.getResultList(); + if (qL.size()<1){ + throw new BadRequestException("未找到字典:"+EI.getExcelColumnName()+","+value); + } + value=qL.get(0); + } + if (columns.toString().length() < 1) { + columns.append(EI.getTableColumnName()); + } else { + columns.append("," + EI.getTableColumnName()); + } + if (values.toString().length() < 1) { + values.append(EI.getFormat(value,EI.getTableColumnType())); + } else { + values.append("," + EI.getFormat(value,EI.getTableColumnType())); + } + } + } + String sql; + if (editEI==null){ + sql = "INSERT INTO "+tableName+" ("+columns+") VALUES ("+values+")"+";"; + }else { + String[] columnArr=columns.toString().split(","); + String[] valuesArr=values.toString().split(","); + for (int j = 0; j < columnArr.length; j++) { + if (editValues.length()<1){ + editValues.append(columnArr[j]+"="+valuesArr[j]); + }else { + editValues.append(","+columnArr[j]+"="+valuesArr[j]); + } + } + sql = "UPDATE "+tableName+" SET "+editValues+" WHERE "+editEI.getTableColumnName()+"="+editEI.getFormat(readMap.get(editEI.getExcelColumnName()),editEI.getTableColumnType())+";"; + } + sqlBf.append(sql); + System.out.println(sql); + entityManager.createNativeQuery(sql) + .executeUpdate(); + } + /*System.out.println(sqlBf); + *//* 执行sql语句*//* + entityManager.createNativeQuery(sqlBf.toString()) + .executeUpdate();*/ + ApiError apiError = ApiError.errorJosn(HttpStatus.OK.value(), "导入成功:" + (i) + "行 新增(" + new_len + ")修改(" + edit_len + ")"); + return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus())); + } + + private String getTableName(String tableNames) { + String tableName=""; + String[] tableNameList= tableNames.split(","); + StringBuilder tableNameSql = new StringBuilder(); + tableNameSql.append("("); + for (int i = 0; i < tableNameList.length; i++) { + tableNameSql.append("'").append(tableNameList[i]).append("'"); + if (i < tableNameList.length - 1) { + tableNameSql.append(","); + } + } + tableNameSql.append(")"); + + String hql =" select dd.`value` from sys_dict_detail dd " + +" left join sys_dict d on dd.dict_id=d.dict_id " + +" where d.`name`='per_table_config' and dd.label in "+tableNameSql+" "; + Query query = entityManager.createNativeQuery(hql); + List qL= query.getResultList(); + if (qL.size()<1){ + throw new BadRequestException("未匹配到权限表"); + }else { + tableName=qL.get(0)+""; + } + return tableName; + } +} \ No newline at end of file diff --git a/youchain-generator/src/main/java/com/youchain/rest/GeneratorController.java b/youchain-generator/src/main/java/com/youchain/rest/GeneratorController.java index 0f025c6..bc04de3 100644 --- a/youchain-generator/src/main/java/com/youchain/rest/GeneratorController.java +++ b/youchain-generator/src/main/java/com/youchain/rest/GeneratorController.java @@ -15,6 +15,7 @@ */ package com.youchain.rest; +import com.youchain.annotation.AnonymousAccess; import com.youchain.exception.BadRequestException; import com.youchain.utils.PageUtil; import io.swagger.annotations.Api; @@ -61,6 +62,13 @@ public class GeneratorController { int[] startEnd = PageUtil.transToStartEnd(page, size); return new ResponseEntity<>(generatorService.getTables(name,startEnd), HttpStatus.OK); } + @ApiOperation("查询数据库所有表") + @GetMapping(value = "/tablesList") + @AnonymousAccess + public ResponseEntity tableList(){ + int[] startEnd = PageUtil.transToStartEnd(0, 1000); + return new ResponseEntity<>(generatorService.getTables(null,startEnd), HttpStatus.OK); + } @ApiOperation("查询字段数据") @GetMapping(value = "/columns") @@ -68,7 +76,6 @@ public class GeneratorController { List columnInfos = generatorService.getColumns(tableName); return new ResponseEntity<>(PageUtil.toPage(columnInfos,columnInfos.size()), HttpStatus.OK); } - @ApiOperation("保存字段数据") @PutMapping public ResponseEntity saveColumn(@RequestBody List columnInfos){ diff --git a/youchain-generator/src/main/java/com/youchain/service/ExcelConfigService.java b/youchain-generator/src/main/java/com/youchain/service/ExcelConfigService.java new file mode 100644 index 0000000..5cece7b --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/service/ExcelConfigService.java @@ -0,0 +1,83 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.service; + +import com.youchain.domain.ExcelConfig; +import com.youchain.service.dto.ExcelConfigDto; +import com.youchain.service.dto.ExcelConfigQueryCriteria; +import org.springframework.data.domain.Pageable; +import java.util.Map; +import java.util.List; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; + +/** +* @website https://eladmin.vip +* @description 服务接口 +* @author baobl +* @date 2024-09-14 +**/ +public interface ExcelConfigService { + + /** + * 查询数据分页 + * @param criteria 条件 + * @param pageable 分页参数 + * @return Map + */ + Map queryAll(ExcelConfigQueryCriteria criteria, Pageable pageable); + + /** + * 查询所有数据不分页 + * @param criteria 条件参数 + * @return List + */ + List queryAll(ExcelConfigQueryCriteria criteria); + + /** + * 根据ID查询 + * @param id ID + * @return ExcelConfigDto + */ + ExcelConfigDto findById(Long id); + + /** + * 创建 + * @param resources / + * @return ExcelConfigDto + */ + ExcelConfigDto create(ExcelConfigDto resources); + + /** + * 编辑 + * @param resources / + */ + void update(ExcelConfig resources); + + /** + * 多选删除 + * @param ids / + */ + void deleteAll(Long[] ids); + + /** + * 导出数据 + * @param all 待导出的数据 + * @param response / + * @throws Exception / + */ + void download(List all, HttpServletResponse response) throws Exception; +} \ No newline at end of file diff --git a/youchain-generator/src/main/java/com/youchain/service/dto/ExcelConfigDto.java b/youchain-generator/src/main/java/com/youchain/service/dto/ExcelConfigDto.java new file mode 100644 index 0000000..75ebfbb --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/service/dto/ExcelConfigDto.java @@ -0,0 +1,65 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.service.dto; + +import com.youchain.domain.ExcelConfigKey; +import lombok.Data; +import java.sql.Timestamp; +import java.io.Serializable; + +/** +* @website https://eladmin.vip +* @description / +* @author baobl +* @date 2024-09-14 +**/ +@Data +public class ExcelConfigDto implements Serializable { + + /** 主键 */ + private Long id; + + /** table表名 */ + private String tableName; + + /** Excel列名 */ + private String excelColumnName; + + /** table字段名 */ + private String tableColumnName; + + /** table字段类型 */ + private String tableColumnType; + + /** 关联表类型 */ + private Integer glbType=0; + /** 唯一标识 */ + private Boolean onlyFlag=false; + + /** 默认值 */ + private String defaultValue; + /** 关联字典value */ + private String dictName; + /** 关联外键表名 */ + private String keyTableName; + /** 表列名 */ + private String keyColumnName; + /** 字段类型 */ + private String keyColumnType; + /** 字段映射 */ + private String keyName; + private ExcelConfigKey ecKey; +} \ No newline at end of file diff --git a/youchain-generator/src/main/java/com/youchain/service/dto/ExcelConfigQueryCriteria.java b/youchain-generator/src/main/java/com/youchain/service/dto/ExcelConfigQueryCriteria.java new file mode 100644 index 0000000..8cf074b --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/service/dto/ExcelConfigQueryCriteria.java @@ -0,0 +1,31 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.service.dto; + +import lombok.Data; +import java.util.List; +import com.youchain.annotation.Query; + +/** +* @website https://eladmin.vip +* @author baobl +* @date 2024-09-14 +**/ +@Data +public class ExcelConfigQueryCriteria{ + @Query(type = Query.Type.EQUAL) + private String tableName; +} \ No newline at end of file diff --git a/youchain-generator/src/main/java/com/youchain/service/impl/ExcelConfigServiceImpl.java b/youchain-generator/src/main/java/com/youchain/service/impl/ExcelConfigServiceImpl.java new file mode 100644 index 0000000..70936ad --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/service/impl/ExcelConfigServiceImpl.java @@ -0,0 +1,146 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.service.impl; + +import com.youchain.domain.ExcelConfig; +import com.youchain.domain.ExcelConfigKey; +import com.youchain.repository.ExcelConfigKeyRepository; +import com.youchain.utils.FileUtil; +import com.youchain.utils.PageUtil; +import com.youchain.utils.QueryHelp; +import com.youchain.utils.ValidationUtil; +import lombok.RequiredArgsConstructor; +import com.youchain.repository.ExcelConfigRepository; +import com.youchain.service.ExcelConfigService; +import com.youchain.service.dto.ExcelConfigDto; +import com.youchain.service.dto.ExcelConfigQueryCriteria; +import com.youchain.service.mapstruct.ExcelConfigMapper; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import java.util.List; +import java.util.Map; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.LinkedHashMap; + +/** +* @website https://eladmin.vip +* @description 服务实现 +* @author baobl +* @date 2024-09-14 +**/ +@Service +@RequiredArgsConstructor +public class ExcelConfigServiceImpl implements ExcelConfigService { + + private final ExcelConfigRepository excelConfigRepository; + private final ExcelConfigKeyRepository excelConfigKeyRepository; + private final ExcelConfigMapper excelConfigMapper; + + @Override + public Map queryAll(ExcelConfigQueryCriteria criteria, Pageable pageable){ + Page page = excelConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + return PageUtil.toPage(page.map(excelConfigMapper::toDto)); + } + + @Override + public List queryAll(ExcelConfigQueryCriteria criteria){ + return excelConfigMapper.toDto(excelConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + } + + @Override + @Transactional + public ExcelConfigDto findById(Long id) { + ExcelConfig excelConfig = excelConfigRepository.findById(id).orElseGet(ExcelConfig::new); + ValidationUtil.isNull(excelConfig.getId(),"ExcelConfig","id",id); + return excelConfigMapper.toDto(excelConfig); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public ExcelConfigDto create(ExcelConfigDto resources) { + ExcelConfig excelConfig=new ExcelConfig(); + if (resources.getId()!=null){ + excelConfig=excelConfigRepository.getById(resources.getId()); + } + /*List excelConfigList=excelConfigRepository.findByTableNameAndTableColumnName(resources.getTableName(),resources.getTableColumnName()); + if (excelConfigList.size()>0){ + excelConfig=excelConfigList.get(0); + }*/ + excelConfig.setDictName(resources.getDictName()); + excelConfig.setTableName(resources.getTableName()); + excelConfig.setTableColumnName(resources.getTableColumnName()); + excelConfig.setTableColumnType(resources.getTableColumnType()); + excelConfig.setExcelColumnName(resources.getExcelColumnName()); + excelConfig.setGlbType(resources.getGlbType()); + excelConfig.setOnlyFlag(resources.getOnlyFlag()); + excelConfig.setDefaultValue(resources.getDefaultValue()); + excelConfigRepository.save(excelConfig); + if (excelConfig.getGlbType()!=null&&excelConfig.getGlbType()==1){ + /* 外键控制*/ + ExcelConfigKey key=excelConfig.getEcKey(); + if (key==null){ + key=new ExcelConfigKey(); + } + key.setTableName(resources.getEcKey().getTableName()); + key.setTableColumnName(resources.getEcKey().getTableColumnName()); + key.setTableColumnType(resources.getEcKey().getTableColumnType()); + key.setKeyName(resources.getEcKey().getKeyName()); + excelConfigKeyRepository.save(key); + excelConfig.setEcKey(key); + excelConfigRepository.save(excelConfig); + } + return null; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void update(ExcelConfig resources) { + ExcelConfig excelConfig = excelConfigRepository.findById(resources.getId()).orElseGet(ExcelConfig::new); + ValidationUtil.isNull( excelConfig.getId(),"ExcelConfig","id",resources.getId()); + excelConfig.copy(resources); + excelConfigRepository.save(excelConfig); + } + + @Override + public void deleteAll(Long[] ids) { + for (Long id : ids) { + ExcelConfig excelConfig=excelConfigRepository.getById(id); + ExcelConfigKey excelConfigKey=excelConfig.getEcKey(); + if (excelConfigKey!=null) { + excelConfigKeyRepository.deleteById(excelConfigKey.getId()); + } + excelConfigRepository.deleteById(id); + } + } + + @Override + public void download(List all, HttpServletResponse response) throws Exception { + List> list = new ArrayList<>(); + for (ExcelConfigDto excelConfig : all) { + Map map = new LinkedHashMap<>(); + map.put("table表名", excelConfig.getTableName()); + map.put("Excel列名", excelConfig.getExcelColumnName()); + map.put("table字段名", excelConfig.getTableColumnName()); + map.put("table字段类型", excelConfig.getTableColumnType()); + list.add(map); + } + FileUtil.downloadExcel(list, response); + } +} \ No newline at end of file diff --git a/youchain-generator/src/main/java/com/youchain/service/mapstruct/ExcelConfigMapper.java b/youchain-generator/src/main/java/com/youchain/service/mapstruct/ExcelConfigMapper.java new file mode 100644 index 0000000..f6aa2e4 --- /dev/null +++ b/youchain-generator/src/main/java/com/youchain/service/mapstruct/ExcelConfigMapper.java @@ -0,0 +1,32 @@ +/* +* Copyright 2019-2020 Zheng Jie +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.youchain.service.mapstruct; + +import com.youchain.base.BaseMapper; +import com.youchain.domain.ExcelConfig; +import com.youchain.service.dto.ExcelConfigDto; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; + +/** +* @website https://eladmin.vip +* @author baobl +* @date 2024-09-14 +**/ +@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface ExcelConfigMapper extends BaseMapper { + +} \ No newline at end of file diff --git a/youchain-system/src/main/java/com/youchain/basicdata/rest/PointController.java b/youchain-system/src/main/java/com/youchain/basicdata/rest/PointController.java index 402a692..cbd5de3 100644 --- a/youchain-system/src/main/java/com/youchain/basicdata/rest/PointController.java +++ b/youchain-system/src/main/java/com/youchain/basicdata/rest/PointController.java @@ -21,7 +21,6 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.youchain.annotation.AnonymousAccess; import com.youchain.annotation.Log; -import com.youchain.basicdata.domain.Area; import com.youchain.basicdata.domain.Item; import com.youchain.basicdata.domain.Point; import com.youchain.basicdata.repository.ItemRepository; @@ -35,7 +34,6 @@ import com.youchain.businessdata.service.dto.InventoryQueryCriteria; import com.youchain.config.FileProperties; import com.youchain.exception.handler.ApiError; import com.youchain.modules.system.domain.Dept; -import com.youchain.utils.BaseStatus; import com.youchain.utils.FileUtil; import com.youchain.utils.UserUtils; import lombok.extern.slf4j.Slf4j; @@ -50,16 +48,12 @@ import io.swagger.annotations.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; -import static org.springframework.http.HttpStatus.BAD_REQUEST; - /** * @author liuxue * @website https://eladmin.vip @@ -107,7 +101,7 @@ public class PointController { int new_len = 0; List> readAll = reader.readAll(); for (i = 0; i < readAll.size(); i++) { - String code = readAll.get(i).get("编码").toString().trim(); + /*String code = readAll.get(i).get("编码").toString().trim(); String types = readAll.get(i).get("存储类型").toString().trim(); String areaCode = readAll.get(i).get("库区").toString().trim(); String ccTYPE = readAll.get(i).get("描述") == null ? "" : readAll.get(i).get("描述").toString().trim(); @@ -142,7 +136,8 @@ public class PointController { pointService.create(point); }else { pointService.update(point); - } + }*/ +// ExcelImportParam } ApiError apiError = ApiError.errorJosn(HttpStatus.OK.value(), "导入成功:" + (i) + "行 新增(" + new_len + ")修改(" + edit_len + ")"); return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus())); diff --git a/youchain-system/src/main/java/com/youchain/modules/system/domain/DictDetail.java b/youchain-system/src/main/java/com/youchain/modules/system/domain/DictDetail.java index 4fa0b1a..b296ecd 100644 --- a/youchain-system/src/main/java/com/youchain/modules/system/domain/DictDetail.java +++ b/youchain-system/src/main/java/com/youchain/modules/system/domain/DictDetail.java @@ -47,6 +47,8 @@ public class DictDetail extends BaseEntity implements Serializable { @ApiModelProperty(value = "字典标签") private String label; + @ApiModelProperty(value = "描述") + private String description; @ApiModelProperty(value = "字典值") private String value; diff --git a/youchain-system/src/main/java/com/youchain/modules/system/rest/DictDetailController.java b/youchain-system/src/main/java/com/youchain/modules/system/rest/DictDetailController.java index bf6f792..1d936dd 100644 --- a/youchain-system/src/main/java/com/youchain/modules/system/rest/DictDetailController.java +++ b/youchain-system/src/main/java/com/youchain/modules/system/rest/DictDetailController.java @@ -15,6 +15,8 @@ */ package com.youchain.modules.system.rest; +import com.youchain.modules.system.domain.Dict; +import com.youchain.modules.system.repository.DictRepository; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; @@ -47,6 +49,7 @@ import java.util.Map; public class DictDetailController { private final DictDetailService dictDetailService; + private final DictRepository dictRepository; private static final String ENTITY_NAME = "dictDetail"; @ApiOperation("查询字典详情") @@ -75,6 +78,12 @@ public class DictDetailController { if (resources.getId() != null) { throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID"); } + if (resources.getDict()!=null&&resources.getDict().getName()!=null){ + Dict dict=dictRepository.getDictDescription(resources.getDict().getName()); + if (dict!=null){ + resources.setDict(dict); + } + } dictDetailService.create(resources); return new ResponseEntity<>(HttpStatus.CREATED); } diff --git a/youchain-system/src/main/java/com/youchain/modules/system/service/dto/DictDetailDto.java b/youchain-system/src/main/java/com/youchain/modules/system/service/dto/DictDetailDto.java index b7f92c1..e638432 100644 --- a/youchain-system/src/main/java/com/youchain/modules/system/service/dto/DictDetailDto.java +++ b/youchain-system/src/main/java/com/youchain/modules/system/service/dto/DictDetailDto.java @@ -35,6 +35,7 @@ public class DictDetailDto extends BaseDTO implements Serializable { private String label; private String value; + private String description; private Integer dictSort; } \ No newline at end of file diff --git a/youchain-system/src/main/java/com/youchain/modules/system/service/impl/DictDetailServiceImpl.java b/youchain-system/src/main/java/com/youchain/modules/system/service/impl/DictDetailServiceImpl.java index 902355a..f74c153 100644 --- a/youchain-system/src/main/java/com/youchain/modules/system/service/impl/DictDetailServiceImpl.java +++ b/youchain-system/src/main/java/com/youchain/modules/system/service/impl/DictDetailServiceImpl.java @@ -114,7 +114,8 @@ public class DictDetailServiceImpl implements DictDetailService { } public void delCaches(DictDetail dictDetail){ - Dict dict = dictRepository.findById(dictDetail.getDict().getId()).orElseGet(Dict::new); + Dict dict; + dict = dictRepository.findById(dictDetail.getDict().getId()).orElseGet(Dict::new); redisUtils.del(CacheKey.DICT_NAME + dict.getName()); } } \ No newline at end of file