<?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="com.yonge.cooleshow.biz.dal.dao.SysUserContractRecordDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.SysUserContractRecord">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="user_type_" jdbcType="VARCHAR" property="userType"/>
        <result column="contract_id_" jdbcType="INTEGER" property="contractId"/>
        <result column="url_" jdbcType="VARCHAR" property="url"/>
        <result column="contract_no_" jdbcType="VARCHAR" property="contractNo"/>
        <result column="status_" jdbcType="VARCHAR" property="status"/>
        <result column="err_msg_" jdbcType="VARCHAR" property="errMsg"/>
        <result column="remark_" jdbcType="VARCHAR" property="remark"/>
        <result column="created_time_" jdbcType="TIMESTAMP" property="createdTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id_ as id
        , t.user_id_ as userId
        , t.user_type_ as userType
        , t.contract_id_ as contractId
        , t.url_ as url
        , t.contract_no_ as contractNo
        , t.status_ as status
        , t.err_msg_ as errMsg
        , t.remark_ as remark
        , t.created_time_ as createdTime
    </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.SysUserContractRecordVo">
        SELECT
        <include refid="baseColumns"/>
        FROM sys_user_contract_record t
        where t.id_ = #{id}
    </select>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.SysUserContractRecordVo">
        SELECT
            <include refid="baseColumns" />,
            tmp.name_ as templateName,
            tmp.type_ as templateType,
            tmp.version_ as version
        FROM sys_user_contract_record t
        LEFT JOIN contract_template tmp on t.contract_id_ = tmp.id_
        <where>
            <if test="param.userId != null">
                and t.user_id_ = #{param.userId}
            </if>
            <if test="param.userType != null">
                and t.user_type_ = #{param.userType}
            </if>
            <if test="param.status != null and param.status != ''">
                and t.status_ = #{param.status}
            </if>
        </where>
        order by t.created_time_ desc
    </select>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.yonge.cooleshow.biz.dal.entity.SysUserContractRecord">
        insert into sys_user_contract_record(user_id_, user_type_, contract_id_, url_, contract_no_,
        status_, err_msg_, remark_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.userId}, #{entity.userType}, #{entity.contractId}, #{entity.url},#{entity.contractNo},
            #{entity.status}, #{entity.errMsg}, #{entity.remark})
        </foreach>
    </insert>
    <update id="contractCallbackUpdate" parameterType="java.util.Map">
        UPDATE sys_user_contract_record
        set status_ = #{status}
          <if test="url != null">
              ,url_ = #{url}
          </if>
          <if test="errMsg != null">
              ,err_msg_ = #{errMsg}
          </if>
        WHERE contract_no_ = #{serialNo}
    </update>

    <select id="checkContractSign" resultType="java.lang.Boolean">
        select count(t.id_) from (
            select
                source_,type_,MAX(version_) as version_
            from contract_template
            where status_ = 1 and source_ = #{contractSource} and type_ = #{contractType}
            group by source_,type_
        ) a
        left join contract_template b on a.source_ = b.source_ and a.type_ = b.type_ and a.version_ = b.version_
        left join sys_user_contract_record t on b.id_ = t.contract_id_ and t.status_ in ('SUCCESS')
        where t.user_id_ = #{userId} and t.user_type_ = #{userType}
    </select>

    <select id="queryLatestContract" resultType="com.yonge.cooleshow.biz.dal.entity.SysUserContractRecord">
        select
            <include refid="baseColumns"/>
        from (
            select
                source_,type_,MAX(version_) as version_
            from contract_template
            where status_ = 1 and source_ = #{contractSource} and type_ = #{contractType}
            group by source_,type_
        ) a
        left join contract_template b on a.source_ = b.source_ and a.type_ = b.type_ and a.version_ = b.version_
        left join sys_user_contract_record t on b.id_ = t.contract_id_ and t.status_ in ('SUCCESS','DOING ')
        where t.user_id_ = #{userId} and t.user_type_ = #{userType}
    </select>

    <select id="checkContractSignV2" resultType="com.yonge.cooleshow.biz.dal.entity.SysUserContractRecord">
        select
            <include refid="baseColumns"/>
        from (
        select
        source_,type_,MAX(version_) as version_
        from contract_template
        where status_ = 1 and source_ = #{contractSource} and type_ = #{contractType}
        group by source_,type_
        ) a
        left join contract_template b on a.source_ = b.source_ and a.type_ = b.type_ and a.version_ = b.version_
        left join sys_user_contract_record t on b.id_ = t.contract_id_
        where t.user_id_ = #{userId} and t.user_type_ = #{userType}
        order by  t.id_ desc
        limit 1
    </select>
</mapper>
