package co.yixiang.yshop.module.product.dal.mysql.campaigninfo;

import java.util.*;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.module.product.controller.admin.campaigninfo.param.CampaignJudgmentParam;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignNumberJudgeVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductReqVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductRespVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignSkuVO;
import co.yixiang.yshop.module.product.dal.dataobject.campaigninfo.CampaignInfoDO;
import co.yixiang.yshop.module.product.enums.campaign.CampaignStateEnum;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import co.yixiang.yshop.module.product.controller.admin.campaigninfo.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 活动信息 Mapper
 *
 * @author moxiangrong
 */
@Mapper
public interface CampaignInfoMapper extends BaseMapperX<CampaignInfoDO> {

    List<Long> checkRepeat(CampaignJudgmentParam param);

    default PageResult<CampaignInfoDO> selectPage(CampaignInfoPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CampaignInfoDO>()
                .likeIfPresent(CampaignInfoDO::getName, reqVO.getName())
                .eqIfPresent(CampaignInfoDO::getType, reqVO.getType())
                .eqIfPresent(CampaignInfoDO::getState, reqVO.getState())
                .betweenIfPresent(CampaignInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CampaignInfoDO::getId));
    }

    default PageResult<CampaignInfoDO> getCanvasCampaignPage(CanvasCampaignPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CampaignInfoDO>()
                .likeIfPresent(CampaignInfoDO::getName, reqVO.getName())
                .eqIfPresent(CampaignInfoDO::getType, reqVO.getType())
                .in(CollectionUtils.isNotEmpty(reqVO.getIds()), CampaignInfoDO::getId, reqVO.getIds())
                .in(CampaignInfoDO::getState, Arrays.asList(CampaignStateEnum.PREHEAT.getValue(),
                        CampaignStateEnum.IN_PROGRESS.getValue()))
                .orderByDesc(CampaignInfoDO::getId));
    }

    default List<CampaignInfoDO> selectList(CampaignInfoExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CampaignInfoDO>()
                .likeIfPresent(CampaignInfoDO::getName, reqVO.getName())
                .eqIfPresent(CampaignInfoDO::getType, reqVO.getType())
                .eqIfPresent(CampaignInfoDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(CampaignInfoDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(CampaignInfoDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(CampaignInfoDO::getIfLimit, reqVO.getIfLimit())
                .eqIfPresent(CampaignInfoDO::getLimitNumber, reqVO.getLimitNumber())
                .eqIfPresent(CampaignInfoDO::getIfEnable, reqVO.getIfEnable())
                .eqIfPresent(CampaignInfoDO::getEnableTime, reqVO.getEnableTime())
                .eqIfPresent(CampaignInfoDO::getState, reqVO.getState())
                .eqIfPresent(CampaignInfoDO::getPerson, reqVO.getPerson())
                .eqIfPresent(CampaignInfoDO::getEffectiveTime, reqVO.getEffectiveTime())
                .betweenIfPresent(CampaignInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CampaignInfoDO::getId));
    }

    List<CampaignOrderVo> getCampaignInfoList(@Param("campaignIds") List<Long> campaignIds);

    List<CampaignProductRespVO> getCampaignProductPage(Page page, @Param("reqVO") CampaignProductReqVO reqVO);

    List<CampaignSkuVO> getCampaignsByProductAttrUniques(@Param("productAttrUniques") List<String> productAttrUniques);

    Integer numberJudge(@Param("vo") CampaignNumberJudgeVO vo);

    Integer stockJudge(@Param("vo") CampaignNumberJudgeVO vo);

    void deStock(@Param("productAttrUnique") String productAttrUnique, @Param("number") Integer number);

}
