package org.dromara.system.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.dromara.common.mybatis.annotation.DataColumn;
import org.dromara.common.mybatis.annotation.DataPermission;
import org.dromara.system.domain.SysTenant;
import org.dromara.system.domain.bo.SysTenantBo;
import org.dromara.system.domain.bo.SysTenantExtendBo;
import org.dromara.system.domain.dto.TenantDTO;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;

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

/**
 * 租户Mapper接口
 *
 * @author Michelle.Chung
 */
@Mapper
public interface SysTenantMapper extends BaseMapperPlus<SysTenant, SysTenantVo> {

    @Select("SELECT COUNT(*) FROM sys_tenant WHERE status = 0")
    Object countStore();

    @Select("SELECT " +
        "SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS count1, " +
        "SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS count2 " +
        "FROM sys_tenant " +
        "WHERE type = 0")
    Map<String, Object> countProduct();


    @Select("SELECT " +
        "t.company_name, " +
        "COALESCE(SUM(p.sales), 0) AS sales, " +
        "COALESCE(SUM(p.sales * p.price), 0) AS total_amount " +
        "FROM sys_tenant t " +
        "LEFT JOIN pms_product p ON t.tenant_id = p.tenant_id " +
        "GROUP BY t.tenant_id, t.company_name " +
        "ORDER BY total_amount DESC " +
        "LIMIT 20")
    List<Map<String, Object>> getTop20Stores();

    @DataPermission({
        @DataColumn(key = "deptName", value = "t.create_dept"),
        @DataColumn(key = "userName", value = "t.create_by")
    })
    IPage<TenantDTO> getlist(@Param("page") Page<SysTenant> page, @Param("query") SysTenantExtendBo query);

    @Select("  SELECT " +
        "        t.*, " +
        "        e.* " +
        "        FROM " +
        "        sys_tenant t " +
        "        LEFT JOIN " +
        "        sys_tenant_extend e ON t.tenant_id = e.tenant_id")
    List<TenantDTO> getAll(SysTenantBo bo);


    @Results({
        @Result(column = "expire_date", property = "expireDate"),
        @Result(column = "promoter_status", property = "promoterStatus")
    })
    @Select("SELECT t.*, e.*, m.nickname AS inviteUserName " +
        "FROM sys_tenant t " +
        "LEFT JOIN sys_tenant_extend e ON t.id = e.store_id " +
        "LEFT JOIN ums_member m ON e.invite_user_id = m.id " +
        "WHERE t.id = #{id}")
    TenantDTO getById(Long id);


    int batchDeleteTenantExtendByStoreIds(List<Long> ids);

    int batchDeleteTenantByIds(List<Long> ids);

    @Select("SELECT * FROM sys_tenant WHERE tenant_id = #{tenantId}")
    SysTenant selectByTenantId(String tenantId);

    @Select("<script>" +
        "SELECT * FROM sys_tenant " +
        "WHERE type = #{tenantType} " +
        "AND create_dept IN " +
        "<foreach item='item' collection='deptIdList' open='(' separator=',' close=')'>#{item}</foreach> " +
        "AND status = 0 " +
        "AND del_flag = '0'" +
        "</script>")
    Page<SysTenant> selectByTenantTypeAndCreateDept(@Param("page") Page<SysTenant> page, @Param("tenantType") int tenantType, @Param("deptIdList") List<Long> deptIdList);


    @Select("<script>" +
        "SELECT * FROM sys_tenant " +
        "WHERE type = #{tenantType} " +
        "AND create_dept IN " +
        "<foreach item='item' collection='deptIdList' open='(' separator=',' close=')'>#{item}</foreach> " +
        "AND status = 0 " +
        "AND del_flag = '0'" +
        "</script>")
    List<SysTenant> selectByTenantTypeAndCreateDept(@Param("tenantType") int tenantType, @Param("deptIdList") List<Long> deptIdList);

    @Select(
        "SELECT " +
        "     sys_tenant.*  " +
        "FROM " +
        "     sys_tenant " +
        "     LEFT JOIN sys_dept ON sys_tenant.tenant_id = sys_dept.tenant_id  " +
        "WHERE " +
        "     sys_dept.dept_id = #{deptId}  " +
        "     AND sys_tenant.del_flag = '0'  " +
        "     AND sys_dept.del_flag = '0'")
    SysTenant queryByDeptId(@Param("deptId") Long deptId);
}
