fix
Helius
2021-07-15 5d934acaa19edb9f0b43972f991cc1d1e2b6ffce
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.xzx.gc.order.service;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.OrderEnum;
import com.xzx.gc.common.constant.UserEnum;
import com.xzx.gc.common.utils.BusinessUtil;
import com.xzx.gc.common.utils.DateUtils;
import com.xzx.gc.common.utils.LocationUtils;
import com.xzx.gc.common.utils.ali.SmsUtil;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.admin.AccountLogModel;
import com.xzx.gc.model.admin.UserAccountModel;
import com.xzx.gc.model.user.UserRecommendListResDTO;
import com.xzx.gc.order.mapper.*;
import com.xzx.gc.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
@Transactional
public class OtherUserService {
 
    public List<OtherUserInfo> findHomeUserForbidden() {
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setDelFlag(0);
        otherUserInfo.setOriginType(Convert.toShort(1));
        otherUserInfo.setIsProhibit(false);
        return  otherUserMapper.select(otherUserInfo);
    }
 
    @Async
    public void sendHomeSms(){
        //发送短信
        List<OtherUserInfo> homeUserForbidden = findHomeUserForbidden();
        for (OtherUserInfo otherUserInfo : homeUserForbidden) {
            SmsUtil.sendPoint(otherUserInfo.getMobilePhone(), "您有新的电器订单,请尽快处理。");
        }
    }
 
    @Autowired
    private OtherUserMapper otherUserMapper;
 
    @Autowired
    private BusinessUtil businessUtil;
 
    @Autowired
    private OrderMapper orderMapper;
 
    @Autowired
    private SysItemPriceMapper sysItemPriceMapper;
 
    @Autowired
    private OrderAppraiseInfoMapper orderAppraiseInfoMapper;
 
    @Autowired
    private AccountLogMapper accountLogMapper;
 
    @Autowired
    private AccountMapper accountMapper;
 
 
 
    public List<OtherUserInfo> findByMobile(String mobile) {
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setDelFlag(Constants.DEL_NOT_FLAG);
        otherUserInfo.setIsProhibit(false);
        otherUserInfo.setMobilePhone(mobile);
        return  otherUserMapper.select(otherUserInfo);
    }
 
    public List<OtherUserInfo> findByMobileForBidden(String mobile) {
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setDelFlag(Constants.DEL_NOT_FLAG);
        otherUserInfo.setMobilePhone(mobile);
        return  otherUserMapper.select(otherUserInfo);
    }
 
 
    public List<OtherUserInfo> findByMobileAndTypeForBidden(String mobile,String userType) {
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setDelFlag(Constants.DEL_NOT_FLAG);
        otherUserInfo.setMobilePhone(mobile);
        otherUserInfo.setUserType(userType);
        return  otherUserMapper.select(otherUserInfo);
    }
 
