k94314517
2025-06-16 15286a9ab39823ddbbf682e1049f39dfc5dd024f
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
package com.doumee.service.business.impl;
 
import cn.emay.sdk.util.StringUtil;
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.CustomerCompanyMapper;
import com.doumee.dao.business.CustomerServiceMapper;
import com.doumee.dao.business.join.CustomerCompanyJoinMapper;
import com.doumee.dao.business.join.CustomerServiceJoinMapper;
import com.doumee.dao.business.model.ApplyChagneDetail;
import com.doumee.dao.business.model.Company;
import com.doumee.dao.business.model.CustomerCompany;
import com.doumee.dao.business.model.CustomerService;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.CompanyService;
import com.doumee.service.business.CustomerCompanyService;
import com.doumee.service.business.CustomerServiceService;
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.mapper.MPJJoinMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 客服信息表Service实现
 * @author 江蹄蹄
 * @date 2024/11/05 18:41
 */
@Service
public class CustomerServiceServiceImpl implements CustomerServiceService {
 
    @Autowired
    private CustomerServiceMapper customerServiceMapper;
 
    @Autowired
    private CustomerCompanyMapper customerCompanyMapper;
 
    @Autowired
    private CustomerServiceJoinMapper customerServiceJoinMapper;
 
    @Autowired
    private CustomerCompanyJoinMapper customerCompanyJoinMapper;
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer create(CustomerService customerService) {
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Objects.isNull(customerService)
        || StringUtils.isBlank(customerService.getMobile())
        || StringUtils.isBlank(customerService.getName())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(customerServiceMapper.selectCount(new QueryWrapper<CustomerService>().lambda()
                .eq(CustomerService::getMobile,customerService.getMobile()))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"手机号["+customerService.getMobile()+"]已存在客服数据");
        }
        customerService.setCreator(loginUserInfo.getId());
        customerService.setCreateDate(new Date());
        customerService.setIsdeleted(Constants.ZERO);
        customerServiceMapper.insert(customerService);
 
