package jnpf.certificate.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fantaibao.permission.annotation.DataScope;
import com.fantaibao.permission.enums.FilterWhereTypeEnum;
import jnpf.base.mapper.SuperMapper;
import jnpf.model.certificate.dto.EmployeeOrganizeDTO;
import jnpf.model.certificate.dto.HealthCertificateStatusDTO;
import jnpf.model.certificate.po.CertificateInstanceEntity;
import jnpf.model.certificate.req.CertificateHealthManageQueryReq;
import jnpf.model.certificate.req.CertificateStoreManageQueryReq;
import jnpf.model.certificate.vo.CertificateHealthDashboardStatusStatVO;
import jnpf.model.certificate.vo.CertificateHealthManageVO;
import jnpf.model.certificate.vo.CertificateStoreCustomStatusTableVO;
import jnpf.model.certificate.vo.CertificateStoreManageVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;

/**
 * 证照实例Mapper。
 */
public interface CertificateInstanceMapper extends SuperMapper<CertificateInstanceEntity> {

    /**
     * 健康证管理分页查询。
     *
     * @param page              分页参数
     * @param req               查询参数
     * @param nearExpireDays    临期天数阈值
     * @return 分页结果
     */
    @DataScope(tableAlias = "fci",tableField = "F_SubjectId")
    Page<CertificateHealthManageVO> queryHealthManagePage(@Param("page") Page<CertificateHealthManageVO> page,
                                                          @Param("req") CertificateHealthManageQueryReq req,
                                                          @Param("nearExpireDays") Integer nearExpireDays);

    @DataScope(tableAlias = "fci",tableField = "F_SubjectId")
    List<HealthCertificateStatusDTO> queryHealthCertificateStatus(@Param("userIds") Collection<String> userIds);

    /**
     * 非健康证看板按状态直接聚合统计（不按组织分组）
     *
     * @param certificateType 证照类型
     * @param templateId      模板ID（仅门店自定义证照场景）
     * @param storeIds         门店IDs筛选
     * @param storeType       门店类型筛选
     * @return 状态聚合结果
     */
    @DataScope(tableAlias = "fci", type = FilterWhereTypeEnum.STORE_FILTER, tableFieldOrg = "F_SubjectId")
    List<CertificateHealthDashboardStatusStatVO> queryStoreDashboardStatusStats(@Param("certificateType") String certificateType,
                                                                                 @Param("templateId") String templateId,
                                                                                 @Param("storeIds") List<String> storeIds,
                                                                                 @Param("storeType") String storeType);

    /**
     * 非健康证看板表格分页查询（按门店聚合）。
     *
     * @param page            分页参数
     * @param certificateType 证照类型
     * @param templateId      模板ID（仅门店自定义证照场景）
     * @param storeIds        门店IDs筛选
     * @param storeType       门店类型筛选
     * @return 门店表格分页数据
     */
    @DataScope(tableAlias = "fci", type = FilterWhereTypeEnum.ORGANIZATION_FILTER, tableFieldOrg = "F_SubjectId")
    Page<CertificateStoreCustomStatusTableVO> queryStoreDashboardTablePage(@Param("page") Page<CertificateStoreCustomStatusTableVO> page,
                                                                            @Param("certificateType") String certificateType,
                                                                            @Param("templateId") String templateId,
                                                                            @Param("storeIds") List<String> storeIds,
                                                                            @Param("storeType") String storeType);

    /**
     * 批量初始化员工健康证实例（仅插入不存在的数据）。
     *
     * @param entities 员工用户ID集合
     * @return 插入条数
     */
    int batchInitHealthCertificate(@Param("entities") Collection<CertificateInstanceEntity> entities);

    /**
     * 根据id查询证照实例
     * @param id
     * @return
     */
    CertificateInstanceEntity selectInstanceById(@Param("id") String id);

    /**
     * 根据多个subjectType和多个status查询证照实例
     * @param subjectTypes
     * @param statuses
     * @return
     */
    @DataScope(tableAlias = "fci",tableField = "F_SubjectId",type = FilterWhereTypeEnum.USER_FILTER)
    List<CertificateInstanceEntity> selectBySubjectTypesAndStatusesFilterUser(@Param("subjectTypes") Collection<Integer> subjectTypes,
                                                                              @Param("statuses") Collection<Integer> statuses,
                                                                              @Param("userIds") Collection<String> userIds);

    /**
     * 根据多个subjectType和多个status查询证照实例
     * @param subjectTypes
     * @param statuses
     * @return
     */
    @DataScope(tableAlias = "fci",tableFieldOrg = "F_SubjectId",type = FilterWhereTypeEnum.ORGANIZATION_FILTER)
    List<CertificateInstanceEntity> selectBySubjectTypesAndStatusesFilterOrganization(@Param("subjectTypes") Collection<Integer> subjectTypes,
                                                                                      @Param("statuses") Collection<Integer> statuses,
                                                                                      @Param("orgId") String orgId);

    @DataScope(tableAlias = "fci",tableFieldOrg = "F_SubjectId",type = FilterWhereTypeEnum.ORGANIZATION_FILTER)
    Page<CertificateStoreManageVO> selectCertificateStoreManageVOPage(@Param("page") Page<CertificateStoreManageVO> page,
                                                                      @Param("queryReq")CertificateStoreManageQueryReq queryReq);
}
