package com.kym.service.admin.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.pagehelper.PageHelper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.kym.common.exception.BusinessException;
import com.kym.common.utils.CommUtil;
import com.kym.entity.admin.ActivityStation;
import com.kym.entity.admin.Coupon;
import com.kym.entity.admin.RechargeRights;
import com.kym.entity.admin.queryParams.RightsQueryParam;
import com.kym.entity.common.PageBean;
import com.kym.entity.common.PageParams;
import com.kym.mapper.admin.RechargeRightsMapper;
import com.kym.service.admin.RechargeRightsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 充值权益表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2023-10-18
 */
@Service
@DS("db-admin")
public class RechargeRightsServiceImpl extends MPJBaseServiceImpl<RechargeRightsMapper, RechargeRights> implements RechargeRightsService {

    /**
     * 所选站点可以参与的充值权益卡活动
     *
     * @param params
     * @return
     */
    @Override
    public PageBean<RechargeRights> listAvailableRechargeRights(RightsQueryParam params) {
        if (CommUtil.isEmptyOrNull(params.getStationId())) {
            throw new BusinessException("请选择充电站点");
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        MPJLambdaWrapper<RechargeRights> wrapper = JoinWrappers.lambda(RechargeRights.class)
                .selectAll(RechargeRights.class)
                .leftJoin(ActivityStation.class, ActivityStation::getActivityId, RechargeRights::getActivityId)
                .eq(RechargeRights::getStatus, Coupon.STATUS_进行中)
                .eq(ActivityStation::getStationId, params.getStationId());
        List<RechargeRights> res = this.selectJoinList(RechargeRights.class, wrapper);
        return new PageBean<>(res);
    }

    @Override
    public PageBean<RechargeRights> listRechargeRight(PageParams pageParams, String activityId) {
        PageHelper.startPage(pageParams.getPageNum(), pageParams.getPageSize());
        var list = lambdaQuery().eq(!CommUtil.isEmptyOrNull(activityId), RechargeRights::getActivityId, activityId).orderByAsc(RechargeRights::getEndTime).list();
        return new PageBean<>(list);
    }
}