        return customerService.getId();
    }
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void authCompany(CustomerService customerService){
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Objects.isNull(customerService)
                || Objects.isNull(customerService.getId())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        customerCompanyMapper.delete(new QueryWrapper<CustomerCompany>().lambda().eq(CustomerCompany::getCustomerServiceId,customerService.getId()));
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(customerService.getCustomerCompanyList())){
            List<CustomerCompany> customerCompanyList = customerService.getCustomerCompanyList();
            //判断数据是否存在异常
            for (CustomerCompany customerCompany:customerCompanyList) {
                if(Objects.isNull(customerCompany.getCompanyId())){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST);
                }
            }
            List<Integer> companyIdList = customerCompanyList.stream().map(m->m.getCompanyId()).collect(Collectors.toList());
            //查询当前选中的组织是否已被添加存在
            if(customerCompanyMapper.selectCount(new QueryWrapper<CustomerCompany>().lambda().in(CustomerCompany::getCompanyId,companyIdList))>0){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"存在企业数据已被添加,请刷新重试");
            };
            for (CustomerCompany customerCompany:customerCompanyList) {
                customerCompany.setCustomerServiceId(customerService.getId());
                customerCompany.setCreator(loginUserInfo.getId());
                customerCompany.setCreateDate(new Date());
                customerCompany.setCustomerServiceId(customerService.getId());
                customerCompany.setIsdeleted(Constants.ZERO);
                customerCompanyMapper.insert(customerCompany);
            }
        }
    }
 
 
    @Override
    public void deleteById(Integer id) {
        customerServiceMapper.deleteById(id);
        customerCompanyMapper.delete(new QueryWrapper<CustomerCompany>().lambda().eq(CustomerCompany::getCustomerServiceId,id));
    }
 
    @Override
    public void delete(CustomerService customerService) {
        UpdateWrapper<CustomerService> deleteWrapper = new UpdateWrapper<>(customerService);
        customerServiceMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        customerServiceMapper.deleteBatchIds(ids);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void updateById(CustomerService customerService) {
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Objects.isNull(customerService)
                || Objects.isNull(customerService.getId())
                || StringUtils.isBlank(customerService.getMobile())
                || StringUtils.isBlank(customerService.getName())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        CustomerService model = customerServiceMapper.selectById(customerService.getId());
        if(Objects.isNull(model)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
 
        if(customerServiceMapper.selectCount(new QueryWrapper<CustomerService>().lambda()
                        .ne(CustomerService::getId,customerService.getId())
                .eq(CustomerService::getMobile,customerService.getMobile()))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"手机号["+customerService.getMobile()+"]已存在客服数据");
        }
        customerService.setEditor(loginUserInfo.getId());
        customerService.setEditDate(new Date());
        customerServiceMapper.updateById(customerService);
    }
 
    @Override
    public void updateByIdInBatch(List<CustomerService> customerServices) {
        if (CollectionUtils.isEmpty(customerServices)) {
            return;
        }
        for (CustomerService customerService: customerServices) {
            this.updateById(customerService);
        }
    }
 
    @Override
    public CustomerService findById(Integer id) {
        CustomerService customerService = customerServiceMapper.selectById(id);
        List<CustomerCompany> customerCompanyList = customerCompanyJoinMapper.
                selectJoinList(CustomerCompany.class,
                        new MPJLambdaWrapper<CustomerCompany>()
                                .selectAll(CustomerCompany.class)
                                .selectAs(Company::getName,CustomerCompany::getCompanyName)
                                .leftJoin(Company.class,Company::getId,CustomerCompany::getCompanyId)
                                .eq(CustomerCompany::getCustomerServiceId,id));
        customerService.setCustomerCompanyList(customerCompanyList);
        return customerService;
    }
 
    @Override
    public CustomerService findOne(CustomerService customerService) {
        QueryWrapper<CustomerService> wrapper = new QueryWrapper<>(customerService);
        return customerServiceMapper.selectOne(wrapper);
    }
 
    @Override
    public List<CustomerService> findList(CustomerService customerService) {
        QueryWrapper<CustomerService> wrapper = new QueryWrapper<>(customerService);
        return customerServiceMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<CustomerService> findPage(PageWrap<CustomerService> pageWrap) {
        IPage<CustomerService> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<CustomerService> queryWrapper = new MPJLambdaWrapper<CustomerService>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.selectAll(CustomerService.class);
        queryWrapper.selectAs(SystemUser::getRealname,CustomerService::getCreateUserName);
        queryWrapper.select(" ( select count(1) from customer_company cc where cc.CUSTOMER_SERVICE_ID = t.id ) as companyNum ");
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,CustomerService::getCreator);
        queryWrapper.eq(CustomerService::getIsdeleted,Constants.ZERO);
 
        if (pageWrap.getModel().getStatus() != null) {
            queryWrapper.eq(CustomerService::getStatus, pageWrap.getModel().getStatus());
        }
        if (pageWrap.getModel().getSortnum() != null) {
            queryWrapper.eq(CustomerService::getSortnum, pageWrap.getModel().getSortnum());
        }
        if (pageWrap.getModel().getName() != null) {
            queryWrapper.like(CustomerService::getName, pageWrap.getModel().getName());
        }
        if (pageWrap.getModel().getMobile() != null) {
            queryWrapper.like(CustomerService::getMobile, pageWrap.getModel().getMobile());
        }
 
        if (pageWrap.getModel().getCompanyName() != null) {
            queryWrapper.apply(" t.id in ( select cc.COMPANY_ID from customer_company cc inner join company c on cc.COMPANY_ID = c.id where c.name like '%"+pageWrap.getModel().getCompanyName()+"%' )  ");
        }
 
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        PageData<CustomerService> pageData = PageData.from(customerServiceJoinMapper.selectJoinPage(page,CustomerService.class, queryWrapper));
        return pageData;
    }
 
    @Override
    public long count(CustomerService customerService) {
        QueryWrapper<CustomerService> wrapper = new QueryWrapper<>(customerService);
        return customerServiceMapper.selectCount(wrapper);
    }
 
    /**
     * 根据企业查询客服人员
     * @param companyId
     * @return
     */
    @Override
    public CustomerService findByCompanyId(Integer companyId) {
        CustomerCompany customerCompany = customerCompanyMapper.selectOne(new QueryWrapper<CustomerCompany>()
                .lambda().eq(CustomerCompany::getCompanyId,companyId)
                .last(" limit 1 ")
        );
        if(Objects.nonNull(customerCompany)){
            CustomerService customerService = customerServiceMapper.selectById(customerCompany.getCustomerServiceId());
            if(Objects.isNull(customerService)){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未配置客服人员,请联系管理员");
            }
            return customerService;
        }else{
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未配置客服人员,请联系管理员");
        }
    }
 
 
}