package com.sky.ioc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.ioc.entity.domain.canteen.Cuisine;
import com.sky.ioc.entity.domain.canteen.Order;
import com.sky.ioc.entity.params.IocParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface CuisineOrderMapper extends BaseMapper<Order> {
    @Select("<script> " +
            "SELECT count(DISTINCT phone) FROM cuisine_order  where 1=1 " +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and department_id=#{iocParam.deptId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.timeRange != null '>" +
            " and order_time BETWEEN #{iocParam.timeRange.startDate} AND #{iocParam.timeRange.endDate} " +
            "</if>" +
            "</script>")
    Integer getTotalPersonByCompanyIdAndDeptId(@Param("iocParam") IocParam iocParam);

    @Select("<script>" +
            "SELECT sum(order_price) FROM cuisine_order where 1=1 " +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and department_id=#{iocParam.deptId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.timeRange != null '>" +
            " and order_time BETWEEN #{iocParam.timeRange.startDate} AND #{iocParam.timeRange.endDate} " +
            "</if>"+
            "<if test='payType!=null '>" +
            " and paytype=#{payType} " +
            "</if>"+
            "</script>")
    Double getTotalPriceByCompanyIdAndDeptId(@Param("iocParam") IocParam iocParam,@Param("payType")Integer payType);

    @Select("<script>" +
            "SELECT avg(order_price) FROM cuisine_order where 1=1 " +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and department_id=#{iocParam.deptId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.timeRange != null '>" +
            " and order_time BETWEEN #{iocParam.timeRange.startDate} AND #{iocParam.timeRange.endDate} " +
            "</if>"+
            "</script>")
    Double getAvgPriceByCompanyIdAndDeptId(@Param("iocParam") IocParam iocParam);

    @Select("<script>" +
            "SELECT count(1) FROM cuisine_order where 1=1 " +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and department_id=#{iocParam.deptId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.timeRange != null '>" +
            " and order_time BETWEEN #{iocParam.timeRange.startDate} AND #{iocParam.timeRange.endDate} " +
            "</if>"+
            "</script>")
    Integer getTotalOrderByCompanyIdAndDeptId(@Param("iocParam") IocParam iocParam);

    /**
     *  "<if test='iocParam!=null and iocParam.timeRange != null '>" +
     *             " and a.order_time BETWEEN #{iocParam.timeRange.startDate} AND #{iocParam.timeRange.endDate} " +
     *             "</if>"+*/
    @Select("<script>" +
            "SELECT count(item.p_name) as total,c.cuisine_name as p_name,item.p_id,c.price,c.cuisine_img from cuisine c " +
            "LEFT JOIN cuisine_order_item  item on c.id= item.p_id  " +
            "LEFT JOIN cuisine_order a  on a.order_id = item.order_num where 1=1 " +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and a.department_id=#{iocParam.deptId} " +
            "</if>"+

            "GROUP BY  item.p_id,c.price,c.cuisine_img,c.cuisine_name ORDER BY total desc limit #{iocParam.limit} " +
            "</script>")
    List<Map<String,String>> getTopListByCompanyIdAndDeptId(@Param("iocParam") IocParam iocParam);

    @Select("<script>" +
            "SELECT order_price as orderPrice,order_time as orderTime,paytype as payType from cuisine_order a where 1=1" +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and a.department_id=#{iocParam.deptId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.timeRange != null '>" +
            "<![CDATA[ and a.order_time >= #{iocParam.timeRange.startDate} AND a.order_time <= #{iocParam.timeRange.endDate} ]]> " +
            "</if>"+
            "</script>")
    List<Order> getListByCompanyIdAndDeptId(@Param("iocParam") IocParam iocParam);

}
