<?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.pcloud.book.personal.dao.impl.PcloudSkillRecordDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.personal.entity.PcloudSkillRecord" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="skill_id" property="skillId" jdbcType="INTEGER" />
    <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR" />
    <result column="robot_wx_id" property="robotWxId" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="state" property="state" jdbcType="TINYINT" />
    <result column="skill_type" property="skillType" jdbcType="TINYINT" />
    <result column="skill_is_delete" property="skillIsDelete" jdbcType="TINYINT" />
  </resultMap>
  <resultMap id="SkillDetailMap" type="com.pcloud.book.keywords.entity.RobotSkillDetail">
    <id column="id" property="id" jdbcType="BIGINT"/>
   <association property="resourceCount" column="id" select="com.pcloud.book.skill.dao.impl.PcloudSubReplyDaoImpl.getResourceCountBySkillId"/>
  </resultMap>
  <sql id="Base_Column_List" >
    id, skill_id, wx_user_id, robot_wx_id, create_time, update_time, state, skill_type,skill_is_delete
  </sql>
  <sql id="Base_Column_List_psr" >
    psr.id,  psr.skill_id,  psr.wx_user_id,  psr.robot_wx_id,  psr.create_time,  psr.update_time,  psr.state,  psr.skill_type, psr.skill_is_delete
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from pcloud_skill_record
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from pcloud_skill_record
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.pcloud.book.personal.entity.PcloudSkillRecord" >
    insert into pcloud_skill_record (id, skill_id, wx_user_id, 
      robot_wx_id, create_time, update_time, 
      state, skill_type)
    values (#{id,jdbcType=INTEGER}, #{skillId,jdbcType=INTEGER}, #{wxUserId,jdbcType=VARCHAR}, 
      #{robotWxId,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{state,jdbcType=TINYINT}, #{skillType,jdbcType=TINYINT})
  </insert>
  <insert id="insertSelective" parameterType="com.pcloud.book.personal.entity.PcloudSkillRecord" >
    insert into pcloud_skill_record
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="skillId != null" >
        skill_id,
      </if>
      <if test="wxUserId != null" >
        wx_user_id,
      </if>
      <if test="robotWxId != null" >
        robot_wx_id,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
      <if test="state != null" >
        state,
      </if>
      <if test="skillType != null" >
        skill_type,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="skillId != null" >
        #{skillId,jdbcType=INTEGER},
      </if>
      <if test="wxUserId != null" >
        #{wxUserId,jdbcType=VARCHAR},
      </if>
      <if test="robotWxId != null" >
        #{robotWxId,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="state != null" >
        #{state,jdbcType=TINYINT},
      </if>
      <if test="skillType != null" >
        #{skillType,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.personal.entity.PcloudSkillRecord" >
    update pcloud_skill_record
    <set >
      <if test="skillId != null" >
        skill_id = #{skillId,jdbcType=INTEGER},
      </if>
      <if test="wxUserId != null" >
        wx_user_id = #{wxUserId,jdbcType=VARCHAR},
      </if>
      <if test="robotWxId != null" >
        robot_wx_id = #{robotWxId,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="state != null" >
        state = #{state,jdbcType=TINYINT},
      </if>
      <if test="skillType != null" >
        skill_type = #{skillType,jdbcType=TINYINT},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.personal.entity.PcloudSkillRecord" >
    update pcloud_skill_record
    set skill_id = #{skillId,jdbcType=INTEGER},
      wx_user_id = #{wxUserId,jdbcType=VARCHAR},
      robot_wx_id = #{robotWxId,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      state = #{state,jdbcType=TINYINT},
      skill_type = #{skillType,jdbcType=TINYINT}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="getBySkillIdAndWxId" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List_psr"/>
    from
    pcloud_skill_record psr
    left join pcloud_skill ps
    on psr.skill_id = ps.id
    where
    skill_is_delete  = 0
    and  ps.id = #{skillId}
    and  psr.wx_user_id = #{wxUserId}
  </select>

  <update id="updateState4ResourceAndActivity" parameterType="map">
    update
    pcloud_skill_record
    set
    state = #{state}
    where
    skill_id = #{skillId}
    and  wx_user_id = #{wxUserId}
    and  skill_is_delete  = 0
  </update>

  <update id="batchUpdateState4ResourceAndActivity" parameterType="map">
    update
    pcloud_skill_record
    set
    state = #{state}
    where
    skill_id in
    <foreach collection="skillIds" item="item" index="index" open="(" close=")" separator=",">
      #{item}
    </foreach>
    and  wx_user_id = #{wxUserId}
    and  skill_is_delete  = 0
  </update>


  <select id="getSKillRecordPage4Activity" parameterType="map" resultType="com.pcloud.book.keywords.entity.RobotSkillDetail">
    select
    ps.id id,
    ps.rel_skill_id relSkillId,
    ps.skill_type type,
    pga.name relSkillName,
    pga.description relSkillIntroduce,
    psr.wx_user_id wechatUserId,
    psr.robot_wx_id robotId,
    psr.state state
    from
    pcloud_skill_record psr
    left join pcloud_skill ps
    on psr.skill_id = ps.id
    left join  pcloud_group_activity pga
    on ps.rel_skill_id = pga.id
    where
    psr.wx_user_id = #{wxUserId}
    and
    psr.skill_type = #{skillType}
    order by psr.update_time desc
  </select>

  <select id="getSKillRecordPage4Resource" parameterType="map" resultMap="SkillDetailMap">
    select
    ps.id id,
    ps.rel_skill_id relSkillId,
    ps.skill_type type,
    pr.name relSkillName,
    pr.description relSkillIntroduce,
    psr.wx_user_id wechatUserId,
    psr.robot_wx_id robotId,
    psr.state state
    from
    pcloud_skill_record psr
    left join pcloud_skill ps
    on psr.skill_id = ps.id
    left join  pcloud_resource pr
    on ps.rel_skill_id = pr.id
    where
    psr.wx_user_id = #{wxUserId}
    and
    psr.skill_type = #{skillType}
    order by psr.update_time desc
  </select>

  <select id="getSKillRecordPage4Task" parameterType="map"  resultType="com.pcloud.book.keywords.entity.RobotSkillDetail">
    select
    ps.id id,
    ps.rel_skill_id relSkillId,
    ps.skill_type type,
    tct.task_name relSkillName,
    tct.task_introduce relSkillIntroduce,
    psr.wx_user_id wechatUserId,
    psr.robot_wx_id robotId,
    psr.state state
    from
    pcloud_skill_record psr
    left join pcloud_skill ps
    on psr.skill_id = ps.id
    left join  time_control_task tct
    on ps.rel_skill_id = tct.task_id
    where
    psr.wx_user_id = #{wxUserId}
    and
    psr.skill_type = #{skillType}
    order by psr.update_time desc
  </select>
</mapper>