郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
src/main/java/com/gk/firework/Controller/AuthorizationController.java
对比新文件
@@ -0,0 +1,145 @@
package com.gk.firework.Controller;
import com.alibaba.fastjson.JSONObject;
import com.gk.firework.Controller.Base.BaseController;
import com.gk.firework.Domain.AuthorizationInfo;
import com.gk.firework.Domain.Enterprise;
import com.gk.firework.Domain.Enum.ErrorCode;
import com.gk.firework.Domain.UserInfo;
import com.gk.firework.Domain.Utils.Msg;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Service.AuthorizationService;
import com.gk.firework.Service.EnterpriseService;
import com.gk.firework.Service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
/**
 * @author : jingjy
 * @date : 2021/7/2 14:31
 */
@Api("授权码")
@RequestMapping("auth")
@RestController
public class AuthorizationController extends BaseController {
    @Autowired
    private AuthorizationService authorizationService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public Msg getAuthListByEnterpriseId(@RequestParam("userId") String userId) {
        if (StringUtils.isBlank(userId)) {
            return new Msg(ErrorCode.ERROR_10001);
        }
        UserInfo userInfo = userService.getById(userId);
        if (userInfo == null) {
            return new Msg(ErrorCode.ERROR_50001);
        }
        List<AuthorizationInfo> authorizationInfo = authorizationService.getAuthByEnterprise(userInfo.getCompanynumber());
        return success(authorizationInfo);
    }
    @PostMapping("/create")
    public Msg createAuth(@RequestBody JSONObject jsonObject) {
        String userId = jsonObject.getString("userId");
        String contractCode = jsonObject.getString("contractCode");
        Byte flag = jsonObject.getByte("flag");
        if (StringUtils.isBlank(userId)) {
            return new Msg(ErrorCode.ERROR_10001);
        }
        UserInfo userInfo = userService.getById(userId);
        if (userInfo == null) {
            return new Msg(ErrorCode.ERROR_50001);
        }
        if (StringUtils.isBlank(contractCode)) {
            return new Msg(ErrorCode.ERROR_10002, "合同编号不能为空");
        }
        AuthorizationInfo authorizationInfo = new AuthorizationInfo();
        List<AuthorizationInfo> authorizationExist = authorizationService.getAuthByEnterprise(userInfo.getCompanynumber());
        String prefix;
        if (authorizationExist.size() > 0) {
            //企业已经生成过授权码,则使用第一次生成的前缀
            prefix = authorizationExist.get(0).getAuthcodeprefix();
        } else {
            //未生成则随机生成,不能重复
            String str;
            str = randomLetter(3);
            boolean isPrefixExist;
            isPrefixExist = authorizationService.checkPrefixExist(str);
            while (isPrefixExist) {
                str = randomLetter(3);
                isPrefixExist = authorizationService.checkPrefixExist(str);
            }
            prefix = str;
        }
        String suffix;
            suffix = randomLetter(3);
        boolean isAuthCodeExist;
        isAuthCodeExist = authorizationService.checkCodeExist(prefix + suffix);
        while (isAuthCodeExist) {
            suffix = randomLetter(3);
            isAuthCodeExist = authorizationService.checkCodeExist(prefix + suffix);
        }
        authorizationInfo.setAuthcode(prefix+suffix);
        authorizationInfo.setAuthcodeprefix(prefix);
        authorizationInfo.setFlag(flag);
        authorizationInfo.setEnterprisenumber(userInfo.getCompanynumber());
        authorizationInfo.setContractcode(contractCode);
        authorizationInfo.setCreatedat(new Date());
        authorizationInfo.setCreatedby(userService.getById(getUser().getId()).getUsername());
        authorizationInfo.setStatus((byte)1);
        authorizationService.save(authorizationInfo);
        return success();
    }
    @PostMapping("status")
    public Msg changeStatus(@RequestBody JSONObject jsonObject){
        String id = jsonObject.getString("id");
        String status = jsonObject.getString("status");
        if (!"0".equals(status) && !"1".equals(status)){
            return new Msg(ErrorCode.ERROR_10004);
        }
        if (StringUtils.isBlank(id)){
            return new Msg(ErrorCode.ERROR_10002);
        }
        AuthorizationInfo authorizationInfo = authorizationService.getById(id);
        if (authorizationInfo == null){
            return new Msg(ErrorCode.ERROR_50001);
        }
        authorizationInfo.setStatus(Byte.parseByte(status));
        authorizationService.updateById(authorizationInfo);
        return success();
    }
    @PostMapping("delete")
    public Msg deleteAuth(@RequestBody JSONObject jsonObject){
        String id = jsonObject.getString("id");
        if (StringUtils.isBlank(id)){
            return new Msg(ErrorCode.ERROR_10002);
        }
        AuthorizationInfo authorizationInfo = authorizationService.getById(id);
        if (authorizationInfo == null){
            return new Msg(ErrorCode.ERROR_50001);
        }
        authorizationService.removeById(id);
        return success();
    }
    /**
     * @param n 随机字符串长度
     * @return 生成的随机字符串
     */
    private String randomLetter(int n) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < n; i++) {
            str.append((char) (Math.random() * 26 + 'A'));
        }
        return str.toString();
    }
}