<?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.cftech.accounts.dao.SysJobMapper">
  <resultMap id="resultMap" type="com.cftech.accounts.model.SysJob">
    <id column="id" property="id" />
    <result column="job_name" jdbcType="VARCHAR" property="jobName" />
    <result column="cron" jdbcType="VARCHAR" property="cron" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="clazz_path" jdbcType="VARCHAR" property="clazzPath" />
    <result column="job_desc" jdbcType="VARCHAR" property="jobDesc" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="create_by" property="createBy"/>
    <result column="update_by" property="updateBy"/>
    <result column="description" property="description"/>
    <result column="jobuid"  property="jobuid" />
    <result column="accounts_id" property="accountsId"/>
  </resultMap>


  <resultMap id="resultRamMap" type="com.cftech.accounts.model.RamJob">
    <id column="id" property="id" />
    <result column="job_json" property="jobJSON"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="create_by" property="createBy"/>
    <result column="update_by" property="updateBy"/>
    <result column="description" property="description"/>
    <result column="accounts_id" property="accountsId"/>
  </resultMap>

  <sql id="sqlWhere">
    <if test="conds!=null">
      <trim prefix="WHERE" prefixOverrides="AND|OR">
        <foreach collection="conds.conds" index="index" item="cond">
          ${cond.linkType}
          <if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
          <if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
          <if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
          <if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
          <if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
          <if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
          <if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND
            #{cond.endValue}
          </if>
          <if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
          <if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
          <if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
          <if test="cond.condType == 'IN'">${cond.param} IN
            <foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
              #{item}
            </foreach>
          </if>
        </foreach>
      </trim>
    </if>
  </sql>


  <sql id="sqlColumns">
    id, job_name, cron,accounts_id, status, create_time, update_time, description, del_flag, create_by, update_by, clazz_path, job_desc, jobuid
  </sql>

  <insert id="save" parameterType="com.cftech.accounts.model.SysJob" keyProperty="id" useGeneratedKeys="true">
    insert into sys_job
    (
   id, job_name, cron,accounts_id, status, create_time, update_time, description, del_flag, create_by, update_by, clazz_path, job_desc, jobuid
    )
    values
    (
    #{id, jdbcType=BIGINT},  #{jobName, jdbcType=VARCHAR},  #{cron, jdbcType=VARCHAR},  #{accountsId, jdbcType=BIGINT},  #{status, jdbcType=VARCHAR},
    now(),  now(),  #{description, jdbcType=VARCHAR},  #{delFlag, jdbcType=TINYINT},
    #{createBy, jdbcType=BIGINT},  #{updateBy, jdbcType=BIGINT},  #{clazzPath, jdbcType=VARCHAR},  #{jobDesc, jdbcType=VARCHAR},
    #{jobuid, jdbcType=VARCHAR}
    )
  </insert>

  <insert id="insertRamJob" parameterType="com.cftech.accounts.model.RamJob">
    Replace into sys_ram_job
    (
    id, accounts_id,  create_time, update_time, description, del_flag, create_by, update_by, job_json
    )
    values
    (
    1,   #{accountsId, jdbcType=BIGINT},
    now(),  now(),  #{description, jdbcType=VARCHAR},  0,
    #{createBy, jdbcType=BIGINT},  #{updateBy, jdbcType=BIGINT},
    #{jobJSON, jdbcType=VARCHAR}
    )
  </insert>


  <select id="getRamJob"  resultMap="resultRamMap">
    SELECT
     job_json,update_time
    FROM sys_ram_job
  </select>


  <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
    SELECT
    <include refid="sqlColumns"/>
    FROM sys_job t
    WHERE t.id=#{id}
  </select>

  <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
    SELECT COUNT(1) FROM sys_job
    <include refid="sqlWhere"/>
  </select>

  <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
    SELECT
    <include refid="sqlColumns"/>
    FROM sys_job t
    <include refid="sqlWhere"/>
    <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
    <if test="limit>0">limit #{offset},#{limit}</if>
  </select>

  <update id="update" parameterType="com.cftech.accounts.model.SysJob">
    update sys_job
    <set>
      <if test="id != null">
        id = #{id, jdbcType=BIGINT},
      </if>
      <if test="jobName != null">
        job_name = #{jobName, jdbcType=VARCHAR},
      </if>
      <if test="accountsId != null">
        accounts_id = #{accountsId, jdbcType=BIGINT},
      </if>
      <if test="status != null">
        status = #{status, jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime, jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time=now(),
      </if>
      <if test="description != null">
        description = #{description, jdbcType=VARCHAR},
      </if>
      <if test="delFlag != null">
        del_flag = #{delFlag, jdbcType=TINYINT},
      </if>
      <if test="createBy != null">
        create_by = #{createBy, jdbcType=BIGINT},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy, jdbcType=BIGINT},
      </if>
      <if test="clazzPath != null">
        clazz_path = #{clazzPath, jdbcType=VARCHAR},
      </if>
      <if test="jobDesc != null">
        job_desc = #{jobDesc, jdbcType=VARCHAR},
      </if>
      <if test="jobuid != null">
        jobuid = #{jobuid, jdbcType=VARCHAR},
      </if>
    </set>
    where id=#{id,jdbcType=BIGINT}
  </update>

  <update id="delete" parameterType="java.lang.Long">
    update sys_job set delflag=1 where id=#{id,jdbcType=BIGINT}
  </update>


  <update id="updateStatus" parameterType="com.cftech.accounts.model.SysJob">
    update sys_job set status = 0 where jobuid=#{jobuid,jdbcType=VARCHAR}
  </update>


  <update id="deleteTemplate" parameterType="java.lang.String">
    update wx_mp_meeting_template set del_flag=1 where uuid=#{uuid,jdbcType=VARCHAR}
  </update>


  <insert id="saveTemplate" parameterType="com.cftech.accounts.model.TemplateTransfer" useGeneratedKeys="true"
          keyProperty="id">
    insert into wx_mp_meeting_template
    (
 id,
                                                type,
                                                send_time,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                meeting_id,
                                                uuid,
                                                name,
                                                template_id,
                                                content,
                                                send_message,
                                                url
    )
    values
    (
    #{id, jdbcType=BIGINT},
    #{type, jdbcType=VARCHAR},
    #{sendTime},
    #{accountsId, jdbcType=BIGINT},
    #{delFlag, jdbcType=TINYINT},
    #{status, jdbcType=VARCHAR},
    now(),
    now(),
    #{description, jdbcType=VARCHAR},
    #{createBy, jdbcType=BIGINT},
    #{updateBy, jdbcType=BIGINT},
    #{meetingId,jdbcType=BIGINT},
    #{uuid,jdbcType=VARCHAR},
    #{name,jdbcType=VARCHAR},
    #{templateId,jdbcType=VARCHAR},
    #{content,jdbcType=CLOB},
    #{sendMessage,jdbcType=VARCHAR},
    #{url}
    )
  </insert>

</mapper>