<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="pwc.taxtech.atms.dao.ProjectMapper">

    <select id="getTemlateWithServiceType" resultType="pwc.taxtech.atms.dpo.ProjectAnaylsisDto">
        select pp.id as reportId,p.id as projectId,p.organization_id as orgId,pst.service_type_id,t.name as
        templateName,
        pp.period as period
        from period_report pp
        left join project p on
        pp.project_id = p.id
        left join project_service_type pst
        on p.id = pst.project_id
        left join template t
        on pp.template_id = t.id
        where p.organization_id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        and p.year = #{year}
        and p.start_period &lt; #{month}
        and p.end_period &gt; #{month}
        and t.code = #{reportCode}
    </select>

    <select id="getTemlateWithServiceType2" resultType="pwc.taxtech.atms.dpo.ProjectAnaylsisDto">
    select pp.id as reportId,p.id as projectId,p.organization_id as orgId,pst.service_type_id,t.name as templateName
    from period_report pp
    left join project p on
    pp.project_id = p.id
    left join project_service_type pst
    on p.id = pst.project_id
    left join template t
    on pp.template_id = t.id
    where p.organization_id = #{orgId}
    and p.year = #{year}
    and p.start_period &lt; #{month}
    and p.end_period &gt; #{month}
    and t.code = #{code}
  </select>

    <select id="queryChildOrgAndProjectByOrgId" parameterType="map" resultMap="BaseResultMap">
        select p.*
        from project p
        left join organization o on p.organization_id=o.id
        where p.year = #{year,jdbcType=INTEGER}
        and o.parent_id = #{orgId,jdbcType=VARCHAR}
    </select>


    <select id="getTemplateCode"  resultType="pwc.taxtech.atms.entity.TemplateQueryDto">

        SELECT
        *
        FROM
        (SELECT
        t.code,
        t.name,
        p.`pay_tax_type`
        FROM
        template t
        LEFT JOIN template_group p
        ON p.id = t.template_group_id) m
        WHERE 1 = 1
        <if test="name != null and  name != '' ">
            AND m.name = #{name}
        </if>

        <if test="type != null and  type != '' ">
            AND m.pay_tax_type = #{type}
        </if>

    </select>

    <insert id="insertOnWhere" parameterType="pwc.taxtech.atms.entity.Project">
    insert into project (id, code, name,
    year, client_code, is_active,
    create_time, update_time, region_id,
    industry_id, rule_type, organization_id,
    db_name, enterprise_account_set_id, start_period,
    end_period)
    select #{id,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
    #{year,jdbcType=INTEGER}, #{clientCode,jdbcType=VARCHAR}, #{isActive,jdbcType=BIT},
    #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{regionId,jdbcType=VARCHAR},
    #{industryId,jdbcType=VARCHAR}, #{ruleType,jdbcType=INTEGER}, #{organizationId,jdbcType=VARCHAR},
    #{dbName,jdbcType=VARCHAR}, #{enterpriseAccountSetId,jdbcType=VARCHAR}, #{startPeriod,jdbcType=INTEGER},
    #{endPeriod,jdbcType=INTEGER}
    FROM dual
    where not exists(
      select
      *
      from
      project
      where
      organization_id = #{organizationId,jdbcType=VARCHAR}
      and
      year = #{year,jdbcType=INTEGER}
      and
      is_active = 1
    )

  </insert>


</mapper>