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);
|
}
|
|
|
|
}
|