package cn.iocoder.yudao.module.system.dal.mysql.carteen;

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.system.controller.admin.carteen.vo.CarteenPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.carteen.vo.CarteenWeekSalesRespVo;
import cn.iocoder.yudao.module.system.dal.dataobject.carteen.CarteenDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.LinkedList;

/**
 * 门店管理 Mapper
 *
 * @author 开发账号
 */
@Mapper
public interface CarteenMapper extends BaseMapperX<CarteenDO> {

    default PageResult<CarteenDO> selectPage(CarteenPageReqVO reqVO,Long userId) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CarteenDO>()
                .likeIfPresent(CarteenDO::getStoresName, reqVO.getStoresName())
                .likeIfPresent(CarteenDO::getStoreAddress, reqVO.getStoreAddress())
                .eqIfPresent(CarteenDO::getStatus, reqVO.getStatus())
                .eqIfPresent(CarteenDO::getMultipleManage, reqVO.getMultipleManage())
                .eqIfPresent(CarteenDO::getAccountOrder, reqVO.getAccountOrder())
                .eqIfPresent(CarteenDO::getPhone, reqVO.getPhone())
                .betweenIfPresent(CarteenDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CarteenDO::getId)
        );
    }
    @Select("<script>"+"SELECT\n" +
            "\tA.weigh as sales,\n" +
            "\tA.carteen_id  AS carteenId,\n" +
            "\tA.create_time AS createTime,\n" +
            "\tB.stores_name as carteenName\n" +
            "FROM\n" +
            "\tt_business AS  A  \n" +
            "INNER JOIN t_carteen as B on B.id=A.carteen_id\n" +
            "WHERE\n" +
            "\tA.create_time BETWEEN #{beginDate} and #{endDate}  ORDER BY A.create_time"+
            "<if test='carteenId != null'> and A.carteen_id= #{carteenId} </if>"+
            "</script>")
    LinkedList<CarteenWeekSalesRespVo> selectWeekSales(@Param("carteenId") Long carteenId,@Param("beginDate") DateTime beginDate, @Param("endDate") DateTime endDate);

    @Select("<script>"+"SELECT\n" +
            "\tb.create_time AS createTime,\n" +
            "\tb.store_name as carteenName,\n" +
            "\tb.store_id as carteenId,\n" +
            "\ta.weight as sales\n" +
            "FROM\n" +
            "\tmember_order_detail AS a\n" +
            "\tINNER JOIN member_dish_order AS b ON a.order_id = b.id\n" +
            "\n" +
            "\twhere a.create_time BETWEEN #{beginDate} and #{endDate} "+
            "<if test='carteenId != null'> and b.store_id= #{carteenId} </if>"+
            "</script>")
    LinkedList<CarteenWeekSalesRespVo>  getCarteenSales(@Param("carteenId")Long carteenId,@Param("beginDate") DateTime beginDate, @Param("endDate") DateTime endDate);
}