package pwc.taxtech.atms.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import pwc.taxtech.atms.MyMapper;
import pwc.taxtech.atms.dto.GroupId;
import pwc.taxtech.atms.dto.vatdto.CellTemplatePerGroupDto;

import java.util.List;

@Mapper
public interface FormulaAdminMapper  extends MyMapper {

    @Select("SELECT  " +
            "    cell_template.ID AS cellTemplateID, " +
            "    template.ID AS reportTemplateID, " +
            "    template.template_group_id AS reportTemplateGroupID, " +
            "    cell_template_config.formula AS formula, " +
            "    cell_template.row_index AS rowIndex, " +
            "    cell_template.column_index AS columnIndex, " +
            "    template.Code AS reportCode, " +
            "    cell_template.column_name AS columnName, " +
            "    cell_template.row_name AS rowName, " +
            "    cell_template.data_type AS resultType " +
            "FROM " +
            "    template " +
            "        JOIN " +
            "    cell_template ON template.id = cell_template.report_template_id " +
            "        JOIN " +
            "    cell_template_config ON cell_template.id = cell_template_config.cell_template_id " +
            "WHERE " +
            "    cell_template_config.data_source_type = 1 " +
            "        AND template.template_group_id = #{groupId}")
    List<CellTemplatePerGroupDto> getCellTemplatePerGroupDto(@Param("groupId") Long groupId);

    
    @Select("SELECT  " +
            "    rule.template_group_id as groupId, rule.is_default as isDefault " +
            "FROM " +
            "    Project p, " +
            "    tax_payer_report_rule rule " +
            "WHERE " +
            "    (p.OrganizationID = rule.organization_id " +
            "        OR rule.is_default = 1) " +
            "        AND p.ID = #{projectId}")
    List<GroupId> getTemplateGroupId(@Param("projectId") String projectId);

    @Select("SELECT  " +
            "    p.DbName " +
            "FROM " +
            "    Project p " +
            "        JOIN " +
            "    ProjectServiceType ps ON p.id = ps.ProjectID " +
            "WHERE " +
            "    ps.ServiceTypeID IN ('2' , '3') " +
            "        AND p.Year = #{year} " +
            "        AND p.OrganizationID = #{orgId} " +
            "        AND p.IsActive = 1 " +
            "ORDER BY ps.ServiceTypeID " +
            "LIMIT 0 , 1")
    String getPastProjectDbName(@Param("year") int year, @Param("orgId") String organizationID);
}