doum
2025-12-12 dce1e83ec27a066ebc6c17a4ac6d03c9ad6ff703
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
package com.doumee.service.business.impl;
 
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.DateUtil;
import com.doumee.core.utils.ID;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.CardMapper;
import com.doumee.dao.business.CardParamMapper;
import com.doumee.dao.business.join.CardJoinMapper;
import com.doumee.dao.business.model.Card;
import com.doumee.dao.business.model.CardParam;
import com.doumee.dao.business.model.Scan;
import com.doumee.dao.web.dto.AddCardDTO;
import com.doumee.dao.web.dto.CardDTO;
import com.doumee.dao.web.dto.QueryCardDTO;
import com.doumee.dao.web.request.DealIntegralRequest;
import com.doumee.service.business.CardService;
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.doumee.service.business.IntegralService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
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.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 充值卡信息表Service实现
 * @author 江蹄蹄
 * @date 2023/04/13 11:22
 */
@Service
public class CardServiceImpl implements CardService {
 
    @Autowired
    private CardMapper cardMapper;
 
    @Autowired
    private CardParamMapper cardParamMapper;
 
    @Autowired
    private CardJoinMapper cardJoinMapper;
 
    @Autowired
    private IntegralService integralService;
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public List<Integer> create(AddCardDTO card) {
 
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
 
        CardParam cardParam = cardParamMapper.selectById(card.getParamId());
        if (Objects.isNull(cardParam)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"卡类型参议异常");
        }
        Integer num = card.getNum();
 
        int startNo = Integer.valueOf(cardParam.getStartNo());
        Card count = new Card();
        count.setParamId(card.getParamId());
        long countNum = Constants.formatLongNum(count(count));
        startNo = countNum != 0 ? (int) (startNo + countNum) : startNo;
 
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i <num; i++) {
            Card insert = new Card();
            insert.setParamId(card.getParamId());
            insert.setCreator(loginUserInfo.getId());
            insert.setEditor(loginUserInfo.getId());
 
            String code = cardParam.getIndexNo()+String.format("%07d", startNo+i);
            String password = ID.nextGUID().substring(0, 8);
 
            insert.setCode(code);
            insert.setPassword(password);
            insert.setMoney(card.getMoney());
            insert.setIsUsed(Constants.ZERO);
            Date validDate = Objects.isNull(card.getValidDate()) ? null : DateUtil.toDate(LocalDateTime.of(card.getValidDate(),  LocalTime.of(23, 59, 59)));
            insert.setValidDate(validDate);
            insert.setStatus(Constants.ZERO);
            insert.setSortnum(startNo+i);
            cardMapper.insert(insert);
            list.add(insert.getId());
        }
 
        return list;
    }
 
    @Override
    public void deleteById(Integer id) {
        Card card = new Card();
        card.setId(id);
        card.setIsdeleted(Constants.ONE);
        cardMapper.updateById(card);
    }
 
    @Override
    public void delete(Card card) {
        UpdateWrapper<Card> deleteWrapper = new UpdateWrapper<>(card);
        cardMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        cardMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(AddCardDTO card) {
        Card update = new Card();
        update.setId(card.getId());
        update.setStatus(card.getStatus());
        Date validDate = Objects.isNull(card.getValidDate()) ? null : DateUtil.toDate(LocalDateTime.of(card.getValidDate(),  LocalTime.of(23, 59, 59)));
        update.setValidDate(validDate);
        cardMapper.updateById(update);
    }
 
    @Override
    public void updateByIdInBatch(List<AddCardDTO> cards) {
        if (CollectionUtils.isEmpty(cards)) {
            return;
        }
        for (AddCardDTO card: cards) {
            this.updateById(card);
        }
    }
 
    @Override
    public Card findById(Integer id) {
        return cardMapper.selectById(id);
    }
 
    @Override
    public Card findOne(Card card) {
        QueryWrapper<Card> wrapper = new QueryWrapper<>(card);
        return cardMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Card> findList(Card card) {
        QueryWrapper<Card> wrapper = new QueryWrapper<>(card);
        return cardMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<CardDTO> findPage(PageWrap<QueryCardDTO> pageWrap) {
        IPage<Card> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<Card> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.eq(Card::getIsdeleted,Constants.ZERO);
        if (pageWrap.getModel().getStartDateTime() != null && pageWrap.getModel().getEndDateTime() != null) {
            queryWrapper.ge(Card::getCreateDate, pageWrap.getModel().getStartDateTime());
            queryWrapper.le(Card::getCreateDate, pageWrap.getModel().getEndDateTime());
        }
 
        if (pageWrap.getModel().getCode() != null) {
            queryWrapper.likeRight(Card::getCode, pageWrap.getModel().getCode());
        }
 
        if (pageWrap.getModel().getIsUsed() != null) {
            queryWrapper.eq(Card::getIsUsed, pageWrap.getModel().getIsUsed());
        }
 
        if (pageWrap.getModel().getStatus() != null) {
            queryWrapper.eq(Card::getStatus, pageWrap.getModel().getStatus());
        }
        queryWrapper.selectAll(Card.class)
                .selectAs(CardParam::getName,CardDTO::getParamName);
 
        queryWrapper.leftJoin(CardParam.class,CardParam::getId,Card::getParamId);
        queryWrapper.orderByDesc(Card::getCreateDate);
//        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
//            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
//                queryWrapper.orderByDesc(sortData.getProperty());
//            } else {
//                queryWrapper.orderByAsc(sortData.getProperty());
//            }
//        }
 
        return PageData.from(cardJoinMapper.selectJoinPage(page, CardDTO.class,queryWrapper));
    }
 
    @Override
    public Long count(Card card) {
        QueryWrapper<Card> wrapper = new QueryWrapper<>(card);
        return cardMapper.selectCount(wrapper);
    }
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public void exchangeCard(String code, String password, Integer memberId) {
 
        QueryWrapper<Card> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Card::getCode,code)
//                        .eq(Card::getPassword,password.toUpperCase())
//                        .eq(Card::getIsdeleted,Constants.ZERO)
                        .last("limit 1");
        Card card = cardMapper.selectOne(wrapper);
        if (Objects.isNull(card)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"卡号不正确");
        }
 
        if (Constants.equalsInteger(card.getIsdeleted(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"卡已删除");
        }
 
        if ((!Objects.isNull(card)) && Constants.equalsInteger(card.getIsUsed(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"充值卡已使用");
        }
 
        if (Constants.equalsInteger(card.getStatus(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"充值卡已经被禁用");
        }
 
        if (Objects.nonNull(card.getValidDate()) && card.getValidDate().before(new Date())){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"充值卡已经过期");
        }
 
        if (!card.getPassword().equalsIgnoreCase(password)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"密码错误");
        }
 
 
        UpdateWrapper<Card> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(Card::getIsUsed,Constants.ONE)
                .set(Card::getMemberId,memberId)
                .set(Card::getUseDate,new Date());
        updateWrapper.lambda().eq(Card::getId,card.getId());
        cardMapper.update(null,updateWrapper);
 
        DealIntegralRequest dealIntegralRequest = new DealIntegralRequest();
        dealIntegralRequest.setIntegralObjType(Constants.IntegralObjType.EXCHANGE_CARD);
        dealIntegralRequest.setIntegralNum(card.getMoney());
        dealIntegralRequest.setMemberId(memberId);
        dealIntegralRequest.setObjId(card.getId());
        dealIntegralRequest.setDealType(Constants.ZERO);
        integralService.dealIntegral(dealIntegralRequest);
    }
}