package cn.newfeifan.mall.module.trade.dal.mysql.aftersale;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.mybatis.core.mapper.BaseMapperX;
import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.module.trade.controller.admin.aftersale.vo.AfterSalePageReqVO;
import cn.newfeifan.mall.module.trade.controller.admin.order.vo.TradeOrderPageReqVO;
import cn.newfeifan.mall.module.trade.dal.dataobject.aftersale.AfterSaleDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;

@Mapper
public interface AfterSaleMapper extends BaseMapperX<AfterSaleDO> {

    default PageResult<AfterSaleDO> selectPage(AfterSalePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AfterSaleDO>()
                .likeIfPresent(AfterSaleDO::getNo, reqVO.getNo())
                .eqIfPresent(AfterSaleDO::getStatus, reqVO.getStatus())
                .eqIfPresent(AfterSaleDO::getSpuId, reqVO.getSpuId())
                .eqIfPresent(AfterSaleDO::getType, reqVO.getType())
                .eqIfPresent(AfterSaleDO::getShopId,reqVO.getShopId())
                .eqIfPresent(AfterSaleDO::getMerchantId,reqVO.getMerchantId())
                .eqIfPresent(AfterSaleDO::getWay, reqVO.getWay())
                .eqIfPresent(AfterSaleDO::getUserId, reqVO.getUserId())
                .inIfPresent(AfterSaleDO::getShopId, reqVO.getShopIds())
                .likeIfPresent(AfterSaleDO::getOrderNo, reqVO.getOrderNo())
                .likeIfPresent(AfterSaleDO::getSpuName, reqVO.getSpuName())
                .betweenIfPresent(AfterSaleDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AfterSaleDO::getId));
    }

    default PageResult<AfterSaleDO> selectPage(Long userId, PageParam pageParam) {
        return selectPage(pageParam, new LambdaQueryWrapperX<AfterSaleDO>()
                .eqIfPresent(AfterSaleDO::getUserId, userId)
                .orderByDesc(AfterSaleDO::getId));
    }

    default int updateByIdAndStatus(Long id, Integer status, AfterSaleDO update) {
        return update(update, new LambdaUpdateWrapper<AfterSaleDO>()
                .eq(AfterSaleDO::getId, id).eq(AfterSaleDO::getStatus, status));
    }

    default AfterSaleDO selectByIdAndUserId(Long id, Long userId) {
        return selectOne(AfterSaleDO::getId, id,
                AfterSaleDO::getUserId, userId);
    }

    default Long selectCountByUserIdAndStatus(Long userId, Collection<Integer> statuses) {
        return selectCount(new LambdaQueryWrapperX<AfterSaleDO>()
                .eqIfPresent(AfterSaleDO::getUserId, userId)
                .in(AfterSaleDO::getStatus, statuses));
    }

    default Long selectCountByUserIdAndStatus(Long userId, Collection<Integer> statuses, TradeOrderPageReqVO reqVO) {
        return selectCount(new LambdaQueryWrapperX<AfterSaleDO>()
                .eqIfPresent(AfterSaleDO::getUserId, userId)
                .in(AfterSaleDO::getStatus, statuses)
                .likeIfPresent(AfterSaleDO::getNo, reqVO.getNo())
                .eqIfPresent(AfterSaleDO::getType, reqVO.getType())
                .eqIfPresent(AfterSaleDO::getShopId,reqVO.getShopId())
                .eqIfPresent(AfterSaleDO::getMerchantId,reqVO.getMerchantId())
                .eqIfPresent(AfterSaleDO::getUserId, reqVO.getUserId())
                .inIfPresent(AfterSaleDO::getShopId, reqVO.getShopIds())
                .betweenIfPresent(AfterSaleDO::getCreateTime, reqVO.getCreateTime())
        );
    }

    default Long selectCount(Integer status,AfterSalePageReqVO reqVO){
        return selectCount(new LambdaQueryWrapperX<AfterSaleDO>()
                .eq(AfterSaleDO::getStatus, status)
                .likeIfPresent(AfterSaleDO::getNo, reqVO.getNo())
                .eqIfPresent(AfterSaleDO::getSpuId, reqVO.getSpuId())
                .eqIfPresent(AfterSaleDO::getType, reqVO.getType())
                .eqIfPresent(AfterSaleDO::getShopId,reqVO.getShopId())
                .eqIfPresent(AfterSaleDO::getMerchantId,reqVO.getMerchantId())
                .eqIfPresent(AfterSaleDO::getWay, reqVO.getWay())
                .eqIfPresent(AfterSaleDO::getUserId, reqVO.getUserId())
                .inIfPresent(AfterSaleDO::getShopId, reqVO.getShopIds())
                .likeIfPresent(AfterSaleDO::getOrderNo, reqVO.getOrderNo())
                .likeIfPresent(AfterSaleDO::getSpuName, reqVO.getSpuName())
                .betweenIfPresent(AfterSaleDO::getCreateTime, reqVO.getCreateTime())
        );
    }

}
