<?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.shzg.project.worn.mapper.WornDeclareBillMapper">
    
    <resultMap type="WornDeclareBill" id="WornDeclareBillResult">
        <result property="id"    column="id"    />
        <result property="billNo"    column="bill_no"    />
        <result property="projectId"    column="project_id"    />
        <result property="contactName"    column="contact_name"    />
        <result property="contactPhone"    column="contact_phone"    />
        <result property="deliveryTime"    column="delivery_time"    />
        <result property="address"    column="address"    />
        <result property="remark"    column="remark"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
    </resultMap>

    <sql id="selectWornDeclareBillVo">
        select id, bill_no, project_id, contact_name, contact_phone, delivery_time, address, remark, status, create_by, create_time, update_by, update_time, is_delete from worn_declare_bill
    </sql>

    <select id="selectWornDeclareBillList" parameterType="WornDeclareBill" resultMap="WornDeclareBillResult">
        <include refid="selectWornDeclareBillVo"/>
        <where>
            <if test="billNo != null  and billNo != ''"> and bill_no = #{billNo}</if>
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="contactName != null  and contactName != ''"> and contact_name like concat('%', #{contactName}, '%')</if>
            <if test="contactPhone != null  and contactPhone != ''"> and contact_phone = #{contactPhone}</if>
            <if test="deliveryTime != null "> and delivery_time = #{deliveryTime}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="isDelete != null  and isDelete != ''"> and is_delete = #{isDelete}</if>
            <if test="isDelete == null or isDelete == ''">and is_delete = '0'</if>
        </where>
        order by create_time desc, id desc
    </select>
    
    <select id="selectWornDeclareBillById" parameterType="Long" resultMap="WornDeclareBillResult">
        <include refid="selectWornDeclareBillVo"/>
        where id = #{id}
    </select>

    <insert id="insertWornDeclareBill" parameterType="WornDeclareBill" useGeneratedKeys="true" keyProperty="id">
        insert into worn_declare_bill
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="billNo != null and billNo != ''">bill_no,</if>
            <if test="projectId != null">project_id,</if>
            <if test="contactName != null and contactName != ''">contact_name,</if>
            <if test="contactPhone != null and contactPhone != ''">contact_phone,</if>
            <if test="deliveryTime != null">delivery_time,</if>
            <if test="address != null and address != ''">address,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDelete != null and isDelete != ''">is_delete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="billNo != null and billNo != ''">#{billNo},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="contactName != null and contactName != ''">#{contactName},</if>
            <if test="contactPhone != null and contactPhone != ''">#{contactPhone},</if>
            <if test="deliveryTime != null">#{deliveryTime},</if>
            <if test="address != null and address != ''">#{address},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDelete != null and isDelete != ''">#{isDelete},</if>
        </trim>
    </insert>

    <update id="updateWornDeclareBill" parameterType="WornDeclareBill">
        update worn_declare_bill
        <trim prefix="SET" suffixOverrides=",">
            <if test="billNo != null and billNo != ''">bill_no = #{billNo},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="contactName != null">contact_name = #{contactName},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="deliveryTime != null">delivery_time = #{deliveryTime},</if>
            <if test="address != null">address = #{address},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWornDeclareBillById" parameterType="Long">
        delete from worn_declare_bill where id = #{id}
    </delete>

    <delete id="deleteWornDeclareBillByIds" parameterType="String">
        delete from worn_declare_bill where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectWornDeclareBillByBillNo" parameterType="String" resultMap="WornDeclareBillResult">
        select id,
               bill_no,
               project_id,
               contact_name,
               contact_phone,
               delivery_time,
               address,
               remark,
               status,
               create_by,
               create_time,
               update_by,
               update_time,
               is_delete
        from worn_declare_bill
        where bill_no = #{billNo}
          and is_delete = '0'
            limit 1
    </select>
    <update id="cancelWornDeclareBill" parameterType="com.shzg.project.worn.domain.WornDeclareBill">
        update worn_declare_bill
        set status = '9',
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and is_delete = '0'
    </update>
</mapper>