k94314517
2024-11-23 b693c55ed17cc94efb45ea595341d443bfd4259d
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.doumee.service.business.impl;
 
import com.doumee.biz.system.SystemDictDataBiz;
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.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.join.CompanyUserApplyJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.system.SystemUserMapper;
import com.doumee.dao.system.model.SystemDictData;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.CompanyUserApplyService;
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 com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
 
/**
 * 集团申请记录表Service实现
 * @author 江蹄蹄
 * @date 2024/10/28 19:16
 */
@Service
public class CompanyUserApplyServiceImpl implements CompanyUserApplyService {
 
    @Autowired
    private CompanyUserApplyMapper companyUserApplyMapper;
    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private MultifileMapper multifileMapper;
    @Autowired
    private CompanyUserApplyJoinMapper companyUserApplyJoinMapper;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @Autowired
    private CompanyPermissionMapper companyPermissionMapper;
    @Autowired
    private NoticesMapper noticesMapper;
 
 
    @Override
    public Integer create(CompanyUserApply companyUserApply) {
        LoginUserInfo loginUserInfo =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Objects.isNull(companyUserApply)
            || Objects.isNull(companyUserApply.getUserId())
            || CollectionUtils.isEmpty(companyUserApply.getMultifileList())
            || StringUtils.isBlank(companyUserApply.getCompanyIds())
            || StringUtils.isBlank(companyUserApply.getCompanyNames())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(!Constants.equalsInteger(loginUserInfo.getType(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"登录用户不允许当前操作");
        }
        //查询当前用户是否存在申请中
        if(companyUserApplyMapper.selectCount(new QueryWrapper<CompanyUserApply>().lambda()
                .eq(CompanyUserApply::getUserId,companyUserApply.getUserId())
                .eq(CompanyUserApply::getIsdeleted, Constants.ZERO)
                .eq(CompanyUserApply::getStatus,Constants.ZERO))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前账户存在待审批数据,无法进行申请");
        };
        SystemUser applyUser = systemUserMapper.selectById(companyUserApply.getUserId());
        if(Objects.isNull(applyUser)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到申请用户信息");
        }
        Company applyCompany = companyMapper.selectById(applyUser.getCompanyId());
        if(Objects.isNull(applyCompany)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到申请用户企业信息");
        }
 
        List<String> companyIds = Arrays.asList(companyUserApply.getCompanyIds().split(","));
        for (String companyId:companyIds) {
            if(Constants.equalsInteger(loginUserInfo.getCompanyId(),Integer.valueOf(companyId))){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"无法申请归属公司权限");
            }
            if(companyPermissionMapper.selectCount(new QueryWrapper<CompanyPermission>().lambda()
                    .eq(CompanyPermission::getCompanyId,companyId)
                    .eq(CompanyPermission::getIsdeleted,Constants.ZERO)
                    .eq(CompanyPermission::getUserId,companyUserApply.getUserId()))>Constants.ZERO
            ){
                Company company = companyMapper.selectById(companyId);
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"企业【"+company.getName()+"】已申请,请勿重复申请!");
            };
        }
        List<Company> companyList = companyMapper.selectList(new QueryWrapper<Company>().lambda()
                .eq(Company::getIsdeleted, Constants.ZERO)
                .eq(Company::getType,Constants.ZERO)
                .in(Company::getId, companyIds)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(companyList) || !Constants.equalsInteger(companyList.size(),companyIds.size())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"企业信息异常,请检查选择企业信息");
        }
        companyUserApply.setStatus(Constants.ZERO);
        companyUserApply.setIsdeleted(Constants.ZERO);
        companyUserApply.setCreateDate(new Date());
        companyUserApply.setCreator(loginUserInfo.getId());
        companyUserApplyMapper.insert(companyUserApply);
 
        List<Multifile> multifileList = companyUserApply.getMultifileList();
        for (Multifile multifile:multifileList) {
            multifile.setIsdeleted(Constants.ZERO);
            multifile.setCreator(loginUserInfo.getId());
            multifile.setCreateDate(new Date());
            multifile.setObjId(companyUserApply.getId());
            multifile.setObjType(Constants.MultiFile.COMPANY_USER_APPLY.getKey());
            multifileMapper.insert(multifile);
        }
 
        //存储待办信息
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.COMPANY_USER_APPLY;
        //平台待办
        Notices notices = new Notices(noticeObjectType,
                Constants.ZERO
                ,companyUserApply.getId(),
                applyCompany.getName() + " - " + applyUser.getRealname() +"提交集团账号申请" ,
                null
                ,Constants.NoticeType.ZERO);
        noticesMapper.insert(notices);
 
        return companyUserApply.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        LoginUserInfo loginUserInfo =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        CompanyUserApply companyUserApply = companyUserApplyMapper.selectById(id);
        if(Objects.isNull(companyUserApply)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(companyUserApply.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前业务状态已流转,不允许该操作");
        }
        companyUserApplyMapper.update(null,new UpdateWrapper<CompanyUserApply>().lambda()
                .set(CompanyUserApply::getIsdeleted,Constants.ONE)
                .set(CompanyUserApply::getEditDate,new Date())
                .set(CompanyUserApply::getEditor,loginUserInfo.getId())
                .eq(CompanyUserApply::getId,id)
        );
    }
 
    @Override
    public void delete(CompanyUserApply companyUserApply) {
        UpdateWrapper<CompanyUserApply> deleteWrapper = new UpdateWrapper<>(companyUserApply);
        companyUserApplyMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        companyUserApplyMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(CompanyUserApply companyUserApply) {
        companyUserApplyMapper.updateById(companyUserApply);
    }
 
    @Override
    public void updateByIdInBatch(List<CompanyUserApply> companyUserApplys) {
        if (CollectionUtils.isEmpty(companyUserApplys)) {
            return;
        }
        for (CompanyUserApply companyUserApply: companyUserApplys) {
            this.updateById(companyUserApply);
        }
    }
 
    @Override
    public CompanyUserApply findById(Integer id) {
        CompanyUserApply companyUserApply = companyUserApplyJoinMapper.selectJoinOne(CompanyUserApply.class,new MPJLambdaWrapper<CompanyUserApply>()
                 .selectAll(CompanyUserApply.class)
                 .select(" t3.name as companyName ")
                 .select(" t1.REALNAME as realName ")
                 .select(" t2.USERNAME as userName ")
                 .leftJoin(" system_user t1 on t1.id = t.creator  ")
                 .leftJoin(" system_user t2 on t2.id = t.user_id  ")
                 .leftJoin(" company t3 on t3.id = t2.company_id  ")
                .eq(CompanyUserApply::getId,id));
        if(Objects.isNull(companyUserApply)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        List<Multifile> multifileList = multifileMapper.selectList(new QueryWrapper<Multifile>().lambda()
                .eq(Multifile::getObjId,id)
                .eq(Multifile::getObjType,Constants.MultiFile.COMPANY_USER_APPLY.getKey())
                .eq(Multifile::getIsdeleted,Constants.ZERO)
                .orderByDesc(Multifile::getId)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(multifileList)){
            String url = systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+
                    systemDictDataBiz.queryByCode(Constants.OSS,Constants.COMPANY_USER_APPLY).getCode();
            for (Multifile multifile:multifileList) {
                if(StringUtils.isNotBlank(multifile.getFileurl())){
                    multifile.setFileurlFull(url + multifile.getFileurl());
                }
            }
            companyUserApply.setMultifileList(multifileList);
        }
        return companyUserApply;
    }
 
    @Override
    public CompanyUserApply findOne(CompanyUserApply companyUserApply) {
        QueryWrapper<CompanyUserApply> wrapper = new QueryWrapper<>(companyUserApply);
        return companyUserApplyMapper.selectOne(wrapper);
    }
 
    @Override
    public List<CompanyUserApply> findList(CompanyUserApply companyUserApply) {
        QueryWrapper<CompanyUserApply> wrapper = new QueryWrapper<>(companyUserApply);
        return companyUserApplyMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<CompanyUserApply> findPage(PageWrap<CompanyUserApply> pageWrap) {
        IPage<CompanyUserApply> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<CompanyUserApply> queryWrapper = new MPJLambdaWrapper<CompanyUserApply>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.selectAll(CompanyUserApply.class);
        queryWrapper.select(" t3.name as companyName ");
        queryWrapper.select(" t1.REALNAME as realName ");
        queryWrapper.select(" t2.USERNAME as userName ");
        queryWrapper.leftJoin(" system_user t1 on t1.id = t.creator  ");
        queryWrapper.leftJoin(" system_user t2 on t2.id = t.user_id  ");
        queryWrapper.leftJoin(" company t3 on t3.id = t2.company_id  ");
        queryWrapper.eq(CompanyUserApply::getIsdeleted,Constants.ZERO);
 
 
        if (pageWrap.getModel().getCompanyId() != null) {
            queryWrapper.apply(" t3.id = "+pageWrap.getModel().getCompanyId()+" ");
        }
 
        if (pageWrap.getModel().getStatus() != null) {
            queryWrapper.eq(CompanyUserApply::getStatus, pageWrap.getModel().getStatus());
        }
 
        if (pageWrap.getModel().getUserId() != null) {
            queryWrapper.eq(CompanyUserApply::getUserId, pageWrap.getModel().getUserId());
        }
 
        queryWrapper.orderByDesc(CompanyUserApply::getCreateDate);
        IPage<CompanyUserApply> iPage = companyUserApplyJoinMapper.selectJoinPage(page,CompanyUserApply.class,queryWrapper);
        return PageData.from(iPage);
    }
 
    @Override
    public long count(CompanyUserApply companyUserApply) {
        QueryWrapper<CompanyUserApply> wrapper = new QueryWrapper<>(companyUserApply);
        return companyUserApplyMapper.selectCount(wrapper);
    }
 
    @Override
    public void audit(CompanyUserApply companyUserApply){
        LoginUserInfo loginUserInfo =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Objects.isNull(companyUserApply)
                ||Objects.isNull(companyUserApply.getId())
                || Objects.isNull(companyUserApply.getStatus())
                || !(Constants.equalsInteger(companyUserApply.getStatus(),Constants.ONE) || Constants.equalsInteger(companyUserApply.getStatus(),Constants.TWO))
                || (StringUtils.isBlank(companyUserApply.getCheckInfo()) && Constants.equalsInteger(companyUserApply.getStatus(),Constants.TWO))
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        CompanyUserApply userApply = companyUserApplyJoinMapper.selectById(companyUserApply.getId());
        if(Objects.isNull(userApply)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(userApply.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"业务状态已流转");
        }
        CompanyUserApply update = new CompanyUserApply();
        update.setId(companyUserApply.getId());
        update.setEditDate(new Date());
        update.setEditor(loginUserInfo.getId());
        update.setStatus(companyUserApply.getStatus());
        update.setCheckInfo(companyUserApply.getCheckInfo());
        update.setCheckDate(new Date());
        update.setCheckorId(loginUserInfo.getId());
        companyUserApplyJoinMapper.updateById(update);
        if(Constants.equalsInteger(companyUserApply.getStatus(),Constants.ONE) && StringUtils.isNotBlank(userApply.getCompanyIds())){
            List<String> ids = Arrays.asList(userApply.getCompanyIds().split(","));
            List<CompanyPermission> companyPermissionList = new ArrayList<>();
            for (String id:ids) {
                CompanyPermission companyPermission = new CompanyPermission();
                companyPermission.setCreator(loginUserInfo.getId());
                companyPermission.setCreateDate(new Date());
                companyPermission.setIsdeleted(Constants.ZERO);
                companyPermission.setUserId(userApply.getUserId());
                companyPermission.setCompanyId(Integer.valueOf(id));
                companyPermission.setType(Constants.ONE);
                companyPermissionList.add(companyPermission);
            }
            companyPermissionMapper.insertBatchSomeColumn(companyPermissionList);
        }
 
 
        //存储待办信息
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.COMPANY_USER_APPLY;
        //删除其他待办
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey())
                .ne(Notices::getType,Constants.NoticeType.SIX.getStatus())
                .eq(Notices::getObjId,update.getId()));
 
    }
}