邱宇豪
2023-11-28 b11dca5c6c4383d915571c47b10269e66c74079a
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
package com.thhy.secure.modules.biz.integralAccount.service.impl;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thhy.general.common.BasicResult;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.utils.ExcelUtils;
import com.thhy.general.utils.UUIDUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.secure.modules.biz.integralAccount.entity.IntegralAccountEntity;
import com.thhy.secure.modules.biz.integralAccount.entity.IntegralDetailDto;
import com.thhy.secure.modules.biz.integralAccount.entity.IntegralDetailEntity;
import com.thhy.secure.modules.biz.integralAccount.mapper.IntegralDetailMapper;
import com.thhy.secure.modules.biz.integralAccount.service.IntegralDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Author QiuYuHao
 * @CreateDate 2023-11-27 18:33:40
 * 积分超市实现
 */
@Service
public class IntegralDetailServiceImpl implements IntegralDetailService
{
 
    @Autowired
    private IntegralDetailMapper integralDetailMapper;
 
    private static final Object Lock= new Object();
    /**
     * 积分超市新增和修改
     * @param integralDetailEntity
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult insertAndUpdate(IntegralDetailEntity integralDetailEntity){
        String id = integralDetailEntity.getId();
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String sysUserId = sysUserInfo.getUserId();
        String companyId = sysUserInfo.getCompanyId();
        //扣积分逻辑
        synchronized (Lock){
            IntegralAccountEntity integralAccountEntity = IntegralAccountEntity
                    .builder()
                    .userId(integralDetailEntity.getUserId())
                    .updateUser(sysUserId)
                    .updateTime(new Date())
                    .id(UUIDUtils.create())
                    .build();
            Map map = new HashMap();
            map.put("userId",integralDetailEntity.getUserId());
            //查询账户
            IntegralAccountEntity queryAccount = integralDetailMapper.selectByOne(map);
            if(queryAccount == null || queryAccount.getTotalIntegral()<integralDetailEntity.getIntegralWater()){//无账户
                return BasicResult.faild("500","积分不足,无法扣除积分",null);
            }
            //有账户
            Double totalConsumption = queryAccount.getTotalConsumption();//总消耗
            Double accumulateIntegral = queryAccount.getAccumulateIntegral();//累计积分
            Double totalIntegral = queryAccount.getTotalIntegral();//当前余额
 
            integralAccountEntity.setTotalIntegral(totalIntegral-integralDetailEntity.getIntegralWater());
            integralAccountEntity.setAccumulateIntegral(accumulateIntegral);
            integralAccountEntity.setTotalConsumption(totalConsumption+integralDetailEntity.getIntegralWater());
            //修改账户
            integralDetailMapper.insertAndUpdateAccount(integralAccountEntity);
            if(id  == null){
                integralDetailEntity.setCompanyId(companyId);
                integralDetailEntity.setId(UUIDUtils.create());
                integralDetailEntity.setCreateTime(new Date());
                integralDetailEntity.setCreateUser(sysUserId);
            }
            integralDetailEntity.setBalanc(totalIntegral-integralDetailEntity.getIntegralWater());//拿到当前余额
 
            integralDetailEntity.setType(2);
            integralDetailEntity.setIntegralSource("积分超市");
            //添加或修改明细
            integralDetailMapper.insertDetailAndUpdate(integralDetailEntity);
        }
        return BasicResult.success();
    }
 
    /**
     * 积分超市
     * @param map
     * @return
     */
    @Override
    public BasicResult selectPageList(Map map){
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        int pageNum = (int) map.get("pageNum");
        int pageSize = (int) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        map.put("companyId",companyId);
        List<IntegralDetailEntity> integralDetailEntities = integralDetailMapper.selectPageList(map);
        return BasicResult.success(new PageInfo<>(integralDetailEntities));
    }
 
 
    /**
     * 积分超市删除
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult delete(String id){
        Map map =new HashMap();
        map.put("id",id);
        IntegralDetailDto integralDetailEntity = integralDetailMapper.selectSafeIntegralInfo(map).get(0);
        Double integralWater = integralDetailEntity.getIntegralWater();//获取消耗积分
        Map account = new HashMap();
        account.put("userId",integralDetailEntity.getUserId());
        //查询账户
        IntegralAccountEntity queryAccount = integralDetailMapper.selectByOne(account);
 
        Double totalConsumption = queryAccount.getTotalConsumption();//总消耗
        Double accumulateIntegral = queryAccount.getAccumulateIntegral();//累计积分
        Double totalIntegral = queryAccount.getTotalIntegral();//当前余额
        synchronized (Lock){
            IntegralAccountEntity integralAccountEntity = IntegralAccountEntity
                    .builder()
                    .userId(integralDetailEntity.getUserId())
                    .totalConsumption(totalConsumption - integralWater)
                    .accumulateIntegral(accumulateIntegral + integralWater)
                    .totalIntegral(totalIntegral + integralWater)
                    .build();
            integralDetailMapper.insertAndUpdateAccount(integralAccountEntity);
            integralDetailMapper.delete(id);
        }
        return BasicResult.success();
    }
 
 
    /**
     * 安全积分列表
     */
    @Override
    public BasicResult selectSafeIntegralPageList(Map map){
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        int pageNum = (int) map.get("pageNum");
        int pageSize = (int) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        map.put("companyId",companyId);
        List<IntegralAccountEntity> integralAccountEntities = integralDetailMapper.selectSafeIntegralPageList(map);
        return BasicResult.success(new PageInfo<>(integralAccountEntities));
    }
 
