<?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.TemplateMapper">
  <resultMap id="BaseResultMap" type="pwc.taxtech.atms.entitiy.Template">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="path" jdbcType="VARCHAR" property="path" />
    <result column="report_type" jdbcType="INTEGER" property="reportType" />
    <result column="template_group_id" jdbcType="BIGINT" property="templateGroupId" />
    <result column="order_index" jdbcType="INTEGER" property="orderIndex" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="is_system_type" jdbcType="INTEGER" property="isSystemType" />
    <result column="is_active_association" jdbcType="INTEGER" property="isActiveAssociation" />
    <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    id, name, code, path, report_type, template_group_id, order_index, create_time, update_time, 
    is_system_type, is_active_association, parent_id, create_by, update_by
  </sql>
  <select id="selectByExample" parameterType="pwc.taxtech.atms.entitiy.TemplateExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from template
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select 
    <include refid="Base_Column_List" />
    from template
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    delete from template
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="pwc.taxtech.atms.entitiy.TemplateExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    delete from template
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="pwc.taxtech.atms.entitiy.Template">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into template (id, name, code, 
      path, report_type, template_group_id, 
      order_index, create_time, update_time, 
      is_system_type, is_active_association, parent_id, 
      create_by, update_by)
    values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR}, 
      #{path,jdbcType=VARCHAR}, #{reportType,jdbcType=INTEGER}, #{templateGroupId,jdbcType=BIGINT}, 
      #{orderIndex,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{isSystemType,jdbcType=INTEGER}, #{isActiveAssociation,jdbcType=INTEGER}, #{parentId,jdbcType=VARCHAR}, 
      #{createBy,jdbcType=VARCHAR}, #{updateBy,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="pwc.taxtech.atms.entitiy.Template">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into template
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="code != null">
        code,
      </if>
      <if test="path != null">
        path,
      </if>
      <if test="reportType != null">
        report_type,
      </if>
      <if test="templateGroupId != null">
        template_group_id,
      </if>
      <if test="orderIndex != null">
        order_index,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="isSystemType != null">
        is_system_type,
      </if>
      <if test="isActiveAssociation != null">
        is_active_association,
      </if>
      <if test="parentId != null">
        parent_id,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="code != null">
        #{code,jdbcType=VARCHAR},
      </if>
      <if test="path != null">
        #{path,jdbcType=VARCHAR},
      </if>
      <if test="reportType != null">
        #{reportType,jdbcType=INTEGER},
      </if>
      <if test="templateGroupId != null">
        #{templateGroupId,jdbcType=BIGINT},
      </if>
      <if test="orderIndex != null">
        #{orderIndex,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="isSystemType != null">
        #{isSystemType,jdbcType=INTEGER},
      </if>
      <if test="isActiveAssociation != null">
        #{isActiveAssociation,jdbcType=INTEGER},
      </if>
      <if test="parentId != null">
        #{parentId,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="pwc.taxtech.atms.entitiy.TemplateExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select count(*) from template
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update template
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.name != null">
        name = #{record.name,jdbcType=VARCHAR},
      </if>
      <if test="record.code != null">
        code = #{record.code,jdbcType=VARCHAR},
      </if>
      <if test="record.path != null">
        path = #{record.path,jdbcType=VARCHAR},
      </if>
      <if test="record.reportType != null">
        report_type = #{record.reportType,jdbcType=INTEGER},
      </if>
      <if test="record.templateGroupId != null">
        template_group_id = #{record.templateGroupId,jdbcType=BIGINT},
      </if>
      <if test="record.orderIndex != null">
        order_index = #{record.orderIndex,jdbcType=INTEGER},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.isSystemType != null">
        is_system_type = #{record.isSystemType,jdbcType=INTEGER},
      </if>
      <if test="record.isActiveAssociation != null">
        is_active_association = #{record.isActiveAssociation,jdbcType=INTEGER},
      </if>
      <if test="record.parentId != null">
        parent_id = #{record.parentId,jdbcType=VARCHAR},
      </if>
      <if test="record.createBy != null">
        create_by = #{record.createBy,jdbcType=VARCHAR},
      </if>
      <if test="record.updateBy != null">
        update_by = #{record.updateBy,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update template
    set id = #{record.id,jdbcType=BIGINT},
      name = #{record.name,jdbcType=VARCHAR},
      code = #{record.code,jdbcType=VARCHAR},
      path = #{record.path,jdbcType=VARCHAR},
      report_type = #{record.reportType,jdbcType=INTEGER},
      template_group_id = #{record.templateGroupId,jdbcType=BIGINT},
      order_index = #{record.orderIndex,jdbcType=INTEGER},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      is_system_type = #{record.isSystemType,jdbcType=INTEGER},
      is_active_association = #{record.isActiveAssociation,jdbcType=INTEGER},
      parent_id = #{record.parentId,jdbcType=VARCHAR},
      create_by = #{record.createBy,jdbcType=VARCHAR},
      update_by = #{record.updateBy,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="pwc.taxtech.atms.entitiy.Template">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update template
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="code != null">
        code = #{code,jdbcType=VARCHAR},
      </if>
      <if test="path != null">
        path = #{path,jdbcType=VARCHAR},
      </if>
      <if test="reportType != null">
        report_type = #{reportType,jdbcType=INTEGER},
      </if>
      <if test="templateGroupId != null">
        template_group_id = #{templateGroupId,jdbcType=BIGINT},
      </if>
      <if test="orderIndex != null">
        order_index = #{orderIndex,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="isSystemType != null">
        is_system_type = #{isSystemType,jdbcType=INTEGER},
      </if>
      <if test="isActiveAssociation != null">
        is_active_association = #{isActiveAssociation,jdbcType=INTEGER},
      </if>
      <if test="parentId != null">
        parent_id = #{parentId,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null">
        create_by = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="pwc.taxtech.atms.entitiy.Template">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update template
    set name = #{name,jdbcType=VARCHAR},
      code = #{code,jdbcType=VARCHAR},
      path = #{path,jdbcType=VARCHAR},
      report_type = #{reportType,jdbcType=INTEGER},
      template_group_id = #{templateGroupId,jdbcType=BIGINT},
      order_index = #{orderIndex,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      is_system_type = #{isSystemType,jdbcType=INTEGER},
      is_active_association = #{isActiveAssociation,jdbcType=INTEGER},
      parent_id = #{parentId,jdbcType=VARCHAR},
      create_by = #{createBy,jdbcType=VARCHAR},
      update_by = #{updateBy,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="selectByExampleWithRowbounds" parameterType="pwc.taxtech.atms.entitiy.TemplateExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from template
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
    <resultMap id="TemplateUniqDto" type="pwc.taxtech.atms.dto.TemplateUniqDto">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="code" jdbcType="VARCHAR" property="code" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="order_index" jdbcType="INTEGER" property="orderIndex" />
        <result column="report_type" jdbcType="INTEGER" property="reportType" />
        <result column="template_group_id" jdbcType="VARCHAR" property="templateGroupId" />
        <result column="template_group_name" jdbcType="VARCHAR" property="templateGroupName" />
        <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
        <result column="parent_name" jdbcType="VARCHAR" property="parentName" />
        <result column="pay_tax_type" jdbcType="INTEGER" property="payTaxType" />
        <result column="industry_ids" jdbcType="VARCHAR" property="industryIds" />
        <result column="service_type_id" jdbcType="VARCHAR" property="serviceTypeId" />
        <result column="is_active_association" jdbcType="INTEGER" property="isActiveAssociation" />
    </resultMap>

    <select id="getTemplateUniqDtosByTemplateAndTemplateGroup" parameterType="map" resultMap="TemplateUniqDto">
        SELECT p.id,p.code,p.name,p.report_type,q.id as template_group_id,q.name as
        template_group_name,p.is_active_association
        FROM template p join template_group q on p.template_group_id=q.id
        <where>
            1=1
            <if test="serviceTypeId!=null">
                AND q.service_type_id=#{serviceTypeId,jdbcType=VARCHAR}
            </if>
            <if test="payTaxType!=null">
                AND q.pay_tax_type = #{payTaxType,jdbcType=INTEGER}
            </if>
            <if test="reportType!=null">
                AND p.report_type =#{reportType,jdbcType=INTEGER}
            </if>
            <if test="industryIds!=null">
                AND q.industry_ids =#{industryID,jdbcType=VARCHAR}
            </if>
        </where>
        ORDER BY p.template_group_id, p.order_index
    </select>
    <resultMap id="TemplateAndGroupDbDto" type="pwc.taxtech.atms.dto.TemplateAndGroupDbDto">
        <id column="id" jdbcType="VARCHAR" property="templateId" />
        <result column="template_group_id" jdbcType="VARCHAR" property="templateGroupId" />
        <result column="template_code" jdbcType="VARCHAR" property="templateCode" />
    </resultMap>
    <select id="getTemplateAndGroupByID" parameterType="java.lang.String" resultMap="TemplateAndGroupDbDto">
    SELECT p.id as id,q.id as template_group_id,p.code as template_code FROM template p JOIN template_group q on p.template_group_id=q.id WHERE p.id=#{id,jdbcType=VARCHAR} LIMIT 1
  </select>
    <select id="getCountOfTemplateAndGroupByTemplateName" parameterType="map" resultType="java.lang.Long">
        SELECT COUNT(1) FROM template p join template_group q on p.template_group_id=q.id
        <where>
            1=1
            <if test="templateGroupId!=null">
                AND q.id = #{templateGroupId,jdbcType=VARCHAR}
            </if>
            <if test="templateId!=null">
                AND p.id&lt;&gt;#{templateId,jdbcType=VARCHAR}
            </if>
            <if test="templateName!=null">
                AND p.Name= #{templateName,jdbcType=VARCHAR}
            </if>
        </where>
    </select>
    <resultMap id="reportDto" type="pwc.taxtech.atms.dto.vatdto.ReportDto">
        <id column="id" jdbcType="VARCHAR" property="id" />
        <result column="period" jdbcType="INTEGER" property="period" />
        <result column="template_id" jdbcType="INTEGER" property="templateId" />
        <result column="template_name" jdbcType="VARCHAR" property="templateName" />
        <result column="template_code" jdbcType="VARCHAR" property="templateCode" />
    </resultMap>

    <select id="getTemplateLeftJoinReport" parameterType="map" resultMap="reportDto">
    SELECT
    d.id     AS id,
    d.period AS period,
    c.id     AS template_id,
    c.name   AS template_name,
    c.code   AS template_code
    FROM (SELECT
    a.id,
    a.name,
    a.code
    FROM tax_admin.template a
    WHERE a.template_group_id = #{templateGroupID,jdbcType=VARCHAR}
    AND a.is_active_association = 1
    ORDER BY a.order_index) c
    LEFT JOIN (SELECT b.id,b.period,b.template_id
    FROM ${dbName}.report b
    WHERE b.period = #{period,jdbcType=INTEGER}
    AND b.project_id = #{projectID,jdbcType=VARCHAR}) d
    ON c.id = d.template_id
  </select>

    <select id="getIDsForExceptTemplate" resultType="java.lang.Long">
    SELECT
        a.id
    FROM
        template a
    JOIN template_group b ON a.template_group_id = b.id
    WHERE
        b.group_type = 2
    AND b.service_type_id = 2
  </select>
</mapper>