<?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.common.grantitems.dao.GrantitemsMapper">

    <resultMap id="resultMap" type="com.cftech.common.grantitems.model.Grantitems">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="appform" property="appform"/>
        <result column="appitem" property="appitem"/>
        <result column="itemname" property="itemname"/>
        <result column="price" property="price"/>
        <result column="app_count" property="appCount"/>
        <result column="grant_count" property="grantCount"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="grant_time" property="grantTime"/>
        <result column="reality_grant_count" property="realityGrantCount"/>
        <result column="cover" property="cover"/>
        <result column="specifications" property="specifications"/>
        <result column="dis_grant_count" property="disGrantCount"/>
        <result column="manager_grant_count" property="managerGrantCount"/>
        <result column="manager_grant_time" property="managerGrantTime"/>
        <result column="dis_grant_time" property="disGrantTime"/>
        <result column="manager_after_stock" property="managerAfterStock"/>
        <result column="dis_after_stock" property="disAfterStock"/>
        <result column="delivery_time" property="deliveryTime"/>
    </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,
                                                number,
                                                appform,
                                                appitem,
                                                itemname,
                                                price,
                                                app_count,
                                                grant_count,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                grant_time,reality_grant_count,cover,specifications,dis_grant_count,manager_grant_count,
                                                manager_grant_time,dis_grant_time,manager_after_stock,dis_after_stock,delivery_time
                        </sql>


    <insert id="save" parameterType="com.cftech.common.grantitems.model.Grantitems" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_common_grantitems
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{appform, jdbcType=BIGINT},
        #{appitem, jdbcType=BIGINT},
        #{itemname, jdbcType=VARCHAR},
        #{price, jdbcType=VARCHAR},
        #{appCount, jdbcType=INTEGER},
        #{grantCount, jdbcType=INTEGER},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{grantTime, jdbcType=TIMESTAMP},
        #{realityGrantCount, jdbcType=INTEGER},
        #{cover, jdbcType=VARCHAR},
        #{specifications, jdbcType=VARCHAR},
        #{disGrantCount,jdbcType=INTEGER},
        #{managerGrantCount,jdbcType=INTEGER},
        #{managerGrantTime, jdbcType=TIMESTAMP},
        #{disGrantTime, jdbcType=TIMESTAMP},
        #{managerAfterStock, jdbcType=INTEGER},
        #{disAfterStock, jdbcType=INTEGER},
        #{deliveryTime}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_mp_common_grantitems cg
        left join wx_mp_common_appitems ca on cg.appitem =ca.id
        left join wx_mp_common_appform cap on cap.id = cg.appform
        left join wx_mp_common_department cd on cap.department = cd.id
        left join wx_mp_common_typeform ct on ct.id = cap.typeform
        <include refid="sqlWhere"/>
    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        cg.itemname itemname,
        cg.price,
        cg.id,
        cg.app_count appCount,
        cg.specifications,
        ca.id appitem,
        cg.cover cover,
        ca.authentic_stock authenticStock,
        cg.grant_count grantCount,
        cg.dis_grant_count disGrantCount,
        cg.manager_grant_count managerGrantCount,
        cg.reality_grant_count realityGrantCount,
        cg.manager_after_stock managerAfterStock,
        cg.dis_after_stock disAfterStock,
        cd.name departmentName,
        cg.grant_time,
        cg.manager_grant_time,
        cg.dis_grant_time,
        cap.name name,
        cap.number formNumber,
        ct.name typeformName,
        cap.manager_submit_remarks managerSubmitRemarks,
        cap.grant_remarks grantRemarks,
        cap.dis_grant_remarks disGrantRemarks,
        cap.grant_source grantSource,
        cg.delivery_time
        FROM wx_mp_common_grantitems cg
        left join wx_mp_common_appitems ca on cg.appitem =ca.id
        left join wx_mp_common_appform cap on cap.id = cg.appform
        left join wx_mp_common_department cd on cap.department = cd.id
        left join wx_mp_common_typeform ct on ct.id = cap.typeform
        <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.common.grantitems.model.Grantitems">
        update wx_mp_common_grantitems
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="appform != null">
                appform = #{appform, jdbcType=BIGINT},
            </if>
            <if test="appitem != null">
                appitem = #{appitem, jdbcType=BIGINT},
            </if>
            <if test="itemname != null">
                itemname = #{itemname, jdbcType=VARCHAR},
            </if>
            <if test="price != null">
                price = #{price, jdbcType=VARCHAR},
            </if>
            <if test="appCount != null">
                app_count = #{appCount, jdbcType=INTEGER},
            </if>
            <if test="grantCount != null">
                grant_count = #{grantCount, jdbcType=INTEGER},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
            <if test="grantTime != null">
                grant_time = #{grantTime, jdbcType=TIMESTAMP},
            </if>
            <if test="realityGrantCount != null">
                reality_grant_count = #{realityGrantCount, jdbcType=INTEGER},
            </if>
            <if test="cover != null">
                cover = #{cover, jdbcType=VARCHAR},
            </if>
            <if test="specifications != null">
                specifications = #{specifications, jdbcType=VARCHAR},
            </if>
            <if test="disGrantCount !=null">
                dis_grant_count = #{disGrantCount,jdbcType=INTEGER},
            </if>
            <if test="managerGrantCount !=null">
                manager_grant_count = #{managerGrantCount,jdbcType=INTEGER},
            </if>
            <if test="managerGrantTime!=null">
                manager_grant_time = #{managerGrantTime,jdbcType=TIMESTAMP},
            </if>
            <if test="disGrantTime!=null">
                dis_grant_time = #{disGrantTime,jdbcType=TIMESTAMP},
            </if>
            <if test="managerAfterStock!=null">
                manager_after_stock = #{managerAfterStock,jdbcType=INTEGER},
            </if>
            <if test="disAfterStock!=null">
                dis_after_stock = #{disAfterStock,jdbcType=INTEGER},
            </if>
            <if test="deliveryTime!=null">
                delivery_time = #{deliveryTime},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <select id="fetchGrantItems" parameterType="java.util.Map" resultType="com.cftech.common.grantitems.model.Grantitems">
        select cg.itemname itemname,cg.price,cg.id,cg.app_count appCount,cg.specifications,ca.id appitem,
        ca.authentic_stock authenticStock,cg.grant_count grantCount,cg.dis_grant_count disGrantCount,
        cg.reality_grant_count realityGrantCount
        ,
        cg.manager_grant_count managerGrantCount,
        ca.dis_stock disStock
        from wx_mp_common_grantitems cg
        left join wx_mp_common_appitems ca on cg.appitem =ca.id
        <include refid="sqlWhere"/>
    </select>

    <insert id="saveBatchGrantitems" parameterType="java.util.Map">
      insert into wx_mp_common_grantitems
        (appform,appitem,itemname,price,app_count,grant_count,accounts_id,del_flag,status,create_time,update_time,description,reality_grant_count,cover,specifications
        ,dis_grant_count,delivery_time) VALUES
       <foreach collection="items" item="item"  separator=",">
           (
           #{item.appform, jdbcType=BIGINT},
           #{item.appitem, jdbcType=BIGINT},
           #{item.itemname, jdbcType=VARCHAR},
           #{item.price, jdbcType=VARCHAR},
           #{item.appCount, jdbcType=INTEGER},
           #{item.grantCount, jdbcType=INTEGER},
           #{item.accountsId, jdbcType=BIGINT},
           #{item.delFlag, jdbcType=TINYINT},
           #{item.status, jdbcType=VARCHAR},
           now(),
           now(),
           #{item.description, jdbcType=VARCHAR},
           #{item.realityGrantCount, jdbcType=INTEGER},
           #{item.cover, jdbcType=VARCHAR},
           #{item.specifications, jdbcType=VARCHAR},
           #{item.disGrantCount,jdbcType=INTEGER},
           #{deliveryTime}
           )
       </foreach>
    </insert>
</mapper>
