xuhy
2024-10-14 e9f36d42efaa757c7f93b00ba3ff9fb0d0bf46c9
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/TAppCouponController.java
@@ -1,35 +1,46 @@
package com.ruoyi.account.controller;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.account.api.dto.CouponListDto;
import com.ruoyi.account.api.dto.GrantCouponDto;
import com.ruoyi.account.api.model.TAppCoupon;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserCar;
import com.ruoyi.account.api.model.TAppUserTag;
import com.ruoyi.account.api.query.ExchangeRecordGoodsQuery;
import com.ruoyi.account.api.vo.CarListVO;
import com.ruoyi.account.api.vo.CouponListVOVO;
import com.ruoyi.account.service.TAppUserService;
import com.ruoyi.account.service.TAppUserTagService;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.chargingPile.api.model.TChargingPile;
import com.ruoyi.other.api.vo.CouponListVOVO;
import com.ruoyi.account.api.vo.ExchangeRecordVO;
import com.ruoyi.account.service.TAppCouponService;
import com.ruoyi.account.service.TAppUserCarService;
import com.ruoyi.chargingPile.api.feignClient.ChargingPileClient;
import com.ruoyi.chargingPile.api.vo.SiteInfoVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.BasePage;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
import com.ruoyi.other.api.domain.TCoupon;
import com.ruoyi.other.api.feignClient.OtherClient;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import java.math.BigDecimal;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
 * <p>
