<?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.lz.modules.luck.dao.LuckRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lz.modules.luck.entity.LuckRecord">
        <id column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="staff_id" property="staffId"/>
        <result column="name" property="name"/>
        <result column="mobile" property="mobile"/>
        <result column="is_luck" property="isLuck"/>
        <result column="weight" property="weight"/>
        <result column="luck_id" property="luckId"/>
        <result column="goods_name" property="goodsName"/>
        <result column="goods_id" property="goodsId"/>
        <result column="remark" property="remark"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id AS id, is_delete AS isDelete, gmt_create AS gmtCreate, gmt_modified AS gmtModified, staff_id AS staffId, name AS name, mobile AS mobile, is_luck AS isLuck, weight AS weight, luck_id AS luckId, goods_name AS goodsName, goods_id AS goodsId, remark AS remark
    </sql>




    <select id="selectLuckRecordById" resultType="LuckRecord" >
        select * from luck_record  where id=#{id} and is_delete = 0 limit 1 
    </select>


    <insert id="insertLuckRecord" parameterType="LuckRecord" useGeneratedKeys="true" keyProperty="id" >
        insert into luck_record(
            <if test="staffId != null">staff_id, </if>
            <if test="name != null">name, </if>
            <if test="mobile != null">mobile, </if>
            <if test="isLuck != null">is_luck, </if>
            <if test="weight != null">weight, </if>
            <if test="luckId != null">luck_id, </if>
            <if test="goodsName != null">goods_name, </if>
            <if test="goodsId != null">goods_id, </if>
            <if test="remark != null">remark, </if>
            is_delete,
            gmt_create,
            gmt_modified
        )values(
            <if test="staffId != null">#{ staffId}, </if>
            <if test="name != null">#{ name}, </if>
        <if test="mobile != null">#{ mobile}, </if>
            <if test="isLuck != null">#{ isLuck}, </if>
            <if test="weight != null">#{ weight}, </if>
            <if test="luckId != null">#{ luckId}, </if>
            <if test="goodsName != null">#{ goodsName}, </if>
            <if test="goodsId != null">#{ goodsId}, </if>
            <if test="remark != null">#{ remark}, </if>
            0,
            now(),
            now()
        )
    </insert>


    <update id="updateLuckRecordById" parameterType="LuckRecord" >
        update
            luck_record
        <trim prefix="set" suffixOverrides=",">
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="gmtCreate != null">gmt_create = #{gmtCreate},</if>
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="isLuck != null">is_luck = #{isLuck},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="luckId != null">luck_id = #{luckId},</if>
            <if test="goodsName != null">goods_name = #{goodsName},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="remark != null">remark = #{remark}</if>                
        </trim>
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="updateCoverLuckRecordById" parameterType="LuckRecord" >
        update
            luck_record    
        set     
            is_delete = #{isDelete},
            gmt_create = #{gmtCreate},
            staff_id = #{staffId},
            name = #{name},
            mobile = #{mobile},
            is_luck = #{isLuck},
            weight = #{weight},
            luck_id = #{luckId},
            goods_name = #{goodsName},
            goods_id = #{goodsId},
            remark = #{remark}                
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="deleteLuckRecordById" parameterType="java.lang.Long">
        update luck_record set is_delete = 1 where id=#{id} limit 1  
    </update>

    <select id="selectLuckRecordByLuckIdAndStaffId" resultType="LuckRecord" >
        select * from luck_record  where luck_id=#{luckId} and staff_id=#{id} and is_delete = 0 limit 1
    </select>

    <select id="selectLuckRecordByLuckId" resultType="LuckRecord" >
        select * from luck_record  where luck_id=#{luckId}and is_delete = 0 order by is_luck desc
    </select>

    <update id="updateLuckRecordByReqWithId" parameterType="LuckRecordReq" >
        update
            luck_record
        set
            is_luck = #{isLuck},
            luck_id = #{luckId},
            goods_name = #{goodsName},
            goods_id = #{goodsId}
        ,gmt_modified = now()
        where id = #{id}
    </update>

    <select id="selectGoodLuckRecordByLuckId" resultType="LuckRecord" >
        select * from luck_record  where luck_id=#{luckId}and is_delete = 0 and is_luck=1 order by goods_id asc
    </select>

    <select id="selectCountGoodLuckRecordByLuckId" parameterType="java.lang.Long" resultType="integer">
        select count(1) from luck_record  where luck_id=#{luckId} and is_delete = 0 and is_luck=1
    </select>

    <select id="selectNoGoodLuckRecordByLuckId" resultType="LuckRecord" >
        select * from luck_record  where luck_id=#{luckId}and is_delete = 0 and is_luck=0 order by is_luck desc
    </select>

    <select id="selectCountRecordByLuckId" parameterType="java.lang.Long" resultType="integer">
        select count(1) from luck_record  where luck_id=#{luckId} and is_delete = 0
    </select>

</mapper>

