TBoxOperationController.java 12.5 KB
Newer Older
wzp's avatar
wzp committed
1 2
package cn.wisenergy.chnmuseum.party.web.controller;

wzp's avatar
wzp committed
3
import cn.wisenergy.chnmuseum.party.auth.util.JwtTokenUtil;
wzp's avatar
wzp committed
4 5 6
import cn.wisenergy.chnmuseum.party.common.log.MethodLog;
import cn.wisenergy.chnmuseum.party.common.log.OperModule;
import cn.wisenergy.chnmuseum.party.common.log.OperType;
liqin's avatar
liqin committed
7
import cn.wisenergy.chnmuseum.party.common.util.RSAUtils;
liqin's avatar
liqin committed
8
import cn.wisenergy.chnmuseum.party.model.TBoxOperation;
wzp's avatar
wzp committed
9
import cn.wisenergy.chnmuseum.party.model.TUser;
liqin's avatar
liqin committed
10
import cn.wisenergy.chnmuseum.party.service.TBoxOperationService;
wzp's avatar
wzp committed
11
import cn.wisenergy.chnmuseum.party.service.impl.TUserServiceImpl;
liqin's avatar
liqin committed
12
import cn.wisenergy.chnmuseum.party.web.controller.base.BaseController;
wzp's avatar
wzp committed
13 14 15 16 17 18 19 20
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
wzp's avatar
wzp committed
21
import org.apache.shiro.authz.annotation.RequiresAuthentication;
wzp's avatar
wzp committed
22
import org.apache.shiro.authz.annotation.RequiresPermissions;
wzp's avatar
wzp committed
23
import org.springframework.data.redis.core.StringRedisTemplate;
wzp's avatar
wzp committed
24
import org.springframework.data.redis.core.ValueOperations;
wzp's avatar
wzp committed
25 26 27
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
wzp's avatar
wzp committed
28
import java.util.*;
wzp's avatar
wzp committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/**
 * <pre>
 * 机顶盒运维信息 前端控制器
 * </pre>
 *
 * @author Danny Lee
 * @since 2021-03-25
 */
@Slf4j
@RestController
@RequestMapping("/boxOperation")
@Api(tags = {"机顶盒运维信息操作接口"})
public class TBoxOperationController extends BaseController {

    @Resource
    private TBoxOperationService tBoxOperationService;

    @Resource
    private TUserServiceImpl userService;

wzp's avatar
wzp committed
50 51 52
    @Resource
    private StringRedisTemplate stringRedisTemplate;

wzp's avatar
wzp committed
53
    private static final String SHIRO_JWT_TOKEN = "shiro:jwt:token:";
wzp's avatar
wzp committed
54

wzp's avatar
wzp committed
55 56 57 58 59 60 61 62
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "organId", value = "所属单位", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态 1.未激活 2.已激活 3.故障", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "areaId", value = "区域", paramType = "query", dataType = "String")
    })
    @PostMapping("/selectPageList")
wzp's avatar
wzp committed
63
    @RequiresAuthentication  //@RequiresPermissions("/boxOperation/selectPageList")
wzp's avatar
wzp committed
64
    @ApiOperation(value = "获取机顶盒基础信息分页列表", notes = "获取机顶盒基础信息分页列表")
wzp's avatar
wzp committed
65
    @MethodLog(operModule = OperModule.STBBASE,operType = OperType.SELECT)
