nidapeng
2024-03-19 6c40e04eb7c2060feb2533735cd3089d438965e5
server/dmvisit_service/src/main/java/com/doumee/service/business/impl/DeviceRoleServiceImpl.java
@@ -1,5 +1,7 @@
package com.doumee.service.business.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
@@ -10,21 +12,24 @@
import com.doumee.core.erp.model.openapi.response.RoleListResponse;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.DeviceMapper;
import com.doumee.dao.business.DeviceRoleMapper;
import com.doumee.dao.business.model.Device;
import com.doumee.dao.business.model.DeviceRole;
import com.doumee.dao.admin.response.MemberInfoDTO;
import com.doumee.dao.business.*;
import com.doumee.dao.business.join.MemberRoleJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.web.response.DeviceRoleVO;
import com.doumee.service.business.DeviceRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doumee.service.business.MemberService;
import com.doumee.service.business.impl.hksync.ferp.HkSyncOrgUserToHKServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
@@ -40,28 +45,49 @@
 * @date 2023/11/30 15:33
 */
@Service
@Slf4j
public class DeviceRoleServiceImpl implements DeviceRoleService {
    @Autowired
    private DeviceRoleMapper deviceRoleMapper;
    @Autowired
    private MemberCardMapper memberCardMapper;
    @Autowired
    private EmpowerMapper empowerMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private MemberRoleJoinMapper memberRoleMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public Integer create(DeviceRole deviceRole) {
        QueryWrapper<DeviceRole> query = new QueryWrapper<>();
        query.lambda()
                .eq(DeviceRole::getIsdeleted,Constants.ZERO)
                .eq(DeviceRole::getType,deviceRole.getType())
                .eq(DeviceRole::getStatus,Constants.ONE);
        List<DeviceRole> deviceRoles = deviceRoleMapper.selectList(query);
        if (CollectionUtils.isNotEmpty(deviceRoles) && Constants.equalsInteger(Constants.ONE,deviceRole.getStatus())){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"同一个用户类型,只能有一个默认角色");
        if(Objects.isNull(deviceRole)
            || StringUtils.isBlank(deviceRole.getName())
            || Objects.isNull(deviceRole.getType())
            || Objects.isNull(deviceRole.getIsDefault())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        // 获取之前是否存在默认门禁组
        DeviceRole newDeviceRole = null;
        if(null != deviceRole.getType()) {
            newDeviceRole = getDeviceRole(deviceRole.getType());
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
        if(null != newDeviceRole && deviceRole.getIsDefault().equals(Constants.ZERO)) {
            DeviceRole updateDeviceRole = new DeviceRole();
            // 若现在新增数据设置默认门禁组;那么先把之前的默认门禁组状态改为不是默认的
            updateDeviceRole.setId(newDeviceRole.getId());
            updateDeviceRole.setEdirot(loginUserInfo.getId().toString());
            updateDeviceRole.setEditDate(new Date());
            updateDeviceRole.setIsDefault(Constants.ONE);
            deviceRoleMapper.updateById(updateDeviceRole);
        }
        DeviceRole insert = new DeviceRole();
        insert.setCreator(loginUserInfo.getId().toString());
        insert.setCreateDate(new Date());
        insert.setEdirot(loginUserInfo.getId().toString());
@@ -69,7 +95,7 @@
        insert.setIsdeleted(Constants.ZERO);
        insert.setRemark(deviceRole.getRemark());
        insert.setName(deviceRole.getName());
        insert.setIsDefault(Constants.equalsInteger(Constants.ONE,deviceRole.getStatus()) ? 0 : 1);
        insert.setIsDefault(deviceRole.getIsDefault());
        insert.setType(deviceRole.getType());
        insert.setDoorIds(deviceRole.getDoorIds());
        insert.setDoorNames(deviceRole.getDoorNames());
@@ -105,39 +131,166 @@
    @Override
    public void updateById(DeviceRole deviceRole) {
        QueryWrapper<DeviceRole> query = new QueryWrapper<>();
        query.lambda()
                .eq(DeviceRole::getIsdeleted,Constants.ZERO)
                .eq(DeviceRole::getType,deviceRole.getType())
                .eq(DeviceRole::getStatus,Constants.ONE);
        List<DeviceRole> deviceRoles = deviceRoleMapper.selectList(query);
        if (CollectionUtils.isNotEmpty(deviceRoles) && Constants.equalsInteger(Constants.ONE,deviceRole.getStatus())){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"同一个用户类型,只能有一个默认角色");
        DeviceRole model = deviceRoleMapper.selectById(deviceRole.getId());
        if(model == null){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        deviceRole.setIsDefault(Constants.equalsInteger(Constants.ONE,deviceRole.getStatus()) ? 0 : 1);
        deviceRoleMapper.updateById(deviceRole);
    }
    @Override
    public void updateStatusById(DeviceRole deviceRole) {
        QueryWrapper<DeviceRole> query = new QueryWrapper<>();
        query.lambda()
                .eq(DeviceRole::getIsdeleted,Constants.ZERO)
                .eq(DeviceRole::getType,deviceRole.getType())
                .eq(DeviceRole::getStatus,Constants.ONE);
        List<DeviceRole> deviceRoles = deviceRoleMapper.selectList(query);
        if (CollectionUtils.isNotEmpty(deviceRoles) && Constants.equalsInteger(Constants.ONE,deviceRole.getStatus())){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"同一个用户类型,只能有一个默认角色");
        DeviceRole newDeviceRole = null;
        if(null != deviceRole.getType()) {
            newDeviceRole = getDeviceRole(deviceRole.getType());
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
        DeviceRole update = new DeviceRole();
        update.setEdirot(loginUserInfo.getId().toString());
        update.setEditDate(new Date());
        update.setId(deviceRole.getId());
        update.setStatus(deviceRole.getStatus());
        update.setIsDefault(Constants.equalsInteger(Constants.ONE,deviceRole.getStatus()) ? 0 : 1);
        deviceRoleMapper.updateById(update);
        if(null != newDeviceRole  && deviceRole.getIsDefault().equals(Constants.ZERO)) {
            DeviceRole update = new DeviceRole();
            update.setEdirot(loginUserInfo.getId().toString());
            update.setEditDate(new Date());
            update.setId(newDeviceRole.getId());
            update.setIsDefault(Constants.ONE);
            deviceRoleMapper.updateById(update);
        }
       int t = deviceRoleMapper.updateById(deviceRole);
       if(t>0){
            Thread tt = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        startUpdateMemberEmpower(model,deviceRole);
                    }catch (Exception e){
                        log.error("======门禁分组修改,更新人员权限失败==="+e.getMessage());
                    }
                }
            });
            tt.start();
       }
    }
    /**
     * 更新该权限下相关的人员最新全新下发
     * @param model
     * @param deviceRole
     */
    private void startUpdateMemberEmpower(DeviceRole model, DeviceRole deviceRole) {
        String[] ids = StringUtils.defaultString(model.getDoorIds()).split(",");
        String[] newIds =StringUtils.defaultString(deviceRole.getDoorIds()).split(",");
        List<Integer>  delIds = getChangeIdsByArray(newIds,ids);//删除的设备
        List<Integer>  addIds = getChangeIdsByArray(ids,newIds);//最新设备
        if((addIds==null ||addIds.size()==0)&&( delIds ==null || delIds.size() == 0 )){
            //如果没有变化,不做处理
            return;
        }
        List<Integer>  allids = getChangeIdsByArray(new String[]{},newIds);//最新设备
        List<MemberRole> memberRoleList = memberRoleMapper.selectList(new QueryWrapper<MemberRole>().lambda()
                .eq(MemberRole::getRoleId,model.getId())
                .eq(MemberRole::getIsdeleted,Constants.ZERO)
        );
        if(memberRoleList!=null && memberRoleList.size()>0){
            for(MemberRole memberRole : memberRoleList){
                //如果有人员授权了该权限
                Member member = memberMapper.selectById(memberRole.getMemberId());
                if(member==null || member.getIsdeleted().equals(Constants.ONE)){
                    continue;
                }
                MPJLambdaWrapper<MemberRole> queryWrapper = new MPJLambdaWrapper<>();
                queryWrapper.leftJoin(DeviceRole.class,DeviceRole::getId,MemberRole::getRoleId );
                queryWrapper.selectAll(MemberRole.class)
                        .selectAs(DeviceRole::getDoorIds, MemberRole::getDoorIds)
                        .eq(MemberRole::getMemberId,memberRole.getMemberId())
                        .eq(MemberRole::getIsdeleted,Constants.ZERO)
                        .ne(MemberRole::getRoleId,deviceRole.getId());
                List<MemberRole> tLis = memberRoleMapper.selectJoinList(MemberRole.class,queryWrapper);
                List<Integer>  actDelIds = getActChangeDoorIds(tLis,delIds);
                List<Integer>  actAddIds = getActChangeDoorIds(tLis,allids);
                HkSyncOrgUserToHKServiceImpl.dealChangeDeviceRoleEmpower(member,actAddIds,actDelIds,deviceRoleMapper,empowerMapper,deviceMapper);
            }
        }
    }
    /**
     * 根据用户当前最新的门禁角色,查询实际应该删除或者增加的门禁点权限
     * @param tLis
     * @param changeIds
     * @return
     */
    private List<Integer> getActChangeDoorIds(List<MemberRole> tLis, List<Integer> changeIds) {
        List<Integer> list = new ArrayList<>();
        for(MemberRole memberRole : tLis){
            String[] ids = StringUtils.defaultString(memberRole.getDoorIds()).split(",");
            if(ids!=null && ids.length>0){
                for (String s:ids){
                    list.add(Integer.parseInt(s));
                }
            }
        }
        List<Integer> actList = new ArrayList<>();
        if(list.size()>0){
            Flag:
            for (Integer t : changeIds){
                for (Integer tt : list){
                    if(Constants.equalsInteger(t,tt)){
                        continue Flag;
                    }
                }
                actList.add(t);
            }
        }
        return  actList;
    }
    private List<Integer> getChangeIdsByArray(String[] ids, String[] newIds) {
        List<Integer> list = new ArrayList<>();
        if(newIds!=null){
            FLAG :
            for(String s :newIds){
                for(String s1 :ids){
                    if(StringUtils.equals(s,s1)){
                        continue FLAG;
                    }
                }
                list.add(Integer.parseInt(s));
            }
        }
        return list;
    }
    @Override
    public void updateStatusById(DeviceRole deviceRole) {
        // 获取之前是否存在默认门禁组
        DeviceRole newDeviceRole = null;
        if(null != deviceRole.getType()) {
            newDeviceRole = getDeviceRole(deviceRole.getType());
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
        List<DeviceRole> deviceRoleList = new ArrayList<>();
        if(null != newDeviceRole) {
            DeviceRole update = new DeviceRole();
            update.setEdirot(loginUserInfo.getId().toString());
            update.setEditDate(new Date());
            update.setId(newDeviceRole.getId());
            update.setStatus(newDeviceRole.getStatus());
            update.setIsDefault(Constants.ONE);
            deviceRoleList.add(update);
        }
        DeviceRole updateTwo = new DeviceRole();
        updateTwo.setEdirot(loginUserInfo.getId().toString());
        updateTwo.setEditDate(new Date());
        updateTwo.setId(deviceRole.getId());
        updateTwo.setStatus(deviceRole.getStatus());
        updateTwo.setIsDefault(deviceRole.getIsDefault());
        deviceRoleList.add(updateTwo);
        // 最多2条,所有就没有写批量修改了
        for (DeviceRole data:deviceRoleList) {
            deviceRoleMapper.updateById(data);
        }
    }
    private DeviceRole getDeviceRole(String type) {
        LambdaQueryWrapper<DeviceRole> query = new LambdaQueryWrapper<>();
        query.eq(DeviceRole::getIsdeleted,Constants.ZERO)
                .eq(DeviceRole::getIsDefault,Constants.ZERO)
                .eq(DeviceRole::getType,type)
                .last(" limit 1");
        // 查询是否有默认门禁组,只能有一组默认门禁组
        DeviceRole getDeviceRole = deviceRoleMapper.selectOne(query);
        return getDeviceRole;
    }
    @Override
@@ -172,6 +325,7 @@
        IPage<DeviceRole> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<DeviceRole> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        pageWrap.getModel().setIsdeleted(Constants.ZERO);
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(DeviceRole::getId, pageWrap.getModel().getId());
        }
@@ -196,13 +350,20 @@
            queryWrapper.lambda().eq(DeviceRole::getRemark, pageWrap.getModel().getRemark());
        }
        if (pageWrap.getModel().getName() != null) {
            queryWrapper.lambda().eq(DeviceRole::getName, pageWrap.getModel().getName());
            queryWrapper.lambda().like(DeviceRole::getName, pageWrap.getModel().getName());
        }
        if (pageWrap.getModel().getIsDefault() != null) {
            queryWrapper.lambda().eq(DeviceRole::getIsDefault, pageWrap.getModel().getIsDefault());
        }
        if (pageWrap.getModel().getType() != null) {
            queryWrapper.lambda().eq(DeviceRole::getType, pageWrap.getModel().getType());
        if (StringUtils.isNotBlank(pageWrap.getModel().getType())) {
            if(pageWrap.getModel().getType().length() > 1) {
                String[] types = pageWrap.getModel().getType().split(",");
                queryWrapper.lambda().and(p-> p.eq(DeviceRole::getType, types[0])
                        .or()
                        .eq(DeviceRole::getType, types[1]));
            }else {
                queryWrapper.lambda().eq(DeviceRole::getType, pageWrap.getModel().getType());
            }
        }
        if (pageWrap.getModel().getDoorIds() != null) {
            queryWrapper.lambda().eq(DeviceRole::getDoorIds, pageWrap.getModel().getDoorIds());
@@ -219,9 +380,6 @@
        if (pageWrap.getModel().getStatus() != null) {
            queryWrapper.lambda().eq(DeviceRole::getStatus, pageWrap.getModel().getStatus());
        }
        queryWrapper.select("t_aa.*," +
                "(SELECT count(DISTINCT `KEY`) FROM `test` WHERE `test`.`key` = `t_aa`.id ) ");
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
@@ -229,7 +387,19 @@
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(deviceRoleMapper.selectPage(page, queryWrapper));
        queryWrapper.lambda().orderByAsc( DeviceRole::getName);
        IPage<DeviceRole> newPage = deviceRoleMapper.selectPage(page, queryWrapper);
        if(null != newPage && newPage.getRecords().size() > 0) {
            List<DeviceRole> deviceRoleList = newPage.getRecords();
            for (DeviceRole dto:deviceRoleList) {
                Member member = new Member();
                member.setIsdeleted(Constants.ZERO);
                member.setRoleIdParam("["+dto.getId()+"]");
                dto.setMemberNum( memberMapper.selectCount(new QueryWrapper<>(member)));
            }
            newPage.setRecords(deviceRoleList);
        }
        return PageData.from(newPage);
    }
    @Override
@@ -243,7 +413,7 @@
        List<DeviceRole> deviceRoleList = deviceRoleMapper.selectList(new QueryWrapper<DeviceRole>().lambda()
                .eq(DeviceRole::getType,type)
                .eq(DeviceRole::getIsdeleted, Constants.ZERO)
                .eq(DeviceRole::getStatus, Constants.ZERO)
                .eq(DeviceRole::getStatus, Constants.ONE)
        );
        List<DeviceRoleVO> deviceRoleVOList = new ArrayList<>();
        for (DeviceRole deviceRole:deviceRoleList) {