<?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.MajorMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.Major">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="name" property="name" />
        <result column="order_id" property="orderId" />
        <result column="financial" property="financial" />
        <result column="allot_type" property="allotType" />
        <result column="client_manager_id" property="clientManagerId" />
        <result column="principal_id" property="principalId" />
        <result column="department_id" property="departmentId" />
        <result column="business_object_type" property="businessObjectType" />
        <result column="special_type" property="specialType" />
        <result column="business_gener" property="businessGener" />
        <result column="evaluate_aim" property="evaluateAim" />
        <result column="entrust_again" property="entrustAgain" />
        <result column="multiple_removal" property="multipleRemoval" />
        <result column="informal_evaluate" property="informalEvaluate" />
        <result column="clientele_type" property="clienteleType" />
        <result column="clientele_id" property="clienteleId" />
        <result column="clientele_contact_id" property="clienteleContactId" />
        <result column="dispense_benefit" property="dispenseBenefit" />
        <result column="nonnative" property="nonnative" />
        <result column="refinance" property="refinance" />
        <result column="loan_limit" property="loanLimit" />
        <result column="loan_period" property="loanPeriod" />
        <result column="loan_nature" property="loanNature" />
        <result column="loan_aim" property="loanAim" />
        <result column="loan_expire" property="loanExpire" />
        <result column="bailor" property="bailor" />
        <result column="bailor_address" property="bailorAddress" />
        <result column="bailor_contact_name" property="bailorContactName" />
        <result column="bailor_contact_tel" property="bailorContactTel" />
        <result column="owner" property="owner" />
        <result column="owner_tel" property="ownerTel" />
        <result column="urgent" property="urgent" />
        <result column="terminal_clientele_id" property="terminalClienteleId" />
        <result column="terminal_clientele_type" property="terminalClienteleType" />
        <result column="terminal_clientele_contact_id" property="terminalClienteleContactId" />
        <result column="remark" property="remark" />
        <result column="members" property="members" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        name, order_id, financial, allot_type, client_manager_id, principal_id, department_id, business_object_type, special_type, business_gener,
            evaluate_aim, entrust_again, multiple_removal, informal_evaluate, clientele_type, clientele_id, clientele_contact_id, dispense_benefit,
            nonnative, refinance, loan_limit, loan_period, loan_nature, loan_aim, loan_expire, bailor, bailor_address, bailor_contact_name,
            bailor_contact_tel, owner, owner_tel, urgent, terminal_clientele_id, terminal_clientele_type, terminal_clientele_contact_id, remark,members,
            production_info,printing,printing_remark
    </sql>

    <resultMap id="majorVoMap" type="com.dayou.vo.MajorListVO">
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="name" property="name" />
        <result column="order_id" property="orderId" />
        <result column="financial" property="financial" />
        <result column="allot_type" property="allotType" />
        <result column="client_manager_id" property="clientManagerId" />
        <result column="clientManager" property="clientManager" />
        <result column="principal_id" property="principalId" />
        <result column="principal" property="principal" />
        <result column="department_id" property="departmentId" />
        <result column="departmentName" property="departmentName" />
        <result column="business_object_type" property="businessObjectType" />
        <result column="clientele_id" property="clienteleId" />
        <result column="clientele" property="clientele" />
        <result column="clientele_contact_id" property="clienteleContactId" />
        <result column="clienteleContact" property="clienteleContact" />
        <result column="clienteleContactTel" property="clienteleContactTel" />
        <result column="currentNodeId" property="currentNodeId" />
        <result column="currentNodeName" property="currentNodeName" />
        <result column="cClienteleName" property="cClienteleName" />
        <result column="cClienteleSubName" property="cClienteleSubName" />
        <result column="cClienteleContactName" property="cClienteleContactName" />
        <result column="cMobile" property="cMobile" />
        <collection property="productions"  ofType="com.dayou.vo.MajorProductionVO" select="selectProduction" column="id"/>
    </resultMap>
    <select id="getPage" parameterType="com.dayou.vo.MajorVO" resultMap="majorVoMap">
        SELECT
            DISTINCT
            m.id,
            m.created,
            m.name,
            m.order_id,
            m.financial,
            m.allot_type,
            m.client_manager_id,
            u.name as clientManager,
            m.principal_id,
            u1.name as principal,
            m.department_id,
            d.name as departmentName,
            m.business_object_type,
            m.clientele_id,
            m.clientele_contact_id,
            m.clientele_id as clientele,
            m.clientele_contact_id as clienteleContact,
            cc1.name as cClienteleName,
            cc2.name as cClienteleSubName,
            cl1.name as cClienteleContactName,
            cl1.mobile as cMobile
        FROM
            major m
                left join user u on u.id = m.client_manager_id
                left join user u1 on u1.id = m.principal_id
                left  join department d on d.id = m.department_id
                inner join  ( SELECT id, node_id, business_id, business_sub_id, state FROM work_flow_node_instance WHERE business_type = 'MAJOR_BUSINESS' and deleted = 0 and state='PENDING') wfni
                            ON wfni.business_id = m.id
                left join customer_company cc1 on cc1.id = m.clientele_id
                left join customer_company cc2 on cc2.id = m.clientele_sub_id
                left join customer_linkman cl1 on cl1.id = m.clientele_contact_id
        where m.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and (
            m.name like concat ('%',#{keyword},'%')
            or m.order_id like concat('%',#{keyword},'%')
            or u.name like concat('%',#{keyword},'%')
            or u1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cl1.name like concat('%',#{keyword},'%')
            or cl1.mobile like concat('%',#{keyword},'%')
            or m.business_object_type like concat('%',#{keyword},'%')
            or d.name like concat('%',#{keyword},'%')
            or m.allot_type like concat('%',#{keyword},'%')
            )
        </if>
        <if test="major!=null and major.financial!=null">
            and m.financial = #{major.financial}
        </if>
        <if test="major!=null and major.startDate!=null and major.startDate!=''">
            and m.created &gt;= #{major.startDate}
        </if>
        <if test="major!=null and major.endDate!=null and major.endDate!=''">
            and m.created &lt;= #{major.endDate}
        </if>
        ORDER BY
            created DESC
    </select>

    <select id="selectProduction" parameterType="java.lang.Long" resultType="com.dayou.vo.MajorProductionVO">
        select *,sum(amount) as realAmount from (SELECT
            mp.id,
            mp.report_no,
            mp.name,
            mp.client_name,
            mp.client_tel,
            mp.evaluate_acreage,
            mp.evaluate_amount,
            mp.repertory_state,
            mp.if_save_file,
            wfni.id AS currentNodeId,
            wn.name AS currentNodeName,
            wfni.state,
            mp.major_id,
            pf.real_amount as amount
            FROM
            (select * from major_production where major_id = #{id} and deleted = 0) mp left join
            ( SELECT id, node_id, business_id, business_sub_id, state FROM work_flow_node_instance WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 AND business_id =  #{id} and state='PENDING' ) wfni

            ON ( wfni.business_id = mp.major_id AND mp.report_no = wfni.business_sub_id )
            LEFT JOIN (select id,name from work_node where deleted =0) wn ON wn.id = wfni.node_id
            left join (select business_id,business_sub_id,real_amount,production_type from production_fund where deleted = 0) pf on (pf.business_id = mp.major_id and pf.business_sub_id = mp.report_no and pf.production_type = mp.production)) t GROUP BY report_no
                    order by id DESC
    </select>

    <select id="getDetail" parameterType="java.lang.Long" resultType="com.dayou.vo.MajorVO">
        SELECT
            m.id,
            m.created,
            m.NAME,
            m.order_id,
            m.financial,
            m.allot_type,
            m.client_manager_id,
            m.principal_id,
            m.department_id,
            m.business_object_type,
            m.special_type,
            m.business_gener,
            m.evaluate_aim,
            m.entrust_again,
            m.multiple_removal,
            m.informal_evaluate,
            m.clientele_type,
            m.clientele_id,
            m.clientele_sub_id,
            m.clientele_contact_id,
            m.dispense_benefit,
            m.nonnative,
            m.refinance,
            m.loan_limit,
            m.loan_period,
            m.loan_nature,
            m.loan_aim,
            m.loan_expire,
            m.bailor,
            m.bailor_address,
            m.bailor_contact_name,
            m.bailor_contact_tel,
            m.OWNER,
            m.owner_tel,
            m.urgent,
            m.terminal_clientele_id,
            m.terminal_clientele_type,
            m.terminal_clientele_contact_id,
            m.remark,
            m.members,
            m.feedback_info,
            u.NAME AS clientManager ,
            cc1.name as cClienteleName,
            cc2.name as cClienteleSubName,
            cl1.name as cClienteleContactName,
            cl1.mobile as cMobile,
            cl1.department as cDepartment,
            cl1.duty as cDuty,
            cc3.name as tTerminalClienteleName,
            cc3.phone as tCompanyMobile,
            cl2.name as tTerminalClienteleContactName,
            cl2.mobile as tMobile
        FROM
            major m
                LEFT JOIN user u ON u.id = m.client_manager_id
                left join customer_company cc1 on cc1.id = m.clientele_id
                left join customer_company cc2 on cc2.id = m.clientele_sub_id
                left join customer_linkman cl1 on cl1.id = m.clientele_contact_id
                left join customer_company cc3 on cc3.id = m.terminal_clientele_id
                left join customer_linkman cl2 on cl2.id = m.terminal_clientele_contact_id where m.id =#{id}
    </select>

    <select id="staticsInformation" resultType="com.dayou.dto.MajorStaticsDTO">
        SELECT
            (SELECT count(wfni.id) FROM
                work_task_record wtr
                    LEFT JOIN work_node_task wnt ON wnt.id = wtr.task_id
                    INNER JOIN (
                    SELECT wfni.id FROM
                        work_flow_node_instance wfni
                            LEFT JOIN work_node wn ON wn.id = wfni.node_id
                    WHERE
                        wfni.business_type = 'MAJOR_BUSINESS'
                      AND wfni.state = 'PENDING'
                      AND wfni.deleted = 0
                      AND wn.deleted = 0
                ) wfni ON wtr.instance_id = wfni.id WHERE
                 wtr.handler_id =#{userId}
              AND wnt.deleted = 0
              AND wtr.deleted = 0 ) as majorToDo,
            (SELECT count(*) FROM work_flow_log WHERE handler_id = #{userId} and business_type='MAJOR_BUSINESS') as majorDone,
            ( SELECT count(*) FROM major_production WHERE deleted = 0 AND if_save_file = 1 ) AS saveFileCount,
            ( SELECT  count(*) FROM work_flow_node_instance WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 AND state = 'PENDING' ) AS pending,
            ( SELECT count(*) FROM major_production WHERE deleted = 0 AND production = 'STATEMENT' and repertory_state is not null ) AS statementCount,
            ( SELECT count(*) FROM major_production WHERE deleted = 0 AND production = 'REPORT' and repertory_state is not null ) AS reportCount,
            ( SELECT count(*) FROM major_production WHERE deleted = 0 AND production = 'LETTER' and repertory_state is not null ) AS letterCount,
            ( SELECT count(*) FROM major_production WHERE deleted = 0 AND repertory_state = 0 ) AS inWarehouseCount
    </select>

    <select id="xPage" parameterType="com.dayou.vo.MajorVO" resultType="com.dayou.vo.MajorVO">
        SELECT
        wfni.currentNodeId AS id,
        wfni.nodeName,
        wfni.productionName,
        wfni.business_sub_id AS statementNo,
        wfni.business_min_id AS reportNo,
        m.id AS majorId,
        m.created,
        m.NAME,
        m.order_id,
        m.financial,
        m.allot_type,
        m.client_manager_id,
        u.NAME AS clientManager,
        m.principal_id,
        u1.NAME AS principal,
        m.department_id,
        d.NAME AS department,
        m.business_object_type,
        m.clientele_id,
        m.clientele_contact_id,
        m.clientele_id AS clientele,
        m.clientele_contact_id AS clienteleContact,
        cc1.NAME AS cClienteleName,
        cc2.NAME AS cClienteleSubName,
        cl1.NAME AS cClienteleContactName,
        cl1.mobile AS cMobile,
        wfni.client_name,
        wfni.client_tel,
        wfni.evaluate_amount,
        wfni.OWNER,
        wfni.HANDLER,
        wfni.nodeCode,
        wfni.currentNodeId
        FROM
        major m
        LEFT JOIN user u ON u.id = m.client_manager_id
        LEFT JOIN user u1 ON u1.id = m.principal_id
        LEFT JOIN department d ON d.id = m.department_id
        INNER JOIN (
        SELECT
        wf.id AS currentNodeId,
        wf.node_id,
        wf.business_id,
        wf.business_sub_id,
        wf.business_min_id,
        wf.state,
        wn.NAME AS nodeName,
        mp.NAME AS productionName,
        mp.client_name,
        mp.client_tel,
        mp.evaluate_amount,
        mp.OWNER,
        u.NAME AS HANDLER,
        wn.CODE AS nodeCode,
        mp.client_address
        FROM
        work_flow_node_instance wf
        LEFT JOIN work_node wn ON wn.id = wf.node_id
        LEFT JOIN ( SELECT report_no, major_id, NAME, client_name,client_address, client_tel, evaluate_amount, OWNER FROM major_production WHERE deleted = 0 ) mp ON ( mp.major_id = wf.business_id AND mp.report_no = ifnull( wf.business_min_id, wf.business_sub_id ) )
        LEFT JOIN work_task_record wtr ON wtr.instance_id = wf.id
        LEFT JOIN user u ON u.id = wtr.handler_id
        WHERE
        wf.business_type = 'MAJOR_BUSINESS'
        AND wf.deleted = 0
        AND wf.state = 'PENDING'
        ) wfni ON wfni.business_id = m.id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = m.clientele_sub_id
        LEFT JOIN customer_linkman cl1 ON cl1.id = m.clientele_contact_id
        WHERE
        m.deleted = 0

        <if test="keyword!=null and keyword!='' ">
            and (
            m.name like concat ('%',#{keyword},'%')
            or m.order_id like concat('%',#{keyword},'%')
            or u.name like concat('%',#{keyword},'%')
            or u1.name like concat('%',#{keyword},'%')
            or cc2.name like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cl1.name like concat('%',#{keyword},'%')
            or cl1.mobile like concat('%',#{keyword},'%')
            or m.business_object_type like concat('%',#{keyword},'%')
            or d.name like concat('%',#{keyword},'%')
            or m.allot_type like concat('%',#{keyword},'%')
            or wfni.client_name like concat('%',#{keyword},'%')
            or wfni.productionName like concat('%',#{keyword},'%')
            or wfni.business_sub_id like concat('%',#{keyword},'%')
            or wfni.owner like concat('%',#{keyword},'%')
            or wfni.business_min_id like concat('%',#{keyword},'%')
            or wfni.client_address like concat('%',#{keyword},'%')
            or wfni.productionName like concat('%',#{keyword},'%')
            )
        </if>
        <if test="major!=null and major.financial!=null">
            and m.financial = #{major.financial}
        </if>
        <if test="major!=null and major.startDate!=null and major.startDate!=''">
            and m.created &gt;= #{major.startDate}
        </if>
        <if test="major!=null and major.endDate!=null and major.endDate!=''">
            and m.created &lt;= #{major.endDate}
        </if>
        <if test="major!=null and major.nodeCode!=null and major.nodeCode!=''">
            and wfni.nodeCode = #{major.nodeCode}
        </if>
        <if test="major!=null and major.clientName!=null and major.clientName!=''">
            and wfni.client_name like concat('%',#{major.clientName},'%')
        </if>
        <if test="major!=null and major.clientManagerId!=null ">
            and m.client_manager_id = #{major.clientManagerId}
        </if>
        <if test="major!=null and major.principalId!=null ">
            and m.principal_id = #{major.principalId}
        </if>
        <if test="major!=null and major.businessObjectType!=null and major.businessObjectType!=''">
            and m.business_object_type = #{major.businessObjectType}
        </if>
        <if test="major!=null and major.departmentId!=null ">
            and m.department_id = #{major.departmentId}
        </if>
        <if test="major!=null and major.userIds!=null and major.userIds.size!=0">
           and (
                    m.principal_id  in
                    <foreach collection="major.userIds" open="(" close=")" separator="," item="userId">
                        #{userId}
                    </foreach>
               or
                    m.client_manager_id  in
                    <foreach collection="major.userIds" open="(" close=")" separator="," item="userId">
                        #{userId}
                    </foreach>
               or  JSON_CONTAINS(JSON_EXTRACT(m.members, '$[*]'),CAST(#{currentUserId} AS CHAR))

            )
        </if>
        order by wfni.currentNodeId DESC
    </select>

    <select id="findMajorSubClient" resultType="com.dayou.dto.MajorSubClientDTO">
        SELECT
            m.id,

            m.clientele_sub_id,

            (select id from customer_company where deleted = 0 and parent_id = m.clientele_id and name = cc.NAME limit 1) as realSubId
        FROM
            major m
            LEFT JOIN customer_company cc ON (m.clientele_sub_id = cc.id )
        WHERE
            m.deleted = 0 and cc.level = 1
    </select>

    <select id="productionDetail" resultType="com.dayou.vo.MajorVO">
        SELECT
            m.id,
            mp.report_no as productionNo,
            mp.evaluate_acreage,
            mp.evaluate_amount/10000 as evaluateAmount,
            m.created,
            m.NAME,
            m.order_id,
            m.financial,
            m.allot_type,
            m.client_manager_id,
            m.principal_id,
            m.department_id,
            m.business_object_type,
            m.special_type,
            m.business_gener,
            m.evaluate_aim,
            m.entrust_again,
            m.multiple_removal,
            m.informal_evaluate,
            m.clientele_type,
            m.clientele_id,
            m.clientele_sub_id,
            m.clientele_contact_id,
            m.dispense_benefit,
            m.nonnative,
            m.refinance,
            m.loan_limit,
            m.loan_period,
            m.loan_nature,
            m.loan_aim,
            m.loan_expire,
            m.bailor,
            m.bailor_address,
            m.bailor_contact_name,
            m.bailor_contact_tel,
            m.OWNER,
            m.owner_tel,
            m.urgent,
            m.terminal_clientele_id,
            m.terminal_clientele_type,
            m.terminal_clientele_contact_id,
            m.remark,
            m.members,
            m.feedback_info,
            u.NAME AS clientManager ,
            cc1.name as cClienteleName,
            cc2.name as cClienteleSubName,
            cl1.name as cClienteleContactName,
            cl1.mobile as cMobile,
            cl1.department as cDepartment,
            cl1.duty as cDuty,
            cc3.name as tTerminalClienteleName,
            cc3.phone as tCompanyMobile,
            cl2.name as tTerminalClienteleContactName,
            cl2.mobile as tMobile
        FROM
            major m
                LEFT JOIN user u ON u.id = m.client_manager_id
                left join (select major_id,report_no, evaluate_acreage,evaluate_amount from major_production where major_id = #{id} and report_no = #{productionNo} and deleted=0) mp on mp.major_id = m.id
                left join customer_company cc1 on cc1.id = m.clientele_id
                left join customer_company cc2 on cc2.id = m.clientele_sub_id
                left join customer_linkman cl1 on cl1.id = m.clientele_contact_id
                left join customer_company cc3 on cc3.id = m.terminal_clientele_id
                left join customer_linkman cl2 on cl2.id = m.terminal_clientele_contact_id where m.id =#{id}

    </select>
</mapper>