liqin's avatar
liqin committed
66
    public Map<String, Object> selectPageList(String organId, String areaId) {
wzp's avatar
wzp committed
67
        TUser user1 = getcurUser();
wzp's avatar
wzp committed
68 69 70 71 72
        TUser user = new TUser();
        if (StringUtils.isNotBlank(organId)) {
            user.setOrgId(organId);
        }
        if (StringUtils.isNotBlank(areaId)) {
wzp's avatar
wzp committed
73
            user.setAreaId(areaId);
wzp's avatar
wzp committed
74
        }
wzp's avatar
wzp committed
75
        //设置数据权限
wzp's avatar
wzp committed
76 77 78 79
//        if (StringUtils.isNotBlank(user1.getAreaId())) {
//            String areaId1 = getAreaId(user1.getAreaId());
//            user.setAreaName(areaId1);
//        }
wzp's avatar
wzp committed
80 81 82
        if (!user1.getRoleList().contains("1")) {
            user.setOrgCode(user1.getOrgCode());
        }
wzp's avatar
wzp committed
83
        try {
liqin's avatar
liqin committed
84
            Page<TBoxOperation> page = tBoxOperationService.selectBoxPage(getPage(), user);
wzp's avatar
wzp committed
85 86 87 88 89
            return getResult(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
wzp's avatar
wzp committed
90 91
    }

wzp's avatar
wzp committed
92
    @PostMapping("/add")
wzp's avatar
wzp committed
93
    @RequiresAuthentication  //@RequiresPermissions("/boxOperation/add")
wzp's avatar
wzp committed
94
    @ApiOperation(value = "添加机顶盒运维信息", notes = "添加机顶盒运维信息")
wzp's avatar
wzp committed
95
    @MethodLog(operModule = OperModule.STBOPERATION,operType = OperType.ADD)
wzp's avatar
wzp committed
96 97
    public Map<String, Object> saveTBoxOperation(TBoxOperation tBoxOperation) {
        // 保存业务节点信息
liqin's avatar
liqin committed
98
        boolean result;
wzp's avatar
wzp committed
99
        try {
liqin's avatar
liqin committed
100 101 102
            if (tBoxOperation != null && StringUtils.isNotBlank(tBoxOperation.getMac())) {
                tBoxOperation.setMac(tBoxOperation.getMac().toUpperCase());
            }
liqin's avatar
liqin committed
103 104 105
            final ArrayList<String> rsaKeys = RSAUtils.createRSAKeys();
            tBoxOperation.setPublicKey(rsaKeys.get(0));
            tBoxOperation.setPrivateKey(rsaKeys.get(1));
wzp's avatar
wzp committed
106 107 108 109
            result = tBoxOperationService.save(tBoxOperation);
            if (!result) {
                return getFailResult();
            }
wzp's avatar
wzp committed
110
            return getSuccessResult();
wzp's avatar
wzp committed
111 112 113
        } catch (Exception e) {
            e.printStackTrace();
        }
liqin's avatar
liqin committed
114 115
        // 保存失败
        return getFailResult();
wzp's avatar
wzp committed
116 117 118
    }

    @PutMapping("/update")
wzp's avatar
wzp committed
119
    @RequiresAuthentication  //@RequiresPermissions("/boxOperation/update")
wzp's avatar
wzp committed
120
    @ApiOperation(value = "修改机顶盒运维信息", notes = "修改机顶盒运维信息")
wzp's avatar
wzp committed
121
    @MethodLog(operModule = OperModule.STBOPERATION,operType = OperType.ACTIVATION)
wzp's avatar
wzp committed
122
    public Map<String, Object> updateTBoxOperation(TBoxOperation tBoxOperation) {
wzp's avatar
wzp committed
123
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
liqin's avatar
liqin committed
124 125 126 127
        try {
            if (tBoxOperation != null && StringUtils.isNotBlank(tBoxOperation.getMac())) {
                tBoxOperation.setMac(tBoxOperation.getMac().toUpperCase());
            }
wzp's avatar
wzp committed
128
            if (2==tBoxOperation.getStatus()) {
wzp's avatar
wzp committed
129
                TBoxOperation byId = tBoxOperationService.getById(tBoxOperation.getId());
wzp's avatar
wzp committed
130
                if (byId.getMac()!=null&&!"".equals(byId.getMac())&&!byId.getMac().equals(tBoxOperation.getMac())){
wzp's avatar
wzp committed
131
                    resultMap.put("resultCode", "400");
wzp's avatar
wzp committed
132 133 134
                    resultMap.put("message", "此账号已绑定机顶盒!");
                    return resultMap;
                }
wzp's avatar
wzp committed
135 136 137
                UpdateWrapper<TBoxOperation> wrapper = new UpdateWrapper<>();
                wrapper.eq("mac",tBoxOperation.getMac());
                TBoxOperation one = tBoxOperationService.getOne(wrapper);
wzp's avatar
wzp committed
138
                if (one!=null&&!one.getOrganId().equals(tBoxOperation.getOrganId())){
wzp's avatar
wzp committed
139
                    resultMap.put("resultCode", "400");
wzp's avatar
wzp committed
140
                    resultMap.put("message", "此mac地址已绑定账号!");
wzp's avatar
wzp committed
141 142
                    return resultMap;
                }
wzp's avatar
wzp committed
143 144 145 146
                final ArrayList<String> rsaKeys = RSAUtils.createRSAKeys();
                tBoxOperation.setPublicKey(rsaKeys.get(0));
                tBoxOperation.setPrivateKey(rsaKeys.get(1));
            }
wzp's avatar
wzp committed
147
            if (3==tBoxOperation.getStatus()) {
wzp's avatar
wzp committed
148
                tBoxOperation.setMac(null);
wzp's avatar
wzp committed
149
                TUser u = userService.getOne(new UpdateWrapper<TUser>().eq("org_id",tBoxOperation.getOrganId()).eq("type", 3).eq("is_deleted", false));
wzp's avatar
wzp committed
150
                Set<String> keys = stringRedisTemplate.keys(SHIRO_JWT_TOKEN + "*");
wzp's avatar
wzp committed
151 152 153 154 155 156
                ValueOperations<String, String> value = stringRedisTemplate.opsForValue();
                for (String key : keys) {
                    if (u.getId().equals(value.get(key))){
                        stringRedisTemplate.delete(key);
                    }
                }
wzp's avatar
wzp committed
157
            }
liqin's avatar
liqin committed
158 159 160 161 162 163 164 165 166 167 168 169 170
            boolean flag = tBoxOperationService.updateById(tBoxOperation);
            UpdateWrapper<TUser> wrapper = new UpdateWrapper<>();
            wrapper.eq("org_id", tBoxOperation.getOrganId());
            wrapper.eq("type", "3");
            TUser user = userService.getOne(wrapper);
            String password = user.getPassword();
            if (flag) {
                return getResult(password);
            }
            return getFailResult();
        } catch (Exception e) {
            return getFailResult();
        }
wzp's avatar
wzp committed
171 172 173
    }

    @DeleteMapping("/delete")
wzp's avatar
wzp committed
174
    @RequiresAuthentication  //@RequiresPermissions("/boxOperation/delete")
wzp's avatar
wzp committed
175 176
    @ApiOperation(value = "根据ID删除机顶盒运维信息", notes = "根据ID删除机顶盒运维信息")
    @ApiImplicitParams(value = {
wzp's avatar
wzp committed
177
            @ApiImplicitParam(name = "id", value = "标识ID", paramType = "query", dataType = "String")
wzp's avatar
wzp committed
178
    })
wzp's avatar
wzp committed
179
    @MethodLog(operModule = OperModule.STBOPERATION,operType = OperType.DELETE)
wzp's avatar
wzp committed
180
    public Map<String, Object> deleteTBoxOperation(String id) {
liqin's avatar
liqin committed
181
        boolean result;
wzp's avatar
wzp committed
182 183 184 185 186
        try {
            result = tBoxOperationService.removeById(id);
            if (!result) {
                return getFailResult();
            }
wzp's avatar
wzp committed
187
            return getSuccessResult();
wzp's avatar
wzp committed
188 189
        } catch (Exception e) {
            e.printStackTrace();
wzp's avatar
wzp committed
190 191 192 193 194
        }
        return getFailResult();
    }

    @GetMapping("/getList")
wzp's avatar
wzp committed
195
    @RequiresAuthentication  //@RequiresPermissions("/boxOperation/getList")
wzp's avatar
wzp committed
196
    @ApiOperation(value = "获取机顶盒运维信息全部列表(无分页)", notes = "获取机顶盒运维信息全部列表(无分页)")
wzp's avatar
wzp committed
197
    @MethodLog(operModule = OperModule.STBOPERATION,operType = OperType.SELECT)
wzp's avatar
wzp committed
198
    public Map<String, Object> getTBoxOperationList(String status) {
wzp's avatar
wzp committed
199
        List<TBoxOperation> tBoxOperationList = null;
wzp's avatar
wzp committed
200
        TUser user = getcurUser();
wzp's avatar
wzp committed
201

wzp's avatar
wzp committed
202
        try {
wzp's avatar
wzp committed
203
            if ("4".equals(user.getType())) {
wzp's avatar
wzp committed
204 205 206 207 208 209 210
                //设置数据权限
                String areaId = user.getAreaId();
                tBoxOperationList = tBoxOperationService.getList(status, getAreaId(areaId));
            }else {
                tBoxOperationList = tBoxOperationService.getList(status, null);
            }

wzp's avatar
wzp committed
211 212 213 214
            return getResult(tBoxOperationList);
        } catch (Exception e) {
            e.printStackTrace();
        }
liqin's avatar
liqin committed
215
        return getFailResult();
wzp's avatar
wzp committed
216 217 218 219 220 221 222 223 224 225
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "organId", value = "所属单位", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态 1.未激活 2.已激活 3.故障", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "areaId", value = "区域", paramType = "query", dataType = "String")
    })
    @PostMapping("/getPageList")
wzp's avatar
wzp committed
226
    @RequiresAuthentication  //@RequiresPermissions("/boxOperation/getPageList")
wzp's avatar
wzp committed
227
    @ApiOperation(value = "获取机顶盒运维信息分页列表", notes = "获取机顶盒运维信息分页列表")
wzp's avatar
wzp committed
228
    @MethodLog(operModule = OperModule.STBOPERATION,operType = OperType.SELECT)
wzp's avatar
wzp committed
229
    public Map<String, Object> getTBoxOperationPageList(String organId, Integer status, String areaId) {
wzp's avatar
wzp committed
230
        TUser user = getcurUser();
wzp's avatar
wzp committed
231
        TBoxOperation tBoxOperation = new TBoxOperation();
wzp's avatar
wzp committed
232
        if (StringUtils.isNotBlank(organId)) {
wzp's avatar
wzp committed
233
            tBoxOperation.setOrganId(organId);
wzp's avatar
wzp committed
234 235
        }
        if (status != null) {
wzp's avatar
wzp committed
236
            tBoxOperation.setStatus(status);
wzp's avatar
wzp committed
237 238
        }
        if (StringUtils.isNotBlank(areaId)) {
wzp's avatar
wzp committed
239
           tBoxOperation.setAreaId(areaId);
wzp's avatar
wzp committed
240
        }
wzp's avatar
wzp committed
241
        if (StringUtils.isNotBlank(user.getAreaId())) {
wzp's avatar
wzp committed
242 243
            if ("4".equals(user.getType())) {
                //如果是运维账号设置数据权限
wzp's avatar
wzp committed
244 245
                tBoxOperation.setAreaName(getAreaId(user.getAreaId()));
            }
wzp's avatar
wzp committed
246
        }
wzp's avatar
wzp committed
247 248 249 250
        if (user.getRoleList().size()>0&&!user.getRoleList().contains("1")) {
            //设置用户数据权限
            tBoxOperation.setOrganCode(user.getOrgCode());
        }
wzp's avatar
wzp committed
251 252
        Page<TBoxOperation> page = null;
        try {
wzp's avatar
wzp committed
253
            page = this.tBoxOperationService.selectPage(getPage(), tBoxOperation);
wzp's avatar
wzp committed
254 255 256 257 258
            return getResult(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
wzp's avatar
wzp committed
259 260 261 262
    }

    @ApiOperation(value = "获取机顶盒运维信息详情", notes = "获取机顶盒运维信息详情")
    @GetMapping("/getById")
wzp's avatar
wzp committed
263
    @RequiresAuthentication  //@RequiresPermissions("/boxOperation/getById")
wzp's avatar
wzp committed
264
    @MethodLog(operModule = OperModule.STBOPERATION,operType = OperType.SELECT)
wzp's avatar
wzp committed
265
    public Map<String, Object> getById(String id) {
wzp's avatar
wzp committed
266 267 268 269 270 271 272 273
        TBoxOperation tBoxOperation = null;
        try {
            tBoxOperation = tBoxOperationService.getById(id);
            return getResult(tBoxOperation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
wzp's avatar
wzp committed
274 275
    }

wzp's avatar
wzp committed
276

wzp's avatar
wzp committed
277
    public static String getAreaId(String areaId) {
wzp's avatar
wzp committed
278 279
        if ("0000".equals(areaId.substring(2))) {
            areaId = areaId.substring(0, 2);
liqin's avatar
liqin committed
280
        } else if ("00".equals(areaId.substring(4))) {
wzp's avatar
wzp committed
281
            areaId = areaId.substring(0, 4);
wzp's avatar
wzp committed
282 283 284 285
        }
        return areaId;
    }

wzp's avatar
wzp committed
286 287
}