<?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.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao">

    <sql id="workClientAttachmentColumns">
        a.id AS "id",
        a.create_by AS "createBy.id",
        a.create_date AS "createDate",
        a.update_by AS "updateBy.id",
        a.update_date AS "updateDate",
        a.remarks AS "remarks",
        a.del_flag AS "delFlag",
        a.url AS "url",
        a.type AS "type",
        a.attachment_id AS "attachmentId",
        a.attachment_user AS "attachmentUser",
        a.attachment_name AS "attachmentName",
        a.attachment_flag AS "attachmentFlag",
        a.company_id AS "companyId",
        createBy.name AS "createBy.name"
    </sql>

    <sql id="workClientAttachmentJoins">
        LEFT JOIN sys_user createBy on createBy.id = a.create_by
    </sql>


    <select id="get" resultType="WorkClientAttachment" >
        SELECT
        <include refid="workClientAttachmentColumns"/>
        FROM work_attachment a
        <include refid="workClientAttachmentJoins"/>
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultType="WorkClientAttachment" >
        SELECT
        <include refid="workClientAttachmentColumns"/>
        FROM work_attachment a
        <include refid="workClientAttachmentJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="attachmentId != null and attachmentId != ''">
                AND a.attachment_id = #{attachmentId}
            </if>
            <if test="attachmentFlag != null and attachmentFlag != ''">
                AND attachment_flag = #{attachmentFlag}
            </if>
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.update_date DESC
            </otherwise>
        </choose>
    </select>

    <select id="findAllList" resultType="WorkClientAttachment" >
        SELECT
        <include refid="workClientAttachmentColumns"/>
        FROM work_attachment a
        <include refid="workClientAttachmentJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            AND attachment_flag = #{attachmentFlag}
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.update_date DESC
            </otherwise>
        </choose>
    </select>

    <insert id="insert">
        INSERT INTO work_attachment(
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        remarks,
        del_flag,
        url,
        type,
        attachment_id,
        attachment_user,
        attachment_name,
        company_id,
        attachment_flag
        ) VALUES (
        #{id},
        #{createBy.id},
        #{createDate},
        #{updateBy.id},
        #{updateDate},
        #{remarks},
        #{delFlag},
        #{url},
        #{type},
        #{attachmentId},
        #{attachmentUser},
        #{attachmentName},
        #{companyId},
        #{attachmentFlag}
        )
    </insert>

    <update id="update">
        UPDATE work_attachment SET
        update_by = #{updateBy.id},
        update_date = #{updateDate},
        remarks = #{remarks},
        url = #{url},
        type = #{type},
        attachment_flag = #{attachmentFlag},
        attachment_id = #{attachmentId},
        attachment_user = #{attachmentUser},
        attachment_name = #{attachmentName},
        company_id = #{companyId}
        WHERE id = #{id}
    </update>


    <!--物理删除-->
    <update id="delete">
        DELETE FROM work_attachment
        WHERE id = #{id}
    </update>
    <!--物理删除-->
    <update id="deleteCase">
        DELETE FROM work_attachment
        WHERE attachment_id = #{attachmentId}
    </update>

    <!--逻辑删除-->
    <update id="deleteByLogic">
        UPDATE work_attachment SET
        del_flag = #{DEL_FLAG_DELETE}
        WHERE id = #{id}
    </update>


    <!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
    <select id="findUniqueByProperty" resultType="WorkClientAttachment" statementType="STATEMENT">
        select * FROM work_attachment  where ${propertyName} = '${value}'
    </select>

</mapper>