package co.yixiang.yshop.module.shop.dal.mysql.shop;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.module.shop.controller.admin.shop.vo.ShopExportReqVO;
import co.yixiang.yshop.module.shop.controller.admin.shop.vo.ShopPageReqVO;
import co.yixiang.yshop.module.shop.dal.dataobject.shop.ShopDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 门店 Mapper
 *
 * @author admin
 */
@Mapper
public interface ShopMapper extends BaseMapperX<ShopDO> {

    default PageResult<ShopDO> selectPage(ShopPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ShopDO>()
                .likeIfPresent(ShopDO::getStoreName, reqVO.getStoreName())
                .eqIfPresent(ShopDO::getStoreIntro, reqVO.getStoreIntro())
                .eqIfPresent(ShopDO::getStorePhone, reqVO.getStorePhone())
                .eqIfPresent(ShopDO::getIconUrl, reqVO.getIconUrl())
                .eqIfPresent(ShopDO::getAddress, reqVO.getAddress())
                .eqIfPresent(ShopDO::getLon, reqVO.getLon())
                .eqIfPresent(ShopDO::getLat, reqVO.getLat())
                .betweenIfPresent(ShopDO::getWriteOffStartTime, reqVO.getWriteOffStartTime())
                .betweenIfPresent(ShopDO::getWriteOffEndTime, reqVO.getWriteOffEndTime())
                .eqIfPresent(ShopDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ShopDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ShopDO::getId));
    }

    default List<ShopDO> selectList(ShopExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ShopDO>()
                .likeIfPresent(ShopDO::getStoreName, reqVO.getStoreName())
                .eqIfPresent(ShopDO::getStoreIntro, reqVO.getStoreIntro())
                .eqIfPresent(ShopDO::getStorePhone, reqVO.getStorePhone())
                .eqIfPresent(ShopDO::getIconUrl, reqVO.getIconUrl())
                .eqIfPresent(ShopDO::getAddress, reqVO.getAddress())
                .eqIfPresent(ShopDO::getLon, reqVO.getLon())
                .eqIfPresent(ShopDO::getLat, reqVO.getLat())
                .betweenIfPresent(ShopDO::getWriteOffStartTime, reqVO.getWriteOffStartTime())
                .betweenIfPresent(ShopDO::getWriteOffEndTime, reqVO.getWriteOffEndTime())
                .eqIfPresent(ShopDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ShopDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ShopDO::getId));
    }

    @Update("update yshop_shop set status = 0 where write_off_end_time < now() and status = 1")
    void handlerExpireShop();
}
