<?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="cn.ezhizao.project.business.workflow.mapper.BizWorkFlowMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.workflow.domain.BizWorkFlow" />

    <!--根据主表id，删除 为的是表单保存时，明细统一物理删除后，再统一插入-->
    <delete id="physicallyDelete">
        DELETE FROM
        biz_work_flow
        WHERE id > 0
        <if test="id != null">
            AND id = #{id}
        </if>
    </delete>

    <!--带条件的List查询-->
    <select id="getList" resultMap="BaseResultMap">
        <include refid="getListSql" />
        order by t1.form_name, t1.sort
    </select>

    <!--带条件的List查询 数据总条数-->
    <select id="getTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM (<include refid="getListSql" />) AS t
    </select>

    <sql id="getListSql">
        SELECT
        t1.*, t2.name as role_name, t3.code as permission_code, t4.name as employee_name, t5.name as department_name
            , case when t5.parent_ids = '' then t5.id else concat(t5.parent_ids, ',', t5.id) end as department_ids
        FROM biz_work_flow t1
        left join sys_role t2 on t2.role_id = t1.role_id
        left join sys_menu t3 on t3.menu_id = t1.permission_id
        left join sys_user t4 on t4.user_id = t1.employee_id
        left join sys_dept t5 on t5.dept_id = t1.department_id
        WHERE t1.deleted = 0
        <if test="id != null">
            AND t1.id = #{id}
        </if>
        <if test="tenantId != null">
            AND t1.tenant_id = #{tenantId}
        </if>
        <if test="permissionType != null">
            AND t1.permission_type = #{permissionType}
        </if>
        <if test="roleId != null">
            AND t1.role_id = #{roleId}
        </if>
        <if test="permissionId != null">
            AND t1.permission_id = #{permissionId}
        </if>
        <if test="employeeId != null">
            AND t1.employee_id = #{employeeId}
        </if>
        <if test="sort != null">
            AND t1.sort = #{sort}
        </if>
        <if test="name != null and name != ''">
            AND t1.name like concat('%', #{name}, '%')
        </if>
        <if test="hasFiles != null">
            AND t1.has_files = #{hasFiles}
        </if>
        <if test="formName != null">
            AND t1.form_name = #{formName}
        </if>
        <if test="remark != null and remark != ''">
            AND t1.remark = #{remark}
        </if>
    </sql>

    <select id="getWaitForVerify" resultMap="BaseResultMap">
        <include refid="waitForVerifySql" />
    </select>
    <sql id="waitForVerifySql">
        <if test="type = '支出'">
            select t1.*, t2.id as master_id from work_flow t1, (SELECT t1.*
            FROM payment t1
            left join payment_type t2 on t2.id = t1.payment_cause_id
            left join payment_subject t3 on t3.id = t1.payment_subject_id
            left join data_dictionary t4 on t4.id = t1.accept_account_type_id
            left join employee t5 on t5.id = t1.applier_id
            WHERE t1.deleted = 0
            <if test="id != null">
                AND t1.id = #{id}
            </if>
            <if test="flowNo != null and flowNo != ''">
                AND t1.flow_no = #{flowNo}
            </if>
            <if test="formDate != null and formDate != ''">
                AND DATE_FORMAT(t1.form_date,'%Y-%m-%d') = #{formDate}
            </if>
            <if test="startDate != null and endDate != null">
                AND (
                DATE_FORMAT(t1.form_date,'%Y-%m-%d') &gt;= #{startDate}
                AND
                DATE_FORMAT(t1.form_date,'%Y-%m-%d') &lt;= #{endDate}
                )
            </if>
            <if test="subsidiaryId != null">
                AND t1.subsidiary_id = #{subsidiaryId}
            </if>
            <if test="subsidiary != null and subsidiary != ''">
                AND t1.subsidiary = #{subsidiary}
            </if>
            <if test="subsidiaryName != null and subsidiaryName != ''">
                AND t1.subsidiary_name = #{subsidiaryName}
            </if>
            <if test="subsidiaryBankAccount != null and subsidiaryBankAccount != ''">
                AND t1.subsidiary_bank_account = #{subsidiaryBankAccount}
            </if>
            <if test="subsidiaryBankName != null and subsidiaryBankName != ''">
                AND t1.subsidiary_bank_name = #{subsidiaryBankName}
            </if>
            <if test="companyId != null">
                AND t1.company_id = #{companyId}
            </if>
            <if test="companyName != null and companyName != ''">
                AND t1.company_name = #{companyName}
            </if>
            <if test="amount != null">
                AND t1.amount = #{amount}
            </if>
            <if test="paymentCauseId != null">
                AND t1.payment_cause_id = #{paymentCauseId}
            </if>
            <if test="paymentSubjectId != null">
                AND t1.payment_subject_id = #{paymentSubjectId}
            </if>
            <if test="paymentStatus != null">
                AND t1.payment_status = #{paymentStatus}
            </if>
            <if test="verifyStatus != null">
                AND t1.verify_status = #{verifyStatus}
            </if>
            <if test="acceptAccount != null and acceptAccount != ''">
                AND t1.accept_account = #{acceptAccount}
            </if>
            <if test="acceptAccountTypeId != null">
                AND t1.accept_account_type_id = #{acceptAccountTypeId}
            </if>
            <if test="acceptAccountNo != null">
                AND t1.accept_account_no = #{acceptAccountNo}
            </if>
            <if test="accountBank != null and accountBank != ''">
                AND t1.account_bank = #{accountBank}
            </if>
            <if test="remark != null and remark != ''">
                AND t1.remark = #{remark}
            </if>
            <if test="createTime != null">
                AND t1.create_time = #{createTime}
            </if>
            <if test="creatorId != null">
                AND t1.creator_id = #{creatorId}
            </if>
            <if test="updateTime != null">
                AND t1.update_time = #{updateTime}
            </if>
            <if test="updaterId != null">
                AND t1.updater_id = #{updaterId}
            </if>
            <if test="deleted != null">
                AND t1.deleted = #{deleted}
            </if>
            <if test="version != null">
                AND t1.version = #{version}
            </if>) t2 where
            not exists (select 1 from work_flow_content where
            work_flow_content.master_table_name = 'payment'
            and work_flow_content.master_id = t2.id
            and work_flow_content.work_flow_id = t1.id)
        </if>
    </sql>
</mapper>
