package pwc.taxtech.atms.dao;

import java.util.Dictionary;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;
import pwc.taxtech.atms.MyMapper;
import pwc.taxtech.atms.dto.taxadmin.ProjectDisplayDto;
import pwc.taxtech.atms.entitiy.Project;
import pwc.taxtech.atms.entitiy.ProjectExample;
import pwc.taxtech.atms.entitiy.ProjectStatusManage;

@Mapper
public interface ProjectMapper extends MyMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    long countByExample(ProjectExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    int deleteByExample(ProjectExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    int deleteByPrimaryKey(String ID);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    int insert(Project record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    int insertSelective(Project record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    List<Project> selectByExampleWithRowbounds(ProjectExample example, RowBounds rowBounds);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    List<Project> selectByExample(ProjectExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    Project selectByPrimaryKey(String ID);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    int updateByExampleSelective(@Param("record") Project record, @Param("example") ProjectExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    int updateByExample(@Param("record") Project record, @Param("example") ProjectExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    int updateByPrimaryKeySelective(Project record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table Project
     *
     * @mbg.generated
     */
    int updateByPrimaryKey(Project record);

    @Select("<script>" +
            "SELECT  " +
            "    p.CreateTime AS CreateTime, " +
            "    p.IsActive  AS IsActive, " +
            "    p.OrganizationID AS OrganizationID, " +
            "    p.Name AS Name, " +
            "    p.Code AS Code, " +
            "    p.ID AS ID, " +
            "    p.IndustryID AS IndustryID, " +
            "    p.Year AS Year, " +
            "    p.RegionID AS RegionID, " +
            "    region.ShortName AS RegionName, " +
            "    p.RuleType AS RuleType, " +
            "    projectServiceType.ServiceTypeID AS ServiceTypeID, " +
            "    p.UpdateTime AS UpdateTime, " +
            "    serviceType.Name AS ServiceTypeName, " +
            "    organization.Name AS OrganizationName, " +
            "    industry.Name AS IndustryName, " +
            "    projectServiceType.TemplateGroupID AS TemplateGroupID, " +
            "    templateGroup.Name AS TemplateGroupName, " +
            "    p.ClientCode AS ClientCode, " +
            "    p.DbName AS DbName, " +
            "    TRUE AS HaveCreateProject, " +
            "    p.EnterpriseAccountSetID AS EnterpriseAccountSetID, " +
            "    p.StartPeriod AS StartPeriod, " +
            "    p.EndPeriod AS EndPeriod " +
            "FROM " +
            "    Project p " +
            "        JOIN " +
            "    Organization org ON p.OrganizationID = org.ID " +
            "        JOIN " +
            "    ProjectServiceType projectServiceType ON p.ID = projectServiceType.ProjectID " +
            "        JOIN " +
            "    Organization organization ON p.OrganizationID = organization.ID " +
            "        JOIN " +
            "    ServiceType serviceType ON projectServiceType.ServiceTypeID = serviceType.ID " +
            "        JOIN " +
            "    Industry industry ON p.IndustryID = industry.ID " +
            "        JOIN " +
            "    TemplateGroup templateGroup ON projectServiceType.TemplateGroupID = templateGroup.ID " +
            "        JOIN " +
            "    Region region ON p.RegionID = region.ID " +
            "        JOIN " +
            "    EnterpriseAccountSetOrg enterOrg ON p.EnterpriseAccountSetID = enterOrg.EnterpriseAccountSetID " +
            "   <where>" +
            "       p.IsActive=1 and serviceType.isActive=1 and p.OrganizationID = enterOrg.OrganizationID" +
            "       <if test=\"orgID != null and orgID !='' \">AND org.ID=#{orgID}</if>" +
            "       <if test=\"serverID != null and serverID !='' \">AND p.serviceType=#{serverID}</if>" +
            "       <if test=\"projectYear != null\">AND p.Year=#{projectYear}</if>" +
            "   </where>" +
            "</script>")
    List<ProjectDisplayDto> getProjectList(@Param("orgID") String orgID, @Param("serverID") String serviceID,
                                           @Param("projectYear") Integer projectYear);

    @Select("SELECT  " +
            "    p.PeriodId, p.Status " +
            "FROM " +
            "    ProjectStatusManage p " +
            "WHERE " +
            "    p.DbName = #{dbName} " +
            "ORDER BY PeriodId , Status")
    List<ProjectStatusManage> getProjectSatusListByDbName(String dbName);

    @Select("<script>" +
            "SELECT  " +
            "    now() AS CreateTime, " +
            "    TRUE AS IsActive, " +
            "    org.ID AS OrganizationID, " +
            "    org.Name AS Name, " +
            "    org.Code AS Code, " +
            "    UUID() AS ID, " +
            "    org.IndustryID AS IndustryID, " +
            "    0 AS Year, " +
            "    org.RegionID AS RegionID, " +
            "    0 AS RuleType, " +
            "    st.ID AS ServiceTypeID, " +
            "    reg.ShortName AS RegionName, " +
            "    now() AS UpdateTime, " +
            "    st.Name AS ServiceTypeName, " +
            "    org.Name AS OrganizationName, " +
            "    ind.Name AS IndustryName, " +
            "    ostg.TemplateGroupID AS TemplateGroupID, " +
            "    '' AS TemplateGroupName, " +
            "    org.ClientCode AS ClientCode, " +
            "    '' AS DbName, " +
            "    FALSE AS HaveCreateProject, " +
            "    ea.EnterpriseAccountSetID AS EnterpriseAccountSetID, " +
            "    1 AS StartPeriod, " +
            "    12 AS EndPeriod, " +
            "    ea.EffectiveDate AS EffectiveDate, " +
            "    ea.ExpiredDate AS ExpiredDate " +
            "FROM " +
            "    Organization org " +
            "        JOIN " +
            "    EnterpriseAccountSetOrg ea ON org.ID = ea.OrganizationID " +
            "        JOIN " +
            "    OrganizationServiceTemplateGroup ostg ON org.ID = ostg.OrganizationID " +
            "        JOIN " +
            "    ServiceType st ON ostg.ServiceTypeID = st.ID " +
            "        JOIN " +
            "    Industry ind ON org.IndustryID = ind.ID " +
            "        JOIN " +
            "    Region reg ON org.RegionID = reg.ID " +
            "WHERE " +
            "  1=1" +
            "       <if test=\"orgID != null and orgID !='' \">AND org.ID=#{orgID}</if>" +
            "        AND org.IsActive = 1 " +
            "        AND st.IsActive = 1 " +
            "       <if test=\"serviceID != null and serviceID !='' \">AND ostg.ServiceTypeID=#{serviceID}</if>" +
            "ORDER BY ea.EffectiveDate,org.Code" +
            "</script>")
    List<ProjectDisplayDto> getProjectFromEnterpriseAccountSetOrg(@Param("orgID") String orgID, @Param("serviceID") String serviceID);

    @Select("SELECT  " +
            "    PeriodId,Status " +
            "FROM " +
            "    ProjectStatusManage p " +
            "WHERE " +
            "    p.DbName = #{dbName} " +
            "ORDER BY PeriodId , Status")
    List<ProjectStatusManage> selectProjectAllStatus(String dbName);
}