<?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.lightinit.hsdataportal.mapper.RequirementMicroappMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdataportal.entity.RequirementMicroapp">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="category_id" jdbcType="BIGINT" property="categoryId" />
    <result column="money" jdbcType="DECIMAL" property="money" />
    <result column="deadline" jdbcType="TIMESTAMP" property="deadline" />
    <result column="contact" jdbcType="VARCHAR" property="contact" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="created_by" jdbcType="BIGINT" property="createdBy" />
    <result column="deleted_at" jdbcType="TIMESTAMP" property="deletedAt" />
    <result column="deleted_by" jdbcType="BIGINT" property="deletedBy" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, category_id, money, deadline, contact, phone, email, created_at, created_by, 
    deleted_at, deleted_by
  </sql>
  <select id="selectByExample" parameterType="com.lightinit.hsdataportal.entity.RequirementMicroappExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from requirement_microapp
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from requirement_microapp
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from requirement_microapp
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdataportal.entity.RequirementMicroappExample">
    delete from requirement_microapp
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdataportal.entity.RequirementMicroapp">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into requirement_microapp (id,category_id, money, deadline,
      contact, phone, email, 
      created_at, created_by, deleted_at, 
      deleted_by)
    values (#{id,jdbcType=BIGINT},#{categoryId,jdbcType=BIGINT}, #{money,jdbcType=DECIMAL}, #{deadline,jdbcType=TIMESTAMP},
      #{contact,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, 
      #{createdAt,jdbcType=TIMESTAMP}, #{createdBy,jdbcType=BIGINT}, #{deletedAt,jdbcType=TIMESTAMP}, 
      #{deletedBy,jdbcType=BIGINT})
  </insert>
  <insert id="insertSelective" parameterType="com.lightinit.hsdataportal.entity.RequirementMicroapp">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into requirement_microapp
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="categoryId != null">
        category_id,
      </if>
      <if test="money != null">
        money,
      </if>
      <if test="deadline != null">
        deadline,
      </if>
      <if test="contact != null">
        contact,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
      <if test="createdBy != null">
        created_by,
      </if>
      <if test="deletedAt != null">
        deleted_at,
      </if>
      <if test="deletedBy != null">
        deleted_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="categoryId != null">
        #{categoryId,jdbcType=BIGINT},
      </if>
      <if test="money != null">
        #{money,jdbcType=DECIMAL},
      </if>
      <if test="deadline != null">
        #{deadline,jdbcType=TIMESTAMP},
      </if>
      <if test="contact != null">
        #{contact,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        #{deletedBy,jdbcType=BIGINT},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.lightinit.hsdataportal.entity.RequirementMicroappExample" resultType="java.lang.Long">
    select count(*) from requirement_microapp
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update requirement_microapp
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.categoryId != null">
        category_id = #{record.categoryId,jdbcType=BIGINT},
      </if>
      <if test="record.money != null">
        money = #{record.money,jdbcType=DECIMAL},
      </if>
      <if test="record.deadline != null">
        deadline = #{record.deadline,jdbcType=TIMESTAMP},
      </if>
      <if test="record.contact != null">
        contact = #{record.contact,jdbcType=VARCHAR},
      </if>
      <if test="record.phone != null">
        phone = #{record.phone,jdbcType=VARCHAR},
      </if>
      <if test="record.email != null">
        email = #{record.email,jdbcType=VARCHAR},
      </if>
      <if test="record.createdAt != null">
        created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.createdBy != null">
        created_by = #{record.createdBy,jdbcType=BIGINT},
      </if>
      <if test="record.deletedAt != null">
        deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.deletedBy != null">
        deleted_by = #{record.deletedBy,jdbcType=BIGINT},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update requirement_microapp
    set id = #{record.id,jdbcType=BIGINT},
      category_id = #{record.categoryId,jdbcType=BIGINT},
      money = #{record.money,jdbcType=DECIMAL},
      deadline = #{record.deadline,jdbcType=TIMESTAMP},
      contact = #{record.contact,jdbcType=VARCHAR},
      phone = #{record.phone,jdbcType=VARCHAR},
      email = #{record.email,jdbcType=VARCHAR},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      created_by = #{record.createdBy,jdbcType=BIGINT},
      deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{record.deletedBy,jdbcType=BIGINT}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.lightinit.hsdataportal.entity.RequirementMicroapp">
    update requirement_microapp
    <set>
      <if test="categoryId != null">
        category_id = #{categoryId,jdbcType=BIGINT},
      </if>
      <if test="money != null">
        money = #{money,jdbcType=DECIMAL},
      </if>
      <if test="deadline != null">
        deadline = #{deadline,jdbcType=TIMESTAMP},
      </if>
      <if test="contact != null">
        contact = #{contact,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        created_by = #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        deleted_by = #{deletedBy,jdbcType=BIGINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lightinit.hsdataportal.entity.RequirementMicroapp">
    update requirement_microapp
    set category_id = #{categoryId,jdbcType=BIGINT},
      money = #{money,jdbcType=DECIMAL},
      deadline = #{deadline,jdbcType=TIMESTAMP},
      contact = #{contact,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      created_by = #{createdBy,jdbcType=BIGINT},
      deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{deletedBy,jdbcType=BIGINT}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="selectOpenList" parameterType="com.lightinit.hsdataportal.model.RequirementMicroappSearchModel" resultType="com.lightinit.hsdataportal.model.RequirementMicroappListModel">
    <if test="rqrmntName != null and rqrmntName.length() &gt; 0">
      <bind name="rqrmntName" value="'%' + _parameter.rqrmntName + '%'" />
    </if>
    SELECT r.`rqrmnt_name`,r.`rqrmnt_summary`,r.`publish_at`,r.`view_times`,r.id rqrmntId,r.rqrmnt_state,rm.deadline,
    (select count(*) from requirement_follow rf where rf.rqrmnt_id = r.id) coun
    FROM `requirement` r
    JOIN  `requirement_microapp` rm
    ON r.`id` = rm.`id`
    <where>
      <if test="rqrmntState != null and rqrmntState.length()>0">
         r.rqrmnt_state = #{rqrmntState}
      </if>
      <if test="rqrmntName != null and rqrmntName.length() &gt; 0">
        AND r.rqrmnt_name like #{rqrmntName}
      </if>
      <if test="flag !=null">
        <choose>
          <when test="flag==0">
            ORDER BY r.publish_at DESC
          </when>
          <when test="flag==1">
            ORDER BY r.view_times DESC
          </when>
          <when test="flag==2">
            ORDER BY coun DESC
          </when>
        </choose>
      </if>
    </where>
    <if test="page != null">
      LIMIT #{page.beginIndex},#{page.pageSize}
    </if>
  </select>
  <select id="selectOpenCount" parameterType="com.lightinit.hsdataportal.model.RequirementMicroappSearchModel" resultType="java.lang.Long">
    <if test="rqrmntName != null and rqrmntName.length() &gt; 0">
      <bind name="rqrmntName" value="'%' + _parameter.rqrmntName + '%'" />
    </if>
    SELECT count(*) FROM `requirement` r
    JOIN  `requirement_microapp` rm
    ON r.`id` = rm.`id`
    <where>
      <if test="rqrmntState != null and rqrmntState.length()>0">
         r.rqrmnt_state = #{rqrmntState}
      </if>
      <if test="rqrmntName != null and rqrmntName.length() &gt; 0">
        AND r.rqrmnt_name like #{rqrmntName}
      </if>
    </where>
  </select>
  <select id="queryView" parameterType="java.lang.Long" resultType="com.lightinit.hsdataportal.model.RequirementMicroappViewModel">
     SELECT r.`rqrmnt_name`,r.`rqrmnt_summary`,r.`publish_at`,r.`view_times`,rm.email,rm.money,
     r.id rqrmntId,rm.deadline,r.rqrmnt_state ,cc.`catelog_name` themeName,
     (select count(*) from requirement_follow rf where rf.rqrmnt_id = r.id) coun,rf.id followId ,rr.id receiveId
    FROM `requirement` r
    JOIN  `requirement_microapp` rm
    ON r.`id` = rm.`id`
   LEFT JOIN resource_catelog cc ON cc.`id` = rm.category_id
      LEFT JOIN `requirement_receive` rr ON  r.id = rr.`rqrmnt_id`
   LEFT JOIN `requirement_follow` rf ON r.id = rf.`rqrmnt_id`


    where r.id = #{id}
  </select>
  <select id="selectReceiveList" parameterType="java.lang.Long" resultType="com.lightinit.hsdataportal.model.APIReceiveListModel">
      SELECT
       u.`username`, IFNULL(rr.`phone`,u.`phone`) AS `phone`,
       rr.`created_at`,rr.solution
       FROM
        `requirement` r
       JOIN `requirement_microapp` rm ON r.id = rm.`id`
       JOIN `requirement_receive` rr ON r.id = rr.`rqrmnt_id`
       JOIN USER u ON rr.`received_by` = u.id
    <where>
      r.id =#{id}
    </where>
  </select>
  <select id="selectCatelog" resultType="com.lightinit.hsdataportal.entity.ResourceCatelog">
    SELECT c.id,c.catelog_name FROM `resource_catelog`  c WHERE c.`catelog_type` ="主题分类" AND c.`parent_id` IS NULL
  </select>
</mapper>