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

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.advertisement.vo.AdvertisementPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.advertisement.vo.AdvertisementRespVO;
import com.cf.imes.module.system.dal.dataobject.funds.advertisement.AdvertisementDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 广告信息表 Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface AdvertisementMapper extends BaseMapperX<AdvertisementDO> {

    default PageResult<AdvertisementDO> selectPage(AdvertisementPageReqVO reqVO) {

        LambdaQueryWrapperX<AdvertisementDO> queryWrapper = new LambdaQueryWrapperX<AdvertisementDO>()
                .likeIfPresent(AdvertisementDO::getAdName, reqVO.getAdName())
                .likeIfPresent(AdvertisementDO::getAdPosition, reqVO.getAdPosition())
                .eqIfPresent(AdvertisementDO::getStatus, reqVO.getStatus())
                .eqIfPresent(AdvertisementDO::getDeleted, false);

        return selectPage(reqVO, queryWrapper);

    }

    default int updateAdStatus(Long id, Integer newStatus) {
        return update(new LambdaUpdateWrapper<AdvertisementDO>()
                .set(AdvertisementDO::getStatus, newStatus)
                .eq(AdvertisementDO::getId, id)
                .eq(AdvertisementDO::getDeleted, false));
    }

    default List<AdvertisementDO> selectList(LocalDateTime now, Integer status) {
        return selectList(new LambdaQueryWrapperX<AdvertisementDO>()
                .le(AdvertisementDO::getStartTime, now)
                .eq(AdvertisementDO::getStatus, status));

    }



    default AdvertisementDO selectAdByAdName(String adName,Long id) {

        LambdaQueryWrapperX<AdvertisementDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(AdvertisementDO::getAdName, adName);

        if(id != null){
            queryWrapper.ne(AdvertisementDO::getId, id);
        }

        return selectOne(queryWrapper);

    }



    void updateAdStatusIsPublish(@Param("now") LocalDate now,@Param("status") Integer status);



    void updateAdStatusIsEndPublished(@Param("now") LocalDate now,@Param("status") Integer status);



    List<AdvertisementRespVO> selectAdList(@Param("adPosition") Integer adPosition,@Param("status") Integer status,@Param("now") LocalDate now);

}
