jiangping
2025-02-19 28e6fd1321dafed1e2083abe50b2aea2467405d8
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
package com.doumee.service.business.impl;
 
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.Constants;
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.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.DiscountMapper;
import com.doumee.dao.business.join.BikeRepairJoinMapper;
import com.doumee.dao.business.join.DiscountJoinMapper;
import com.doumee.dao.business.model.BaseParam;
import com.doumee.dao.business.model.BikeRepair;
import com.doumee.dao.business.model.Discount;
import com.doumee.dao.business.model.Member;
import com.doumee.dao.business.web.request.BikeRepairDTO;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.DiscountService;
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.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.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 骑行套餐信息表Service实现
 * @author 江蹄蹄
 * @date 2025/02/17 09:43
 */
@Service
public class DiscountServiceImpl implements DiscountService {
 
    @Autowired
    private DiscountMapper discountMapper;
 
    @Autowired
    private DiscountJoinMapper discountJoinMapper;
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    @Override
    public String create(Discount discount) {
        LoginUserInfo userInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        this.vaildReq(discount);
        discount.setStatus(Constants.ZERO);
        discount.setIsdeleted(Constants.ZERO);
        discount.setCreateDate(new Date());
        discount.setCreator(userInfo.getId());
        if(Constants.equalsInteger(discount.getUseType(),Constants.ZERO)){
            discount.setUseDays((int) (DateUtil.getBetweenDays(DateUtil.dateToString(discount.getUseStartDate(),"yyyy-MM-dd"),DateUtil.dateToString(discount.getUseEndDate(),"yyyy-MM-dd"))));
        }
        discountMapper.insert(discount);
        return discount.getId();
    }
 
 
    public void vaildReq(Discount discount) {
        if(Objects.isNull(discount)
                || StringUtils.isBlank(discount.getName())
                || Objects.isNull(discount.getType())
                || Objects.isNull(discount.getLimitType())
                || (Constants.equalsInteger(discount.getLimitTime(),Constants.ONE) && Objects.isNull(discount.getLimitTime()))
                || Objects.isNull(discount.getPrice()) || discount.getPrice().compareTo(BigDecimal.ZERO) <= Constants.ZERO
                || Objects.isNull(discount.getChannel())
                || Objects.isNull(discount.getStartDate())
                || Objects.isNull(discount.getEndDate())
                || Objects.isNull(discount.getUseType())
                || (Constants.equalsInteger(discount.getUseType(),Constants.ZERO) && ( Objects.isNull(discount.getUseStartDate()) || Objects.isNull(discount.getUseEndDate()) ))
                || (Constants.equalsInteger(discount.getUseType(),Constants.ONE) && Objects.isNull(discount.getUseDays()))
                || (Constants.equalsInteger(discount.getUseType(),Constants.TWO) && ( Objects.isNull(discount.getUseStartDate()) || Objects.isNull(discount.getUseDays()) ))
                || (Objects.isNull(discount.getUseHoliday()) || Objects.isNull(discount.getUseWorkday()) )
                || (Objects.isNull(discount.getIsbike()) || Objects.isNull(discount.getIselecbike()) )
        ){
            throw new BusinessException(ResponseStatus.SERVER_ERROR);
        }
        if(discount.getStartDate().getTime()>discount.getEndDate().getTime()){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"销售时段信息错误");
        }
        //固定日期生效
        if(Constants.equalsInteger(discount.getUseType(),Constants.ZERO) &&
                ( discount.getUseStartDate().getTime()>discount.getUseEndDate().getTime() ||
                        discount.getUseStartDate().getTime() < discount.getStartDate().getTime() ||
                        discount.getUseEndDate().getTime() > discount.getEndDate().getTime()
                ) ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"使用时段信息错误");
        }
 
        //指定日期生效
        if(Constants.equalsInteger(discount.getUseType(),Constants.TWO) &&
                ( discount.getUseStartDate().getTime()<discount.getStartDate().getTime() ||
                        discount.getUseStartDate().getTime() > discount.getEndDate().getTime()
                ) ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"使用时段信息错误");
        }
    }
 
 
    @Override
    public void deleteById(String id) {
        Discount discount = discountMapper.selectById(id);
        if(Objects.isNull(discount)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(Constants.equalsInteger(discount.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非禁用数据,无法进行删除");
        }
        discountMapper.update(null,new UpdateWrapper<Discount>().lambda().set(Discount::getIsdeleted,Constants.ONE).eq(Discount::getId,discount.getId()));
    }
 
 
    @Override
    public void updStatus(Discount discount) {
        if(Objects.isNull(discount)
            || StringUtils.isBlank(discount.getId())
            || Objects.isNull(discount.getStatus())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        discountMapper.update(null,new UpdateWrapper<Discount>().lambda().set(Discount::getStatus,discount.getStatus()).eq(Discount::getId,discount.getId()));
    }
 
 
    @Override
    public void delete(Discount discount) {
        UpdateWrapper<Discount> deleteWrapper = new UpdateWrapper<>(discount);
        discountMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        discountMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(Discount discount) {
        discountMapper.updateById(discount);
    }
 
    @Override
    public void updateByIdInBatch(List<Discount> discounts) {
        if (CollectionUtils.isEmpty(discounts)) {
            return;
        }
        for (Discount discount: discounts) {
            this.updateById(discount);
        }
    }
 
    @Override
    public Discount findById(String id) {
        Discount discount = discountJoinMapper.selectOne(new MPJLambdaWrapper<Discount>()
                .select(" (select count(1) from goodsorder g where g.obj_type = 0 and g.obj_id = t.id and g.pay_status = 1 ) ",Discount::getSaleNum)
                .leftJoin(SystemUser.class,SystemUser::getId,Discount::getCreator)
                .eq(Discount::getId,id)
        );
        if(Objects.isNull(discount)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        this.dealPrice(discount);
        if(StringUtils.isNotBlank(discount.getImgurl())){
            String path =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode()+
                    systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.DISCOUNT).getCode();
            discount.setFullImgUrl(path + discount.getImgurl());
        }
        return discount;
    }
 
    @Override
    public Discount findOne(Discount discount) {
        QueryWrapper<Discount> wrapper = new QueryWrapper<>(discount);
        return discountMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Discount> findList(Discount discount) {
        QueryWrapper<Discount> wrapper = new QueryWrapper<>(discount);
        return discountMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<Discount> findPage(PageWrap<Discount> pageWrap) {
        IPage<Discount> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<Discount> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        Discount modele = pageWrap.getModel();
        queryWrapper.selectAll(Discount.class)
                .select(" (select count(1) from goodsorder g where g.obj_type = 0 and g.obj_id = t.id and g.pay_status = 1 ) ",Discount::getSaleNum)
                .leftJoin(SystemUser.class,SystemUser::getId,Discount::getCreator)
                .like(StringUtils.isNotEmpty(pageWrap.getModel().getName()),Discount::getName,pageWrap.getModel().getName())
                .eq(Objects.nonNull(modele.getStatus()),Discount::getStatus, modele.getStatus())
                .eq(Objects.nonNull(modele.getBikeOrElec()) && Constants.equalsInteger(modele.getBikeOrElec(),Constants.ONE),Discount::getIsbike, Constants.ONE)
                .eq(Objects.nonNull(modele.getBikeOrElec()) && Constants.equalsInteger(modele.getBikeOrElec(),Constants.TWO),Discount::getIselecbike, Constants.ONE)
                .eq(Discount::getIsdeleted, Constants.ZERO)
                .eq( pageWrap.getModel().getStatus() !=null,BikeRepair::getStatus,pageWrap.getModel().getStatus());
                queryWrapper.orderByDesc(Discount::getCreateDate);
        PageData<Discount> pageData = PageData.from(discountJoinMapper.selectJoinPage(page, Discount.class,queryWrapper));
        for (Discount discount:pageData.getRecords()) {
            dealPrice(discount);
        }
        return pageData;
    }
 
 
 
    @Override
    public void dealPrice(Discount discount){
        if(Objects.isNull(discount)
            || Objects.isNull(discount.getUseEndDate())
            || Objects.isNull(discount.getUseStartDate())
            || Objects.isNull(discount.getPrice())
            || discount.getPrice().compareTo(BigDecimal.ZERO)==0
        ){
            discount.setDayPrice(BigDecimal.ZERO);
            return;
        }
        discount.setDayPrice(discount.getPrice().divide(new BigDecimal(Long.toString(discount.getUseDays())),2));
    }
 
 
    @Override
    public long count(Discount discount) {
        QueryWrapper<Discount> wrapper = new QueryWrapper<>(discount);
        return discountMapper.selectCount(wrapper);
    }
}