lyh
2025-01-13 76e6eaff3a5be5f73ddb90b9169381dc46503ccd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package org.jeecg.modules.dnc.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.dnc.entity.Button;
import org.jeecg.modules.dnc.entity.ObjectBase;
import org.jeecg.modules.dnc.entity.ObjectButton;
import org.jeecg.modules.dnc.entity.ObjectButtonPermission;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.DncObjectMapper;
import org.jeecg.modules.dnc.request.ObjectBaseRequest;
import org.jeecg.modules.dnc.response.CommonCode;
import org.jeecg.modules.dnc.response.ObjectCode;
import org.jeecg.modules.dnc.response.QueryPageResponseResult;
import org.jeecg.modules.dnc.service.IButtonService;
import org.jeecg.modules.dnc.service.IObjectButtonPermissionService;
import org.jeecg.modules.dnc.service.IObjectButtonService;
import org.jeecg.modules.dnc.service.IObjectService;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
 
@Service
public class ObjectServiceImpl extends ServiceImpl<DncObjectMapper, ObjectBase> implements IObjectService {
    @Autowired
    private IObjectButtonService objectButtonService;
    @Autowired
    private IButtonService buttonService;
    @Autowired
    private IObjectButtonPermissionService objectButtonPermissionService;
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addObject(ObjectBase objectBase) {
        if(objectBase == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(!ValidateUtil.validateString(objectBase.getObjectPerm()))
            ExceptionCast.cast(ObjectCode.OBJ_PERM_CODE_NONE);
        if(!ValidateUtil.validateString(objectBase.getObjectName()))
            ExceptionCast.cast(ObjectCode.OBJ_NAME_NONE);
        ObjectBase en = getByPermCode(objectBase.getObjectPerm());
        if(en != null)
            ExceptionCast.cast(ObjectCode.OBJ_PERM_CODE_EXIST);
        return super.save(objectBase);
    }
 
    @Override
    public ObjectBase getByPermCode(String permCode) {
        if(!ValidateUtil.validateString(permCode))
            return null;
        List<ObjectBase> list = super.lambdaQuery().eq(ObjectBase::getObjectPerm, permCode).list();
        if(list == null || list.isEmpty())
            return null;
        return list.get(0);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignButton(String objectId, List<Button> buttonList) {
        if(!ValidateUtil.validateString(objectId))
            ExceptionCast.cast(ObjectCode.OBJ_ID_NONE);
        ObjectBase objectBase = super.getById(objectId);
        if(objectBase == null)
            ExceptionCast.cast(ObjectCode.OBJ_NOT_EXIST);
        boolean b = objectButtonService.deleteByObjectId(objectId);
        if(!b)
            ExceptionCast.cast(CommonCode.FAIL);
        if(buttonList != null && !buttonList.isEmpty()) {
            List<ObjectButton> objButtons = new ArrayList<>();
            List<String> ids = new ArrayList<>();
            buttonList.forEach(item -> {
                final ObjectButton en = new ObjectButton();
                en.setObjectId(objectBase.getObjectId());
                en.setButtonId(item.getButtonId());
                en.setButtonUrl(item.getButtonUrl());
                if(ValidateUtil.validateString(item.getButtonAlias())) {
                    en.setButtonAlias(item.getButtonAlias());
                }else {
                    en.setButtonAlias(item.getButtonName());
                }
                en.setPermCode(objectBase.getObjectPerm() + "_" + item.getButtonPerm());
                objButtons.add(en);
                ids.add(item.getButtonId());
            });
            Collection<Button> buttons = buttonService.listByIds(ids);
            if(buttons == null || buttons.isEmpty() || buttons.size() != buttonList.size())
                ExceptionCast.cast(CommonCode.INVALID_PARAM);
            if(!objButtons.isEmpty())
                return objectButtonService.saveBatch(objButtons);
        }
        return b;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean editObject(String id, ObjectBase objectBase) {
        if(!ValidateUtil.validateString(id) || objectBase == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        ObjectBase en = super.getById(id);
        if (en == null) {
            ExceptionCast.cast(ObjectCode.OBJ_ID_NOT_EXIST);
        }
        objectBase.setObjectId(id);
        objectBase.setObjectPerm(null);
        return super.updateById(objectBase);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteObjectById(String id) {
        if(!ValidateUtil.validateString(id)) {
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        ObjectBase en = super.getById(id);
        if (en == null)
            ExceptionCast.cast(ObjectCode.OBJ_ID_NOT_EXIST);
        List<ObjectButtonPermission> objectButtonPermissions = objectButtonPermissionService.findByObjectId(id);
        if(objectButtonPermissions != null && !objectButtonPermissions.isEmpty())
            ExceptionCast.cast(ObjectCode.OBJ_ROLE_EXIST);
        boolean b = objectButtonService.deleteByObjectId(id);
        if(!b)
            ExceptionCast.cast(CommonCode.FAIL);
        return super.removeById(id);
    }
 
    @Override
    public QueryPageResponseResult<ObjectBase> findPageList(int page, int size, ObjectBaseRequest buttonRequest) {
        if(page < 1 || size < 1) {
            ExceptionCast.cast(CommonCode.INVALID_PAGE);
        }
        IPage<ObjectBase> pageData = new Page<>(page, size);
        LambdaQueryChainWrapper<ObjectBase> lambdaQuery = super.lambdaQuery();
        if(buttonRequest != null) {
            if(ValidateUtil.validateString(buttonRequest.getObjectName())) {
                lambdaQuery.eq(ObjectBase::getObjectName, buttonRequest.getObjectName());
            }
            if(ValidateUtil.validateString(buttonRequest.getObjectPerm())) {
                lambdaQuery.like(ObjectBase::getObjectPerm, buttonRequest.getObjectPerm());
            }
            if(ValidateUtil.validateString(buttonRequest.getAscStr())) {
                String[] ascArr = buttonRequest.getAscStr().split(",");
//                ((Page<ObjectBase>) pageData).setAsc(ascArr);
            }
            if(ValidateUtil.validateString(buttonRequest.getDescStr())) {
                String[] descStr = buttonRequest.getDescStr().split(",");
//                ((Page<ObjectBase>) pageData).setDesc(descStr);
            }
        }
        IPage<ObjectBase> userIPage = lambdaQuery.page(pageData);
        return new QueryPageResponseResult<>(CommonCode.SUCCESS, userIPage);
    }
}