jiangping
2023-12-18 98e52a2d1a05276e11d2e5c99ec05e670228fbd6
server/dmvisit_service/src/main/java/com/doumee/service/business/impl/DeviceRoleServiceImpl.java
@@ -1,20 +1,39 @@
package com.doumee.service.business.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.erp.model.openapi.request.RoleListRequest;
import com.doumee.core.erp.model.openapi.response.DeviceListResponse;
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.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 io.swagger.models.auth.In;
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;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 门禁角色信息表Service实现
@@ -27,10 +46,47 @@
    @Autowired
    private DeviceRoleMapper deviceRoleMapper;
    @Override
    public Integer create(DeviceRole deviceRole) {
        deviceRoleMapper.insert(deviceRole);
        return deviceRole.getId();
        // 获取之前是否存在默认门禁组
        DeviceRole newDeviceRole = null;
        if(Constants.ONE == deviceRole.getStatus() && null != deviceRole.getType()) {
            newDeviceRole = getDeviceRole(deviceRole.getStatus(), deviceRole.getType());
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
        if(null != newDeviceRole) {
            DeviceRole updateDeviceRole = new DeviceRole();
            // 若现在新增数据设置默认门禁组;那么先把之前的默认门禁组状态改为不是默认的
            updateDeviceRole.setId(newDeviceRole.getId());
            updateDeviceRole.setEdirot(loginUserInfo.getId().toString());
            updateDeviceRole.setEditDate(new Date());
            updateDeviceRole.setIsdeleted(Constants.ONE);
            updateDeviceRole.setStatus(Constants.ZERO);
            deviceRoleMapper.updateById(updateDeviceRole);
        }
        DeviceRole insert = new DeviceRole();
        insert.setCreator(loginUserInfo.getId().toString());
        insert.setCreateDate(new Date());
        insert.setEdirot(loginUserInfo.getId().toString());
        insert.setEditDate(new Date());
        insert.setIsdeleted(Constants.ZERO);
        insert.setRemark(deviceRole.getRemark());
        insert.setName(deviceRole.getName());
        insert.setIsDefault(Constants.equalsInteger(Constants.ONE,deviceRole.getStatus()) ? 0 : 1);
        insert.setType(deviceRole.getType());
        insert.setDoorIds(deviceRole.getDoorIds());
        insert.setDoorNames(deviceRole.getDoorNames());
        insert.setParkIds(deviceRole.getParkIds());
        insert.setParkNames(deviceRole.getParkNames());
        insert.setStatus(deviceRole.getStatus());
        insert.setHkId(deviceRole.getHkId());
        insert.setHkStatus(deviceRole.getHkStatus());
        insert.setHkDate(deviceRole.getHkDate());
        deviceRoleMapper.insert(insert);
        return insert.getId();
    }
    @Override
@@ -54,7 +110,50 @@
    @Override
    public void updateById(DeviceRole deviceRole) {
        deviceRole.setIsDefault(Constants.equalsInteger(Constants.ONE,deviceRole.getStatus()) ? 0 : 1);
        deviceRoleMapper.updateById(deviceRole);
    }
    @Override
    public void updateStatusById(DeviceRole deviceRole) {
        // 获取之前是否存在默认门禁组
        DeviceRole newDeviceRole = null;
        if(Constants.ONE == deviceRole.getStatus() && null != deviceRole.getType()) {
            newDeviceRole = getDeviceRole(deviceRole.getStatus(), 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(Integer status, Integer type) {
        LambdaQueryWrapper<DeviceRole> query = new LambdaQueryWrapper<>();
        query.eq(DeviceRole::getIsdeleted,Constants.ZERO)
                .eq(DeviceRole::getIsDefault,Constants.ZERO)
                .eq(DeviceRole::getType,type)
                .eq(DeviceRole::getStatus,status);
        // 查询是否有默认门禁组,只能有一组默认门禁组
        DeviceRole getDeviceRole = deviceRoleMapper.selectOne(query);
        return getDeviceRole;
    }
    @Override
@@ -83,7 +182,7 @@
        QueryWrapper<DeviceRole> wrapper = new QueryWrapper<>(deviceRole);
        return deviceRoleMapper.selectList(wrapper);
    }
    @Override
    public PageData<DeviceRole> findPage(PageWrap<DeviceRole> pageWrap) {
        IPage<DeviceRole> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
@@ -136,6 +235,9 @@
        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());
@@ -151,4 +253,27 @@
        QueryWrapper<DeviceRole> wrapper = new QueryWrapper<>(deviceRole);
        return deviceRoleMapper.selectCount(wrapper);
    }
    @Override
    public List<DeviceRoleVO> findListByType(Integer type) {
        List<DeviceRole> deviceRoleList = deviceRoleMapper.selectList(new QueryWrapper<DeviceRole>().lambda()
                .eq(DeviceRole::getType,type)
                .eq(DeviceRole::getIsdeleted, Constants.ZERO)
                .eq(DeviceRole::getStatus, Constants.ZERO)
        );
        List<DeviceRoleVO> deviceRoleVOList = new ArrayList<>();
        for (DeviceRole deviceRole:deviceRoleList) {
            DeviceRoleVO deviceRoleVO = new DeviceRoleVO();
            BeanUtils.copyProperties(deviceRole,deviceRoleVO);
            deviceRoleVOList.add(deviceRoleVO);
        }
        return deviceRoleVOList;
    }
}