<?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.unis.base.server.dao.DCfxxsjyUpdateLogDao">

    <resultMap id="DCfxxsjyUpdateLogResMap" type="com.unis.base.common.vo.DCfxxsjyUpdateLogVO">
        <result column="fid" jdbcType="BIGINT" property="fid"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="creator" jdbcType="VARCHAR" property="creator"/>
        <result column="creatorcode" jdbcType="VARCHAR" property="creatorCode"/>
        <result column="createdate" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="refresh_time" jdbcType="TIMESTAMP" property="refreshTime"/>
        <result column="refresher" jdbcType="VARCHAR" property="refresher"/>
        <result column="refresher_code" jdbcType="VARCHAR" property="refresherCode"/>
        <result column="tyshxydm" jdbcType="VARCHAR" property="tyshxydm"/>
        <result column="update_col" jdbcType="VARCHAR" property="updateCol"/>
        <result column="before_data" jdbcType="VARCHAR" property="beforeData"/>
        <result column="after_data" jdbcType="VARCHAR" property="afterData"/>
        <result column="data_ver" jdbcType="INTEGER" property="dataVer"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="qymc" jdbcType="VARCHAR" property="qymc"/>
        <result column="gfcfdm" jdbcType="VARCHAR" property="gfcfdm"/>
        <result column="gfcfmc" jdbcType="VARCHAR" property="gfcfmc"/>
        <result column="change_reason" jdbcType="VARCHAR" property="changeReason"/>
    </resultMap>

    <select id="listUpdateLog" resultMap="DCfxxsjyUpdateLogResMap">
        select
            t1.fid,
            t1.tyshxydm,
            t1.update_col,
            -- decode(t1.update_col, '仓房状态', t3.dict_name, '仓房用途', t4.dict_name, '油罐状态', t5.dict_name) before_data,
            -- decode(t1.update_col, '仓房状态', t6.dict_name, '仓房用途', t7.dict_name, '油罐状态', t8.dict_name) after_data,
            case when t1.update_col = '仓房状态' then t3.dict_name when t1.update_col = '仓房用途' then t4.dict_name when t1.update_col = '油罐状态' then t5.dict_name end before_data,
            case when t1.update_col = '仓房状态' then t6.dict_name when t1.update_col = '仓房用途' then t7.dict_name when t1.update_col = '油罐状态' then t8.dict_name end after_data,
            t2.qymc qymc,
            t1.gfcfdm,
            t1.gfcfmc,
            t1.change_reason,
            t1.data_ver,
            t1.refresh_time
        from d_cfxxsjy_update_log t1
        left join tz_slqy t2 on t2.tyshxydm = t1.tyshxydm and t2.status = 1
        left join usercenter.sys_dictionary t3 on t3.dict_code = t1.before_data and t3.group_id = 'CFZT'and t1.update_col = '仓房状态'
        left join usercenter.sys_dictionary t4 on t4.dict_code = t1.before_data and t4.group_id = 'CFYT'and t1.update_col = '仓房用途'
        left join usercenter.sys_dictionary t5 on t5.dict_code = t1.before_data and t5.group_id = 'YGZT'and t1.update_col = '油罐状态'
        left join usercenter.sys_dictionary t6 on t6.dict_code = t1.after_data and t6.group_id = 'CFZT'and t1.update_col = '仓房状态'
        left join usercenter.sys_dictionary t7 on t7.dict_code = t1.after_data and t7.group_id = 'CFYT'and t1.update_col = '仓房用途'
        left join usercenter.sys_dictionary t8 on t8.dict_code = t1.after_data and t8.group_id = 'YGZT'and t1.update_col = '油罐状态'
        <trim prefix="where" prefixOverrides="and|or">
            <if test="qymc != null and qymc != '' ">
                and instr(t2.qymc, #{qymc, jdbcType=VARCHAR}) > 0
            </if>
            <if test="gfcfmc != null and gfcfmc != '' ">
                and instr(t1.gfcfmc, #{gfcfmc, jdbcType=VARCHAR}) > 0
            </if>
            <if test="updateCol != null and updateCol != '' ">
                and t1.update_col = #{updateCol, jdbcType=VARCHAR}
            </if>
            <if test="gfcfdm != null and gfcfdm != '' ">
                and t1.gfcfdm = #{gfcfdm, jdbcType=VARCHAR}
            </if>
            <if test="tyshxydm != null and tyshxydm != '' ">
                and t1.tyshxydm = #{tyshxydm, jdbcType=VARCHAR}
            </if>
            and t1.status = 1
        </trim>
        order by t1.refresh_time desc
    </select>

    <select id="listCurrentVerData" resultMap="DCfxxsjyUpdateLogResMap">
        select
            t1.fid,
            t1.tyshxydm,
            t1.update_col,
            t1.before_data,
            t1.after_data,
            t1.gfcfdm,
            t1.gfcfmc,
            t1.change_reason,
            t1.refresh_time
        from d_cfxxsjy_update_log t1
        where t1.gfcfdm = #{gfcfdm, jdbcType=VARCHAR}
          and t1.data_ver = #{dataVer, jdbcType=INTEGER}
    </select>

    <update id="updateRefresher">
        update d_cfxxsjy_update_log set
            change_reason = #{changeReason, jdbcType=VARCHAR},
            refresher = #{refresher, jdbcType=VARCHAR},
            refresh_time = NOW()
        where gfcfdm = #{gfcfdm, jdbcType=VARCHAR}
            and data_ver = #{dataVer, jdbcType=INTEGER}
    </update>

    <update id="updateStatusByVer">
        update d_cfxxsjy_update_log set
            status = 1,
            after_data = #{newVal, jdbcType=VARCHAR}
        where update_col = #{updateCol, jdbcType=VARCHAR}
          and gfcfdm = #{gfcfdm, jdbcType=VARCHAR}
          and data_ver = #{dataVer, jdbcType=INTEGER}
    </update>

    <select id="getChangeReason" resultType="java.lang.String">
        select  change_reason from d_cfxxsjy_update_log
        where gfcfdm = #{gfcfdm, jdbcType=VARCHAR} and data_ver = #{dataVer, jdbcType=INTEGER} limit 0,1
    </select>

</mapper>
