package com.ruoyi.system.mapper.house;

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.BusinessEntitySell;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BusinessEntitySellMapper {

    // 插入业务实体销售信息；以经营主体侧记录一条修改日志
    // 字段级：以经营主体为 subject，记录 BE_YEAR_SELL 的新增
    @DataChangeLog(subjectType = SubjectType.BUSINESS_ENTITY_INFO, operation = OperationType.UPDATE, idExpr = "businessEntityInfoId")
    @DataFieldChangeLog(
            subjectType = SubjectType.BUSINESS_ENTITY_INFO,
            operation = OperationType.INSERT,
            idExpr = "businessEntityInfoId",
            fields = {FieldCode.BE_YEAR_SELL}
    )
    void insertBusinessEntitySell(BusinessEntitySell businessEntitySell);

    // 根据ID查询业务实体销售信息
    BusinessEntitySell selectBusinessEntitySellById(@Param("id") String id);

    // 更新业务实体销售信息；以经营主体侧记录一条修改日志
    // 使用 preLoad，避免 update 的 <set> 中未带 businessEntityInfoId 时取不到
    @DataChangeLog(
            subjectType = SubjectType.BUSINESS_ENTITY_INFO,
            operation = OperationType.UPDATE,
            preLoadMapper = "com.ruoyi.system.mapper.house.BusinessEntitySellMapper.selectBusinessEntitySellById",
            idExpr = "@preload[*].businessEntityInfoId"
    )
    @DataFieldChangeLog(
            subjectType = SubjectType.BUSINESS_ENTITY_INFO,
            operation = OperationType.UPDATE,
            idExpr = "@preload[*].businessEntityInfoId",
            preLoadMapper = "com.ruoyi.system.mapper.house.BusinessEntitySellMapper.selectBusinessEntitySellById",
            fields = {FieldCode.BE_YEAR_SELL}
    )
    void updateBusinessEntitySell(BusinessEntitySell businessEntitySell);

    // 删除业务实体销售信息；改为逻辑删除后以经营主体侧记录一条修改日志
    @DataChangeLog(
            subjectType = SubjectType.BUSINESS_ENTITY_INFO,
            operation = OperationType.UPDATE,
            preLoadMapper = "com.ruoyi.system.mapper.house.BusinessEntitySellMapper.selectBusinessEntitySellById",
            idExpr = "@preload[*].businessEntityInfoId"
    )
    void deleteBusinessEntitySellById(@Param("id") String id);

    // 查询所有业务实体销售信息
    List<BusinessEntitySell> selectAllBusinessEntitySells();

    @DataChangeLog(subjectType = SubjectType.BUSINESS_ENTITY_INFO, operation = OperationType.UPDATE, idExpr = "businessEntityId")
    void deleteBusinessEntitySellByEntityId(@Param("businessEntityId") String businessEntityId);

    List<BusinessEntitySell> selectBusinessEntitySellByEntityId(@Param("entityId") String entityId);

    List<BusinessEntitySell> selectBusinessEntitySellByEntityIds(@Param("entityIds") List<String> entityIds);

    BusinessEntitySell selectBusinessEntitySellByBusinessEntityInfoId(@Param("businessEntityInfoId") String businessEntityInfoId, @Param("year") Integer year);

    BusinessEntitySell selectBusinessEntityTaxByBusinessEntityInfoId(@Param("businessEntityInfoId") String businessEntityInfoId, @Param("year") Integer year);
}
