ZQN
2024-06-18 ccf9dfb83b9846974c569161502b784c668ee307
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
package com.project.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.common.constant.UserConstants;
import com.project.common.core.domain.entity.SysUser;
import com.project.common.exception.base.BaseException;
import com.project.common.utils.SecurityUtils;
import com.project.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hutool.core.convert.Convert;
import com.project.common.utils.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.transaction.annotation.Transactional;
import com.project.system.domain.vo.SysCompanyVo;
import com.project.system.domain.bo.editBo.SysCompanyBo;
import com.project.system.domain.bo.queryBo.SysCompanyQueryBo;
import com.project.system.domain.SysCompany;
import com.project.system.mapper.SysCompanyMapper;
import com.project.system.service.ISysCompanyService;
 
import java.util.Collection;
import java.util.List;
 
/**
 * 企业信息Service业务层处理
 *
 * @author manton
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompany> implements ISysCompanyService {
 
    private final ISysUserService userService;
 
    @Override//列表查询
    public List<SysCompanyVo> queryList(SysCompanyQueryBo bo)
    {
        QueryWrapper<SysCompany> qw = getQw(bo);
        List<SysCompany> list = this.list(qw);
        return Convert.toList(SysCompanyVo.class , list);
    }
 
    @Override//id查询
    public SysCompanyVo queryById(Long companyId)
    {
        SysCompany db = this.baseMapper.selectById(companyId);
        return Convert.convert(SysCompanyVo.class , db);
    }
 
 
    @Override//添加
    @Transactional
    public Boolean insertByBo(SysCompanyBo bo)
    {
        SysCompany add = Convert.convert(SysCompany.class, bo);
        validEntityBeforeSave(add, 0);
        boolean save = this.save(add);
        addCompanyUser(add);
        return save;
    }
 
    @Override//修改
    @Transactional
    public Boolean updateByBo(SysCompanyBo bo)
    {
        SysCompany old = this.getById(bo.getCompanyId());
        SysCompany update = Convert.convert(SysCompany.class, bo);
        if (!old.getCompanyPhone().equals(update.getCompanyPhone())){
            boolean delUser = delCompanyUser(old.getCompanyId());
            if (!delUser) {
                throw new BaseException("原企业用户清除失败,请联系管理员!");
            }
            this.addCompanyUser(update);
        }
        validEntityBeforeSave(update, 0);
        return this.updateById(update);
    }
 
    @Override//删除
    @Transactional
    public Boolean deleteByIds(Collection<Long> ids)
    {
        for (Long id : ids) {
            boolean b = delCompanyUser(id);
            if (!b) {
                throw new BaseException(String.format("原企业 %1$s 用户清除失败,请联系管理员!", id));
            }
        }
        return this.removeByIds(ids);
    }
 
    @Override//导入
    @Transactional
    public Boolean importList(List<SysCompanyVo> list)
    {
        if (list==null || list.size()<1){
            throw new BaseException("未获取到导入信息");
        }
        List<SysCompany> companies = Convert.toList(SysCompany.class, list);
        companies.forEach(e-> validEntityBeforeSave(e,1));
        boolean b = this.saveOrUpdateBatch(companies);
 
        companies.forEach(this::addCompanyUser);
        return b;
    }
 
 
//-------------------------------------------------------------------------------------
 
    /**
     * 保存前校验
     * @param entity    参数
     * @param isImport  是否导入:0否,1是
     */
    private void validEntityBeforeSave(SysCompany entity, int isImport)
    {
        entity.setIsImport(isImport);
        if (isImport!=1){
            if (StringUtils.isEmpty(entity.getCompanyImg())){
                throw new BaseException(String.format("%1$s,请上传营业执照", entity.getCompanyName()));
            }
        }
        if (StringUtils.isEmpty(entity.getCompanyCode())){
            throw new BaseException(String.format("%1$s,请填写企业社会编码!", entity.getCompanyName()));
        }
        if (StringUtils.isEmpty(entity.getCompanyUser())){
            throw new BaseException(String.format("%1$s,请填写企业联系人!", entity.getCompanyName()));
        }
        if (StringUtils.isEmpty(entity.getCompanyPhone())){
            throw new BaseException(String.format("%1$s,请填写企业联系人电话!", entity.getCompanyName()));
        }
        if (StringUtils.isEmpty(entity.getCompanyName())){
            throw new BaseException("请填写企业名!");
        }
        if (entity.getCompanyId()==null){ //新增
            int codeCount = this.count(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()));
            if (codeCount>0){
                throw new BaseException(String.format("%1$s,企业社会编码已存在!", entity.getCompanyName()));
            }
            int nameCount = this.count(lq().eq(SysCompany::getCompanyName, entity.getCompanyName()));
            if (nameCount>0){
                throw new BaseException(String.format("%1$s,企业名已存在!", entity.getCompanyName()));
            }
            int phoneCount = this.count(lq().eq(SysCompany::getCompanyPhone, entity.getCompanyPhone()));
            if (phoneCount>0){
                throw new BaseException(String.format("%1$s,企业联系电话已存在!", entity.getCompanyName()));
            }
        } else {
            int codeCount = this.count(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()).ne(SysCompany::getCompanyId,entity.getCompanyId()));
            if (codeCount>0){
                throw new BaseException(String.format("%1$s,企业社会编码已存在!", entity.getCompanyName()));
            }
            int nameCount = this.count(lq().eq(SysCompany::getCompanyName, entity.getCompanyName()).ne(SysCompany::getCompanyId,entity.getCompanyId()));
            if (nameCount>0){
                throw new BaseException(String.format("%1$s,企业名已存在!", entity.getCompanyName()));
            }
            int phoneCount = this.count(lq().eq(SysCompany::getCompanyPhone, entity.getCompanyPhone()).ne(SysCompany::getCompanyId,entity.getCompanyId()));
            if (phoneCount>0){
                throw new BaseException(String.format("%1$s,企业联系电话已存在!", entity.getCompanyName()));
            }
        }
        if (isImport==1){
            entity.setCheckStatus(2);
        }
    }
 
 
    /**
     * 添加企业用户
     * @param entity    企业信息
     */
    @Async
    protected void addCompanyUser(SysCompany entity)
    {
        SysUser user = new SysUser();
        SysCompany one = this.getOne(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()));
        String phone = entity.getCompanyPhone();
        user.setPhonenumber(phone);
        user.setUserName(phone);
        user.setNickName(entity.getCompanyUser());
        user.setPassword(phone);
        user.setDeptId(one.getCompanyId());
        user.setUserType("02");
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user)))
        {
            return;
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
        {
            return;
        }
        else if (StringUtils.isNotEmpty(user.getEmail())
                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
        {
            return;
        }
        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setRecommendUser(user.getPassword());
        userService.insertUser(user);
    }
 
    /**
     * 删除企业用户
     * @param companyId    企业id
     */
    protected boolean delCompanyUser(Long companyId)
    {
        return userService.deleteUserByDeptId(companyId)>0;
    }
 
    /**
     * 获取查询参数
     * @param bo    参数
     * @return  查询参数
     */
    private QueryWrapper<SysCompany> getQw(SysCompanyQueryBo bo)
    {
        QueryWrapper<SysCompany> qw = Wrappers.query();
 
            qw.like(StringUtils.isNotEmpty(bo.getCompanyName()), "company_name", bo.getCompanyName());
            qw.eq(StringUtils.isNotEmpty(bo.getCompanyCode()), "company_code", bo.getCompanyCode());
            qw.eq(StringUtils.isNotEmpty(bo.getCompanyUser()), "company_user", bo.getCompanyUser());
            qw.eq(StringUtils.isNotEmpty(bo.getCompanyPhone()), "company_phone", bo.getCompanyPhone());
            qw.eq(StringUtils.isNotEmpty(bo.getCompanyAddress()), "company_address", bo.getCompanyAddress());
            qw.eq(StringUtils.isNotEmpty(bo.getCompanyImg()), "company_img", bo.getCompanyImg());
            qw.eq(StringUtils.isNotEmpty(bo.getCompanyType()), "company_type", bo.getCompanyType());
            qw.eq(bo.getIsImport() != null, "is_import", bo.getIsImport());
            qw.eq(bo.getCompanyStatus() != null, "company_status", bo.getCompanyStatus());
            qw.eq(StringUtils.isNotEmpty(bo.getRegionCode()), "region_code", bo.getRegionCode());
            qw.like(StringUtils.isNotEmpty(bo.getRegionName()), "region_name", bo.getRegionName());
            qw.eq(bo.getCheckStatus() != null, "check_status", bo.getCheckStatus());
            qw.eq(bo.getSortNum() != null, "sort_num", bo.getSortNum());
        if (StringUtils.isNotEmpty(bo.getIsAsc()) && StringUtils.isNotEmpty(bo.getOrderByColumn())){
            if ("acs".equals(bo.getIsAsc())) {
                qw.orderByAsc(bo.getOrderByColumn());
            } else if ("desc".equals(bo.getIsAsc())) {
                qw.orderByDesc(bo.getOrderByColumn());
            }
        }
        return qw;
    }
}