wangmengzhao
2023-12-13 d8228730095cdcaccb40dd2dc290fc44779e340a
wumei-smart-master/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java
@@ -4,6 +4,7 @@
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
@@ -26,6 +27,7 @@
import com.ruoyi.iot.service.IDeviceService;
import com.ruoyi.iot.service.IProductService;
import com.ruoyi.iot.service.IToolService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysUserService;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
@@ -37,6 +39,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
@@ -85,6 +88,9 @@
    @Autowired
    @Lazy
    private EmqxService emqxService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    /**
     * 查询设备
@@ -154,8 +160,15 @@
     * @throws Exception
     */
    @Override
    public int selectDeviceCount(int id) throws Exception {
        return deviceMapper.selectDeviceCount(id);
    public AjaxResult selectDeviceCount(int id) throws Exception {
        //根据用户登录的角色身份查询总设备数(台)
        int count = deviceMapper.selectDeviceCount(id);
        //根据登录用户的角色身份查询设备的使用人数
        int total = deviceMapper.selectUserDeviceByCount(id);
        AjaxResult ajax = new AjaxResult();
        ajax.put("deviceCount", count);
        ajax.put("userTotal", total);
        return AjaxResult.success("查询成功", ajax);
    }
    /**
@@ -169,6 +182,11 @@
    @Override
    public int selectUserDeviceByCount(int id) throws Exception {
        return deviceMapper.selectUserDeviceByCount(id);
    }
    @Override
    public List<Device> selectDeviceAll() throws Exception {
        return deviceMapper.selectDeviceAll();
    }
    /**
@@ -226,6 +244,7 @@
        // 查询物模型
        String thingsModels = thingsModelService.getCacheThingsModelByProductId(input.getProductId());
        JSONObject thingsModelObject = JSONObject.parseObject(thingsModels);
        //ThingsModelValueItemDto——物模型
        List<ThingsModelValueItemDto> valueList = null;
        if (type == 1) {
            JSONArray properties = thingsModelObject.getJSONArray("properties");
@@ -237,6 +256,7 @@
        // 查询物模型值
        ThingsModelValuesOutput deviceThings = deviceMapper.selectDeviceThingsModelValueBySerialNumber(input.getDeviceNumber());
        //物模型值的项——ThingsModelValueItem
        List<ThingsModelValueItem> thingsModelValues = JSONObject.parseArray(deviceThings.getThingsModelValue(), ThingsModelValueItem.class);
        for (int i = 0; i < input.getThingsModelValueRemarkItem().size(); i++) {
@@ -382,17 +402,36 @@
     * @return 设备
     */
    @Override
    public List<DeviceShortOutput> selectDeviceShortList(Device device) {
    public List<DeviceShortOutput> selectDeviceShortList(Device device, boolean repairFlag) {
        SysUser user = getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        for (int i = 0; i < roles.size(); i++) {
            if (roles.get(i).getRoleKey().equals("tenant")) {
                // 租户查看产品下所有设备
                device.setTenantId(user.getUserId());
                if (repairFlag == true) {
                    device.setInstallFlag(1);
                }
                break;
            } else if (roles.get(i).getRoleKey().equals("general")) {
                // 用户查看自己设备
                device.setUserId(user.getUserId());
                if (repairFlag == true) {
                    device.setInstallFlag(1);
                }
                break;
            } else if (roles.get(i).getRoleKey().equals("tenanthelper")) {
                //联营商合伙人查看设备
                //联营商合伙人需要根据他自己的userid去查询他上级的用户信息,tenantid是租户id和他的一级联营商所拥有的设备
                //通过用户ID查询用户根据当前登录的联营商合伙人去查询他的上级部门id是几,再通过他的上级部门id去查询所对应的一级联营商和所对应的tenantid
                SysUser sysUser = userService.selectUserById(user.getUserId());//拿到所在的部门id
                SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());  //根据部门id去查询信息得到父级的部门id
                SysDept sysDeptParent = sysDeptMapper.selectDeptById(sysDept.getParentId());  //根据父级部门的id去查询信息
                SysUser sysUserDept = userService.selectUserByDeptId(sysDeptParent.getDeptId());//根据得到的父级的部门id去sys_user表里面查询他的用户信息
                device.setTenantId(sysUserDept.getUserId());
                if (repairFlag == true) {
                    device.setInstallFlag(1);
                }
                break;
            }
        }
@@ -810,12 +849,12 @@
        // 设备编号:D + userId + 15位随机字母和数字
        SysUser user = getLoginUser().getUser();
        String number = "D" + user.getUserId().toString() + toolService.getStringRandom(10);
//        int count = deviceMapper.getDeviceNumCount(number);
//        if (count == 0) {
//            return number;
//        } else {
//            generationDeviceNum();
//        }
        int count = deviceMapper.getDeviceNumCount(number);
        if (count == 0) {
            return number;
        } else {
            generationDeviceNum();
        }
        return "";
    }