package com.ruoyi.system.mapper.house;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.system.domain.changerecord.annotation.DataChangeLog;
import com.ruoyi.system.domain.changerecord.enums.OperationType;
import com.ruoyi.system.domain.changerecord.enums.SubjectType;
import com.ruoyi.system.domain.fieldchangerecord.annotation.DataFieldChangeLog;
import com.ruoyi.system.domain.fieldchangerecord.enums.FieldCode;
import com.ruoyi.system.domain.house.HouseResource;
import com.ruoyi.system.domain.house.vo.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface HouseResourceMapper {

    @DataChangeLog(subjectType = SubjectType.HOUSE_RESOURCE, operation = OperationType.INSERT, idExpr = "id")
    @DataFieldChangeLog(
            subjectType = SubjectType.HOUSE_RESOURCE,
            operation = OperationType.INSERT,
            idExpr = "id",
            fields = {
                    FieldCode.HR_BUSINESS_STATUS,
                    FieldCode.HR_RESOURCE_TYPE,
                    FieldCode.HR_AREA,
                    FieldCode.HR_EQUITY_TEL,
                    FieldCode.HR_UNIT_PRICE,
                    FieldCode.HR_IMAGE,
                    FieldCode.HR_LICENSE_IMAGE
            }
    )
    void insertHouseResource(HouseResource houseResource);

    HouseResource selectHouseResourceById(String id);

    @DataChangeLog(subjectType = SubjectType.HOUSE_RESOURCE, operation = OperationType.UPDATE, idExpr = "id")
    @DataFieldChangeLog(
            subjectType = SubjectType.HOUSE_RESOURCE,
            operation = OperationType.UPDATE,
            idExpr = "id",
            preLoadMapper = "com.ruoyi.system.mapper.house.HouseResourceMapper.selectHouseResourceById",
            fields = {
                    FieldCode.HR_BUSINESS_STATUS,
                    FieldCode.HR_RESOURCE_TYPE,
                    FieldCode.HR_AREA,
                    FieldCode.HR_EQUITY_TEL,
                    FieldCode.HR_UNIT_PRICE,
                    FieldCode.HR_IMAGE,
                    FieldCode.HR_LICENSE_IMAGE
            }
    )
    void updateHouseResource(HouseResource houseResource);

    @DataChangeLog(subjectType = SubjectType.HOUSE_RESOURCE, operation = OperationType.DELETE, idExpr = ".")
    void deleteHouseResourceById(String id);

    @DataChangeLog(subjectType = SubjectType.HOUSE_RESOURCE, operation = OperationType.INSERT, idExpr = "list[*].id")
    @DataFieldChangeLog(
            subjectType = SubjectType.HOUSE_RESOURCE,
            operation = OperationType.INSERT,
            idExpr = "list[*].id",
            fields = {
                    FieldCode.HR_BUSINESS_STATUS,
                    FieldCode.HR_RESOURCE_TYPE,
                    FieldCode.HR_AREA,
                    FieldCode.HR_EQUITY_TEL,
                    FieldCode.HR_UNIT_PRICE,
                    FieldCode.HR_IMAGE,
                    FieldCode.HR_LICENSE_IMAGE
            }
    )
    void batchInsertHouseResources(List<HouseResource> houseResources);

    IPage<HouseResourcePage> selectPage(IPage<HouseResource> page, @Param("query") HouseResourcePageQuery houseResourcePageQuery);

    List<HouseResourcePage> selectProfileGraph(@Param("query") HouseResourcePageQuery houseResourcePageQuery);

    List<HouseResourcePage> selectList(@Param("query") HouseResourcePageQuery houseResourcePageQuery);

    List<HouseResourcePage> exportList(@Param("query") HouseResourcePageQuery houseResourcePageQuery);

    List<HouseResource> selectAll();

    List<HouseResourcePage> selectPageBusinessEntityStatistics();

    List<HouseResourcePage> selectForContrast(@Param("two") String two, @Param("name") String name);

    List<HouseResourcePage> selectByIdList(@Param("idList") List<String> idList);

    /**
     * 时间窗内房源（subject_type = HOUSE_RESOURCE）的新增 / 修改**详情**行。
     *
     * <p>每行一个 (type, operation_type, subject_id) 元组，由 Service 层按 (type, operation_type)
     * 分组聚合出 count + ids。仅包含 operation_type IN (1, 2)。强制 {@code hr.delete_flag = 0}，
     * 保证 id 列表可以安全回传给 {@code /api/house/page} 做 drilldown。</p>
     */
    List<HouseResourceDataCollectionDetailRow> selectHouseResourceChangeDetails(@Param("query") HouseResourceDataCollectionQuery query);

    /**
     * 时间窗内房源（subject_type = HOUSE_RESOURCE）的**删除**计数，按 house_resource.type 分组。
     *
     * <p>只含 operation_type = 3，**不**过滤 {@code hr.delete_flag}（需要统计已逻辑删除的房源）。</p>
     */
    List<HouseResourceDataCollectionRow> selectHouseResourceDeleteCount(@Param("query") HouseResourceDataCollectionQuery query);

    /**
     * 时间窗内经营主体（subject_type = BUSINESS_ENTITY_INFO）的新增 / 修改**详情**行。
     *
     * <p>每行一个 (operation_type, subject_id) 元组（type 恒为 0）。仅包含 operation_type IN (1, 2)。
     * 强制 {@code bei.delete_flag = 0}。</p>
     */
    List<HouseResourceDataCollectionDetailRow> selectBusinessEntityChangeDetails(@Param("query") HouseResourceDataCollectionQuery query);

    /**
     * 时间窗内经营主体（subject_type = BUSINESS_ENTITY_INFO）的**删除**计数。
     *
     * <p>只含 operation_type = 3，**不**过滤 {@code bei.delete_flag}。返回 0 或 1 行。</p>
     */
    List<HouseResourceDataCollectionRow> selectBusinessEntityDeleteCount(@Param("query") HouseResourceDataCollectionQuery query);
}
