xiaoyong931011
2021-03-16 1028a2a78292ce0942bf9c07c23cfb22a33c4600
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.matrix.system.fenxiao.service;
 
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.matrix.biz.bean.BizUser;
import com.matrix.biz.dao.BizUserDao;
import com.matrix.biz.service.BizUserService;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.VerificationResult;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.fenxiao.constant.FenxiaoSettingConstant;
import com.matrix.system.fenxiao.dao.ShopSalesmanApplyDao;
import com.matrix.system.fenxiao.dao.ShopSalesmanGradeDao;
import com.matrix.system.fenxiao.dto.*;
import com.matrix.system.fenxiao.entity.ShopSalesmanApply;
import com.matrix.system.fenxiao.entity.ShopSalesmanGrade;
import com.matrix.system.fenxiao.vo.*;
import com.matrix.system.hive.action.util.QueryUtil;
import com.matrix.system.shopXcx.dao.ShopOrderDaoV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * @description 推广员申请记录
 * @author jyy
 * @date 2021-03-10 15:22
 */
@Service
public class ShopSalesmanApplyService  extends ServiceImpl<ShopSalesmanApplyDao, ShopSalesmanApply>{
 
 
    @Autowired
    BusParameterSettingsDao busParameterSettingsDao;
 
    @Autowired
    ShopSalesmanApplyDao shopSalesmanApplyDao;
    
    @Autowired
    ShopSalesmanGradeDao shopSalesmanGradeDao;
 
    @Autowired
    private BizUserDao bizUserDao;
 
    @Autowired
    BizUserService bizUserService;
 
    @Autowired
    ShopOrderDaoV2 shopOrderDaoV2;
 
