<?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.dayou.mapper.PersonalMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.Personal">
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="deleted" property="deleted" />
        <result column="order_id" property="orderId" />
        <result column="location" property="location" />
        <result column="evaluate_aim" property="evaluateAim" />
        <result column="purpose" property="purpose" />
        <result column="clientele_type" property="clienteleType" />
        <result column="clientele_id" property="clienteleId" />
        <result column="clientele_sub_id" property="clienteleSubId" />
        <result column="clientele_contact_id" property="clienteleContactId" />
        <result column="agent" property="agent" />
        <result column="loan_nature" property="loanNature" />
        <result column="loan_aim" property="loanAim" />
        <result column="loan_limit" property="loanLimit" />
        <result column="loan_period" property="loanPeriod" />
        <result column="loan_expire" property="loanExpire" />
        <result column="bailorA" property="bailorA" />
        <result column="bailorB" property="bailorB" />
        <result column="bailorA_tel" property="bailoraTel" />
        <result column="bailorB_tel" property="bailorbTel" />
        <result column="terminal_clientele_type" property="terminalClienteleType" />
        <result column="terminal_clientele_contact_id" property="terminalClienteleContactId" />
        <result column="terminal_clientele_id" property="terminalClienteleId" />
        <result column="credentials" property="credentials" />
        <result column="client_manager_id" property="clientManagerId" />
        <result column="outward_staff" property="outwardStaff" />
        <result column="inward_staff" property="inwardStaff" />
        <result column="pricing_staff" property="pricingStaff" />
        <result column="remark" property="remark" />
        <result column="save_file_check" property="saveFileCheck" />
        <result column="if_save_file" property="ifSaveFile" />
        <result column="save_file_date" property="saveFileDate" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        created,
        modified,
        deleted,
        order_id, location, evaluate_aim, purpose, clientele_type, clientele_id, clientele_sub_id, clientele_contact_id, agent, loan_nature, loan_aim, loan_limit, loan_period, loan_expire, bailorA, bailorB, bailorA_tel, bailorB_tel, terminal_clientele_type, terminal_clientele_contact_id, terminal_clientele_id, credentials, is_online, client_manager_id, outward_staff, inward_staff, pricing_staff, remark
    </sql>

    <sql id="Common_query_sql">
        SELECT
            wf.id AS currentNodeId,
            wf.node_id,
            wf.business_id,
            wf.business_sub_id,
            wf.modified AS nodeTime,
            wn.NAME as currentNodeName,
            wn.CODE currentNodeCode,
            p.id,
            p.order_id,
            p.location,
            p.agent,
            p.bailorA,
            p.bailorB,
            p.bailorA_tel,
            p.bailorB_tel,
            p.client_manager_id,
            p.remark,
            pt.acreage,
            pt.price,
            pt.amount,
            cc1.NAME AS clientName,
            cc2.NAME AS clientSubName,
            cl1.NAME AS contactName,
            cl1.mobile AS contactTel,
            u.NAME AS clientManager,
            u1.NAME AS outwardStaffName,
            u2.NAME AS inwardStaffName,
            u3.NAME AS pricingStaffName,
            u4.name as handlerName,
            u4.id as handlerId,
            p.created,
            c.comments
        FROM
            work_flow_node_instance wf
                LEFT JOIN work_node wn ON wn.id = wf.node_id
                LEFT JOIN work_task_record wtr ON wtr.instance_id = wf.id
                LEFT JOIN user u4 ON u4.id = wtr.handler_id
                LEFT JOIN personal p ON p.id = wf.business_id
                LEFT JOIN personal_target pt ON pt.personal_id =  wf.business_id
                LEFT JOIN customer_company cc1 ON cc1.id = p.clientele_id
                LEFT JOIN customer_company cc2 ON cc2.id = p.clientele_sub_id
                LEFT JOIN customer_linkman cl1 ON cl1.id = p.clientele_contact_id
                LEFT JOIN user u ON u.id = p.client_manager_id
                LEFT JOIN user u1 ON u1.id = p.outward_staff
                LEFT JOIN user u2 ON u2.id = p.inward_staff
                LEFT JOIN user u3 ON u3.id = p.pricing_staff
                left join (
                select business_id,comments from work_flow_log where concat(business_id,modified) in (
                    select concat(business_id,max(modified)) from work_flow_log where business_type = 'PERSONAL_BUSINESS' and comments is not null  group BY business_id
                )
            ) c on p.id = c.business_id
        WHERE
            wf.business_type = "PERSONAL_BUSINESS"
          AND wf.state = 'PENDING'
          AND wf.deleted = 0
          AND wn.deleted = 0
          AND p.deleted = 0

    </sql>
    <select id="xSelectPage" parameterType="com.dayou.vo.PersonalVO" resultType="com.dayou.vo.PersonalVO">
        <include refid="Common_query_sql" />
        <if test="keyword!=null and keyword!='' ">
            and (
            p.location like concat ('%',#{keyword},'%')
            or p.order_id like concat('%',#{keyword},'%')
            or p.agent like concat('%',#{keyword},'%')
            or p.bailorA like concat('%',#{keyword},'%')
            or p.bailorB like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or cl1.name like concat('%',#{keyword},'%')
            or p.bailorA_tel like concat('%',#{keyword},'%')
            or p.bailorB_tel like concat('%',#{keyword},'%')
            )
        </if>
        <if test="personal!=null and personal.startDate!=null and personal.startDate!=''">
            and p.created &gt;= #{personal.startDate}
        </if>
        <if test="personal!=null and personal.endDate!=null and personal.endDate!=''">
            and p.created &lt;= #{personal.endDate}
        </if>
        <if test="personal!=null and personal.nodeId!=null">
            and wn.id = #{personal.nodeId}
        </if>
        <if test="personal!=null and personal.clientName!=null and personal.clientName!=''">
            and cc1.NAME like concat('%',#{personal.clientName},'%')
        </if>
        <if test="personal!=null and personal.clientSubName!=null and personal.clientSubName!=''">
            and cc2.NAME like concat('%',#{personal.clientSubName},'%')
        </if>
        <if test="personal!=null and personal.clientManager!=null and personal.clientManager!='' ">
            and u.name like concat('%',#{personal.clientManager},'%')
        </if>
        <if test="personal!=null and personal.outwardStaffName!=null and personal.outwardStaffName!='' ">
            and u1.name like concat('%',#{personal.outwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.inwardStaffName!=null and personal.inwardStaffName!='' ">
            and u2.name like concat('%',#{personal.inwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.pricingStaffName!=null and personal.pricingStaffName!='' ">
            and u3.name like concat('%',#{personal.pricingStaffName},'%')
        </if>
        <if test="personal!=null and personal.handlerName!=null and personal.handlerName!='' ">
            and u4.name like concat('%',#{personal.handlerName},'%')
        </if>
        <if test="personal!=null and personal.isCancellation!=null and personal.isCancellation==true">
            and u4.name = '项目作废'
        </if>
        <if test="personal!=null and personal.isCancellation!=null and personal.isCancellation==false">
            and u4.name != '项目作废'
        </if>
        <if test="personal!=null and personal.userIds!=null and personal.userIds.size!=0">
            and p.client_manager_id in
            <foreach collection="personal.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by wf.modified DESC
    </select>

    <select id="getDetail" parameterType="java.lang.Long" resultType="com.dayou.vo.PersonalVO">
        SELECT
            p.*,
            cc1.NAME AS clientName,
            cc2.NAME AS clientSubName,
            cl1.NAME AS contactName,
            cl1.mobile AS contactTel,
            cl1.duty AS cDuty,
            cl1.department as cDepartment,
            u.NAME AS clientManager,
            u1.NAME AS outwardStaffName,
            u2.NAME AS inwardStaffName,
            u3.NAME AS pricingStaffName,
            cc3.name as tTerminalClienteleName,
            cc3.phone as tCompanyMobile,
            cl2.name as tTerminalClienteleContactName,
            cl2.mobile as tMobile
        FROM
            personal p
                LEFT JOIN personal_target pt ON p.id = pt.personal_id
                LEFT JOIN customer_company cc1 ON cc1.id = p.clientele_id
                LEFT JOIN customer_company cc2 ON cc2.id = p.clientele_sub_id
                LEFT JOIN customer_linkman cl1 ON cl1.id = p.clientele_contact_id
                left join customer_company cc3 on cc3.id = p.terminal_clientele_id
                left join customer_linkman cl2 on cl2.id = p.terminal_clientele_contact_id
                LEFT JOIN user u ON u.id = p.client_manager_id
                LEFT JOIN user u1 ON u1.id = p.outward_staff
                LEFT JOIN user u2 ON u2.id = p.inward_staff
                LEFT JOIN user u3 ON u3.id = p.pricing_staff
        where p.deleted = 0 and p.id = #{id}
    </select>

    <select id="getByTargetId" parameterType="java.lang.Long" resultType="com.dayou.entity.Personal">
        select id,credentials from personal where id = (select personal_id from personal_target where id = #{targetId} and deleted = 0) and deleted = 0
    </select>

    <select id="waitingSaveFile" parameterType="com.dayou.vo.PersonalVO" resultType="com.dayou.vo.PersonalVO">
        select
            wf.id AS currentNodeId,
            wtr.id as taskId,
            wtr.created as taskCreated,
            p.id,
            p.order_id,
            p.location,
            pt.acreage,
            pt.price,
            pt.amount,
            cc1.NAME AS clientName,
            cc2.NAME AS clientSubName,
            u.NAME AS clientManager,
            u1.NAME AS outwardStaffName,
            u2.NAME AS inwardStaffName,
            u3.NAME AS pricingStaffName,
            p.created,
            o.real_amount
        from (select id,node_id,business_id from work_flow_node_instance where business_type = "PERSONAL_BUSINESS" and state = 'PENDING' and deleted = 0) wf
                 LEFT JOIN work_node wn ON wn.id = wf.node_id
                 LEFT JOIN work_task_record wtr ON wtr.instance_id = wf.id
                 LEFT JOIN personal p ON p.id = wf.business_id
                 LEFT JOIN personal_target pt ON pt.personal_id =  wf.business_id
                 LEFT JOIN customer_company cc1 ON cc1.id = p.clientele_id
                 LEFT JOIN customer_company cc2 ON cc2.id = p.clientele_sub_id
                 LEFT JOIN user u ON u.id = p.client_manager_id
                 LEFT JOIN user u1 ON u1.id = p.outward_staff
                 LEFT JOIN user u2 ON u2.id = p.inward_staff
                 LEFT JOIN user u3 ON u3.id = p.pricing_staff
                 left join (select business_id,real_amount from order_fund where business_type='PERSONAL_BUSINESS' and deleted = 0) o on o.business_id = p.id
        where  wn.code = 'BUSINESS_ARCHIVING'
          AND wn.deleted = 0
          AND p.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and (
            p.location like concat ('%',#{keyword},'%')
            or p.order_id like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or p.bailorA_tel like concat('%',#{keyword},'%')
            or p.bailorB_tel like concat('%',#{keyword},'%')
            )
        </if>
        order by p.order_id DESC
    </select>

    <update id="updateSaveFileInfo" parameterType="java.util.List">
        update personal set if_save_file = 1 ,save_file_date= now() where id in (
        <foreach collection="list" index="index" item="item" separator=",">
            #{item}
        </foreach>
        )
    </update>

    <select id="saveFileDone" parameterType="com.dayou.vo.PersonalVO" resultType="com.dayou.vo.PersonalVO">
        select
            p.id,
            p.order_id,
            p.location,
            pt.acreage,
            pt.price,
            pt.amount,
            cc1.NAME AS clientName,
            cc2.NAME AS clientSubName,
            u.NAME AS clientManager,
            u1.NAME AS outwardStaffName,
            u2.NAME AS inwardStaffName,
            u3.NAME AS pricingStaffName,
            p.created,
            o.real_amount,
            p.save_file_date
        from
            personal p
                LEFT JOIN personal_target pt ON pt.personal_id =  p.id
                LEFT JOIN customer_company cc1 ON cc1.id = p.clientele_id
                LEFT JOIN customer_company cc2 ON cc2.id = p.clientele_sub_id
                LEFT JOIN user u ON u.id = p.client_manager_id
                LEFT JOIN user u1 ON u1.id = p.outward_staff
                LEFT JOIN user u2 ON u2.id = p.inward_staff
                LEFT JOIN user u3 ON u3.id = p.pricing_staff
                left join (select business_id,real_amount from order_fund where business_type='PERSONAL_BUSINESS' and deleted = 0) o on o.business_id = p.id
        where
            p.deleted = 0 and p.if_save_file = 1 and pt.deleted= 0
        <if test="keyword!=null and keyword!='' ">
            and (
            p.location like concat ('%',#{keyword},'%')
            or p.order_id like concat('%',#{keyword},'%')
            or p.bailorA like concat('%',#{keyword},'%')
            or p.bailorB like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or p.bailorA_tel like concat('%',#{keyword},'%')
            or p.bailorB_tel like concat('%',#{keyword},'%')
            )
        </if>
        <if test="personal!=null and personal.startDate!=null and personal.startDate!=''">
            and p.created &gt;= #{personal.startDate}
        </if>
        <if test="personal!=null and personal.endDate!=null and personal.endDate!=''">
            and p.created &lt;= #{personal.endDate}
        </if>
        <if test="personal!=null and personal.clientName!=null and personal.clientName!=''">
            and cc1.NAME like concat('%',#{personal.clientName},'%')
        </if>
        <if test="personal!=null and personal.clientSubName!=null and personal.clientSubName!=''">
            and cc2.NAME like concat('%',#{personal.clientSubName},'%')
        </if>
        <if test="personal!=null and personal.clientManager!=null and personal.clientManager!='' ">
            and u.name like concat('%',#{personal.clientManager},'%')
        </if>
        <if test="personal!=null and personal.outwardStaffName!=null and personal.outwardStaffName!='' ">
            and u1.name like concat('%',#{personal.outwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.inwardStaffName!=null and personal.inwardStaffName!='' ">
            and u2.name like concat('%',#{personal.inwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.pricingStaffName!=null and personal.pricingStaffName!='' ">
            and u3.name like concat('%',#{personal.pricingStaffName},'%')
        </if>
    </select>

    <sql id="myOrderSql">
        select
        p.id,
        p.order_id,
        p.location,
        p.location as orderName,
        pt.acreage,
        pt.price,
        pt.amount,
        cc1.NAME AS clientName,
        cc2.NAME AS clientSubName,
        p.agent,
        cl.name as contactName,
        p.bailorA,
        p.bailorB,
        cl.mobile as contactTel,
        u1.NAME AS outwardStaffName,
        u2.NAME AS inwardStaffName,
        u3.NAME AS pricingStaffName,
        p.created,
        o.real_amount,
        o.should_amount,
        p.save_file_date,
        o.id as orderFundId,
        fi.invoiceAmount
        from
            personal p
                LEFT JOIN personal_target pt ON pt.personal_id =  p.id
                LEFT JOIN customer_company cc1 ON cc1.id = p.clientele_id
                LEFT JOIN customer_company cc2 ON cc2.id = p.clientele_sub_id
                left join customer_linkman cl on cl.id = p.clientele_contact_id
                LEFT JOIN user u1 ON u1.id = p.outward_staff
                LEFT JOIN user u2 ON u2.id = p.inward_staff
                LEFT JOIN user u3 ON u3.id = p.pricing_staff
                left join (select id,business_id,real_amount,should_amount from order_fund where business_type='PERSONAL_BUSINESS' and deleted = 0) o on o.business_id = p.id
        left join (select sum(real_amount) as invoiceAmount,order_fund_id from finance_invoice where state ='已开票' and deleted = 0 GROUP BY order_fund_id) fi on fi.order_fund_id = o.id
        where
            p.deleted = 0  and p.client_manager_id = #{personal.clientManagerId}
          AND IF(#{personal.hasRealAmount}, o.real_amount IS NOT NULL, o.real_amount IS NULL)
        <if test="keyword!=null and keyword!='' ">
            and (
            p.location like concat ('%',#{keyword},'%')
            or p.order_id like concat('%',#{keyword},'%')
            or p.agent like concat('%',#{keyword},'%')
            or p.bailorA like concat('%',#{keyword},'%')
            or p.bailorB like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or p.bailorA_tel like concat('%',#{keyword},'%')
            or p.bailorB_tel like concat('%',#{keyword},'%')
            )
        </if>
        <if test="personal!=null and personal.orderId!=null">
            AND p.order_id like concat('%',#{personal.orderId},'%')
        </if>
        <if test="personal!=null and personal.bailor!=null">
            AND (
                p.bailorA like concat('%',#{personal.bailor},'%')
                or p.bailorB like concat('%',#{personal.bailor},'%')
            )
        </if>
        <if test="personal!=null and personal.clientName!=null">
            AND cc1.name like concat('%',#{personal.clientName},'%')
        </if>
        <if test="personal!=null and personal.clientSubName!=null">
            AND cc2.name like concat('%',#{personal.clientSubName},'%')
        </if>
        <if test="personal!=null and personal.shouldAmount!=null">
            AND o.should_amount = #{personal.shouldAmount}
        </if>
        <if test="personal!=null and personal.invoiceAmountCondition!=null">
            AND fi.invoiceAmount = #{personal.invoiceAmountCondition}
        </if>
        <if test="personal!=null and personal.startDate!=null and personal.startDate!=''">
            and p.created &gt;= #{personal.startDate}
        </if>
        <if test="personal!=null and personal.endDate!=null and personal.endDate!=''">
            and p.created &lt;= #{personal.endDate}
        </if>
        <if test="personal!=null and personal.nodeCode!=null and personal.nodeCode!=''">
            and wn.code = #{personal.nodeCode}
        </if>
        <if test="personal!=null and personal.clientName!=null and personal.clientName!=''">
            and cc1.NAME like concat('%',#{personal.clientName},'%')
        </if>
        <if test="personal!=null and personal.clientSubName!=null and personal.clientSubName!=''">
            and cc2.NAME like concat('%',#{personal.clientSubName},'%')
        </if>
        ORDER BY
        <if test="personal!=null and personal.invoiceAmount!=null">
            fi.invoiceAmount=${personal.invoiceAmount} desc,
        </if>
        p.created DESC
    </sql>

    <select id="myOrder" parameterType="com.dayou.vo.PersonalVO" resultType="com.dayou.vo.PersonalVO">
        <include refid="myOrderSql"/>
    </select>

    <select id="exportMyOrder" resultType="com.dayou.vo.PersonalVO">
        <include refid="myOrderSql"/>
    </select>

    <select id="waitingAllotOrderPage" resultType="com.dayou.vo.PersonalVO">
        SELECT
            wfni.*,
            p.location,
            p.clientele_type,
            p.agent,
            p.bailorA,
            p.bailorA_tel,
            p.bailorB,
            p.bailorB_tel,
            cc1.NAME AS clientName,
            cc2.NAME AS clientSubName,
            cl1.NAME AS contactName,
            cl1.mobile AS contactTel,
            u.NAME AS clientManager,
        c.comments
        FROM
            ( SELECT wf.id as currentNodeId, wn.name as currentNodeName,wf.node_id, wf.business_id as id, wf.business_sub_id as orderId FROM work_flow_node_instance wf
                                                                                                                                                 left join work_node wn on (wn.id = wf.node_id and wf.flow_id = wn.flow_id)
              WHERE wf.business_type = 'PERSONAL_BUSINESS' AND wf.state = 'PENDING' AND wf.deleted = 0  and wn.code = 'DEPARTMENT_ALLOCATION') wfni
                LEFT JOIN personal p ON p.id = wfni.id
                LEFT JOIN customer_company cc1 ON cc1.id = p.clientele_id
                LEFT JOIN customer_company cc2 ON cc2.id = p.clientele_sub_id
                LEFT JOIN customer_linkman cl1 ON cl1.id = p.clientele_contact_id
                LEFT JOIN `user` u ON u.id = p.client_manager_id
        left join (
        select business_id,comments from work_flow_log where concat(business_id,created) in (
        select concat(business_id,max(created)) from work_flow_log where business_type = 'PERSONAL_BUSINESS' and comments is not null  group BY business_id
        )
        ) c on c.business_id = p.id
        WHERE
            p.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and (
            p.location like concat ('%',#{keyword},'%')
            or p.order_id like concat('%',#{keyword},'%')
            or p.agent like concat('%',#{keyword},'%')
            or p.bailorA like concat('%',#{keyword},'%')
            or p.bailorB like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or cl1.name like concat('%',#{keyword},'%')
            or p.bailorA_tel like concat('%',#{keyword},'%')
            or p.bailorB_tel like concat('%',#{keyword},'%')
            )
        </if>
        order by p.created DESC
    </select>

    <update id="batchUpdateOutWardStaffId">
        update personal set outward_staff = #{userId} ,pricing_staff = #{priceStaffId} where id in (
            <foreach collection="list" item="item" separator="," index="index">
                #{item}
            </foreach>
            )
    </update>

    <select id="selectOrder" parameterType="com.dayou.vo.PersonalVO" resultType="com.dayou.vo.PersonalVO">
        SELECT
        p.id,
        p.order_id,
        p.location,
        p.agent,
        p.bailorA,
        p.bailorB,
        p.bailorA_tel,
        p.bailorB_tel,
        p.client_manager_id,
        p.remark,
        pt.acreage,
        pt.price,
        pt.amount,
        ps.created AS valueTiming,
        found.real_amount,
        cc1.NAME AS clientName,
        cc2.NAME AS clientSubName,
        cl1.NAME AS contactName,
        cl1.mobile AS contactTel,
        u.NAME AS clientManager,
        u1.NAME AS outwardStaffName,
        u2.NAME AS inwardStaffName,
        u3.NAME AS pricingStaffName,
        p.created,
        p.created as xCreated,
        pt.productions
        FROM
        personal p
        LEFT JOIN (select personal_id,acreage,price,amount,created,productions from personal_target where deleted = 0) pt ON pt.personal_id = p.id
        LEFT JOIN (
        SELECT
        f.business_id,
        f.real_amount
        FROM
        order_fund f
        WHERE
        f.business_type = 'PERSONAL_BUSINESS'
        AND f.deleted = 0
        ) found ON found.business_id = p.id
        left join (select personal_id, created  from personal_scene) ps on ps.personal_id = p.id
        LEFT JOIN (select id,name from customer_company where deleted=0) cc1 ON cc1.id = p.clientele_id
        LEFT JOIN (select id,name from customer_company where deleted=0) cc2 ON cc2.id = p.clientele_sub_id
        LEFT JOIN (select id,name,mobile from customer_linkman where deleted = 0) cl1 ON cl1.id = p.clientele_contact_id
        LEFT JOIN (select id,name from user where deleted = 0) u ON u.id = p.client_manager_id
        LEFT JOIN (select id,name from user where deleted = 0) u1 ON u1.id = p.outward_staff
        LEFT JOIN (select id,name from user where deleted = 0) u2 ON u2.id = p.inward_staff
        LEFT JOIN (select id,name from user where deleted = 0) u3 ON u3.id = p.pricing_staff
        WHERE
        p.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and (
            p.location like concat ('%',#{keyword},'%')
            or p.order_id like concat('%',#{keyword},'%')
            or p.agent like concat('%',#{keyword},'%')
            or p.bailorA like concat('%',#{keyword},'%')
            or p.bailorB like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or cl1.name like concat('%',#{keyword},'%')
            or p.bailorA_tel like concat('%',#{keyword},'%')
            or p.bailorB_tel like concat('%',#{keyword},'%')
            )
        </if>
        <if test="personal!=null and personal.personalRealAmount!=null and personal.personalRealAmount==true ">
            and (found.real_amount is not null and found.real_amount !=0)
        </if>
        <if test="personal!=null and personal.personalRealAmount!=null and personal.personalRealAmount==false ">
            and (found.real_amount is null or found.real_amount=0)
        </if>
        <if test="personal!=null and personal.startDate!=null and personal.startDate!=''">
            and p.created &gt;= #{personal.startDate}
        </if>
        <if test="personal!=null and personal.endDate!=null and personal.endDate!=''">
            and p.created &lt;= #{personal.endDate}
        </if>
        <if test="personal!=null and personal.clientName!=null and personal.clientName!=''">
            and cc1.NAME like concat('%',#{personal.clientName},'%')
        </if>
        <if test="personal!=null and personal.clientSubName!=null and personal.clientSubName!=''">
            and cc2.NAME like concat('%',#{personal.clientSubName},'%')
        </if>
        <if test="personal!=null and personal.clientManager!=null and personal.clientManager!='' ">
            and u.name like concat('%',#{personal.clientManager},'%')
        </if>
        <if test="personal!=null and personal.outwardStaffName!=null and personal.outwardStaffName!='' ">
            and u1.name like concat('%',#{personal.outwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.inwardStaffName!=null and personal.inwardStaffName!='' ">
            and u2.name like concat('%',#{personal.inwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.pricingStaffName!=null and personal.pricingStaffName!='' ">
            and u3.name like concat('%',#{personal.pricingStaffName},'%')
        </if>
        <if test="personal!=null and personal.handlerName!=null and personal.handlerName!='' ">
            and u4.name like concat('%',#{personal.handlerName},'%')
        </if>
        <if test="personal!=null and personal.productions!=null and personal.productions!='' ">
            and pt.productions like concat('%',#{personal.productions},'%')
        </if>
        order by p.created desc
    </select>

    <select id="getAllOrderCount" parameterType="com.dayou.vo.PersonalVO" resultType="java.lang.Long">
        SELECT
            count(1)
        FROM
        personal p
        LEFT JOIN (select personal_id,acreage,price,amount,created from personal_target where deleted = 0) pt ON pt.personal_id = p.id
        LEFT JOIN (
        SELECT
        f.business_id,
        f.real_amount
        FROM
        order_fund f
        WHERE
        f.business_type = 'PERSONAL_BUSINESS'
        AND f.deleted = 0
        ) found ON found.business_id = p.id
        left join (select personal_id, created  from personal_scene) ps on ps.personal_id = p.id
        LEFT JOIN (select id,name from customer_company where deleted=0) cc1 ON cc1.id = p.clientele_id
        LEFT JOIN (select id,name from customer_company where deleted=0) cc2 ON cc2.id = p.clientele_sub_id
        LEFT JOIN (select id,name,mobile from customer_linkman where deleted = 0) cl1 ON cl1.id = p.clientele_contact_id
        LEFT JOIN (select id,name from user where deleted = 0) u ON u.id = p.client_manager_id
        LEFT JOIN (select id,name from user where deleted = 0) u1 ON u1.id = p.outward_staff
        LEFT JOIN (select id,name from user where deleted = 0) u2 ON u2.id = p.inward_staff
        LEFT JOIN (select id,name from user where deleted = 0) u3 ON u3.id = p.pricing_staff
        WHERE
        p.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and (
            p.location like concat ('%',#{keyword},'%')
            or p.order_id like concat('%',#{keyword},'%')
            or p.agent like concat('%',#{keyword},'%')
            or p.bailorA like concat('%',#{keyword},'%')
            or p.bailorB like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or cl1.name like concat('%',#{keyword},'%')
            or p.bailorA_tel like concat('%',#{keyword},'%')
            or p.bailorB_tel like concat('%',#{keyword},'%')
            )
        </if>
        <if test="personal!=null and personal.personalRealAmount!=null and personal.personalRealAmount==true ">
            and (found.real_amount is not null and found.real_amount !=0)
        </if>
        <if test="personal!=null and personal.personalRealAmount!=null and personal.personalRealAmount==false ">
            and (found.real_amount is null or found.real_amount=0)
        </if>
        <if test="personal!=null and personal.startDate!=null and personal.startDate!=''">
            and p.created &gt;= #{personal.startDate}
        </if>
        <if test="personal!=null and personal.endDate!=null and personal.endDate!=''">
            and p.created &lt;= #{personal.endDate}
        </if>
        <if test="personal!=null and personal.clientName!=null and personal.clientName!=''">
            and cc1.NAME like concat('%',#{personal.clientName},'%')
        </if>
        <if test="personal!=null and personal.clientSubName!=null and personal.clientSubName!=''">
            and cc2.NAME like concat('%',#{personal.clientSubName},'%')
        </if>
        <if test="personal!=null and personal.clientManager!=null and personal.clientManager!='' ">
            and u.name like concat('%',#{personal.clientManager},'%')
        </if>
        <if test="personal!=null and personal.outwardStaffName!=null and personal.outwardStaffName!='' ">
            and u1.name like concat('%',#{personal.outwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.inwardStaffName!=null and personal.inwardStaffName!='' ">
            and u2.name like concat('%',#{personal.inwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.pricingStaffName!=null and personal.pricingStaffName!='' ">
            and u3.name like concat('%',#{personal.pricingStaffName},'%')
        </if>
        <if test="personal!=null and personal.handlerName!=null and personal.handlerName!='' ">
            and u4.name like concat('%',#{personal.handlerName},'%')
        </if>
        order by p.created desc
    </select>
</mapper>
