<?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.goafanti.common.dao.TOrderLegalAffairsMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TOrderLegalAffairs">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="attachment_url" jdbcType="VARCHAR" property="attachmentUrl" />
    <result column="operation_time" jdbcType="DATE" property="operationTime" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
  </resultMap>
  <sql id="Base_Column_List">
    id, order_no, `type`, `status`, remarks, attachment_url, operation_time, create_time, 
    aid
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_order_legal_affairs
    where id = #{id,jdbcType=INTEGER}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_order_legal_affairs
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.TOrderLegalAffairs" useGeneratedKeys="true">
    insert into t_order_legal_affairs (order_no, `type`, `status`, 
      remarks, attachment_url, operation_time, 
      create_time, aid)
    values (#{orderNo,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, 
      #{remarks,jdbcType=VARCHAR}, #{attachmentUrl,jdbcType=VARCHAR}, #{operationTime,jdbcType=DATE}, 
      #{createTime,jdbcType=TIMESTAMP}, #{aid,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.TOrderLegalAffairs" useGeneratedKeys="true">
    insert into t_order_legal_affairs
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="attachmentUrl != null">
        attachment_url,
      </if>
      <if test="operationTime != null">
        operation_time,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="aid != null">
        aid,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="attachmentUrl != null">
        #{attachmentUrl,jdbcType=VARCHAR},
      </if>
      <if test="operationTime != null">
        #{operationTime,jdbcType=DATE},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TOrderLegalAffairs">
    update t_order_legal_affairs
    <set>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=INTEGER},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="attachmentUrl != null">
        attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      </if>
      <if test="operationTime != null">
        operation_time = #{operationTime,jdbcType=DATE},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TOrderLegalAffairs">
    update t_order_legal_affairs
    set order_no = #{orderNo,jdbcType=VARCHAR},
      `type` = #{type,jdbcType=INTEGER},
      `status` = #{status,jdbcType=INTEGER},
      remarks = #{remarks,jdbcType=VARCHAR},
      attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      operation_time = #{operationTime,jdbcType=DATE},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      aid = #{aid,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="lagalAffairsLogList" resultType="com.goafanti.order.bo.OutLegalAffairsLogBo">
    select a.id,a.order_no orderNo,a.`type` ,a.status,a.remarks ,a.attachment_url attachmentUrl,
           date_format(a.operation_time ,'%Y-%m-%d')operationDate,date_format(a.create_time ,'%Y-%m-%d %H:%i:%S')createDate,
           b.name adminName,c.buyer_name userName
    from t_order_legal_affairs a left join admin b on a.aid=b.id left join t_order_mid c on a.order_no =c.order_no
    where a.order_no = #{orderNo} order by a.create_time desc
  </select>

  <insert id="insertBatch">
    INSERT INTO t_order_legal_affairs
      (order_no, `type`,  create_time, aid)
    values
    <foreach item="item" index="index" collection="list" separator=",">
      (#{item},3,now(),'1')
    </foreach>
  </insert>
</mapper>