package com.yhy.module.core.dal.mysql.resource;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yhy.module.core.controller.admin.resource.vo.ResourceMergedPageReqVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceMergedRespVO;
import com.yhy.module.core.dal.dataobject.resource.ResourceMergedDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 复合工料机 Mapper
 *
 * @author yihuiyong
 */
@Mapper
public interface ResourceMergedMapper extends BaseMapperX<ResourceMergedDO> {

    /**
     * 根据ID查询复合工料机（JOIN 第四层数据）
     *
     * @param id 主键
     * @return 复合工料机（包含第四层信息）
     */
    @Select("SELECT " +
            "  rm.id, rm.tenant_id, rm.merged_id, rm.source_id, rm.region_code, " +
            "  rm.quota_consumption, rm.tax_excl_market_price, " +
            "  rm.creator, rm.create_time, rm.updater, rm.update_time, " +
            "  ri.code, ri.name, ri.unit, ri.spec, " +
            "  ri.catalog_item_id, ri.category_id, ri.tax_rate, " +
            "  ri.tax_excl_base_price, ri.tax_incl_base_price, " +
            "  ri.tax_excl_compile_price, ri.tax_incl_compile_price, " +
            "  ri.calc_base, " +
            "  rc.name AS category_name, " +
            "  ROUND(COALESCE(ri.tax_excl_base_price, 0) * COALESCE(rm.quota_consumption, 0) / 100, 4) AS tax_excl_base_total, " +
            "  ROUND(COALESCE(ri.tax_incl_base_price, 0) * COALESCE(rm.quota_consumption, 0) / 100, 4) AS tax_incl_base_total, " +
            "  ROUND(COALESCE(ri.tax_excl_compile_price, 0) * COALESCE(rm.quota_consumption, 0) / 100, 4) AS tax_excl_compile_total, " +
            "  ROUND(COALESCE(ri.tax_incl_compile_price, 0) * COALESCE(rm.quota_consumption, 0) / 100, 4) AS tax_incl_compile_total " +
            "FROM yhy_resource_merged rm " +
            "LEFT JOIN yhy_resource_item ri ON rm.source_id = ri.id AND ri.deleted = 0 " +
            "LEFT JOIN yhy_resource_category rc ON ri.category_id = rc.id AND rc.deleted = 0 " +
            "WHERE rm.id = #{id} AND rm.deleted = 0")
    ResourceMergedRespVO selectByIdWithDetails(@Param("id") Long id);

    /**
     * 查询复合工料机分页（JOIN 第四层数据）
     *
     * @param pageReqVO 分页查询条件
     * @return 复合工料机列表（包含第四层信息）
     */
    @Select("<script>" +
            "SELECT " +
            "  rm.id, rm.tenant_id, rm.merged_id, rm.source_id, rm.region_code, " +
            "  rm.quota_consumption, rm.tax_excl_market_price, " +
            "  rm.creator, rm.create_time, rm.updater, rm.update_time, " +
            "  ri.code, ri.name, ri.unit, ri.spec, " +
            "  ri.catalog_item_id, ri.category_id, ri.tax_rate, " +
            "  ri.tax_excl_base_price, ri.tax_incl_base_price, " +
            "  ri.tax_excl_compile_price, ri.tax_incl_compile_price, " +
            "  ri.calc_base, " +
            "  rc.name AS category_name, " +
            "  ROUND(COALESCE(ri.tax_excl_base_price, 0) * COALESCE(rm.quota_consumption, 0) / 100, 4) AS tax_excl_base_total, " +
            "  ROUND(COALESCE(ri.tax_incl_base_price, 0) * COALESCE(rm.quota_consumption, 0) / 100, 4) AS tax_incl_base_total, " +
            "  ROUND(COALESCE(ri.tax_excl_compile_price, 0) * COALESCE(rm.quota_consumption, 0) / 100, 4) AS tax_excl_compile_total, " +
            "  ROUND(COALESCE(ri.tax_incl_compile_price, 0) * COALESCE(rm.quota_consumption, 0) / 100, 4) AS tax_incl_compile_total " +
            "FROM yhy_resource_merged rm " +
            "LEFT JOIN yhy_resource_item ri ON rm.source_id = ri.id AND ri.deleted = 0 " +
            "LEFT JOIN yhy_resource_category rc ON ri.category_id = rc.id AND rc.deleted = 0 " +
            "WHERE rm.deleted = 0 " +
            "<if test='pageReqVO.mergedId != null'>" +
            "  AND rm.merged_id = #{pageReqVO.mergedId} " +
            "</if>" +
            "<if test='pageReqVO.sourceId != null'>" +
            "  AND rm.source_id = #{pageReqVO.sourceId} " +
            "</if>" +
            "<if test='pageReqVO.regionCode != null and pageReqVO.regionCode != \"\"'>" +
            "  AND rm.region_code = #{pageReqVO.regionCode} " +
            "</if>" +
            "ORDER BY rm.merged_id ASC, rm.source_id ASC, rm.region_code ASC " +
            "LIMIT #{pageReqVO.pageSize} OFFSET #{offset}" +
            "</script>")
    List<ResourceMergedRespVO> selectPageWithDetails(@Param("pageReqVO") ResourceMergedPageReqVO pageReqVO, 
                                                      @Param("offset") int offset);

    /**
     * 查询复合工料机总数
     *
     * @param pageReqVO 查询条件
     * @return 总数
     */
    default Long selectCountWithDetails(ResourceMergedPageReqVO pageReqVO) {
        return selectCount(new LambdaQueryWrapperX<ResourceMergedDO>()
                .eqIfPresent(ResourceMergedDO::getMergedId, pageReqVO.getMergedId())
                .eqIfPresent(ResourceMergedDO::getSourceId, pageReqVO.getSourceId())
                .eqIfPresent(ResourceMergedDO::getRegionCode, pageReqVO.getRegionCode()));
    }

    /**
     * 查询指定 merged_id 的所有第五层数据
     *
     * @param mergedId 复合工料机ID
     * @return 第五层数据列表
     */
    default List<ResourceMergedDO> selectByMergedId(Long mergedId) {
        return selectList(new LambdaQueryWrapperX<ResourceMergedDO>()
                .eq(ResourceMergedDO::getMergedId, mergedId));
    }
}
