<?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.iformall.mapper.WxFlowRecordMapper">

	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxFlowRecord">
		<id column="id" property="id" />
		<result column="business_id" property="businessId" />
		<result column="business_type" property="businessType" />
		<result column="user_id" property="userId" />
		<result column="remark" property="remark" />
		<result column="status" property="status" />
		<result column="task_id" property="taskId" />
		<result column="process_instance_id" property="processInstanceId" />
		<result column="create_date" property="createDate" />
		<result column="update_date" property="updateDate" />
		<result column="tenant_id"  property="tenantId" />
		<result column="user_name" property="userName" />
		<result column="task_key" property="taskKey" />
		<result column="task_name" property="taskName" />
		<result column="curr_status" property="currStatus" />
		<result column="variables" property="variables" />

		<result column="start_date" property="startDate" />
		<result column="contractType" property="contractType" />
		<result column="contractNumber" property="contractNumber" />
		<result column="endProperty" property="endProperty" />
	</resultMap>
	
	<sql id="allColumns">
	    `id`,`business_id`,`business_type`,`user_id`,`remark`,`status`,`task_id`,`process_instance_id`,`create_date`,`update_date`,`tenant_id`,`user_name`,task_key,task_name,curr_status,variables,
	    REPLACE (JSON_EXTRACT(variables,'$.contractType'),'"','') contractType,
		REPLACE (JSON_EXTRACT(variables,'$.endProperty'),'"','') endProperty,
		REPLACE (JSON_EXTRACT(variables,'$.contractNumber'),'"','') contractNumber,
		start_date
	</sql>

	<sql id="dynamicWhereConditions">
	        where 1 = 1
		<if test=" null != id ">
			and `id` = #{id}
		</if>
		<if test=" null != businessId ">
			and `business_id` = #{businessId}
		</if>
		<if test=" null != userId ">
			and `user_id` = #{userId}
		</if>
		<if test=" null != status ">
			and `status` = #{status}
		</if>
		<if test=" null != tenantId ">
			and `tenant_id`  = #{tenantId}
		</if>
		<if test=" null != ids ">
			and id in
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test=" null != approvalType ">
			and json_extract(variables,'$.approvalType')  = #{approvalType}
		</if>
		order by create_date desc
	</sql>
	
	<select id="findList" parameterType="com.iformall.domain.po.WxFlowRecord" resultMap="BaseResultMap">
		select <include refid="allColumns" /> from wx_flow_record
		<include refid="dynamicWhereConditions" />
	</select>

	<select id="myHandelList" parameterType="com.iformall.domain.po.WxFlowRecord" resultMap="BaseResultMap">
		select f.* from wx_flow_record f
		where f.user_id = #{userId} and f.status not in(1,4,5)
		order by create_date desc
	</select>

	<update id="updateCurrStatus" parameterType="com.iformall.domain.po.WxFlowRecord">
		update wx_flow_record set curr_status=#{currStatus}
		<where>
			and status = 1
			<if test=" null != processInstanceId ">and process_instance_id=#{processInstanceId}</if>
			<if test=" null != businessId ">and business_id = #{businessId}</if>
		</where>
	</update>

</mapper>
