| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- package com.cyksj.service.mange.group;
- import cn.hutool.core.date.DateField;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.cyksj.common.constant.Constant;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
- import com.cyksj.common.util.SmsUtil;
- import com.cyksj.mapper.*;
- import com.cyksj.mapper.manage.GroupsTripsAccountReplaceRecordMapper;
- import com.cyksj.mapper.manage.cms.CmsUserMapper;
- import com.cyksj.mapper.market.task.UserBindDetailMapper;
- import com.cyksj.model.entity.*;
- import com.cyksj.model.request.ReplaceTripsAccountReq;
- import com.cyksj.model.views.GroupsAccountView;
- import com.cyksj.service.mange.CmsGroupService;
- import com.ejlchina.searcher.BeanSearcher;
- import com.ejlchina.searcher.util.MapUtils;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.Date;
- import java.util.List;
- import java.util.Optional;
- /**
- * @author chan
- * @date 2022/9/27 6:27 PM
- */
- @Slf4j
- @Service
- @RequiredArgsConstructor
- public class CmsGroupServiceImpl extends ServiceImpl<GroupsMapper,GroupsTrips> implements CmsGroupService {
- private final GoodsDonSkuMapper skuMapper;
- private final GroupsRelationMapper relationMapper;
- private final OrderDonMapper orderDonMapper;
- private final AccountMapper accountMapper;
- private final BeanSearcher beanSearcher;
- private final GroupsTripsAccountReplaceRecordMapper replaceRecordMapper;
- private final CmsUserMapper cmsUserMapper;
- private final GoodsDonMapper goodsDonMapper;
- private UserBindDetailMapper userBindDetailMapper;
- private final UserMapper userMapper;
- private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
- @Override
- public GroupsTrips issuance(GroupsTrips groups) {
- GoodsDonSku sku = skuMapper.selectById(groups.getSkuId());
- if (sku == null) {
- throw new BusinessRuntimeException("该商品规格不存在!");
- }
- groups.setNum(sku.getNum());
- groups.setAvailableNum(sku.getNum());
- if (groups.getAccountId() == null || accountMapper.selectById(groups.getAccountId()) == null) {
- throw new BusinessRuntimeException("该账号不存在");
- }
- groups.setStatus(GroupsTrips.Status.validity);
- this.save(groups);
- for (Integer i = 1; i <= groups.getNum(); i++) {
- GroupsRelation relation = new GroupsRelation();
- relation.setGroupsId(groups.getId());
- relation.setStatus(GroupsRelation.Status.none);
- relation.setNum(i);
- relationMapper.insert(relation);
- }
- return groups;
- }
- @Override
- public void close(Long groupId) {
- GroupsTrips groupsTrips = this.getById(groupId);
- if(groupsTrips == null){
- throw new BusinessRuntimeException("非法参数.车队不存在");
- }
- Integer count = relationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
- .eq(GroupsRelation::getGroupsId, groupId)
- .in(GroupsRelation::getStatus, List.of(GroupsRelation.Status.locking, GroupsRelation.Status.validity)));
- if (count > 0) {
- throw new BusinessRuntimeException("该车队已有用户购买或锁定.无法删除");
- }else {
- relationMapper.delete(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId,groupId));
- this.removeById(groupId);
- }
- }
- @Override
- @Transactional(rollbackFor = Throwable.class)
- public void setAccount(GroupsTrips groupsTrips) {
- int count = count(Wrappers.lambdaQuery(GroupsTrips.class).eq(GroupsTrips::getAccountId, groupsTrips.getAccountId()));
- if (count > 0) {
- throw new BusinessRuntimeException("该账号已发车.请选择未发车账户");
- }
- List<GroupsRelation> groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()).eq(GroupsRelation::getStatus, GroupsRelation.Status.validity));
- groupsRelations.forEach((relation)->{
- if (relation.getExpiryTime() == null) {
- List<OrderDon> orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getUserId, relation.getUserId()).eq(OrderDon::getRelationId, relation.getId()).eq(OrderDon::getStatus, OrderDon.Status.hasPayment));
- orderDonList.forEach((orderDon)->{
- //如果续费增加时间,如果首次则设置当前时间为初始时间
- Date expiryTime = Optional.ofNullable(relation.getExpiryTime()).orElse(new Date());
- log.info("设置账号,该用户 {} 初始时间:{} ",orderDon.getUserId(),DateUtil.format(expiryTime,"yyyy-MM-dd HH:mm:ss"));
- GoodsDonSku donSku = skuMapper.selectById(orderDon.getSkuId());
- Date newDate = DateUtil.offset(expiryTime, DateField.MONTH, donSku.getMonths() * orderDon.getNum());
- relation.setExpiryTime(newDate);
- log.info("设置账号,该用户 {} 过期时间:{} ",orderDon.getUserId(),DateUtil.format(newDate,"yyyy-MM-dd HH:mm:ss"));
- relation.setStartTime(relation.getStartTime() == null ? new Date() : null);
- relationMapper.updateById(relation);
- orderDon.setStatus(OrderDon.Status.complete);
- orderDonMapper.updateById(orderDon);
- });
- }
- });
- groupsTrips.setStatus(GroupsTrips.Status.validity);
- updateById(groupsTrips);
- }
- @Override
- @Transactional(rollbackFor = Throwable.class)
- public void replaceTripsAccount(ReplaceTripsAccountReq req) {
- Long groupsId = req.getGroupsId();
- String account = req.getAccount().trim();
- String password = req.getPassword().trim();
- String remark = req.getRemark();
- GroupsAccountView groupsAccountView = beanSearcher.searchFirst(GroupsAccountView.class, MapUtils.builder()
- .field(GroupsAccountView::getGroupsId, groupsId)
- .build());
- if (groupsId == null) {
- throw BusinessRuntimeException.getInstance("车队已不存在");
- }
- Long accountId = groupsAccountView.getAccountId();
- if (accountId == null) {
- throw BusinessRuntimeException.getInstance("该车队暂未配置账号");
- }
- Long goodsId = groupsAccountView.getGoodsId();
- if (!account.contains("客服")) {
- int count = accountMapper.selectCount(Wrappers.lambdaQuery(Account.class)
- .eq(Account::getAccount, account)
- .eq(Account::getGoodsId, goodsId));
- if (count > 0) {
- throw BusinessRuntimeException.getInstance("该平台下已生成该账号");
- }
- }
- //新增账号
- Account replace = new Account();
- replace.setAccount(account);
- replace.setPassword(password);
- replace.setGoodsId(goodsId);
- replace.setStartTime(groupsAccountView.getStartTime());
- replace.setExpiryTime(groupsAccountView.getExpiryTime());
- replace.setRemark(remark);
- accountMapper.insert(replace);
- //替换车队账号
- this.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
- .set(GroupsTrips::getAccountId, replace.getId())
- .eq(GroupsTrips::getId, groupsId));
- //删除旧的账号
- accountMapper.deleteById(accountId);
- //保存替换记录
- GroupsTripsAccountReplaceRecord replaceRecord = new GroupsTripsAccountReplaceRecord();
- replaceRecord.setGroupsId(groupsId);
- replaceRecord.setOldAccountId(accountId);
- replaceRecord.setOldAccount(groupsAccountView.getAccount());
- replaceRecord.setOldPassword(groupsAccountView.getPassword());
- replaceRecord.setOldStartTime(groupsAccountView.getStartTime());
- replaceRecord.setOldExpiryTime(groupsAccountView.getExpiryTime());
- Long adminId = req.getAdminId();
- Optional.ofNullable(cmsUserMapper.selectById(adminId)).ifPresent(admin -> replaceRecord.setOperator(admin.getNickname()));
- replaceRecord.setNewAccountId(replace.getId());
- replaceRecord.setNewAccount(replace.getAccount());
- replaceRecord.setNewPassword(replace.getPassword());
- replaceRecord.setNewStartTime(replace.getStartTime());
- replaceRecord.setNewExpiryTime(replace.getExpiryTime());
- replaceRecordMapper.insert(replaceRecord);
- TASK_EXECUTOR.execute(() -> sendChangeAccountMsg(groupsId, goodsId));;
- }
- private void sendChangeAccountMsg(Long groupsId,Long goodsId) {
- GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
- List<GroupsRelation> groupsRelations = beanSearcher.searchAll(GroupsRelation.class, MapUtils.builder().field(GroupsRelation::getGroupsId, groupsId).build());
- groupsRelations.forEach(relation->{
- Long userId = relation.getUserId();
- User user = userMapper.selectById(userId);
- String phone = null;
- if (user != null) {
- phone = user.getLoginPhone();
- if (StrUtil.isEmpty(phone)) {
- UserBindDetail userBindDetail = userBindDetailMapper.selectById(userId);
- if (userBindDetail != null) {
- phone = userBindDetail.getPhone();
- }
- }
- }
- if (StrUtil.isNotBlank(phone)) {
- String title = goodsDon != null ? goodsDon.getTitle() : "车票";
- SmsUtil.sendLuoKey2Msg(phone, String.format(Constant.CHANGE_GROUPS_TRIPS_ACCOUNT, title));
- }
- });
- }
- }
|