<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the yifu4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yifu.cloud.plus.v1.yifu.archives.mapper.TFascTemplateDetailMapper">

  <resultMap id="tFascTemplateDetailMap" type="com.yifu.cloud.plus.v1.yifu.archives.entity.TFascTemplateDetail">
    <id property="id" column="id"/>
    <result property="signTemplateId" column="sign_template_id"/>
    <result property="fascField" column="fasc_field"/>
    <result property="fascFieldId" column="fasc_field_id"/>
    <result property="isMust" column="is_must"/>
    <result property="deleteFlag" column="delete_flag"/>
    <result property="hrField" column="hr_field"/>
    <result property="hrFieldId" column="hr_field_id"/>
    <result property="isEdit" column="is_edit"/>
    <result property="createBy" column="CREATE_BY"/>
    <result property="createName" column="CREATE_NAME"/>
    <result property="createTime" column="CREATE_TIME"/>
    <result property="updateBy" column="UPDATE_BY"/>
    <result property="updateTime" column="UPDATE_TIME"/>
    <result property="signTemplateName" column="sign_template_name"/>
    <result property="signTemplateStatus" column="sign_template_status"/>
    <result property="contractType" column="contract_type"/>
    <result property="fascDocId" column="fasc_doc_id"/>
    <result property="fieldType" column="field_type"/>
    <result property="fieldValue" column="field_value"/>
  </resultMap>

	<resultMap id="tFascTemplateDetailExportMap" type="com.yifu.cloud.plus.v1.yifu.archives.vo.TFascTemplateDetailExportVo">
		<result property="signTemplateName" column="sign_template_name"/>
		<result property="signTemplateId" column="sign_template_id"/>
		<result property="signTemplateStatus" column="sign_template_status"/>
		<result property="fascField" column="fasc_field"/>
		<result property="fascFieldId" column="fasc_field_id"/>
		<result property="isMust" column="is_must"/>
		<result property="deleteFlag" column="delete_flag"/>
		<result property="contractType" column="contract_type"/>
		<result property="hrField" column="hr_field"/>
		<result property="hrFieldId" column="hr_field_id"/>
		<result property="isEdit" column="is_edit"/>
	</resultMap>
    <sql id="Base_Column_List">
                a.id,
                a.sign_template_id,
                a.fasc_doc_id,
                a.fasc_field,
                a.fasc_field_id,
                a.is_must,
                a.delete_flag,
                a.hr_field,
                a.hr_field_id,
                a.is_edit,
                a.CREATE_BY,
                a.CREATE_NAME,
                a.CREATE_TIME,
                a.UPDATE_BY,
                a.UPDATE_TIME,
                b.sign_template_name,
                b.sign_template_status,
                b.contract_type
    </sql>
    <sql id="tFascTemplateDetail_where">
        <if test="tFascTemplateDetail != null">
                    <if test="tFascTemplateDetail.id != null and tFascTemplateDetail.id.trim() != ''">
                        AND a.id = #{tFascTemplateDetail.id}
                    </if>
                    <if test="tFascTemplateDetail.signTemplateId != null and tFascTemplateDetail.signTemplateId.trim() != ''">
                        AND a.sign_template_id = #{tFascTemplateDetail.signTemplateId}
                    </if>
                    <if test="tFascTemplateDetail.fascField != null and tFascTemplateDetail.fascField.trim() != ''">
                        AND a.fasc_field like concat('%', #{tFascTemplateDetail.fascField}, '%')
                    </if>
                    <if test="tFascTemplateDetail.fascFieldId != null and tFascTemplateDetail.fascFieldId.trim() != ''">
                        AND a.fasc_field_id = #{tFascTemplateDetail.fascFieldId}
                    </if>
                    <if test="tFascTemplateDetail.isMust != null and tFascTemplateDetail.isMust.trim() != ''">
                        AND a.is_must = #{tFascTemplateDetail.isMust}
                    </if>
                    <if test="tFascTemplateDetail.deleteFlag != null and tFascTemplateDetail.deleteFlag.trim() != ''">
                        AND a.delete_flag = #{tFascTemplateDetail.deleteFlag}
                    </if>
                    <if test="tFascTemplateDetail.hrField != null and tFascTemplateDetail.hrField.trim() != ''">
                        AND a.hr_field like concat('%', #{tFascTemplateDetail.hrField}, '%')
                    </if>
                    <if test="tFascTemplateDetail.hrFieldId != null and tFascTemplateDetail.hrFieldId.trim() != ''">
                        AND a.hr_field_id = #{tFascTemplateDetail.hrFieldId}
                    </if>
                    <if test="tFascTemplateDetail.isEdit != null and tFascTemplateDetail.isEdit.trim() != ''">
                        AND a.is_edit = #{tFascTemplateDetail.isEdit}
                    </if>
                    <if test="tFascTemplateDetail.createBy != null and tFascTemplateDetail.createBy.trim() != ''">
                        AND a.CREATE_BY = #{tFascTemplateDetail.createBy}
                    </if>
                    <if test="tFascTemplateDetail.createName != null and tFascTemplateDetail.createName.trim() != ''">
                        AND a.CREATE_NAME = #{tFascTemplateDetail.createName}
                    </if>
                    <if test="tFascTemplateDetail.createTime != null">
                        AND a.CREATE_TIME = #{tFascTemplateDetail.createTime}
                    </if>
                    <if test="tFascTemplateDetail.updateBy != null and tFascTemplateDetail.updateBy.trim() != ''">
                        AND a.UPDATE_BY = #{tFascTemplateDetail.updateBy}
                    </if>

                    /* 合同类型是否配置 空（空值），“是”（1），“否”（0） */
                    <if test="tFascTemplateDetail.haveContractType != null and tFascTemplateDetail.haveContractType.trim() != ''">
						<if test='tFascTemplateDetail.haveContractType == "1"'>
							AND b.contract_type is not null
						</if>
						<if test='tFascTemplateDetail.haveContractType == "0"'>
							AND b.contract_type is null
						</if>
                    </if>

                    <if test="tFascTemplateDetail.updateTime != null">
                        AND a.UPDATE_TIME = #{tFascTemplateDetail.updateTime}
                    </if>

			<if test="tFascTemplateDetail.signTemplateName != null and tFascTemplateDetail.signTemplateName.trim() != ''">
				AND b.sign_template_name like concat('%', #{tFascTemplateDetail.signTemplateName}, '%')
			</if>
			<if test="tFascTemplateDetail.signTemplateStatus != null and tFascTemplateDetail.signTemplateStatus.trim() != ''">
				AND b.sign_template_status = #{tFascTemplateDetail.signTemplateStatus}
			</if>
			<if test="tFascTemplateDetail.contractType != null and tFascTemplateDetail.contractType.trim() != ''">
				AND b.contract_type = #{tFascTemplateDetail.contractType}
			</if>
			<if test="tFascTemplateDetail.companyId != null and tFascTemplateDetail.companyId.trim() != ''">
				AND b.company_id = #{tFascTemplateDetail.companyId}
			</if>
        </if>
    </sql>
    <!--tFascTemplateDetail简单分页查询-->
    <select id="getTFascTemplateDetailPage" resultMap="tFascTemplateDetailMap">
        SELECT
        	<include refid="Base_Column_List"/>
        FROM t_fasc_template_detail a
        left join t_fasc_template b on a.sign_template_id = b.sign_template_id
        <where>
            b.delete_Flag = '0'
            <include refid="tFascTemplateDetail_where"/>
        </where>
		ORDER BY a.CREATE_TIME ASC
    </select>
    <!--推送时专用查询，含字段对应格式化-->
    <select id="getTDetailListByFieldType" resultMap="tFascTemplateDetailMap">
		SELECT
			a.id,
			a.sign_template_id,
			a.fasc_doc_id,
			a.fasc_field,
			a.fasc_field_id,
			a.is_must,
			a.delete_flag,
			a.hr_field,
			a.hr_field_id,
			a.is_edit,
			h.field_type,h.field_value
		FROM t_fasc_template_detail a
				 left join t_fasc_template b on a.sign_template_id = b.sign_template_id
				 left join t_fasc_hr_field h on h.hr_field_id = a.hr_field_id and h.hr_field = a.hr_field
		where b.delete_Flag = '0' and a.delete_flag = '0' and a.hr_field_id is not null and a.sign_template_id = #{fadadaTemplateId}
    </select>

    <!--tFascTemplateDetail简单分页查询-->
    <select id="getTFascTemplateDetailList" resultMap="tFascTemplateDetailMap">
        SELECT
        	<include refid="Base_Column_List"/>
        FROM t_fasc_template_detail a
		left join t_fasc_template b on a.sign_template_id = b.sign_template_id
        <where>
            1=1
            <include refid="tFascTemplateDetail_where"/>
        </where>
		ORDER BY a.CREATE_TIME ASC
    </select>

    <!--导出-->
    <select id="getTFascTemplateDetailExport" resultMap="tFascTemplateDetailExportMap">
        SELECT
			b.sign_template_name,
			a.sign_template_id,
			if(b.sign_template_status = 'valid', '启用', if(b.sign_template_status = 'invalid', '停用', '草稿')) sign_template_status,
			a.fasc_field,
			a.fasc_field_id,
			if(a.is_must='0','否','是') is_must,
			if(a.delete_flag='0','否','是') delete_flag,
			b.contract_type,
			a.hr_field,
			a.hr_field_id,
			if(a.is_edit='1','已配置','未配置') is_edit
        FROM t_fasc_template_detail a
		left join t_fasc_template b on a.sign_template_id = b.sign_template_id
        <where>
			b.delete_Flag = '0'
			<include refid="tFascTemplateDetail_where"/>
			<if test="idList != null and idList.size>0">
				AND a.id in
				<foreach item="idStr" index="index" collection="idList" open="(" separator="," close=")">
					#{idStr}
				</foreach>
			</if>
        </where>
		ORDER BY a.CREATE_TIME ASC
		<if test="tFascTemplateDetail != null">
			<if test="tFascTemplateDetail.limitStart != null">
				limit #{tFascTemplateDetail.limitStart},#{tFascTemplateDetail.limitEnd}
			</if>
		</if>
    </select>

    <select id="getTFascTemplateDetailExportCount" resultType="java.lang.Long">
        SELECT
			count(1)
        FROM t_fasc_template_detail a
		left join t_fasc_template b on a.sign_template_id = b.sign_template_id
        <where>
			b.delete_Flag = '0'
			<include refid="tFascTemplateDetail_where"/>
			<if test="idList != null and idList.size>0">
				AND a.id in
				<foreach item="idStr" index="index" collection="idList" open="(" separator="," close=")">
					#{idStr}
				</foreach>
			</if>
        </where>
    </select>

    <select id="getDetailByHaveHrField" resultMap="tFascTemplateDetailMap">
        SELECT
			a.id,a.hr_field
        FROM t_fasc_template_detail a
		where a.sign_template_id = #{templateId} and a.hr_field is not null and a.hr_field != ''
    </select>

	<!-- 修改模板的合同类型，重置皖信字段 -->
	<update id="initDetailHrField">
		update t_fasc_template_detail
		set hr_field = null,
			hr_field_id = null,
			is_edit = '0'
		where sign_template_id = #{templateId}
	</update>

</mapper>