    /**
     * 申请成为推广员
     * @param openId
     * @param invitationId
     * @return
     */
    public ShopSalesmanApply applyToBeAnSalesman(String openId,String gradeId,String invitationId,int applyWay) {
 
        BizUser loginUser=bizUserDao.findByOpenId(openId);
        //校验审核状态,和是否重复发起
        QueryWrapper<ShopSalesmanApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",loginUser.getOpenId());
        queryWrapper.in("apply_status",Arrays.asList("1,2".split(",")));
        ShopSalesmanApply checkApply = shopSalesmanApplyDao.selectOne(queryWrapper);
        if(checkApply==null||
                checkApply.getApplyStatus()==ShopSalesmanApply.APPLY_STATUS_WTG){
 
            ShopSalesmanApply shopSalesmanApply=new ShopSalesmanApply();
            shopSalesmanApply.setUserId(openId);
            
            shopSalesmanApply.setCreateBy(MatrixConstance.SYSTEM_USER);
            shopSalesmanApply.setApplyWay(ShopSalesmanApply.APPLY_WAY_SELF);
 
            shopSalesmanApply.setCompanyId(loginUser.getCompanyId());
            shopSalesmanApply.setUpdateBy(MatrixConstance.SYSTEM_USER);
            Date date = new Date();
            shopSalesmanApply.setCreateTime(date);
            shopSalesmanApply.setUpdateTime(date);
 
            if(StringUtils.isNotBlank(invitationId)){
                shopSalesmanApply.setParentUserId(invitationId);
            }else if(StringUtils.isNotBlank(loginUser.getParentOpenId())){
                //如果曾经是被邀请进来的则自动绑定为之前邀请人的下级
                shopSalesmanApply.setParentUserId(loginUser.getParentOpenId());
            }
            if(StringUtils.isNotBlank(gradeId)){
                shopSalesmanApply.setGradeId(Long.parseLong(gradeId));
            }else{
                //获取初始等级ID(公司id,是否为初始等级)
                QueryWrapper<ShopSalesmanGrade> queryWrapperOrepool = new QueryWrapper<>();
                queryWrapperOrepool.eq("company_id", loginUser.getCompanyId());
                queryWrapperOrepool.eq("is_default", 1);
                ShopSalesmanGrade shopSalesmanGrade = shopSalesmanGradeDao.selectOne(queryWrapperOrepool);
                shopSalesmanApply.setGradeId(shopSalesmanGrade.getId());
            }
            shopSalesmanApply.setApplyWay(applyWay);
            
            BusParameterSettings busParameterSettings = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_AUDIT_METHOD, loginUser.getCompanyId());
            if(busParameterSettings!=null
                    &&busParameterSettings.getParamValue().equals("1")){
                //自动审核
                shopSalesmanApply.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_TG);
                bizUserService.setToBeAnSalesman(loginUser.getOpenId(),invitationId);
 
            }else{
                shopSalesmanApply.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_DSH);
            }
            shopSalesmanApplyDao.insert(shopSalesmanApply);
            return  shopSalesmanApply;
        }else{
            throw  new GlobleException("不能重复申请");
        }
 
    }
 
 
    /**
     * 判断是否满足申请成为分销员的条件
     */
    public VerificationResult isAbleToBeAnSalesman(String openId) {
        BizUser bizUser = bizUserDao.findByOpenId(openId);
        VerificationResult verificationResult=null;
        //申请条件
        BusParameterSettings applyCondition = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_APPLY_CONDITION, bizUser.getCompanyId());
        switch (applyCondition.getParamValue()){
            case FenxiaoSettingConstant.FX_APPLY_CONDITION_WTJ:
                VerificationResult.buildVerificationResult(true);
            case FenxiaoSettingConstant.FX_APPLY_CONDITION_XFZDCP:
                VerificationResult.buildVerificationResult(true);
            case FenxiaoSettingConstant.FX_APPLY_CONDITION_XFRYCP:
 
                //判断用户是否有确认收货的产品
                int receivedOrderCount= shopOrderDaoV2.selectReceivedOrderCount(openId);
 
                if(receivedOrderCount>0){
                    verificationResult= VerificationResult.buildVerificationResult(true);
                }else{
                    verificationResult= VerificationResult.buildVerificationResult(false,"不符合申请条件,请消费任意产品后再申请!");
                }
 
            case FenxiaoSettingConstant.FX_APPLY_CONDITION_XCZDJE:
                VerificationResult.buildVerificationResult(true);
            default:
                VerificationResult.buildVerificationResult(true);
        }
        return verificationResult;
    }
 
 
    public IPage<ShopSalesmanApplyVo> findShopSalesmanApplyList(Page<ShopSalesmanApplyVo> page, ShopSalesmanApplyDto shopSalesmanApplyDto) {
        return shopSalesmanApplyDao.findShopSalesmanApplyList(page,shopSalesmanApplyDto);
    }
 
    public IPage<ShopSalesmanAppliingVo> findShopSalesmanAppliingList(Page<ShopSalesmanAppliingVo> page,
            ShopSalesmanAppliingDto shopSalesmanAppliingDto) {
        return shopSalesmanApplyDao.findShopSalesmanAppliingList(page,shopSalesmanAppliingDto);
    }
 
    public IPage<ShopSalesmanAppliingVo> selectBizUserApplyList(Page<ShopSalesmanAppliingVo> page, ShopSalesmanAppliingDto shopSalesmanAppliingDto) {
        return shopSalesmanApplyDao.selectBizUserApplyList(page,shopSalesmanAppliingDto);
    }
 
    public List<ShopSalesmanGradeVo> getShopSalesmanGradeVo(Long companyId) {
        return shopSalesmanGradeDao.getShopSalesmanGradeVo(companyId);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void addSaleManApply(String userId,String gradeId) {
        BizUser user = bizUserDao.findByOpenId(userId);
        applyToBeAnSalesman(user.getOpenId(),gradeId, "",ShopSalesmanApply.APPLY_WAY_HAND_ADD);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void examineSaleManApply(ShopSalesmanApply shopSalesmanApply, Integer applyState) {
        String userId = shopSalesmanApply.getUserId();
        String parentUserId = shopSalesmanApply.getParentUserId();
        BizUser bizUser = bizUserDao.findByOpenId(userId);
        //修改审核记录
        if(ShopSalesmanApply.APPLY_STATUS_TG == applyState) {
            shopSalesmanApply.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_TG);
            bizUser.setIsSales(BizUser.IS_SALES);
        }else {
            shopSalesmanApply.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_WTG);
            bizUser.setIsSales(BizUser.NOT_SALES);
        }
        shopSalesmanApplyDao.updateById(shopSalesmanApply);
        //修改USER的状态
        bizUser.setParentOpenId(parentUserId);
        bizUser.setBindingParentTime(new Date());
        bizUserDao.updateByModel(bizUser);
    }
 
    public IPage<ShopSalesmanApplyVo> findShopSalesmanDetail(Page<ShopSalesmanApplyVo> page,
            ShopSalesmanDetailDto shopSalesmanDetailDto) {
        // TODO Auto-generated method stub
        return null;
    }
 
    public SalesmanBasicDetailVo selectShopSalesmanDetailByOpenId(String userId, long applyId) {
        //获取个人信息
        BizUser bizUser = bizUserDao.findByOpenId(userId);
        Long companyId = bizUser.getCompanyId();
        return shopSalesmanApplyDao.selectShopSalesmanDetailByOpenId(userId,companyId,applyId);
    }
 
    public IPage<ShopCustomDetailVo> findCustomDetail(Page<ShopCustomDetailVo> page,
                                                      ShopSalesmanDetailDto shopSalesmanDetailDto) {
        return shopSalesmanApplyDao.findCustomDetail(page,shopSalesmanDetailDto);
    }
 
    public IPage<ShopCustomDetailVo> findCustomLow(Page<ShopCustomDetailVo> pageLow,
            ShopSalesmanDetailDto shopSalesmanDetailDto) {
        return shopSalesmanApplyDao.findCustomLow(pageLow,shopSalesmanDetailDto);
    }
 
    public IPage<ShopOrderDetailVo> findShopOrderDetail(Page<ShopOrderDetailVo> pageOrder,
            ShopSalesmanDetailDto shopSalesmanDetailDto) {
        return shopSalesmanApplyDao.findShopOrderDetail(pageOrder,shopSalesmanDetailDto);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult changeSaleManGrade(ChangeSaleManGradeDto changeSaleManGradeDto) {
        //设置用户公司ID
        QueryUtil.setQueryLimitCom(changeSaleManGradeDto);
        String userId = changeSaleManGradeDto.getUserId();
        Long companyId = changeSaleManGradeDto.getCompanyId();
        //获取对应的公司的等级
        QueryWrapper<ShopSalesmanGrade> queryWrapperOrepool = new QueryWrapper<>();
        queryWrapperOrepool.eq("is_default", 1);
        queryWrapperOrepool.eq("company_id", companyId);
        ShopSalesmanGrade shopSalesmanGrade = shopSalesmanGradeDao.selectOne(queryWrapperOrepool);
        //修改Biz_User的salesman_grade
        BizUser findByOpenId = bizUserDao.findByOpenId(userId);
        if(ObjectUtil.isEmpty(findByOpenId)) {
            return AjaxResult.buildFailInstance("当前用户已不存在");
        }
        //不是分销员不允许修改
        Integer isSales = findByOpenId.getIsSales();
        if(BizUser.IS_SALES != isSales) {
            return AjaxResult.buildFailInstance("当前用户未通过审核,不允许修改");
        }
        
        //修改申请记录的等级
        QueryWrapper<ShopSalesmanApply> queryShopSalesmanApply = new QueryWrapper<>();
        queryShopSalesmanApply.eq("apply_status", 2);
        queryShopSalesmanApply.eq("user_id", userId);
        queryShopSalesmanApply.eq("company_id", companyId);
        ShopSalesmanApply shopSalesmanApply = shopSalesmanApplyDao.selectOne(queryShopSalesmanApply);
        Long gradeId = changeSaleManGradeDto.getGradeId();
        if(ObjectUtil.isEmpty(gradeId)) {
            findByOpenId.setSalesmanGrade(shopSalesmanGrade.getId());
            shopSalesmanApply.setGradeId(shopSalesmanGrade.getId());
        }else {
            findByOpenId.setSalesmanGrade(gradeId);
            shopSalesmanApply.setGradeId(gradeId);
        }
        bizUserDao.updateByModel(findByOpenId);
        shopSalesmanApplyDao.updateById(shopSalesmanApply);
        
        return AjaxResult.buildFailInstance("修改等级成功");
    }
 
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult unbundlingSaleMan(UnbundlingSaleManDto unbundlingSaleManDto) {
        //设置用户公司ID
        QueryUtil.setQueryLimitCom(unbundlingSaleManDto);
        String userId = unbundlingSaleManDto.getUserId();
        //将Biz_User的parent_open_id置空
        BizUser findByOpenId = bizUserDao.findByOpenId(userId);
        if(ObjectUtil.isEmpty(findByOpenId)) {
            return AjaxResult.buildFailInstance("当前用户已不存在");
        }
        findByOpenId.setParentOpenId("");
        bizUserDao.updateByModel(findByOpenId);
        return AjaxResult.buildFailInstance("解绑成功");
    }
 
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delSaleManGradeApply(DelSaleManGradeApplyDto delSaleManGradeApplyDto) {
        //设置用户公司ID
        QueryUtil.setQueryLimitCom(delSaleManGradeApplyDto);
        String userId = delSaleManGradeApplyDto.getUserId();
        Long applyid = delSaleManGradeApplyDto.getApplyId();
        
        //将biz_user的is_sale设置成否
        BizUser findByOpenId = bizUserDao.findByOpenId(userId);
        if(ObjectUtil.isEmpty(findByOpenId)) {
            return AjaxResult.buildFailInstance("当前用户已不存在");
        }
        findByOpenId.setIsSales(BizUser.NOT_SALES);
        bizUserDao.updateByModel(findByOpenId);
        //将记录设置成系统取消
        ShopSalesmanApply selectById = shopSalesmanApplyDao.selectById(applyid);
        selectById.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_XTQX);
        shopSalesmanApplyDao.updateById(selectById);
        return AjaxResult.buildFailInstance("操作成功");
    }
 
    public IPage<FyfaManageVo> findFyfaManageList(Page<FyfaManageVo> page, FyfaManageDto fyfaManageDto) {
        return shopSalesmanGradeDao.findFyfaManageList(page,fyfaManageDto);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void addFyfa(SysUsers user) {
        ShopSalesmanGrade shopSalesmanGrade = new ShopSalesmanGrade();
        shopSalesmanGrade.setIsDefault(2);
        shopSalesmanGrade.setCreateBy(user.getSuName());
        shopSalesmanGrade.setUpdateBy(user.getSuName());
        shopSalesmanGrade.setCreateTime(new Date());
        shopSalesmanGrade.setUpdateTime(new Date());
        shopSalesmanGrade.setCompanyId(user.getCompanyId());
        shopSalesmanGradeDao.insert(shopSalesmanGrade);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delFyfaApply(DelFyfaApplyDto delFyfaApplyDto) {
        //设置用户公司ID
        QueryUtil.setQueryLimitCom(delFyfaApplyDto);
        Long gradeId = delFyfaApplyDto.getGradeId();
        ShopSalesmanGrade selectById = shopSalesmanGradeDao.selectById(gradeId);
        if(ObjectUtil.isEmpty(selectById)) {
            return AjaxResult.buildFailInstance("当前行数据不存在,请刷新页面");
        }
        shopSalesmanGradeDao.deleteById(gradeId);
        return AjaxResult.buildFailInstance("操作成功");
    }
 
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateFyfa(UpdateFyfaDto updateFyfaDto) {
        
        ShopSalesmanGrade selectById = shopSalesmanGradeDao.selectById(updateFyfaDto.getId());
        if(ObjectUtil.isEmpty(selectById)) {
            return AjaxResult.buildFailInstance("当前行数据不存在,请刷新页面");
        }
        String name = updateFyfaDto.getName();
        if(StrUtil.isEmpty(name)) {
            return AjaxResult.buildFailInstance("请输入方案名称");
        }
        Double sealesCommission = updateFyfaDto.getSealesCommission();
        if(ObjectUtil.isEmpty(sealesCommission)) {
            return AjaxResult.buildFailInstance("请输入正确的推广提成");
        }
        Double invitationCommission = updateFyfaDto.getInvitationCommission();
        if(ObjectUtil.isEmpty(invitationCommission)) {
            return AjaxResult.buildFailInstance("请输入正确的邀请提成");
        }
        //设置用户公司ID
        QueryUtil.setQueryLimitCom(updateFyfaDto);
        selectById.setCompanyId(updateFyfaDto.getCompanyId());
        selectById.setName(updateFyfaDto.getName());
        selectById.setSealesCommission(sealesCommission);
        selectById.setInvitationCommission(invitationCommission);
        selectById.setGradeCondition(updateFyfaDto.getGradeCondition());
        shopSalesmanGradeDao.updateById(selectById);
        return AjaxResult.buildFailInstance("操作成功");
    }
    
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateTgjh(UpdateTgjhDto updateTgjhDto) {
        //设置用户公司ID
        QueryUtil.setQueryLimitCom(updateTgjhDto);
        //获取推广计划的Param
        Long companyId = updateTgjhDto.getCompanyId();
        BusParameterSettings busParameterSettings = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_TG_PLAN, companyId);
        busParameterSettings.setParamValue3(updateTgjhDto.getTgfa());
        busParameterSettingsDao.updateByModel(busParameterSettings);
        return AjaxResult.buildFailInstance("保存成功");
    }
 
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateTgtp(UpdateTgtpDto updateTgtpDto) {
        //设置用户公司ID
        QueryUtil.setQueryLimitCom(updateTgtpDto);
        //获取推广计划的Param
        Long companyId = updateTgtpDto.getCompanyId();
        BusParameterSettings busParameterSettings = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_TG_POSTER, companyId);
        busParameterSettings.setParamValue(updateTgtpDto.getImageUrl());
        busParameterSettingsDao.updateByModel(busParameterSettings);
        return AjaxResult.buildFailInstance("保存成功");
    }
 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}