package cn.huge.module.syncgrid.service; import cn.huge.base.common.constant.GzRegionBaseEnum; import cn.huge.base.common.exception.ServiceException; import cn.huge.base.common.utils.DateUtils; import cn.huge.base.common.utils.IdUtils; import cn.huge.base.common.utils.ObjectUtils; import cn.huge.module.client.api.impl.UtilsClientImpl; import cn.huge.module.constant.BaseConsts; import cn.huge.module.ctuser.domain.po.CtAccount; import cn.huge.module.ctuser.domain.po.CtUnit; import cn.huge.module.ctuser.domain.po.CtUser; import cn.huge.module.ctuser.domain.po.CtUserole; import cn.huge.module.ctuser.service.CtUnitService; import cn.huge.module.cust.constant.UserBaseConsts; import cn.huge.module.syncgrid.dao.mapper.ThGridUnitMapper; import cn.huge.module.syncgrid.domain.po.ThGridUnit; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.DigestUtils; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.annotation.PostConstruct; import java.io.InputStream; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; /** * @title: 第三方网格单位信息业务逻辑处理 * @Description 第三方网格单位信息业务逻辑处理 * @company hugeinfo * @author liyj * @Time 2024-12-18 11:40:51 * @version 1.0.0 */ @Slf4j @Service @Transactional(rollbackFor = Exception.class) public class ThGridUnitService extends ServiceImpl{ @Autowired private ThGridUnitMapper mapper; @Autowired private UtilsClientImpl utilsClient; @Autowired private CtUnitService ctUnitService; /** * 更新对象 * @param entity 对象 */ public void updateThGridUnit(ThGridUnit entity){ try{ mapper.updateThGridUnit(entity); }catch (Exception e){ log.error("[ThGridUnitService.updateThGridUnit]调用失败,异常信息:"+e, e); throw new ServiceException("ThGridUnitService.updateThGridUnit", e); } } /** * 条件更新对象 * @param entity 对象 * @param terms 条件 */ public void updateThGridUnitTerms(ThGridUnit entity, Map terms){ try{ mapper.updateThGridUnitTerms(entity, terms); }catch (Exception e){ log.error("[ThGridUnitService.updateThGridUnitTerms]调用失败,异常信息:"+e, e); throw new ServiceException("ThGridUnitService.updateThGridUnitTerms", e); } } /** * 根据编号物理删除 * @param id 查询条件集合 */ public void deleteThGridUnit(String id){ try{ mapper.deleteThGridUnit(id); }catch (Exception e){ log.error("[ThGridUnitService.deleteThGridUnit]调用失败,异常信息:"+e, e); throw new ServiceException("ThGridUnitService.deleteThGridUnit", e); } } /** * 按条件查询 * @param terms 条件 * @return List */ public List listTerms(Map terms){ return mapper.listTerms(terms); } /** * 按条件统计 * @param terms 条件 * @return long */ public long countTerms(Map terms){ return mapper.countTerms(terms); } /** * 按条件分页查询 * @param page 分页对象 * @param terms 条件 * @return Page */ public Page pageQuery(PageRequest page, Map terms){ long total = mapper.countTerms(terms); List content = mapper.pageTerms(page, terms); return new PageImpl(content, page, total); } /** * 获取单元格数据 * @param sheet * @param r * @param c * @return */ public String transform(Sheet sheet, int r, int c) { if (ObjectUtils.isNotEmpty(sheet)) { Cell cell = sheet.getRow(r).getCell(c); if (ObjectUtils.isNotEmpty(cell)) { cell.setCellType(CellType.STRING); String val = cell.getStringCellValue(); return val.replace(" ", ""); } else { return null; } } else { return null; } } /** * 通过Excel操作-找出对应映射的单位信息 * @param request */ public void inputByExcel241218(MultipartHttpServletRequest request){ try{ Iterator itr = request.getFileNames(); while (itr.hasNext()) { MultipartFile file = request.getFile(itr.next()); String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); if (".xls".equals(fileSuffix) || ".xlsx".equals(fileSuffix) || "xls".equals(fileSuffix) || "xlsx".equals(fileSuffix)) { InputStream inputStream = file.getInputStream(); Workbook wb = null; //当excel是2003时 if (".xls".equals(fileSuffix)) { wb = new HSSFWorkbook(inputStream); } else { wb = new XSSFWorkbook(inputStream); } //得到第一个shell Sheet sheet = wb.getSheetAt(0); //得到Excel的行数 int totalRows = sheet.getPhysicalNumberOfRows(); //人员信息 Date nowDate = DateUtils.getNowDate(); for (int r = 1; r < totalRows; r++) { try { // 获取数据 String thirdUnitId = this.transform(sheet, r, 0); String thirdGridCode = this.transform(sheet, r, 1); String thirdUnitCode = this.transform(sheet, r, 2); String thirdUnitName = this.transform(sheet, r, 3); // 新增第三方网格单位 ThGridUnit thGridUnit = new ThGridUnit(); thGridUnit.setThirdUnitId(thirdUnitId); thGridUnit.setThirdGridCode(thirdGridCode); thGridUnit.setThirdUnitCode(thirdUnitCode); thGridUnit.setThirdUnitName(thirdUnitName); thGridUnit.setCreateTime(nowDate); thGridUnit.setUpdateTime(nowDate); thGridUnit.setThirdUnitName(thirdUnitName); // 查询对应的平台单位 QueryWrapper ctUnitQueryWrapper = new QueryWrapper<>(); ctUnitQueryWrapper.eq("area", GzRegionBaseEnum.AREA_7.getIndex()); int count = thirdGridCode.split("-").length; // 判断是区级单位 if (count == 1){ List ctUnitList = null; ctUnitQueryWrapper.eq("unit_grade", UserBaseConsts.UNIT_GRADE_2); if (thirdUnitName.indexOf("综治中心") != -1){ ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101); ctUnitList = ctUnitService.list(ctUnitQueryWrapper); }else if ("白云区".equals(thirdUnitName)){ ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101); ctUnitList = ctUnitService.list(ctUnitQueryWrapper); }else { String newUnitName = thirdUnitName.replace("白云区", ""); ctUnitQueryWrapper.like("unit_name", newUnitName); ctUnitList = ctUnitService.list(ctUnitQueryWrapper); } // 判断是否匹配 if (CollectionUtils.isNotEmpty(ctUnitList)){ thGridUnit.setDyhUnitId(ctUnitList.get(0).getId()); }else { thGridUnit.setUnitCode("区级-未匹配"); } }else if (count == 2){ // 判断是镇街单位 List ctUnitList = null; ctUnitQueryWrapper.eq("unit_grade", UserBaseConsts.UNIT_GRADE_3).eq("road", thirdGridCode); if (thirdUnitName.indexOf("综治中心") != -1){ ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101); ctUnitList = ctUnitService.list(ctUnitQueryWrapper); }else if (thirdUnitName.indexOf("同和街综合治理办公室") != -1){ ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101); ctUnitList = ctUnitService.list(ctUnitQueryWrapper); }else if ("三元里街".equals(thirdUnitName) || "景泰街".equals(thirdUnitName) || "同德街".equals(thirdUnitName) || "黄石街".equals(thirdUnitName) || "棠景街".equals(thirdUnitName) || "新市街".equals(thirdUnitName) || "京溪街".equals(thirdUnitName) || "嘉禾街".equals(thirdUnitName) || "均禾街".equals(thirdUnitName) || "石井街".equals(thirdUnitName) || "金沙街".equals(thirdUnitName) || "云城街".equals(thirdUnitName) || "鹤龙街".equals(thirdUnitName) || "白云湖街".equals(thirdUnitName) || "石门街".equals(thirdUnitName) || "龙归街".equals(thirdUnitName) || "大源街".equals(thirdUnitName) || "人和镇".equals(thirdUnitName) || "太和镇".equals(thirdUnitName) || "钟落潭镇".equals(thirdUnitName) || "永平街".equals(thirdUnitName) || "同和街".equals(thirdUnitName) || "江高镇".equals(thirdUnitName) || "鹤龙街".equals(thirdUnitName)){ ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101); ctUnitList = ctUnitService.list(ctUnitQueryWrapper); }else { String newUnitName = thirdUnitName.replace("三元里街", "").replace("三元里", "") .replace("景泰街", "").replace("景泰", "") .replace("同德街", "").replace("同德", "") .replace("黄石街", "").replace("黄石", "") .replace("棠景街", "").replace("棠景", "") .replace("新市街", "").replace("新市", "") .replace("京溪街", "").replace("京溪", "") .replace("嘉禾街", "").replace("嘉禾", "") .replace("均禾街", "").replace("均禾", "") .replace("石井街", "").replace("石井", "") .replace("金沙街", "").replace("金沙", "") .replace("云城街", "").replace("云城", "") .replace("鹤龙街", "").replace("鹤龙", "") .replace("白云湖街", "").replace("白云湖", "") .replace("石门街", "").replace("石门", "") .replace("龙归街", "").replace("龙归", "") .replace("大源街", "").replace("大源", "") .replace("人和镇", "") .replace("太和镇", "") .replace("钟落潭镇", "") .replace("永平街", "").replace("永平", "") .replace("同和街", "").replace("同和", "") .replace("江高镇", "") .replace("鹤龙街", "").replace("鹤龙", ""); ctUnitQueryWrapper.like("unit_name", newUnitName); ctUnitList = ctUnitService.list(ctUnitQueryWrapper); } // 判断是否匹配 if (CollectionUtils.isNotEmpty(ctUnitList)){ thGridUnit.setDyhUnitId(ctUnitList.get(0).getId()); }else { thGridUnit.setUnitCode("镇街级-未匹配"); } }else if (count == 3){ // 判断是村级单位 ctUnitQueryWrapper.eq("unit_grade", UserBaseConsts.UNIT_GRADE_4).eq("village", thirdGridCode); // String newUnitName = thirdUnitName.replace("三元里街", "") // .replace("景泰街", "") // .replace("同德街", "") // .replace("黄石街", "") // .replace("棠景街", "") // .replace("新市街", "") // .replace("京溪街", "") // .replace("嘉禾街", "") // .replace("均禾街", "") // .replace("石井街", "") // .replace("金沙街", "") // .replace("云城街", "") // .replace("鹤龙街", "") // .replace("白云湖街", "") // .replace("石门街", "") // .replace("龙归街", "") // .replace("大源街", "") // .replace("人和镇", "") // .replace("太和镇", "") // .replace("钟落潭镇", "") // .replace("永平街", "") // .replace("同和街", "") // .replace("江高镇", "") // .replace("鹤龙街", ""); // ctUnitQueryWrapper.like("unit_name", newUnitName); List ctUnitList = ctUnitService.list(ctUnitQueryWrapper); // 判断是否匹配 if (CollectionUtils.isNotEmpty(ctUnitList)){ thGridUnit.setDyhUnitId(ctUnitList.get(0).getId()); }else { thGridUnit.setUnitCode("村居级-未匹配"); } }else { thGridUnit.setUnitCode("网格机构"); } this.save(thGridUnit); } catch (Exception e) { log.error(e.getMessage(), e); continue; } } } } }catch (Exception e){ log.error("[CtUnitService.inputByExcel241124]调用失败,异常信息:"+e, e); throw new ServiceException("CtUnitService.inputByExcel241124", e); } } /** * 通过Excel操作-更新映射表映射关系和新增对应单位信息 * @param request */ public void inputByExcel241219(MultipartHttpServletRequest request){ try{ Iterator itr = request.getFileNames(); while (itr.hasNext()) { MultipartFile file = request.getFile(itr.next()); String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); if (".xls".equals(fileSuffix) || ".xlsx".equals(fileSuffix) || "xls".equals(fileSuffix) || "xlsx".equals(fileSuffix)) { InputStream inputStream = file.getInputStream(); Workbook wb = null; //当excel是2003时 if (".xls".equals(fileSuffix)) { wb = new HSSFWorkbook(inputStream); } else { wb = new XSSFWorkbook(inputStream); } //得到第一个shell Sheet sheet = wb.getSheetAt(0); //得到Excel的行数 int totalRows = sheet.getPhysicalNumberOfRows(); //人员信息 Date nowDate = DateUtils.getNowDate(); for (int r = 1; r < totalRows; r++) { try { // 获取数据 String thirdUnitId = this.transform(sheet, r, 0); String thirdGridCode = this.transform(sheet, r, 1); String thirdUnitCode = this.transform(sheet, r, 2); String thirdUnitName = this.transform(sheet, r, 3); String dyhUnitId = this.transform(sheet, r, 4); // 新增第三方网格单位 UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.set("dyh_unit_id", dyhUnitId).set("unit_code", null) .eq("third_unit_id", thirdUnitId); this.update(updateWrapper); } catch (Exception e) { log.error(e.getMessage(), e); continue; } } } } }catch (Exception e){ log.error("[CtUnitService.inputByExcel241124]调用失败,异常信息:"+e, e); throw new ServiceException("CtUnitService.inputByExcel241124", e); } } /** * 通过Excel操作-更新映射表映射关系和新增对应单位信息 * @param request */ public void inputByExcel250121(MultipartHttpServletRequest request){ try{ Iterator itr = request.getFileNames(); while (itr.hasNext()) { MultipartFile file = request.getFile(itr.next()); String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); if (".xls".equals(fileSuffix) || ".xlsx".equals(fileSuffix) || "xls".equals(fileSuffix) || "xlsx".equals(fileSuffix)) { InputStream inputStream = file.getInputStream(); Workbook wb = null; //当excel是2003时 if (".xls".equals(fileSuffix)) { wb = new HSSFWorkbook(inputStream); } else { wb = new XSSFWorkbook(inputStream); } //得到第一个shell Sheet sheet = wb.getSheetAt(0); //得到Excel的行数 int totalRows = sheet.getPhysicalNumberOfRows(); //人员信息 Date nowDate = DateUtils.getNowDate(); for (int r = 1; r < totalRows; r++) { try { // 获取数据 String thirdUnitCode = this.transform(sheet, r, 0); String thirdUnitName = this.transform(sheet, r, 1); String dyhUnitId = this.transform(sheet, r, 2); String dyhUnitName = this.transform(sheet, r, 3); // 查询第三方网格单位 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("third_unit_code", thirdUnitCode); ThGridUnit thGridUnit = this.getOne(queryWrapper); if (ObjectUtils.isNotEmpty(thGridUnit)){ thGridUnit.setDyhUnitId(dyhUnitId); thGridUnit.setUnitCode(null); this.updateById(thGridUnit); } } catch (Exception e) { log.error(e.getMessage(), e); continue; } } } } }catch (Exception e){ log.error("[CtUnitService.inputByExcel250121]调用失败,异常信息:"+e, e); throw new ServiceException("CtUnitService.inputByExcel250121", e); } } /** * 手动操作-更新映射表映射关系和新增对应单位信息 */ public void updateDyh250121(){ try{ // 查询第三方网格单位 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("unit_code", "网格机构"); List thGridUnitList = this.list(queryWrapper); for (ThGridUnit thGridUnit : thGridUnitList){ String thirdGridCode = thGridUnit.getThirdGridCode(); String parentCode = thirdGridCode.substring(0, thirdGridCode.length() - 4); QueryWrapper queryWrapper2 = new QueryWrapper<>(); queryWrapper2.eq("third_grid_code", parentCode); List parentThGridUnits = this.list(queryWrapper2); if (CollectionUtils.isNotEmpty(parentThGridUnits)){ thGridUnit.setDyhUnitId(parentThGridUnits.get(0).getDyhUnitId()); thGridUnit.setUnitCode(null); this.updateById(thGridUnit); } } }catch (Exception e){ log.error("[CtUnitService.inputByExcel250121]调用失败,异常信息:"+e, e); throw new ServiceException("CtUnitService.inputByExcel250121", e); } } }