package com.sky.ioc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.ioc.entity.domain.supermarket.SupermarketOrder;
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 SupermarketOrderMapper extends BaseMapper<SupermarketOrder> {

    @Select("<script> " +
            "SELECT  count(1) as salesVolume,s.name,s.img,s.price " +
            " from supermarket_order o " +
            " JOIN supermarket s on s.name=o.product_name  where 1=1 " +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and dept_id=#{iocParam.deptId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.companyId != null '>" +
            " and company_id=#{iocParam.companyId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.timeRange != null '>" +
            " and order_time BETWEEN #{iocParam.timeRange.startDate} AND #{iocParam.timeRange.endDate} " +
            "</if>"+
            "GROUP BY product_name,s.name,s.img,s.price " +
            "ORDER by salesVolume DESC " +
            "</script> ")
    List<Map<String,String>> getListByCompanyIdAndDeptId(@Param("iocParam") IocParam iocParam);

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

    @Select("<script>" +
            "SELECT count(1) FROM supermarket_order where 1=1 " +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and dept_id=#{iocParam.deptId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.companyId != null '>" +
            " and company_id=#{iocParam.companyId} " +
            "</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);

    @Select("<script> " +
            "SELECT count(DISTINCT user_id) FROM supermarket_order  where 1=1 " +
            "<if test='iocParam!=null and iocParam.deptId != null '>" +
            " and dept_id=#{iocParam.deptId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.companyId != null '>" +
            " and company_id=#{iocParam.companyId} " +
            "</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 supermarket_order " +
            "WHERE order_type='微信' " +
            "<if test='iocParam!=null and iocParam.timeRange != null '>" +
            " and order_time BETWEEN #{iocParam.timeRange.startDate} AND #{iocParam.timeRange.endDate} " +
            "</if>" +
            " </script>")
    Double getThirdTotalPriceByCompanyIdAndDeptId(@Param("iocParam") IocParam iocParam);

}
