package com.stylefeng.guns.modular.system.service.impl;
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.stylefeng.guns.core.shiro.ShiroKit;
|
import com.stylefeng.guns.core.util.DateUtil;
|
import com.stylefeng.guns.modular.system.controller.resp.TAgentResp;
|
import com.stylefeng.guns.modular.system.dao.*;
|
import com.stylefeng.guns.modular.system.enums.OrderStateEnum;
|
import com.stylefeng.guns.modular.system.enums.PayStatusEnum;
|
import com.stylefeng.guns.modular.system.enums.StatusEnum;
|
import com.stylefeng.guns.modular.system.enums.UserTypeEnum;
|
import com.stylefeng.guns.modular.system.model.*;
|
import com.stylefeng.guns.modular.system.service.ITAgentService;
|
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.ui.Model;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.StringUtils;
|
|
import java.math.BigDecimal;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 代理商 服务实现类
|
* </p>
|
*
|
* @author stylefeng
|
* @since 2023-02-20
|
*/
|
@Service
|
public class TAgentServiceImpl extends ServiceImpl<TAgentMapper, TAgent> implements ITAgentService {
|
|
|
@Autowired
|
private TAgentMapper tAgentMapper;
|
|
@Autowired
|
private TOrderMapper tOrderMapper;
|
|
@Autowired
|
private TCouponMapper tCouponMapper;
|
|
@Autowired
|
private TDriverMapper tDriverMapper;
|
|
@Autowired
|
private TRechargeRecordMapper tRechargeRecordMapper;
|
@Autowired
|
private TUserToCouponMapper tUserToCouponMapper;
|
|
@Override
|
public EntityWrapper<TAgent> getAgentWrapper(String principal, String principalPhone, String createTime,Integer status) {
|
EntityWrapper<TAgent> wrapper = new EntityWrapper<>();
|
// 昵称
|
if(StringUtils.hasLength(principal)){
|
wrapper.like("principal",principal);
|
}
|
// 手机号
|
if(StringUtils.hasLength(principalPhone)){
|
wrapper.like("principalPhone",principalPhone);
|
}
|
// 开始,结束时间
|
if(StringUtils.hasLength(createTime)){
|
String[] split = createTime.split(" - ");
|
Date startTime = DateUtil.getDate_str3(split[0]+" 00:00:00");
|
Date endTime = DateUtil.getDate_str3(split[1]+" 23:59:59");
|
wrapper.between("createTime",startTime,endTime);
|
}
|
// 判断是否为代理商
|
if(Objects.requireNonNull(ShiroKit.getUser()).getRoleType() == 3){
|
wrapper.eq("id",ShiroKit.getUser().getObjectId());
|
}
|
// 状态
|
if(Objects.nonNull(status)){
|
wrapper.eq("status",status);
|
}
|
wrapper.ne("status", StatusEnum.DELETE.getCode());
|
wrapper.orderBy("createTime",false);
|
return wrapper;
|
}
|
|
@Override
|
public void detail(Integer agentId, Model model) {
|
// 查询代理商用户信息
|
TAgent tAgent = tAgentMapper.selectById(agentId);
|
model.addAttribute("createTime",DateUtil.formatDate(tAgent.getCreateTime()));
|
|
model.addAttribute("startTimeToEndTime",new SimpleDateFormat("yyyy-MM-dd").format(tAgent.getCreateTime()).replace("-",".") + "-" +
|
new SimpleDateFormat("yyyy-MM-dd").format(new Date()).replace("-","."));
|
|
model.addAttribute("principal",tAgent.getPrincipal());
|
model.addAttribute("principalPhone",tAgent.getPrincipalPhone());
|
model.addAttribute("email",tAgent.getEmail());
|
model.addAttribute("serviceCalls",tAgent.getServiceCalls());
|
model.addAttribute("area",tAgent.getProvinceName()+tAgent.getCityName());
|
// 订单信息
|
// 订单总量
|
List<TOrder> orderList = tOrderMapper.selectList(new EntityWrapper<TOrder>().eq("agentId",tAgent.getId()));
|
model.addAttribute("orderSum",orderList.size());
|
// 过滤已完成和待评价订单,且支付金额在14元以上
|
List<TOrder> orders = orderList.stream().filter(order -> (order.getState().equals(OrderStateEnum.FINISH.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()))
|
&& 0 < order.getOrderMoney().compareTo(new BigDecimal("14")) && order.getAgentId().equals(tAgent.getId())).collect(Collectors.toList());
|
model.addAttribute("validOrder",orders.size());
|
// 过滤已使用优惠券
|
List<TOrder> usedCoupon = orderList.stream().filter(order -> (order.getState().equals(OrderStateEnum.FINISH.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()))
|
&& Objects.nonNull(order.getCouponId()) && order.getAgentId().equals(tAgent.getId())).collect(Collectors.toList());
|
// model.addAttribute("usedCoupon",usedCoupon.size());
|
// 已使用优惠券金额求和,已使用优惠券id
|
// List<Integer> usedCouponId = usedCoupon.stream().map(TOrder::getCouponId).collect(Collectors.toList());
|
// if(CollectionUtils.isEmpty(usedCouponId)){
|
// // 如果优惠券为空
|
// model.addAttribute("couponPriceSum", BigDecimal.ZERO);
|
// }else {
|
// // 查询已使用优惠券金额求和
|
// List<TCoupon> tCoupons1 = tCouponMapper.selectList(new EntityWrapper<TCoupon>().in("id", usedCouponId));
|
// if(CollectionUtils.isEmpty(tCoupons1)){
|
// model.addAttribute("couponPriceSum", BigDecimal.ZERO);
|
// }else {
|
// tCoupons1.stream().map(TCoupon::getCouponPreferentialAmount).reduce(BigDecimal::add).ifPresent(bigDecimal -> model.addAttribute("couponPriceSum", bigDecimal));
|
// }
|
// }
|
// 优惠券信息
|
// 过滤代理商发放的优惠券
|
List<TUserToCoupon> tUserToCoupons = tUserToCouponMapper.selectList(new EntityWrapper<TUserToCoupon>().eq("objectId",tAgent.getId())
|
.eq("roleType",3));
|
BigDecimal couponPriceSum = BigDecimal.ZERO;
|
for (TUserToCoupon userToCoupon : tUserToCoupons) {
|
TCoupon tCoupons1 = tCouponMapper.selectById(userToCoupon.getCouponId());
|
if(Objects.nonNull(tCoupons1)){
|
couponPriceSum = couponPriceSum.add(tCoupons1.getCouponPreferentialAmount().multiply(new BigDecimal(userToCoupon.getCouponTotal())));
|
}
|
}
|
model.addAttribute("couponPriceSum", couponPriceSum);
|
// 过滤代理商发放的优惠券
|
int sum = tUserToCoupons.stream().mapToInt(TUserToCoupon::getCouponTotal).sum();
|
model.addAttribute("issuedCoupon",sum);
|
// 总量减去有效数量和过期数量为使用数量
|
int validCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getValidCount).sum();//有效数量
|
int expireCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getExpireCount).sum();//过期数量
|
model.addAttribute("usedCoupon",sum-validCount-expireCount);
|
// 司机信息
|
// 司机数
|
List<TDriver> drivers = tDriverMapper.selectList(new EntityWrapper<TDriver>().eq("agentId", tAgent.getId()));
|
model.addAttribute("driverCount",drivers.size());
|
// 司机充值计算
|
List<Integer> driverIds = drivers.stream().map(TDriver::getId).collect(Collectors.toList());
|
List<TRechargeRecord> tRechargeRecords = tRechargeRecordMapper.selectList(new EntityWrapper<TRechargeRecord>()
|
.in("userId", driverIds)
|
.eq("type", UserTypeEnum.AGENT.getCode())
|
.eq("payStatus", PayStatusEnum.FINISH.getCode()));
|
if(CollectionUtils.isEmpty(tRechargeRecords)){
|
model.addAttribute("driverRecharge",BigDecimal.ZERO);
|
}else {
|
tRechargeRecords.stream().map(TRechargeRecord::getAmount).reduce(BigDecimal::add).ifPresent(record->model.addAttribute("driverRecharge",record));
|
}
|
}
|
|
@Override
|
public List<TAgentResp> getAgentResp(List<TAgent> tAgents) {
|
// 查询订单
|
List<TOrder> tOrders = tOrderMapper.selectList(new EntityWrapper<TOrder>());
|
// 查询优惠券
|
List<TCoupon> tCoupons = tCouponMapper.selectList(new EntityWrapper<TCoupon>());
|
// 用户优惠券
|
List<TUserToCoupon> tUserToCoupons = tUserToCouponMapper.selectList(new EntityWrapper<TUserToCoupon>());
|
// 查询司机信息
|
List<TDriver> tDrivers = tDriverMapper.selectList(new EntityWrapper<TDriver>());
|
|
List<TAgentResp> tAgentRespList = new ArrayList<>(tAgents.size());
|
|
for (TAgent tAgent : tAgents) {
|
TAgentResp tAgentResp = new TAgentResp();
|
BeanUtils.copyProperties(tAgent,tAgentResp);
|
|
tAgentResp.setArea(tAgent.getProvinceName()+tAgent.getCityName());
|
|
if(!CollectionUtils.isEmpty(tOrders)){
|
// 订单总量
|
List<TOrder> orderList = tOrders.stream().filter(order -> tAgent.getId().equals(order.getAgentId())).collect(Collectors.toList());
|
tAgentResp.setOrderSum(orderList.size());
|
// 过滤已完成和待评价订单,且支付金额在14元以上
|
List<TOrder> orders = tOrders.stream().filter(order -> (order.getState().equals(OrderStateEnum.FINISH.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()))
|
&& 0 < order.getOrderMoney().compareTo(new BigDecimal("14")) && tAgent.getId().equals(order.getAgentId())).collect(Collectors.toList());
|
tAgentResp.setValidOrder(orders.size());
|
// 过滤已使用优惠券
|
List<TOrder> usedCoupon = tOrders.stream().filter(order -> (order.getState().equals(OrderStateEnum.FINISH.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()))
|
&& Objects.nonNull(order.getCouponId()) && tAgent.getId().equals(order.getAgentId())).collect(Collectors.toList());
|
// tAgentResp.setUsedCoupon(usedCoupon.size());
|
// 已使用优惠券金额求和,已使用优惠券id
|
// List<Integer> usedCouponId = usedCoupon.stream().map(TOrder::getCouponId).collect(Collectors.toList());
|
// if(CollectionUtils.isEmpty(usedCouponId)){
|
// tAgentResp.setCouponPriceSum(BigDecimal.ZERO);
|
// }else {
|
// // 查询已使用优惠券金额求和
|
// BigDecimal couponPriceSum = BigDecimal.ZERO;
|
// for (Integer id : usedCouponId) {
|
// TCoupon tCoupons1 = tCouponMapper.selectById(id);
|
// couponPriceSum = couponPriceSum.add(tCoupons1.getCouponPreferentialAmount());
|
// }
|
// tAgentResp.setCouponPriceSum(couponPriceSum);
|
// }
|
}
|
|
if(!CollectionUtils.isEmpty(tUserToCoupons)){
|
// 过滤代理商发放的优惠券
|
List<TUserToCoupon> userToCoupons = tUserToCoupons.stream().filter(coupon -> tAgent.getId().equals(coupon.getObjectId()) && coupon.getRoleType() == 3).collect(Collectors.toList());
|
int sum = userToCoupons.stream().mapToInt(TUserToCoupon::getCouponTotal).sum();
|
tAgentResp.setIssuedCoupon(sum);
|
// 总量减去有效数量和过期数量为使用数量
|
int validCount = userToCoupons.stream().mapToInt(TUserToCoupon::getValidCount).sum();//有效数量
|
int expireCount = userToCoupons.stream().mapToInt(TUserToCoupon::getExpireCount).sum();//过期数量
|
tAgentResp.setUsedCoupon(sum-validCount-expireCount);
|
BigDecimal couponPriceSum = BigDecimal.ZERO;
|
for (TUserToCoupon userToCoupon : userToCoupons) {
|
TCoupon tCoupons1 = tCouponMapper.selectById(userToCoupon.getCouponId());
|
if(Objects.nonNull(tCoupons1)){
|
couponPriceSum = couponPriceSum.add(tCoupons1.getCouponPreferentialAmount().multiply(new BigDecimal(userToCoupon.getCouponTotal())));
|
}
|
}
|
tAgentResp.setCouponPriceSum(couponPriceSum);
|
}
|
|
if(!CollectionUtils.isEmpty(tDrivers)){
|
// 司机数
|
List<TDriver> drivers = tDrivers.stream().filter(driver -> tAgent.getId().equals(driver.getAgentId())).collect(Collectors.toList());
|
tAgentResp.setDriverCount(drivers.size());
|
// 司机充值计算
|
List<Integer> driverIds = drivers.stream().map(TDriver::getId).collect(Collectors.toList());
|
List<TRechargeRecord> tRechargeRecords = tRechargeRecordMapper.selectList(new EntityWrapper<TRechargeRecord>()
|
.eq("agentId", tAgent.getId())
|
.eq("type", UserTypeEnum.AGENT.getCode())
|
.eq("payStatus", PayStatusEnum.FINISH.getCode()));
|
tRechargeRecords.stream().map(TRechargeRecord::getAmount).reduce(BigDecimal::add).ifPresent(tAgentResp::setDriverRecharge);
|
}
|
|
tAgentRespList.add(tAgentResp);
|
}
|
return tAgentRespList;
|
}
|
|
@Override
|
public void getDataStatistics(Integer agentId, Model model,Map<String, Object> map) {
|
// 代理商总数
|
Integer agentTotal = tAgentMapper.selectCount(new EntityWrapper<TAgent>()
|
.eq("id", agentId));
|
// 代理商最近一个月新增数量
|
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime localDate = now.minusMonths(1);
|
Integer agentMonthTotal = tAgentMapper.selectCount(new EntityWrapper<TAgent>()
|
.eq("id", agentId)
|
.between("createTime", localDate, now));
|
model.addAttribute("agentTotal",agentTotal);
|
model.addAttribute("agentMonthTotal",agentMonthTotal);
|
map.put("agentTotal",agentTotal);
|
map.put("agentMonthTotal",agentMonthTotal);
|
}
|
|
@Override
|
public void getDataStatisticsByIds(List<Integer> ids, Model model, Map<String, Object> map) {
|
// 代理商总数
|
Integer agentTotal = tAgentMapper.selectCount(new EntityWrapper<TAgent>()
|
.in("id", ids));
|
// 代理商最近一个月新增数量
|
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime localDate = now.minusMonths(1);
|
Integer agentMonthTotal = tAgentMapper.selectCount(new EntityWrapper<TAgent>()
|
.in("id", ids)
|
.between("createTime", localDate, now));
|
model.addAttribute("agentTotal",agentTotal);
|
model.addAttribute("agentMonthTotal",agentMonthTotal);
|
map.put("agentTotal",agentTotal);
|
map.put("agentMonthTotal",agentMonthTotal);
|
}
|
}
|