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

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.productpromotion.vo.ProductPromotionPageReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.productpromotion.ProductPromotionDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 产品优惠活动 product_promotion Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface ProductPromotionMapper extends BaseMapperX<ProductPromotionDO> {
    default PageResult<ProductPromotionDO> selectPage(ProductPromotionPageReqVO reqVO) {
        LambdaQueryWrapperX<ProductPromotionDO> queryWrapper = new LambdaQueryWrapperX<ProductPromotionDO>()
                .likeIfPresent(ProductPromotionDO::getActivityName, reqVO.getActivityName())
                .eqIfPresent(ProductPromotionDO::getPurchaseDuration, reqVO.getPurchaseDuration())
                .eqIfPresent(ProductPromotionDO::getGiftDuration, reqVO.getGiftDuration())
                .betweenIfPresent(ProductPromotionDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(ProductPromotionDO::getEndTime, reqVO.getEndTime())
                .likeIfPresent(ProductPromotionDO::getRemark, reqVO.getRemark());

        if (reqVO.getOrder() == null) {
            queryWrapper.orderByDesc(ProductPromotionDO::getId);
        }else {
            if (reqVO.getOrder().equals("ascend")) {
                switch (reqVO.getField()) {
                    case "id" -> queryWrapper.orderByAsc(ProductPromotionDO::getId);
                    case "purchaseDuration" -> queryWrapper.orderByAsc(ProductPromotionDO::getPurchaseDuration);
                    case "giftDuration" -> queryWrapper.orderByAsc(ProductPromotionDO::getGiftDuration);
                    case "startTime" -> queryWrapper.orderByAsc(ProductPromotionDO::getStartTime);
                    case "endTime" -> queryWrapper.orderByAsc(ProductPromotionDO::getEndTime);
                    case "updateTime" -> queryWrapper.orderByAsc(ProductPromotionDO::getUpdateTime);
                    default -> {
                        // default不排序
                    }
                }
            }else if (reqVO.getOrder().equals("descend")) {
                switch (reqVO.getField()) {
                    case "id" -> queryWrapper.orderByDesc(ProductPromotionDO::getId);
                    case "purchaseDuration" -> queryWrapper.orderByDesc(ProductPromotionDO::getPurchaseDuration);
                    case "giftDuration" -> queryWrapper.orderByDesc(ProductPromotionDO::getGiftDuration);
                    case "startTime" -> queryWrapper.orderByDesc(ProductPromotionDO::getStartTime);
                    case "endTime" -> queryWrapper.orderByDesc(ProductPromotionDO::getEndTime);
                    case "updateTime" -> queryWrapper.orderByDesc(ProductPromotionDO::getUpdateTime);
                    default -> {
                        // default不排序
                    }
                }
            }
        }

        return selectPage(reqVO, queryWrapper);
    }
}