    /**
     * 转换成回收员
     * @param userId
     * @return
     */
    public OtherUserInfo turnReceiver(String userId){
        OtherUserInfo byId = findById(userId);
        String mobilePhone = byId.getMobilePhone();
        List<OtherUserInfo> byMobile = findByMobile(mobilePhone);
        if(CollUtil.isNotEmpty(byMobile)){
            List<OtherUserInfo> collect = byMobile.stream().filter(x -> x.getUserType().equals(CommonEnum.回收员.getValue())).collect(Collectors.toList());
            List<OtherUserInfo> collect2 = byMobile.stream().filter(x -> x.getUserType().equals(CommonEnum.入库员.getValue())).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(collect)){
                return collect.get(0);
            }else if(CollUtil.isNotEmpty(collect2)){
                return collect2.get(0);
            }
        }
        return null;
    }
 
 
    public OtherUserInfo findByMobileAndUserType(String mobile, String userType) {
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setDelFlag(0);
        otherUserInfo.setIsProhibit(false);
        otherUserInfo.setUserType(userType);
        otherUserInfo.setMobilePhone(mobile);
        return  otherUserMapper.selectOne(otherUserInfo);
    }
 
    /**
     * 根据合伙人查找
     * @param partnerId
     * @return
     */
    public List<OtherUserInfo> findByPartnerId(String partnerId) {
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setDelFlag(0);
        otherUserInfo.setPartnerId(partnerId);
        return  otherUserMapper.select(otherUserInfo);
    }
 
 
    public OtherUserInfo findById(String userId) {
        return  otherUserMapper.selectByPrimaryKey(userId);
    }
 
 
 
    public void updateById(OtherUserInfo otherUserInfo){
        otherUserMapper.updateByPrimaryKeySelective(otherUserInfo);
    }
 
    public void deleteById(String userId){
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setUserId(userId);
        otherUserInfo.setDelFlag(Constants.DEL_FLAG);
        updateById(otherUserInfo);
    }
 
 
 
    /**
     * 获取回收员头部信息
     * @param otherUserInfo
     * @param createUserId  创建用户即普通用户
     * @return
     */
    public UserRecommendListResDTO getHeadInfo(OtherUserInfo otherUserInfo, String longitude, String latitude, String createUserId, SysEnvironmentalInfo sysEnvironmentalInfo){
 
        Long itemId = otherUserInfo.getItemId();
        String userId = otherUserInfo.getUserId();
        UserRecommendListResDTO resDTO=new UserRecommendListResDTO();
        BeanUtil.copyProperties(otherUserInfo,resDTO);
        resDTO.setType(UserEnum.回收员类型.getValue());
        if(StrUtil.isNotBlank(latitude)&&StrUtil.isNotBlank(longitude)) {
            //单位米
            double distance = LocationUtils.getDistance(Convert.toDouble(otherUserInfo.getLatitude(), Convert.toDouble(0)), Convert.toDouble(otherUserInfo.getLongitude(), Convert.toDouble(0)), Convert.toDouble(latitude), Convert.toDouble(longitude));
            //距离
            resDTO.setDistance(businessUtil.changeWeight(Convert.toStr(NumberUtil.div(distance,1000))));
        }
        //服务次数
        List<OrderInfo> serviceOrderList = orderMapper.findServiceOrderByUserIdAndStatus(userId, CollUtil.newArrayList(OrderEnum.待入库.getValue(),OrderEnum.完成.getValue(),OrderEnum.入库中.getValue()));
        resDTO.setServiceNum(serviceOrderList.size());
        //自己报价
        if(itemId!=null){
            Example example = new Example(SysItemPrice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("itemId", itemId);
            criteria.andEqualTo("delFlag", Constants.DEL_NOT_FLAG);
            example.setOrderByClause("price desc");
            List<SysItemPrice> sysItemPrices = sysItemPriceMapper.selectByExample(example);
            if (CollUtil.isNotEmpty(sysItemPrices)) {
                BigDecimal price = CollUtil.getFirst(sysItemPrices).getPrice();
                resDTO.setSelfMoney(businessUtil.changeMoney(price));
            }
        }
        //平台报价
        if(sysEnvironmentalInfo!=null){
            resDTO.setPlatMoney(businessUtil.changeMoney(sysEnvironmentalInfo.getPrice()));
        }
 
        //预计时间 服务中多一个用户多1小时
        int fwzNum;
        List<OrderInfo> list = orderMapper.findListOrderReceiveTime(userId);
        if(CollUtil.isEmpty(list)){
            fwzNum= 0;
        }else {
            List<String> collect = list.stream().map(x -> x.getCreateUserId()).collect(Collectors.toList());
            if(StrUtil.isNotBlank(createUserId)) {
                int i = collect.indexOf(createUserId);
                i=i==-1?collect.size():i;
                fwzNum= i;
            }else {
                fwzNum= collect.size();
            }
        }
 
        DateTime dateTime;
        String hour = DateUtil.format(new Date(), DateUtils.DATE_FORMAT_H);
        //超过下午6点从明天上午8点开始预约
        if(Convert.toInt(hour)>=18){
            String format = DateUtil.format(DateUtil.offsetDay(new Date(), 1), DateUtils.DATE_FORMAT_YMD);
            format=format+" 08:00:00";
            dateTime = DateUtil.offsetHour(DateUtil.parse(format), fwzNum);
        }else{
            fwzNum=fwzNum==0?1:fwzNum;
            dateTime = DateUtil.offsetHour(new Date(), fwzNum);
        }
        resDTO.setScheduledTime(DateUtil.format(dateTime,DateUtils.DATE_FORMAT_HM));
        //排队人数
        resDTO.setQueueNum(fwzNum);
        //是否服务过
        if(StrUtil.isNotBlank(createUserId)) {
            long num = serviceOrderList.stream().filter(x->x.getCreateUserId().equals(createUserId)).count();
            if(num>0){
                resDTO.setServiceFlag(true);
            }
        }
        //是否高于平台
        if(Convert.toBigDecimal(resDTO.getSelfMoney(),Constants.MONEY_INIT).compareTo(Convert.toBigDecimal(resDTO.getPlatMoney(),Constants.MONEY_INIT))>0){
            resDTO.setHighThanPlatFlag(true);
        }
 
        //平均取货时间(分钟)=(所有单完成时间-接单时间)/单数
        long perMinuteOrder=0;
        for (OrderInfo orderInfo : serviceOrderList) {
            String completeTime = orderInfo.getCompleteTime();
            String receiveTime = orderInfo.getReceiveTime();
            if(StrUtil.isNotBlank(completeTime)&&StrUtil.isNotBlank(receiveTime)){
                perMinuteOrder += DateUtil.between(DateUtil.parseDateTime(receiveTime), DateUtil.parseDateTime(completeTime), DateUnit.MINUTE);
            }
        }
 
        if(CollUtil.isNotEmpty(serviceOrderList)) {
            resDTO.setPerMinuteOrder(perMinuteOrder / serviceOrderList.size());
        }else {
            resDTO.setPerMinuteOrder(0);
        }
 
        //满意度
        Example example=new Example(OrderAppraiseInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag",Convert.toShort(Constants.DEL_NOT_FLAG));
        criteria.andEqualTo("receiverId",otherUserInfo.getUserId());
        List<OrderAppraiseInfo> orderAppraiseInfos = orderAppraiseInfoMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(orderAppraiseInfos)) {
            double sum = orderAppraiseInfos.stream().mapToDouble(x -> Convert.toDouble(x.getStar(),Convert.toDouble(Constants.SCORE_MAX))).sum();
            resDTO.setSatisfaction(Convert.toInt(sum/(orderAppraiseInfos.size()*Constants.SCORE_MAX)*100));
        }
 
        return resDTO;
    }
 
 
    public void addOtherUserAccountLog(UserAccountModel accountModel, BigDecimal money, String payOrderId, String accountMoney){
        BigDecimal newAccountMoney= new BigDecimal(accountMoney).add(new BigDecimal(accountModel.getMoney()));
        accountMapper.updateUserAccount(accountModel.getAccountId(),DoubleUtil.roundTwo(newAccountMoney.toString()),accountModel.getOverdraftLimit());
        //写入日志
        AccountLogModel log = new AccountLogModel();
        log.setAccountId(accountModel.getAccountId());
        log.setChannelType("12");
        log.setCreateTime(cn.hutool.core.date.DateUtil.now());
        log.setCreateUserId(accountModel.getUserId());
        BigDecimal oldMoney = new BigDecimal(accountMoney);
        log.setNewFixedLimit(accountModel.getFixedLimit());
        log.setNewLimit(accountModel.getLimit()+"");
        log.setNewMoney(DoubleUtil.roundTwo(newAccountMoney.toString()));
        log.setOldFixedLimit(accountModel.getFixedLimit());
        log.setOldLimit(accountModel.getLimit()+"");
        log.setOldMoney(DoubleUtil.roundTwo(oldMoney.toString()));
        log.setOrderId(payOrderId);
        accountLogMapper.insertAccountLog(log);
    }
 
 
 
}