add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.iotechn.unimall.admin.api.coupon;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.CouponDO;
import com.iotechn.unimall.data.dto.CouponAdminDTO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.CouponMapper;
import com.iotechn.unimall.data.mapper.CouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.util.Date;
import java.util.List;
 
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: kbq
 * Date: 2019-07-12
 * Time: 下午11:26
 */
@Service
public class AdminCouponServiceImpl implements AdminCouponService {
 
    @Autowired
    private CouponMapper couponMapper;
 
    @Autowired
    private CouponUserMapper couponUserMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CouponDO create(String title, Integer type, Integer isVip, String description, Integer total, Integer limit, Integer discount, Integer min, Integer status, Long categoryId, Integer days, Long gmtStart, Long gmtEnd, Long adminId) throws ServiceException {
        Date start = null;
        Date end = null;
        if (gmtEnd != null && gmtStart != null) {
            start = new Date(gmtStart);
            end = new Date(gmtEnd);
        }
 
        CouponDO couponDO = new CouponDO();
        couponDO.setTitle(title);
        couponDO.setType(type);
        couponDO.setIsVip(isVip);
        couponDO.setDescription(description);
        couponDO.setTotal(total);
        couponDO.setSurplus(total);
        couponDO.setLimit(limit);
        couponDO.setDiscount(discount);
        couponDO.setMin(min);
        couponDO.setStatus(status);
        couponDO.setCategoryId(categoryId);
        couponDO.setDays(days);
        couponDO.setGmtStart(start);
        couponDO.setGmtEnd(end);
        Date now = new Date();
        couponDO.setGmtCreate(now);
        couponDO.setGmtUpdate(now);
        if (couponMapper.insert(couponDO) > 0) {
            return couponDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long id, Long adminId) throws ServiceException {
        QueryWrapper wrapperC = new QueryWrapper();
        wrapperC.eq("id", id);
        if (couponMapper.delete(wrapperC) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        QueryWrapper wrapperUC = new QueryWrapper();
        wrapperUC.eq("coupon_id", id);
        couponUserMapper.delete(wrapperUC);
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(Long id, String title, Integer type, Integer isVip, String description, Integer total, Integer surplus, Integer limit, Integer discount, Integer min, Integer status, Long categoryId, Integer days, Date gmtStart, Date gmtEnd, Long adminId) throws ServiceException {
        CouponDO couponDO = new CouponDO();
        couponDO.setId(id);
        couponDO.setTitle(title);
        couponDO.setType(type);
        couponDO.setIsVip(isVip);
        couponDO.setDescription(description);
        couponDO.setTotal(total);
        couponDO.setSurplus(total);
        couponDO.setLimit(limit);
        couponDO.setDiscount(discount);
        couponDO.setMin(min);
        couponDO.setStatus(status);
        couponDO.setCategoryId(categoryId);
        couponDO.setDays(days);
        couponDO.setGmtStart(gmtStart);
        couponDO.setGmtEnd(gmtEnd);
        List<CouponDO> couponDOList = couponMapper.selectList(new QueryWrapper<CouponDO>().eq("id", id));
        if (CollectionUtils.isEmpty(couponDOList)) {
            throw new AdminServiceException(ExceptionDefinition.COUPON_NOT_EXIST);
        }
        Date now = new Date();
        couponDO.setGmtCreate(couponDOList.get(0).getGmtCreate());
        couponDO.setGmtUpdate(now);
        return couponMapper.updateById(couponDO) > 0;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCouponStatus(Long id, Integer status, Long adminId) throws ServiceException {
        CouponDO couponDO = new CouponDO();
        couponDO.setId(id);
        couponDO.setStatus(status);
        couponDO.setGmtUpdate(new Date());
        return couponMapper.updateById(couponDO) > 0;
    }
 
    @Override
    public Page<CouponAdminDTO> list(String title, Integer type, Integer status, Integer pageNo, Integer limit, Long adminId) throws ServiceException {
        Page<CouponAdminDTO> page = couponMapper.getAdminCouponList(Page.div(pageNo, limit, CouponAdminDTO.class), title, type, status, new Date());
        return page;
    }
}