@@ -44,13 +55,95 @@
public class TAppCouponController {
    @Autowired
    private TAppCouponService tAppCouponService;
    @Autowired
    @Resource
    private OtherClient otherClient;
    @Autowired
    private TAppUserCarService appUserCarService;
    @Autowired
    @Resource
    private TokenService tokenService;
    @Resource
    private ChargingPileClient chargingPileClient;
    @Resource
    private ChargingOrderClient chargingOrderClient;
    @Resource
    private ChargingGunClient chargingGunClient;
    @Autowired
    private TAppUserTagService appUserTagService;
    @Autowired
    private TAppUserService appUserService;
    /**
     * 后台退款 回退优惠券使用状态
     * @return
     */
    @PostMapping(value = "/refund/{id}")
    public R refund(@PathVariable("id") String id) {
        TAppCoupon byId = tAppCouponService.getById(id);
        byId.setStatus(1);
        tAppCouponService.updateById(byId);
        return R.ok();
    }
    /**
     * 小程序扫一扫 添加车辆
     * @param dto
     * @return
     */
    @ApiOperation(value = "添加车辆", tags = {"小程序-扫一扫"})
    @PostMapping(value = "/scan/addCar")
    public AjaxResult addCar(@RequestBody TAppUserCar dto) {
        dto.setAppUserId(tokenService.getLoginUserApplet().getUserId());
        appUserCarService.save(dto);
        return AjaxResult.success();
    }
    /**
     * 查询用户可用优惠券数量
     * @return
     */
    @ApiOperation(value = "通过充电枪id和预付金额获取电站详情", tags = {"小程序-扫一扫"})
    @PostMapping(value = "/scan/siteInfo")
    public AjaxResult<SiteInfoVO> siteInfo(Integer id, BigDecimal money) {
        TChargingGun chargingGun = chargingGunClient.getChargingGunById(id).getData();
        TChargingPile chargingPile = chargingPileClient.getChargingPileById(chargingGun.getChargingPileId()).getData();
        SiteInfoVO data = chargingPileClient.getSiteInfoByNumber1(chargingPile.getCode().toString()).getData();
        data.setChargingGunId(id);
        data.setSiteId(chargingGun.getId());
        data.setChargeMode(chargingGun.getChargeMode());
        List<TAppUserCar> cars = appUserCarService.list(new QueryWrapper<TAppUserCar>()
                .eq("app_user_id",tokenService.getLoginUserApplet().getUserId())
                .orderByDesc("create_time"));
        Long data1 = chargingOrderClient.getCar(tokenService.getLoginUserApplet().getUserId().toString()).getData();
        Integer data2 = otherClient.getAddCarIntegral().getData();
        data.setIntegral(data2);
        if (!cars.isEmpty()){
            if (null == data1 || data1 == -1){
                // 没有充电订单 展示最新添加的车辆
                data.setLicensePlate(cars.get(0).getLicensePlate());
                data.setVehicleBrand(cars.get(0).getVehicleBrand());
                data.setId(cars.get(0).getId());
            }else{
                for (TAppUserCar car : cars) {
                    if (car.getId().equals(data1)){
                        data.setLicensePlate(car.getLicensePlate());
                        data.setVehicleBrand(car.getVehicleBrand());
                        data.setId(car.getId());
                    }
                }
            }
        }
        BigDecimal electrovalence = data.getElectrovalence();
        BigDecimal vipElectrovalence = data.getVipElectrovalence();
        // 计算预付金额能充多少度普通电
        BigDecimal divide1 = money.divide(electrovalence, 2, BigDecimal.ROUND_HALF_UP);
        // 计算冲会员电能充多少度会员电
        BigDecimal divide = vipElectrovalence.divide(electrovalence, 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal subtract = divide.subtract(divide1);
        // 优惠金额
        data.setMoney(subtract.multiply(vipElectrovalence).setScale(2, BigDecimal.ROUND_HALF_UP));
        return AjaxResult.ok(data);
    }
    /**
     * 查询用户可用优惠券数量
     * @param dto
@@ -63,24 +156,23 @@
        return AjaxResult.ok(res);
    }
    /**
     * 选择车辆列表
     * 选择车辆列表 远程调用
     * @return
     */
    @ApiOperation(value = "选择车辆列表", tags = {"小程序-扫一扫"})
    @PostMapping(value = "/scan/carList")
    public AjaxResult<List<TAppUserCar>> carList() {
        // todo 用户id
    public R<List<TAppUserCar>> carList() {
        List<TAppUserCar> appUserId = appUserCarService.list(new QueryWrapper<TAppUserCar>()
                .eq("app_user_id",tokenService.getLoginUserApplet().getUserId()));
        return AjaxResult.ok(appUserId);
        return R.ok(appUserId);
    }
    /**
     * 管理后台远程调用 根据优惠券ids 查询对应的发放数量
     * @return 优惠券ids 查询每个优惠券的发放数量
     */
    @PostMapping("/getCountByCouponIds")
    public R<List<Integer>> getCountByCouponIds(String couponIds) {
    @PostMapping("/getCountByCouponIds/{couponIds}")
    public R<List<Integer>> getCountByCouponIds(@PathVariable("couponIds")String couponIds) {
        // 最终结果 和优惠券id一一对应
        List<Integer> res = new ArrayList<>();
        String[] split = couponIds.split(",");
@@ -99,8 +191,8 @@
     * @param couponId
     * @return
     */
    @PostMapping("/getUseCountByCouponId")
    public R<Integer> getUseCountByCouponId(Integer couponId){
    @PostMapping("/getUseCountByCouponId/{couponId}")
    public R<Integer> getUseCountByCouponId(@PathVariable("couponId") Integer couponId){
        return R.ok(tAppCouponService.list(new QueryWrapper<TAppCoupon>()
                .eq("coupon_id", couponId)
                .eq("status",2)).size());
@@ -111,27 +203,108 @@
     * @return
     */
    @PostMapping("/getExchangeRecordByCouponId")
    public R<PageInfo<ExchangeRecordVO>> getExchangeRecordByCouponId(@RequestBody ExchangeRecordGoodsQuery couponId){
    public R<List<ExchangeRecordVO>> getExchangeRecordByCouponId(@RequestBody ExchangeRecordGoodsQuery couponId){
        return R.ok(tAppCouponService.pagelist(couponId));
    }
    @ApiOperation(value = "优惠卷可用数量", tags = {"小程序-站点管理-站点详情"})
    @GetMapping(value = "/couponCount")
    public R<Long> couponCount() {
        Long userId = tokenService.getLoginUserApplet().getUserId();
        Long count = tAppCouponService.lambdaQuery().eq(TAppCoupon::getAppUserId, userId).ge(TAppCoupon::getEndTime, LocalDateTime.now()).eq(TAppCoupon::getStatus, 1).count();
        if(Objects.isNull(count)){
            count = 0L;
        }
        return R.ok(count);
    }
    /**
     * 后台远程调用 给用户发放优惠券
     */
    @PostMapping("/grantCoupon")
    public R  grantCoupon(@RequestBody GrantCouponDto dto){
        dto.setWaysToObtain(3);
        List<TAppCoupon> res = new ArrayList<>();
        for (String s : dto.getUserIds().split(",")) {
            TAppCoupon tAppCoupon = new TAppCoupon();
            tAppCoupon.setAppUserId(Long.valueOf(s));
            tAppCoupon.setCouponId(dto.getCouponId());
            tAppCoupon.setEndTime(dto.getEndTime());
            tAppCoupon.setWaysToObtain(dto.getWaysToObtain());
            tAppCoupon.setStatus(1);
            res.add(tAppCoupon);
        TCoupon coupon = otherClient.getCouponById(dto.getCouponId()).getData();
        switch (dto.getType()){
            case 1:
                // 根据标签ids 查询用户ids
                List<Long> collect = appUserTagService.list(new QueryWrapper<TAppUserTag>()
                        .in("user_tag_id", Arrays.asList(dto.getTags().split(","))))
                        .stream().map(TAppUserTag::getAppUserId).collect(Collectors.toList());
                for (Long l : collect) {
                    TAppCoupon tAppCoupon = new TAppCoupon();
                    tAppCoupon.setAppUserId(l);
                    tAppCoupon.setCouponId(dto.getCouponId());
                    tAppCoupon.setEndTime(dto.getEndTime());
                    tAppCoupon.setWaysToObtain(dto.getWaysToObtain());
                    tAppCoupon.setStatus(1);
                    tAppCoupon.setCouponJson(JSON.toJSONString(coupon));
                    res.add(tAppCoupon);
                }
                tAppCouponService.saveBatch(res);
                break;
            case 2:
                // 根据市codes 查询用户ids
                List<Long> collect1 = new ArrayList<>();
                // 根据市codes 查询用户ids
                if (StringUtils.hasLength(dto.getProvinceCode())){
                    List<Long> collect2 = appUserService.list(new QueryWrapper<TAppUser>()
                            .in("province_code", Arrays.asList(dto.getProvinceCode().split(","))))
                            .stream().map(TAppUser::getId).collect(Collectors.toList());
                    collect1.addAll(collect2);
                }
                if (StringUtils.hasLength(dto.getCityCode())){
                    List<Long> collect2 = appUserService.list(new QueryWrapper<TAppUser>()
                            .in("city_code", Arrays.asList(dto.getCityCode().split(","))))
                            .stream().map(TAppUser::getId).collect(Collectors.toList());
                    collect1.addAll(collect2);
                }
                for (Long l : collect1) {
                    TAppCoupon tAppCoupon = new TAppCoupon();
                    tAppCoupon.setAppUserId(l);
                    tAppCoupon.setCouponId(dto.getCouponId());
                    tAppCoupon.setEndTime(dto.getEndTime());
                    tAppCoupon.setWaysToObtain(dto.getWaysToObtain());
                    tAppCoupon.setStatus(1);
                    tAppCoupon.setCouponJson(JSON.toJSONString(coupon));
                    res.add(tAppCoupon);
                }
                tAppCouponService.saveBatch(res);
                break;
            case 3:
                for (String s : dto.getUserIds().split(",")) {
                    TAppCoupon tAppCoupon = new TAppCoupon();
                    tAppCoupon.setAppUserId(Long.valueOf(s));
                    tAppCoupon.setCouponId(dto.getCouponId());
                    tAppCoupon.setEndTime(dto.getEndTime());
                    tAppCoupon.setWaysToObtain(dto.getWaysToObtain());
                    tAppCoupon.setStatus(1);
                    tAppCoupon.setCouponJson(JSON.toJSONString(coupon));
                    res.add(tAppCoupon);
                }
                tAppCouponService.saveBatch(res);
                break;
        }
        tAppCouponService.saveBatch(res);
        return R.ok();
    }
    /**
     * 根据id获取优惠券领取记录
     * @param id
     * @return
     */
    @PostMapping("/getAppCouponById")
    public R<TAppCoupon> getAppCouponById(@RequestParam("id") Long id){
        TAppCoupon appCoupon = tAppCouponService.getById(id);
        return R.ok(appCoupon);
    }
}