package com.cf.imes.module.system.dal.mysql.funds.recharge;

import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.system.controller.admin.funds.recharge.vo.RechargePageReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.recharge.GiftMoneyDetailsDO;
import com.cf.imes.module.system.enums.recharge.RechargeTypeEnum;
import org.apache.ibatis.annotations.Mapper;

import java.util.Arrays;

/**
 * 赠送金活动详情表 gift_money_details Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface GiftMoneyDetailsMapper extends BaseMapperX<GiftMoneyDetailsDO> {
    default PageResult<GiftMoneyDetailsDO> selectPage(RechargePageReqVO reqVO) {
        LambdaQueryWrapperX<GiftMoneyDetailsDO> queryWrapper = new LambdaQueryWrapperX<GiftMoneyDetailsDO>()
                .likeIfPresent(GiftMoneyDetailsDO::getActivityName, reqVO.getActivityName())
                .eqIfPresent(GiftMoneyDetailsDO::getRechargeGiftType, reqVO.getRechargeGiftType())
                .eqIfPresent(GiftMoneyDetailsDO::getGiftAmount, reqVO.getGiftAmount())
                .eqIfPresent(GiftMoneyDetailsDO::getRechargeAmount, reqVO.getRechargeAmount())
                .betweenIfPresent(GiftMoneyDetailsDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(GiftMoneyDetailsDO::getEndTime, reqVO.getEndTime());

        Integer rechargeType = reqVO.getRechargeType();
        // WECHAT_AND_ALIPAY查全部、否则查WECHAT_AND_ALIPAY+查询类型
        if (ObjectUtil.isNotNull(rechargeType) && !RechargeTypeEnum.isWechatAndAlipay(rechargeType)) {
            queryWrapper.in(GiftMoneyDetailsDO::getRechargeType, Arrays.asList(RechargeTypeEnum.WECHAT_AND_ALIPAY.getStatus(), rechargeType));
        }

        if (reqVO.getOrder() == null) {
            queryWrapper.orderByDesc(GiftMoneyDetailsDO::getId);
        }else {
            if (reqVO.getOrder().equals("ascend")) {
                switch (reqVO.getField()) {
                    case "id" -> queryWrapper.orderByAsc(GiftMoneyDetailsDO::getId);
                    case "rechargeAmount" -> queryWrapper.orderByAsc(GiftMoneyDetailsDO::getRechargeAmount);
                    case "giftAmount" -> queryWrapper.orderByAsc(GiftMoneyDetailsDO::getGiftAmount);
                    case "startTime" -> queryWrapper.orderByAsc(GiftMoneyDetailsDO::getStartTime);
                    case "endTime" -> queryWrapper.orderByAsc(GiftMoneyDetailsDO::getEndTime);
                    case "updateTime" -> queryWrapper.orderByAsc(GiftMoneyDetailsDO::getUpdateTime);
                    default -> {
                        // default不排序
                    }
                }
            }else if (reqVO.getOrder().equals("descend")) {
                switch (reqVO.getField()) {
                    case "id" -> queryWrapper.orderByDesc(GiftMoneyDetailsDO::getId);
                    case "rechargeAmount" -> queryWrapper.orderByDesc(GiftMoneyDetailsDO::getRechargeAmount);
                    case "giftAmount" -> queryWrapper.orderByDesc(GiftMoneyDetailsDO::getGiftAmount);
                    case "startTime" -> queryWrapper.orderByDesc(GiftMoneyDetailsDO::getStartTime);
                    case "endTime" -> queryWrapper.orderByDesc(GiftMoneyDetailsDO::getEndTime);
                    case "updateTime" -> queryWrapper.orderByDesc(GiftMoneyDetailsDO::getUpdateTime);
                    default -> {
                        // default不排序
                    }
                }
            }
        }

        return selectPage(reqVO, queryWrapper);
    }
}