    /**
     * 安全积分明细
     * @param map
     * @return
     */
    @Override
    public BasicResult selectSafeIntegralInfo(Map map){
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        IntegralAccountEntity integralAccountEntity = integralDetailMapper.selectByOne(map);
        map.put("integral",integralAccountEntity.getTotalIntegral());
        int pageNum = (int) map.get("pageNum");
        int pageSize = (int) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        map.put("companyId",companyId);
        List<IntegralDetailDto> integralDetailEntities = integralDetailMapper.selectSafeIntegralInfo(map);
        map.put("data",new PageInfo<>(integralDetailEntities));
        return BasicResult.success(map);
    }
 
    @Override
    public void exportList(Map map, HttpServletResponse rsp) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        map.put("companyId",companyId);
        List<IntegralDetailEntity> integralDetailEntities = integralDetailMapper.selectPageList(map);
 
        integralDetailEntities.forEach(obj->{
            if(obj.getUserType() ==1){
                obj.setUserTypeName("管服人员");
            }
            if(obj.getUserType() ==2){
                obj.setUserTypeName("劳务人员");
            }
        });
        ExcelUtils.downExcel(integralDetailEntities,IntegralDetailEntity.class,rsp,"班组列表");
    }
 
    @Override
    public void exportSafeIntegralInfo(Map map, HttpServletResponse rsp) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
 
        IntegralAccountEntity integralAccountEntity = integralDetailMapper.selectByOne(map);
        map.put("integral",integralAccountEntity.getTotalIntegral());
        map.put("companyId",companyId);
        List<IntegralDetailDto> integralDetailEntities = integralDetailMapper.selectSafeIntegralInfo(map);
        integralDetailEntities.forEach(obj->{
            if(obj.getUserType() ==1){
                obj.setUserTypeName("管服人员");
            }
            if(obj.getUserType() ==2){
                obj.setUserTypeName("劳务人员");
            }
            if(obj.getType() == 1){
                obj.setTypeName("获取");
            }
            if(obj.getType() == 2){
                obj.setTypeName("消耗");
            }
        });
        ExcelUtils.downExcel(integralDetailEntities,IntegralDetailDto.class,rsp,"班组列表");
    }
 
}