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

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.business.vo.BusinessPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.business.BusinessDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * 营业统计 Mapper
 *
 * @author 开发账号
 */
@Mapper
public interface BusinessMapper extends BaseMapperX<BusinessDO> {

    default PageResult<BusinessDO> selectPage(BusinessPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BusinessDO>()
                .eqIfPresent(BusinessDO::getTurnover, reqVO.getTurnover())
                .eqIfPresent(BusinessDO::getOrderSum, reqVO.getOrderSum())
                .eqIfPresent(BusinessDO::getCustomerSum, reqVO.getCustomerSum())
                .eqIfPresent(BusinessDO::getPriceAvg, reqVO.getPriceAvg())
                .eqIfPresent(BusinessDO::getRechargePurse, reqVO.getRechargePurse())
                .eqIfPresent(BusinessDO::getSubsidyPurse, reqVO.getSubsidyPurse())
                .eqIfPresent(BusinessDO::getGratis, reqVO.getGratis())
                .eqIfPresent(BusinessDO::getBreakfast, reqVO.getBreakfast())
                .eqIfPresent(BusinessDO::getLunch, reqVO.getLunch())
                .eqIfPresent(BusinessDO::getDinner, reqVO.getDinner())
                .eqIfPresent(BusinessDO::getWeigh, reqVO.getWeigh())
                .eqIfPresent(BusinessDO::getCarteenId, reqVO.getCarteenId())
                .betweenIfPresent(BusinessDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BusinessDO::getId));
    }

    @Select("select id from t_carteen where deleted = false")
    List<Long>  getAllStoreId();


    @Select("select * from t_business where carteen_id = #{carteenId} and create_time between #{startOfDay} and #{endOfDay}  limit 1 for update")
    BusinessDO getTodayBusiness(@Param("carteenId") Long carteenId, @Param("startOfDay") LocalDateTime startOfDay, @Param("endOfDay") LocalDateTime endOfDay);

}