diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/DistributedLock.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/DistributedLock.java new file mode 100644 index 0000000000..298a0b238f --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/DistributedLock.java @@ -0,0 +1,79 @@ +package com.epmet.commons.tools.distributedlock; + +import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.redis.RedisKeys; +import org.apache.commons.lang3.StringUtils; +import org.redisson.api.RLock; +import org.redisson.api.RedissonClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @Author zxc + * @DateTime 2020/10/28 9:05 上午 + */ +@Component +public class DistributedLock { + + @Autowired + private RedissonClient redissonClient; + + /** + * @Description 抢锁🔒 每个锁持有十分钟 + * @Param name + * @author zxc + * @date 2020/10/28 2:52 下午 + */ + public RLock getLock(String name){ + RLock lock = null; + if (StringUtils.isNotBlank(name)) { + lock = redissonClient.getLock(name); + // 持续时间为 -1 时,直到其明确释放锁才会释放【宕机就完蛋了】, + lock.lock(10,TimeUnit.MINUTES); + } + return lock; + } + + /** + * @Description + * @Param name 锁名 + * @Param leaseTime 持锁时间 单位:min + * @Param waitTime 获取锁最长等待时间 单位:min + * @author zxc + * @date 2020/10/29 9:04 上午 + */ + public RLock getLock(String name,Long leaseTime,Long waitTime){ + RLock lock = null; + if (StringUtils.isNotBlank(name) && leaseTime > 0 && waitTime > 0 ){ + lock = redissonClient.getLock(name); + Boolean lockStatus; + try { + lockStatus = lock.tryLock(waitTime,leaseTime,TimeUnit.MINUTES); + if (!lockStatus){ + throw new RenException("获取锁🔒失败了......"); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + return lock; + } + + /** + * @Description 释放锁🔒 + * @Param rLock + * @author zxc + * @date 2020/10/28 2:52 下午 + */ + public void unLock(RLock rLock){ + if (null != rLock){ + if (rLock.isHeldByCurrentThread()){ + rLock.unlock(); + } + } + } + +} diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/LockConstants.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/LockConstants.java new file mode 100644 index 0000000000..3174a43a8e --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/distributedlock/LockConstants.java @@ -0,0 +1,12 @@ +package com.epmet.commons.tools.distributedlock; + +/** + * @Author zxc + * @DateTime 2020/10/28 9:23 上午 + */ +public interface LockConstants { + + String TEST_LOCK_NAME = "testLock"; + + String STATS_LOCK_NAME = "stats"; +} diff --git a/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/form/BranchCountFormDTO.java b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/form/BranchCountFormDTO.java new file mode 100644 index 0000000000..1d2d9cb77f --- /dev/null +++ b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/form/BranchCountFormDTO.java @@ -0,0 +1,24 @@ +package com.epmet.evaluationindex.screen.dto.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Author zxc + * @DateTime 2020/10/27 11:08 上午 + */ +@Data +public class BranchCountFormDTO implements Serializable { + + private static final long serialVersionUID = -4869326660700557193L; + + public interface BranchCount{} + + /** + * 机关ID + */ + @NotBlank(message = "机关ID不能为空",groups = {BranchCount.class}) + private String agencyId; +} diff --git a/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/AnNingSubAgencyIndexRankResultDTO.java b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/AnNingSubAgencyIndexRankResultDTO.java index c4653c9a71..38df1662f2 100644 --- a/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/AnNingSubAgencyIndexRankResultDTO.java +++ b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/AnNingSubAgencyIndexRankResultDTO.java @@ -1,5 +1,6 @@ package com.epmet.evaluationindex.screen.dto.result; +import com.epmet.commons.tools.constant.NumConstant; import lombok.Data; import java.io.Serializable; @@ -38,6 +39,21 @@ public class AnNingSubAgencyIndexRankResultDTO implements Serializable { */ private Double serviceAbility = 0.0; + /** + * 党建能力权重 + */ + private Double partyWeight; + + /** + * 治理能力权重 + */ + private Double governWeight; + + /** + * 服务能力权重 + */ + private Double serviceWeight; + /** * 组织id或者网格id */ @@ -47,4 +63,10 @@ public class AnNingSubAgencyIndexRankResultDTO implements Serializable { * 组织类型 */ private String orgType = ""; + + public AnNingSubAgencyIndexRankResultDTO() { + this.partyWeight = NumConstant.ZERO_DOT_ZERO; + this.governWeight = NumConstant.ZERO_DOT_ZERO; + this.serviceWeight = NumConstant.ZERO_DOT_ZERO; + } } diff --git a/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/BranchCountResultDTO.java b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/BranchCountResultDTO.java new file mode 100644 index 0000000000..5a2c8cd391 --- /dev/null +++ b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/BranchCountResultDTO.java @@ -0,0 +1,36 @@ +package com.epmet.evaluationindex.screen.dto.result; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +/** + * @Author zxc + * @DateTime 2020/10/27 11:03 上午 + */ +@Data +public class BranchCountResultDTO implements Serializable { + + private static final long serialVersionUID = -5915969126291502360L; + + private String agencyId; + + /** + * 机关名称 + */ + private String name; + + @JsonIgnore + private String level; + + private List partyDistribution; + + public BranchCountResultDTO() { + this.agencyId = ""; + this.name = ""; + this.partyDistribution = new ArrayList<>(); + } +} diff --git a/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/DepartmentNameListResultDTO.java b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/DepartmentNameListResultDTO.java index 94cfb8fc51..033acfa1ed 100644 --- a/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/DepartmentNameListResultDTO.java +++ b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/DepartmentNameListResultDTO.java @@ -2,6 +2,7 @@ package com.epmet.evaluationindex.screen.dto.result; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; import java.io.Serializable; import java.util.List; @@ -12,6 +13,7 @@ import java.util.List; */ @Data @AllArgsConstructor +@NoArgsConstructor public class DepartmentNameListResultDTO implements Serializable { private static final long serialVersionUID = -801407836277197080L; diff --git a/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/SubBranchCountResultDTO.java b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/SubBranchCountResultDTO.java new file mode 100644 index 0000000000..eac362ef84 --- /dev/null +++ b/epmet-module/data-report/data-report-client/src/main/java/com/epmet/evaluationindex/screen/dto/result/SubBranchCountResultDTO.java @@ -0,0 +1,52 @@ +package com.epmet.evaluationindex.screen.dto.result; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; + +import java.io.Serializable; + +/** + * @Author zxc + * @DateTime 2020/10/27 5:22 下午 + */ +@Data +public class SubBranchCountResultDTO implements Serializable { + + private static final long serialVersionUID = -4174988002147169566L; + + private String subId; + + /** + * 机关名称 + */ + private String name; + + /** + * 中心点位 + */ + private String centerMark; + + /** + * 社区下的党支部数 + */ + private Integer totalNum; + + /** + * 坐标区域 + */ + private String areaMarks; + + @JsonIgnore + private String allParentIds; + @JsonIgnore + private String subAgencyId; + + public SubBranchCountResultDTO() { + this.subId = ""; + this.name = ""; + this.centerMark = ""; + this.totalNum = 0; + this.areaMarks = ""; + this.subAgencyId = ""; + } +} diff --git a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/controller/screen/DistributionController.java b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/controller/screen/DistributionController.java index 25bf9e5434..fb9eeb8860 100644 --- a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/controller/screen/DistributionController.java +++ b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/controller/screen/DistributionController.java @@ -39,7 +39,19 @@ public class DistributionController { } /** - * @Description 2、用户分布 【只显示 screen_customer_biz_org 表中存在的orgId的数据】 + * @Description 查询机关下的党支部 + * @Param branchCountFormDTO + * @author zxc + * @date 2020/10/27 11:11 上午 + */ + @PostMapping("branchcount") + public Result branchCount(@RequestBody BranchCountFormDTO branchCountFormDTO){ + ValidatorUtils.validateEntity(branchCountFormDTO, BranchCountFormDTO.BranchCount.class); + return new Result().ok(distributionService.branchCount(branchCountFormDTO)); + } + + /** + * @Description 2、用户分布 【只显示 is_display = 1 的数据】 * @param userFormDTO * @author zxc * @date 2020/8/18 11:10 上午 @@ -51,7 +63,7 @@ public class DistributionController { } /** - * @Description 3、党员分布 【只显示 screen_customer_biz_org 表中存在的orgId的数据】 + * @Description 3、党员分布 【只显示 is_display = 1 的数据】 * @param parymemberFormDTO * @author zxc * @date 2020/8/18 11:20 上午 diff --git a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/dao/evaluationindex/screen/ScreenCustomerAgencyDao.java b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/dao/evaluationindex/screen/ScreenCustomerAgencyDao.java index 7efd4d1853..52ddea4bd0 100644 --- a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/dao/evaluationindex/screen/ScreenCustomerAgencyDao.java +++ b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/dao/evaluationindex/screen/ScreenCustomerAgencyDao.java @@ -104,4 +104,12 @@ public interface ScreenCustomerAgencyDao { * @date 2020/10/23 3:54 下午 */ CompartmentResultDTO getAgencyInfoByAegncyId(@Param("agencyId") String agencyId); + + /** + * @Description 查询组织的下级组织ID + * @Param agencyId + * @author zxc + * @date 2020/10/28 10:33 上午 + */ + List selectSubAgencyId(@Param("agencyId") String agencyId); } \ No newline at end of file diff --git a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/dao/evaluationindex/screen/ScreenCustomerGridDao.java b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/dao/evaluationindex/screen/ScreenCustomerGridDao.java index b6196411f8..a024d60791 100644 --- a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/dao/evaluationindex/screen/ScreenCustomerGridDao.java +++ b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/dao/evaluationindex/screen/ScreenCustomerGridDao.java @@ -48,6 +48,15 @@ public interface ScreenCustomerGridDao { */ List selectBranch(@Param("agencyId")String agencyId); + /** + * @Description 查询机关下的党支部 + * @Param branchCountFormDTO + * @author zxc + * @date 2020/10/27 11:11 上午 + */ + BranchCountResultDTO selectAgencyInfo(@Param("agencyId")String agencyId); + List selectBranchCount(@Param("agencyId")String agencyId); + /** * @Description 查询子级用户分布【网格级别】 * @param parentId diff --git a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/DistributionService.java b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/DistributionService.java index a73d1ccfcd..9f2552329b 100644 --- a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/DistributionService.java +++ b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/DistributionService.java @@ -21,6 +21,14 @@ public interface DistributionService { */ List branch(BranchFormDTO formDTO); + /** + * @Description 查询机关下的党支部 + * @Param branchCountFormDTO + * @author zxc + * @date 2020/10/27 11:11 上午 + */ + BranchCountResultDTO branchCount(BranchCountFormDTO branchCountFormDTO); + /** * @Description 2、用户分布 * @param userFormDTO diff --git a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/impl/AgencyServiceImpl.java b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/impl/AgencyServiceImpl.java index 24a5367d87..3232cf7793 100644 --- a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/impl/AgencyServiceImpl.java +++ b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/impl/AgencyServiceImpl.java @@ -138,12 +138,7 @@ public class AgencyServiceImpl implements AgencyService { }); sub.setChildren(treeResultDTOS); }else { - List subAgency = new ArrayList<>(); - if (sub.getPids().contains(ScreenConstant.COMMA)){ - subAgency = getDepartmentList(sub.getPids() + ScreenConstant.COMMA + sub.getValue()); - }else { - subAgency = getDepartmentList(sub.getPids() + ScreenConstant.COLON + sub.getValue()); - } + List subAgency = getDepartmentList(sub.getPids() + ScreenConstant.COMMA + sub.getValue()); sub.setChildren(subAgency); } }); @@ -176,6 +171,9 @@ public class AgencyServiceImpl implements AgencyService { subAgency = getDepartmentListByBiz(sub.getPids() + ScreenConstant.COMMA + sub.getValue()); }else { subAgency = getDepartmentListByBiz(sub.getPids() + ScreenConstant.COLON + sub.getValue()); + if (null == subAgency){ + subAgency = getDepartmentListByBiz(sub.getPids() + ScreenConstant.COMMA + sub.getValue()); + } } sub.setChildren(subAgency); } diff --git a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/impl/DistributionServiceImpl.java b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/impl/DistributionServiceImpl.java index b65c0fe059..c94e85dc89 100644 --- a/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/impl/DistributionServiceImpl.java +++ b/epmet-module/data-report/data-report-server/src/main/java/com/epmet/datareport/service/evaluationindex/screen/impl/DistributionServiceImpl.java @@ -11,12 +11,16 @@ import com.epmet.datareport.service.evaluationindex.screen.DistributionService; import com.epmet.evaluationindex.screen.constant.ScreenConstant; import com.epmet.evaluationindex.screen.dto.form.*; import com.epmet.evaluationindex.screen.dto.result.*; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * 中央区相关各指标查询 @@ -24,6 +28,7 @@ import java.util.List; * @author yinzuomei@elink-cn.com * @date 2020/8/18 10:19 */ +@Slf4j @Service @DataSource(DataSourceConstant.EVALUATION_INDEX) public class DistributionServiceImpl implements DistributionService { @@ -50,6 +55,45 @@ public class DistributionServiceImpl implements DistributionService { return branchResultDTOS; } + /** + * @Description 查询机关下的党支部 + * @Param branchCountFormDTO + * @author zxc + * @date 2020/10/27 11:11 上午 + */ + @DataSource(value = DataSourceConstant.EVALUATION_INDEX,datasourceNameFromArg = true) + @Override + public BranchCountResultDTO branchCount(BranchCountFormDTO branchCountFormDTO) { + BranchCountResultDTO branchCountResultDTO = screenCustomerGridDao.selectAgencyInfo(branchCountFormDTO.getAgencyId()); + if (null == branchCountResultDTO){ + return new BranchCountResultDTO(); + } + if (branchCountResultDTO.getLevel().equals(ScreenConstant.COMMUNITY)){ + return new BranchCountResultDTO(); + } + List agencyIds = screenCustomerAgencyDao.selectSubAgencyId(branchCountFormDTO.getAgencyId()); + List subList = screenCustomerGridDao.selectBranchCount(branchCountFormDTO.getAgencyId()); + if (!CollectionUtils.isEmpty(subList)){ + subList.forEach(sub -> { + agencyIds.forEach(agencyId -> { + if (sub.getAllParentIds().contains(agencyId.getSubId())){ + sub.setSubAgencyId(agencyId.getSubId()); + } + }); + }); + } + Map> groupBySubAgency = subList.stream().collect(Collectors.groupingBy(sub -> sub.getSubAgencyId())); + agencyIds.forEach(a -> { + groupBySubAgency.forEach((agencyId,value) -> { + if (a.getSubId().equals(agencyId)){ + a.setTotalNum(value.stream().collect(Collectors.summingInt(v -> v.getTotalNum()))); + } + }); + }); + branchCountResultDTO.setPartyDistribution(CollectionUtils.isEmpty(agencyIds) ? new ArrayList<>() : agencyIds); + return branchCountResultDTO; + } + /** * @Description 2、用户分布 * @param userFormDTO diff --git a/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenCustomerAgencyDao.xml b/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenCustomerAgencyDao.xml index f16395c129..8dd92ec0d1 100644 --- a/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenCustomerAgencyDao.xml +++ b/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenCustomerAgencyDao.xml @@ -197,4 +197,19 @@ del_flag = 0 AND agency_id = #{agencyId} + + + \ No newline at end of file diff --git a/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenCustomerGridDao.xml b/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenCustomerGridDao.xml index c072214600..be82c07755 100644 --- a/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenCustomerGridDao.xml +++ b/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenCustomerGridDao.xml @@ -26,10 +26,10 @@ scg.party_mark AS partyMark FROM screen_customer_grid scg - LEFT JOIN screen_customer_biz_org bo ON bo.ORG_ID = scg.PARENT_AGENCY_ID + LEFT JOIN screen_customer_agency sca ON sca.AGENCY_ID = scg.PARENT_AGENCY_ID AND sca.IS_DISPLAY = 1 WHERE scg.del_flag = 0 - AND bo.BIZ_TYPE = 'community' + AND sca.DEL_FLAG = 0 AND scg.all_parent_ids LIKE concat('%', #{agencyId}, '%') @@ -84,4 +84,41 @@ ORDER BY created_time DESC + + + + + + \ No newline at end of file diff --git a/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenIndexDataMonthlyDao.xml b/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenIndexDataMonthlyDao.xml index c5a9a6305d..55dd281ff5 100644 --- a/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenIndexDataMonthlyDao.xml +++ b/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenIndexDataMonthlyDao.xml @@ -69,6 +69,9 @@ service_ablity * SERVICE_ABLITY_WEIGHT AS serviceAbility, party_dev_ablity * PARTY_DEV_WEIGHT AS partyDevAbility, govern_ablity * GOVERN_ABLITY_WEIGHT AS governAbility, + PARTY_DEV_WEIGHT AS partyWeight, + GOVERN_ABLITY_WEIGHT AS governWeight, + SERVICE_ABLITY_WEIGHT AS serviceWeight, ORG_ID orgId, ORG_TYPE orgType FROM diff --git a/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenPartyLinkMassesDataDao.xml b/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenPartyLinkMassesDataDao.xml index 2bbdece16c..7a59c7f16d 100644 --- a/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenPartyLinkMassesDataDao.xml +++ b/epmet-module/data-report/data-report-server/src/main/resources/mapper/screen/ScreenPartyLinkMassesDataDao.xml @@ -6,13 +6,14 @@ \ No newline at end of file diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java index 83ad985174..70b38697ce 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java @@ -3,7 +3,8 @@ package com.epmet.service.evaluationindex.indexcoll.impl; import com.epmet.commons.dynamic.datasource.annotation.DataSource; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.StrConstant; -import com.epmet.commons.tools.redis.RedisKeys; +import com.epmet.commons.tools.distributedlock.DistributedLock; +import com.epmet.commons.tools.distributedlock.LockConstants; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.constant.DataSourceConstant; import com.epmet.constant.OrgTypeConstant; @@ -29,7 +30,6 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import lombok.extern.slf4j.Slf4j; import org.redisson.api.RLock; -import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -89,7 +89,7 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService { @Autowired private ScreenCustomerAgencyDao screenCustomerAgencyDao; @Autowired - private RedissonClient redissonClient; + private DistributedLock distributedLock; @Override @Transactional(rollbackFor = Exception.class) @@ -216,15 +216,9 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService { if (NumConstant.SIX != monthId.length()) { throw new RuntimeException("入参monthId格式不正确:monthId =" + monthId); } - RLock lock = redissonClient.getLock(RedisKeys.getScreenIndexDataLockKey()); + RLock lock = distributedLock.getLock(LockConstants.STATS_LOCK_NAME); try { - AtomicInteger tryTimes = new AtomicInteger(0); - boolean lockFlag = false; - do { - lockFlag = lock.tryLock(1, 10, TimeUnit.MINUTES); - } while (!lockFlag && tryTimes.addAndGet(1) < 3); - if (!lockFlag) { - log.error("插入大屏指数方法获取锁失败"); + if (null==lock) { return; } allParentIds.cleanUp(); @@ -250,13 +244,8 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService { // 根据年,汇总今年各项得到,计算平均值后 插入年表 screen_index_data_yearly this.insertIndexDataYear(monthId, customerId); allParentIds.invalidateAll(); - } catch (InterruptedException inter) { - log.error("插入计算指标数据异常", inter); } finally { - //判断是否是当前线程 持有锁 - if (lock.isHeldByCurrentThread()) { - lock.unlock(); - } + distributedLock.unLock(lock); } } diff --git a/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/RedissonTest.java b/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/RedissonTest.java index 6f01774eab..9df96f6d70 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/RedissonTest.java +++ b/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/RedissonTest.java @@ -1,6 +1,10 @@ package com.epmet.stats.test; +import com.alibaba.fastjson.JSON; import com.epmet.DataStatsApplication; +import com.epmet.commons.tools.distributedlock.DistributedLock; +import com.epmet.commons.tools.distributedlock.LockConstants; +import com.epmet.stats.test.zxc.ZxcDTO; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; @@ -10,6 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; import java.util.concurrent.TimeUnit; /** @@ -22,8 +29,43 @@ public class RedissonTest { @Autowired private RedissonClient redissonClient; + @Autowired + private DistributedLock distributedLock; @Test + public void lock(){ + long start = System.currentTimeMillis(); + RLock lock = distributedLock.getLock(LockConstants.TEST_LOCK_NAME); + if (null==lock){ + System.out.println("shibai"); + } + try { + List list = new ArrayList<>(); + for (int i = 0; i < 50000; i++) { + ZxcDTO zxcDTO = new ZxcDTO(); + zxcDTO.setA(UUID.randomUUID().toString()); + zxcDTO.setB(UUID.randomUUID().toString()); + zxcDTO.setC(UUID.randomUUID().toString()); + zxcDTO.setD(UUID.randomUUID().toString()); + zxcDTO.setE(UUID.randomUUID().toString()); + zxcDTO.setF(i); + zxcDTO.setG(i+1); + zxcDTO.setH(i+2); + list.add(zxcDTO); + } +// log.info(JSON.toJSONString(list)); + + + }finally { +// distributedLock.unLock(lock); + } + long end = System.currentTimeMillis(); + log.info((end-start)+"hs"); + + + } + + /*@Test public void lockTest() { //获取一个名为 lockName 的锁实例 RLock lock = redissonClient.getLock("lockName"); @@ -38,10 +80,10 @@ public class RedissonTest { //lock.lock(); //异步方式 - /* RFuture future = lock.lockAsync(); + *//* RFuture future = lock.lockAsync(); if (future.isSuccess()){ //todo something - }*/ + }*//* if (bs) { // 业务代码 System.out.println("进入业务代码: " + 123); @@ -56,6 +98,6 @@ public class RedissonTest { lock.unlock(); } } - } + }*/ } diff --git a/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/zxc/ZxcDTO.java b/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/zxc/ZxcDTO.java new file mode 100644 index 0000000000..148310ef15 --- /dev/null +++ b/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/zxc/ZxcDTO.java @@ -0,0 +1,21 @@ +package com.epmet.stats.test.zxc; + +import lombok.Data; + +/** + * @Author zxc + * @DateTime 2020/10/28 3:15 下午 + */ +@Data +public class ZxcDTO { + + private String a; + private String b; + private String c; + private String d; + private String e; + private Integer f; + private Integer g; + private Integer h; + +}