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
package com.iotechn.unimall.admin.api.advert;
 
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.component.CacheComponent;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.constant.CacheConst;
import com.iotechn.unimall.data.domain.AdvertDO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.AdvertMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
 
import java.util.Date;
 
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: kbq
 * Date: 2019-07-08
 * Time: 下午9:24
 */
@Service("adminAdvertService")
public class AdminAdvertServiceImpl implements AdminAdvertService {
 
    @Autowired
    private AdvertMapper advertMapper;
 
    @Autowired
    private CacheComponent cacheComponent;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(Integer type, Integer unionType, String title, String unionValue, String imgUrl, Integer status, String color, Long adminId) throws ServiceException {
        Date now = new Date();
        AdvertDO advertDO = new AdvertDO();
        advertDO.setType(type);
        advertDO.setTitle(title);
        advertDO.setUnionType(unionType);
        advertDO.setUnionValue(unionValue);
        advertDO.setImgUrl(imgUrl);
        advertDO.setStatus(status);
        advertDO.setColor(color);
        advertDO.setGmtCreate(now);
        advertDO.setGmtUpdate(now);
        if (advertMapper.insert(advertDO) > 0) {
            this.clearCache(type);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADVERTISEMENT_SQL_ADD_FAILED);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Integer adType, Long adId, Long adminId) throws ServiceException {
        if (advertMapper.delete(new QueryWrapper<AdvertDO>()
                .eq("id", adId)
                .eq("type", adType)) > 0) {
            this.clearCache(adType);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADVERTISEMENT_SQL_DELETE_FAILED);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long adId, Integer type, Integer unionType, String title, String unionValue, String imgUrl, Integer status, String color, Long adminId) throws ServiceException {
        AdvertDO advertDO = new AdvertDO();
        advertDO.setId(adId);
        advertDO.setType(type);
        advertDO.setUnionType(unionType);
        advertDO.setTitle(title);
        advertDO.setUnionValue(unionValue);
        advertDO.setImgUrl(imgUrl);
        advertDO.setStatus(status);
        advertDO.setColor(color);
        advertDO.setGmtUpdate(new Date());
        if (advertMapper.updateById(advertDO) > 0) {
            this.clearCache(type);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADVERTISEMENT_SQL_UPDATE_FAILED);
    }
 
    @Override
    public Page<AdvertDO> list(Long adminId, Integer adType, Integer page, Integer limit, Integer status) throws ServiceException {
        QueryWrapper<AdvertDO> wrapper = new QueryWrapper<AdvertDO>();
        if (adType != null) {
            wrapper.eq("type", adType);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        return advertMapper.selectPage(Page.div(page, limit, AdvertDO.class), wrapper);
    }
 
    public void clearCache(Integer adType) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                cacheComponent.del(CacheConst.ADVERT_TYPE + null);
                cacheComponent.del(CacheConst.ADVERT_TYPE + adType);
                cacheComponent.del(CacheConst.INTEGRAL_INDEX);
            }
        });
    }
}