package cn.iocoder.yudao.module.member.dal.mysql.customertraffic;

import cn.hutool.core.date.DateTime;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.customertraffic.CustomerTrafficDO;
import cn.iocoder.yudao.module.member.dal.dataobject.customertraffic.CustomerTrafficDetailDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 顾客流量 Mapper
 *
 * @author 开发账号
 */
@Mapper
public interface CustomerTrafficMapper extends BaseMapperX<CustomerTrafficDO> {

    default PageResult<CustomerTrafficDO> selectPage(CustomerTrafficPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CustomerTrafficDO>()
                .eqIfPresent(CustomerTrafficDO::getTimePoint, reqVO.getTimePoint())
                .eqIfPresent(CustomerTrafficDO::getStoreId, reqVO.getStoreId())
                .eqIfPresent(CustomerTrafficDO::getCustomerNum, reqVO.getCustomerNum())
                .betweenIfPresent(CustomerTrafficDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CustomerTrafficDO::getId));
    }
    @Select("<script>"+"SELECT\n" +
            "\ta.customer_num,\n" +
            "\ta.id,\n" +
            "\ta.time_point,\n" +
            "\ta.store_id,a.create_time,\n" +
            "\tb.stores_name \n" +
            "FROM\n" +
            "\tmember_customer_traffic AS a\n" +
            "\tINNER JOIN t_carteen AS b ON a.store_id = b.id \n" +
            "\tAND a.create_time BETWEEN  #{start} AND #{end} "+
            "<if test=' storeId != null'> and a.store_id= #{storeId} </if>"
            +"</script>")
    List<CustomerTrafficDetailDO> getAllCustomerNum(@Param("storeId")Long storeId,@Param("start")DateTime start,@Param("end")DateTime end);
}