<?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.pubmodules.oss.mapper.OssServiceMapper">
    <delete id="deleteById">
        delete from work_attachment where id = #{id}
    </delete>
    <delete id="deleteByAttachmentId">
        delete from work_attachment where attachment_id = #{attachmentId}
    </delete>
    <delete id="deleteByAttachmentIdNotInIds">
        delete from work_attachment where attachment_id = #{attachmentId}
        and id not in
        <foreach collection="delIds" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>

    <select id="selectSaveById" resultType="java.lang.Integer">
    SELECT
        COUNT( 0 )
    FROM
        work_attachment
    WHERE
        del_flag = 0
        AND attachment_id = (
        SELECT
            attachment_id
        FROM
            work_attachment
    WHERE
        id = #{id})
  </select>

  <update id="updateProjectRecord">
    UPDATE project_records
    SET file_upload_type = 0
    WHERE
        id = (
        SELECT
            attachment_id
        FROM
            work_attachment
    WHERE
        id = #{id})
  </update>

    <select id="findList" resultType="com.jeeplus.pubmodules.oss.service.dto.AttachmentDTO">
        SELECT a.id, a.url, a.type, a.attachment_id, a.attachment_name, a.attachment_flag,
        a.module_type, a.attachment_type, a.file_size, a.sort, a.description,
        a.create_time, a.create_by_id as "create_by.id", su.name as "createBy.name" , a.update_time, a.update_by_id as "updateBy.id", a.del_flag
         FROM work_attachment a
         left join sys_user su on su.id = a.create_by_id
         ${ew.customSqlSegment}
         ORDER BY a.sort ASC;
    </select>

    <insert id="insertWorkAttachment">
        INSERT INTO work_attachment (
            id,
            create_by_id,
            create_time,
            update_by_id,
            update_time,
            del_flag,
            url,
            type,
            attachment_id,
            attachment_name,
            attachment_flag,
            file_size,
            sort,
            remarks
            )
        VALUES(
            #{workAttachment.id},
            #{userDto.id},
            #{workAttachment.createTime},
            #{userDto.id},
            #{workAttachment.updateTime},
            #{workAttachment.delFlag},
            #{workAttachment.url},
            #{workAttachment.type},
            #{workAttachment.attachmentId},
            #{workAttachment.attachmentName},
            #{workAttachment.attachmentFlag},
            #{workAttachment.fileSize},
            #{workAttachment.sort},
            #{workAttachment.remarks}
            )
    </insert>

    <select id="getByAttachmentIdAndUrlAndAttachmentFlag" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        SELECT a.id, a.url, a.type, a.attachment_id, a.attachment_name, a.attachment_flag,
        a.module_type, a.attachment_type, a.file_size, a.sort, a.description,
        a.create_time, a.create_by_id as "create_by.id", su.name as "createBy.name" , a.update_time, a.update_by_id as "updateBy.id", a.del_flag
         FROM work_attachment a
         left join sys_user su on su.id = a.create_by_id
         <where>
             a.del_flag = 0
             <if test="attachmentId != null and attachmentId != ''">
                 and a.attachment_id = #{attachmentId}
             </if>
             <if test="attachmentName != null and attachmentName != ''">
                 and a.attachment_name = #{attachmentName}
             </if>
             <if test="attachmentFlag != null and attachmentFlag != ''">
                 and a.attachment_flag = #{attachmentFlag}
             </if>
         </where>
    </select>

    <select id="selectWorkAttachmentByAttachmentId"
            resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">

        select a.* from work_attachment a
        left join cw_project_report_file cf on cf.report_file_id = a.id
        where a.attachment_id = #{id} and cf.seal_type = 1 and a.del_flag = 0
    </select>
    <select id="selectListByAttachmentId" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        select a.* from work_attachment a where a.attachment_id = #{id} order by a.sort ASC
    </select>



    <select id="getElectronicEngineeringInvoiceAttachmentList" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        SELECT a.id, a.url, a.type, a.attachment_id, a.attachment_name, a.attachment_flag,
        a.module_type, a.attachment_type, a.file_size, a.sort, a.description,
        a.create_time, a.create_by_id as "create_by.id" , a.update_time, a.update_by_id as "updateBy.id", a.del_flag
        FROM work_attachment a
        <where>
            a.del_flag = 0

            <if test="attachmentIdList != null">
                AND a.attachment_id IN
                <foreach collection="attachmentIdList" item="attachmentId" index="index" open="(" close=")" separator=",">
                    #{attachmentId}
                </foreach>
            </if>
            <if test="attachmentFlag != null and attachmentFlag != ''">
                and a.attachment_flag = #{attachmentFlag}
            </if>
        </where>
    </select>


    <select id="isUsedByInvoiceNumber" resultType="int">
        select  MAX(counts)
        from(select count(a.number) as counts from reimbursement_amount_info a
        left join reimbursement_info ri on ri.id = a.info_id
        where a.number  = #{invoiceNumber} and a.del_flag = 0 and ri.del_flag = 0  and (ri.type = '2' or ri.type = '5')
        union all
        select count(a.number) as counts from cw_reimbursement_amount_info a
        left join cw_reimbursement_info ri on ri.id = a.info_id
        where a.number  = #{invoiceNumber} and a.del_flag = 0 and ri.del_flag = 0  and (ri.type = '2' or ri.type = '5')
        union all
        select count(a.number) as counts from ccpm_reimbursement_amount_info a
        left join ccpm_reimbursement_info ri on ri.id = a.info_id
        where a.number  = #{invoiceNumber} and a.del_flag = 0 and ri.del_flag = 0  and (ri.type = '2' or ri.type = '5')
        union all
        select count(a.number) as counts from zs_reimbursement_amount_info a
        left join zs_reimbursement_info ri on ri.id = a.info_id
        where a.number  = #{invoiceNumber} and a.del_flag = 0 and ri.del_flag = 0  and (ri.type = '2' or ri.type = '5')
        union all
        select count(a.number) as counts from consultancy_reimbursement_amount_info a
        left join consultancy_reimbursement_info ri on ri.id = a.info_id
        where a.number  = #{invoiceNumber} and a.del_flag = 0 and ri.del_flag = 0  and (ri.type = '2' or ri.type = '5')
        ) as result
    </select>


    <select id="findDtos" resultType="com.jeeplus.sys.service.dto.WorkAttachmentInfoDTO">
        SELECT
            id,
            url,
            attachment_name AS `name`,
            create_by_id AS `by`,
            create_time,
            attachment_flag AS attachmentFlag
        FROM
            work_attachment
        WHERE
            del_flag = 0
          AND attachment_id = #{id}
          AND attachment_flag = #{attachmentFlag}

    </select>


    <select id="getByAttachmentIdListAndFlag"
            resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        select a.*
        from work_attachment a
        where a.del_flag = 0
        and a.attachment_id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
</mapper>
