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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.CommissionDeclare">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="business_type" property="businessType" />
        <result column="commission_rate_id" property="commissionRateId" />
        <result column="business_id" property="businessId" />
        <result column="production_id" property="productionId" />
        <result column="declare_user_id" property="declareUserId" />
        <result column="declare_result" property="declareResult" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        business_type, commission_rate_id, business_id, production_id, declare_user_id, declare_result
    </sql>

    <select id="majorMarketDeclarePage" parameterType="com.dayou.vo.CommissionDeclareVO" resultType="com.dayou.vo.CommissionDeclareVO">
        SELECT
        (@i := @i + 1) AS id,
        bus.*,
        dd.NAME AS commissionType
        FROM
        (
        SELECT
        IFNULL( cd.id,( SELECT id FROM commission_declare WHERE deleted = 0 AND business_id = major.businessId AND production_id IS NULL
        AND business_type ='COMMISSION_DECLARE_MAJOR_MARKET' and declare_user_id =#{marketUserId}) ) cId,
        IFNULL( cd.commission_rate_id,( SELECT commission_rate_id FROM commission_declare WHERE deleted = 0 AND business_id = major.businessId AND production_id IS NULL
        AND business_type ='COMMISSION_DECLARE_MAJOR_MARKET' and declare_user_id =#{marketUserId} ) ) commission_rate_id,
        IFNULL( cd.declare_result,( SELECT declare_result FROM commission_declare WHERE deleted = 0 AND business_id = major.businessId AND production_id IS NULL
        AND business_type ='COMMISSION_DECLARE_MAJOR_MARKET' and declare_user_id =#{marketUserId}) ) declare_result,
        major.*
        FROM
        (
        SELECT
        m.id AS businessId,
        mp.id AS productionId,
        ifnull( mp.id, m.id ) AS mId,
        m.NAME AS orderName,
        m.order_id,
        m.business_object_type,
        m.bailor_contact_name,
        m.bailor_contact_tel,
        m.bailor,
        m.created AS orderCreated,
        u.NAME AS clientManager,
        u1.NAME AS principal,
        cc1.NAME AS clienteleName,
        cc2.NAME AS clienteleSubName,
        mp.NAME,
        mp.report_no,
        mp.created,
        (case mp.production when 'STATEMENT' THEN '意见书' when 'LETTER' THEN '复评函' else '报告' end ) production
        FROM
        (SELECT @i := 0) AS sort,
        major m
        LEFT JOIN ( SELECT id, NAME, major_id, report_no, created,production FROM major_production WHERE deleted = 0 ) mp ON m.id = mp.major_id
        LEFT JOIN `user` u ON u.id = m.client_manager_id
        LEFT JOIN `user` u1 ON u1.id = m.principal_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = m.clientele_sub_id
        WHERE
        m.deleted = 0
        AND m.client_manager_id = #{marketUserId}

        ) major
        LEFT JOIN (
        SELECT
        id,
        ifnull( production_id, business_id ) AS xId,
        commission_rate_id,
        declare_result
        FROM
        commission_declare
        WHERE
        deleted = 0
        AND business_type='COMMISSION_DECLARE_MAJOR_MARKET'
        AND declare_user_id = #{marketUserId}) cd ON major.mId = cd.xId) bus left join business_commission_rate bcr on
        bus.commission_rate_id = bcr.id
        LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                            <where>
                                <if test="declare!=null and declare.orderId!=null and declare.orderId!=''">
                                        and bus.order_id like concat ('%',#{declare.orderId},'%')
                                </if>
                                <if test="declare!=null and declare.name!=null and declare.name!=''">
                                    and (bus.orderName like concat ('%',#{declare.name},'%') or bus.name like concat ('%',#{declare.name},'%') )
                                </if>
                                <if test="declare!=null and declare.reportNo!=null and declare.reportNo!=''">
                                    and bus.report_no like concat ('%',#{declare.reportNo},'%')
                                </if>
                                <if test="declare!=null and declare.commissionRateId!=null ">
                                    and bus.commission_rate_id = #{declare.commissionRateId}
                                </if>
                                <if test="declare!=null and declare.declareType!=null and declare.declareType==true">
                                    and bus.productionId is not null
                                </if>
                                <if test="declare!=null and declare.declareType!=null and declare.declareType==false">
                                    and bus.productionId is null
                                </if>
                                <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='待申报'">
                                    and bus.declare_result is null
                                </if>
                                <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核中'">
                                    and bus.declare_result = #{declare.declareResult}
                                </if>
                                <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核通过'">
                                    and bus.declare_result = #{declare.declareResult}
                                </if>
                                <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核拒绝'">
                                    and bus.declare_result = #{declare.declareResult}
                                </if>
                            </where>
        order by bus.created DESC ,bus.orderCreated DESC
    </select>

    <sql id="majorMarketDeclarePageByDepartmentSql">
        SELECT
        (@i := @i + 1) AS id,
        bus.*,
        dd.NAME AS commissionType
        FROM
        (
        SELECT
        IFNULL( cd.id,( SELECT id FROM commission_declare WHERE deleted = 0 AND business_id = major.businessId AND production_id IS NULL
        AND business_type ='COMMISSION_DECLARE_MAJOR_MARKET') ) cId,
        IFNULL( cd.commission_rate_id,( SELECT commission_rate_id FROM commission_declare WHERE deleted = 0 AND business_id = major.businessId AND production_id IS NULL
        AND business_type ='COMMISSION_DECLARE_MAJOR_MARKET') ) commission_rate_id,
        IFNULL( cd.declare_result,( SELECT declare_result FROM commission_declare WHERE deleted = 0 AND business_id = major.businessId AND production_id IS NULL
        AND business_type ='COMMISSION_DECLARE_MAJOR_MARKET') ) declare_result,
        major.*,
        cd.created AS cdCreated
        FROM
        (
        SELECT
        m.id AS businessId,
        mp.id AS productionId,
        ifnull( mp.id, m.id ) AS mId,
        m.NAME AS orderName,
        m.order_id,
        m.business_object_type,
        m.bailor_contact_name,
        m.bailor_contact_tel,
        m.bailor,
        m.created AS orderCreated,
        u.NAME AS clientManager,
        u1.NAME AS principal,
        cc1.NAME AS clienteleName,
        cc2.NAME AS clienteleSubName,
        mp.NAME,
        mp.report_no,
        mp.created,
        (case mp.production when 'STATEMENT' THEN '意见书' when 'LETTER' THEN '复评函' else '报告' end ) production,
        m.client_manager_id
        FROM
        (SELECT @i := 0) AS sort,
        major m
        LEFT JOIN ( SELECT id, NAME, major_id, report_no, created,production FROM major_production WHERE deleted = 0 ) mp ON m.id = mp.major_id
        LEFT JOIN `user` u ON u.id = m.client_manager_id
        LEFT JOIN `user` u1 ON u1.id = m.principal_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = m.clientele_sub_id
        WHERE
        m.deleted = 0
        ) major
        LEFT JOIN (
        SELECT
        id,
        ifnull( production_id, business_id ) AS xId,
        commission_rate_id,
        declare_result,
        created
        FROM
        commission_declare
        WHERE
        deleted = 0
        AND business_type='COMMISSION_DECLARE_MAJOR_MARKET'
        ) cd ON major.mId = cd.xId) bus left join business_commission_rate bcr on
        bus.commission_rate_id = bcr.id
        LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
        LEFT JOIN (SELECT DISTINCT department.id, department.name, user_post.user_id FROM user_post,post,department WHERE user_post.post_id = post.id AND post.department_id = department.id AND department.`name` IN ('市场一部','市场二部','资产评估事业部')) AS department ON department.user_id = client_manager_id
        WHERE department.id = #{departmentId}
        <if test="declare != null and declare.startTime != null and declare.endTime!= null">
            AND cdCreated BETWEEN #{declare.startTime} AND #{declare.endTime}
        </if>
        <if test="declare!=null and declare.orderId!=null and declare.orderId!=''">
            and bus.order_id like concat ('%',#{declare.orderId},'%')
        </if>
        <if test="declare!=null and declare.name!=null and declare.name!=''">
            and (bus.orderName like concat ('%',#{declare.name},'%') or bus.name like concat ('%',#{declare.name},'%') )
        </if>
        <if test="declare!=null and declare.reportNo!=null and declare.reportNo!=''">
            and bus.report_no like concat ('%',#{declare.reportNo},'%')
        </if>
        <if test="declare!=null and declare.commissionRateId!=null ">
            and bus.commission_rate_id = #{declare.commissionRateId}
        </if>
        <if test="declare!=null and declare.declareType!=null and declare.declareType==true">
            and bus.productionId is not null
        </if>
        <if test="declare!=null and declare.declareType!=null and declare.declareType==false">
            and bus.productionId is null
        </if>
        <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='待申报'">
            and bus.declare_result is null
        </if>
        <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核中'">
            and bus.declare_result = #{declare.declareResult}
        </if>
        <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核通过'">
            and bus.declare_result = #{declare.declareResult}
        </if>
        <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核拒绝'">
            and bus.declare_result = #{declare.declareResult}
        </if>
        order by bus.created DESC ,bus.orderCreated DESC
    </sql>

    <select id="majorMarketDeclarePageByDepartment" resultType="com.dayou.vo.CommissionDeclareVO">
        <include refid="majorMarketDeclarePageByDepartmentSql" />
    </select>

    <select id="majorMarketDeclarePageByDepartmentExport" resultType="com.dayou.vo.CommissionDeclareVO">
        <include refid="majorMarketDeclarePageByDepartmentSql" />
    </select>

    <select id="majorEvaluateDeclarePage" parameterType="com.dayou.vo.CommissionDeclareVO" resultType="com.dayou.vo.CommissionDeclareVO">
        select
        (@i := @i + 1) AS id,bus.*,dd.name as commissionType from (SELECT
        IFNULL(cd.id,(SELECT id from commission_declare where deleted = 0 and business_id= major.businessId and production_id is null
        AND business_type ='COMMISSION_DECLARE_MAJOR_EVALUATE') ) cId,
        IFNULL(cd.commission_rate_id,(SELECT commission_rate_id from commission_declare where deleted = 0 and business_id= major.businessId and production_id is null
        AND business_type ='COMMISSION_DECLARE_MAJOR_EVALUATE') ) commission_rate_id,
        IFNULL(cd.declare_result,(SELECT declare_result from commission_declare where deleted = 0 and business_id= major.businessId and production_id is null
        AND business_type ='COMMISSION_DECLARE_MAJOR_EVALUATE') ) declare_result,
        major.*
        FROM
        (
        SELECT
        m.id AS businessId,
        mp.id AS productionId,
        ifnull( mp.id, m.id ) AS mId,
        m.NAME AS orderName,
        m.order_id,
        m.business_object_type,
        m.bailor_contact_name,
        m.bailor_contact_tel,
        m.bailor,
        m.created AS orderCreated,
        u.NAME AS clientManager,
        u1.NAME AS principal,
        cc1.NAME AS clienteleName,
        cc2.NAME AS clienteleSubName,
        mp.NAME,
        mp.report_no ,
        mp.created,
        (case mp.production when 'STATEMENT' THEN '意见书' when 'LETTER' THEN '复评函' else '报告' end ) production,
        m.principal_id
        FROM (SELECT @i := 0) AS sort,
        major m
        LEFT JOIN ( SELECT id, NAME, major_id, report_no,created,production FROM major_production WHERE deleted = 0 ) mp ON m.id = mp.major_id
        LEFT JOIN user u ON u.id = m.client_manager_id
        LEFT JOIN user u1 ON u1.id = m.principal_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = m.clientele_sub_id
        WHERE
        m.deleted = 0
        ) major
        LEFT JOIN ( SELECT id, ifnull( production_id, business_id ) AS xId, commission_rate_id,declare_result FROM commission_declare
        WHERE deleted = 0
        AND business_type='COMMISSION_DECLARE_MAJOR_EVALUATE'
        ) cd ON major.mId = cd.xId) bus left join business_commission_rate bcr on
        bus.commission_rate_id = bcr.id left join dict_data dd on dd.id = bcr.business_cate_id
        LEFT JOIN (SELECT DISTINCT department.id, department.name, user_post.user_id FROM user_post,post,department WHERE user_post.post_id = post.id AND post.department_id = department.id AND department.`name` IN ('评估一部','评估二部','评估三部')) AS department ON department.user_id = principal_id
        <where>
            <if test="declare!=null and declare.orderId!=null and declare.orderId!=''">
                and bus.order_id like concat ('%',#{declare.orderId},'%')
            </if>
            <if test="declare!=null and declare.name!=null and declare.name!=''">
                and (bus.orderName like concat ('%',#{declare.name},'%') or bus.name like concat ('%',#{declare.name},'%') )
            </if>
            <if test="declare!=null and declare.reportNo!=null and declare.reportNo!=''">
                and bus.report_no like concat ('%',#{declare.reportNo},'%')
            </if>
            <if test="declare!=null and declare.commissionRateId!=null ">
                and bus.commission_rate_id = #{declare.commissionRateId}
            </if>
            <if test="declare!=null and declare.declareType!=null and declare.declareType==true">
                and bus.productionId is not null
            </if>
            <if test="declare!=null and declare.declareType!=null and declare.declareType==false">
                and bus.productionId is null
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='待申报'">
                and bus.declare_result is null
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核中'">
                and bus.declare_result = #{declare.declareResult}
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核通过'">
                and bus.declare_result = #{declare.declareResult}
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核拒绝'">
                and bus.declare_result = #{declare.declareResult}
            </if>
        </where>
        order by bus.created DESC ,bus.orderCreated DESC
    </select>

    <sql id="majorEvaluateDeclarePageByDepartmentSql">
        select
        (@i := @i + 1) AS id,bus.*,dd.name as commissionType from (SELECT
        IFNULL(cd.id,(SELECT id from commission_declare where deleted = 0 and business_id= major.businessId and production_id is null
        AND business_type ='COMMISSION_DECLARE_MAJOR_EVALUATE') ) cId,
        IFNULL(cd.commission_rate_id,(SELECT commission_rate_id from commission_declare where deleted = 0 and business_id= major.businessId and production_id is null
        AND business_type ='COMMISSION_DECLARE_MAJOR_EVALUATE') ) commission_rate_id,
        IFNULL(cd.declare_result,(SELECT declare_result from commission_declare where deleted = 0 and business_id= major.businessId and production_id is null
        AND business_type ='COMMISSION_DECLARE_MAJOR_EVALUATE') ) declare_result,
        major.*,
        cd.created AS cdCreated
        FROM
        (
        SELECT
        m.id AS businessId,
        mp.id AS productionId,
        ifnull( mp.id, m.id ) AS mId,
        m.NAME AS orderName,
        m.order_id,
        m.business_object_type,
        m.bailor_contact_name,
        m.bailor_contact_tel,
        m.bailor,
        m.created AS orderCreated,
        u.NAME AS clientManager,
        u1.NAME AS principal,
        cc1.NAME AS clienteleName,
        cc2.NAME AS clienteleSubName,
        mp.NAME,
        mp.report_no ,
        mp.created,
        (case mp.production when 'STATEMENT' THEN '意见书' when 'LETTER' THEN '复评函' else '报告' end ) production,
        m.principal_id
        FROM (SELECT @i := 0) AS sort,
        major m
        LEFT JOIN ( SELECT id, NAME, major_id, report_no,created,production FROM major_production WHERE deleted = 0 ) mp ON m.id = mp.major_id
        LEFT JOIN user u ON u.id = m.client_manager_id
        LEFT JOIN user u1 ON u1.id = m.principal_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = m.clientele_sub_id
        WHERE
        m.deleted = 0
        ) major
        LEFT JOIN ( SELECT id, ifnull( production_id, business_id ) AS xId, commission_rate_id,declare_result,created FROM commission_declare
        WHERE deleted = 0
        AND business_type='COMMISSION_DECLARE_MAJOR_EVALUATE'
        ) cd ON major.mId = cd.xId) bus left join business_commission_rate bcr on
        bus.commission_rate_id = bcr.id left join dict_data dd on dd.id = bcr.business_cate_id
        LEFT JOIN (SELECT DISTINCT department.id, department.name, user_post.user_id FROM user_post,post,department WHERE user_post.post_id = post.id AND post.department_id = department.id AND department.`name` IN ('评估一部','评估二部','评估三部')) AS department ON department.user_id = principal_id
        WHERE department.id = #{departmentId}
        <if test="declare != null and declare.startTime != null and declare.endTime!= null">
            AND cdCreated BETWEEN #{declare.startTime} AND #{declare.endTime}
        </if>
        <if test="declare!=null and declare.orderId!=null and declare.orderId!=''">
            and bus.order_id like concat ('%',#{declare.orderId},'%')
        </if>
        <if test="declare!=null and declare.name!=null and declare.name!=''">
            and (bus.orderName like concat ('%',#{declare.name},'%') or bus.name like concat ('%',#{declare.name},'%') )
        </if>
        <if test="declare!=null and declare.reportNo!=null and declare.reportNo!=''">
            and bus.report_no like concat ('%',#{declare.reportNo},'%')
        </if>
        <if test="declare!=null and declare.commissionRateId!=null ">
            and bus.commission_rate_id = #{declare.commissionRateId}
        </if>
        <if test="declare!=null and declare.declareType!=null and declare.declareType==true">
            and bus.productionId is not null
        </if>
        <if test="declare!=null and declare.declareType!=null and declare.declareType==false">
            and bus.productionId is null
        </if>
        <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='待申报'">
            and bus.declare_result is null
        </if>
        <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核中'">
            and bus.declare_result = #{declare.declareResult}
        </if>
        <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核通过'">
            and bus.declare_result = #{declare.declareResult}
        </if>
        <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核拒绝'">
            and bus.declare_result = #{declare.declareResult}
        </if>
        order by bus.created DESC ,bus.orderCreated DESC
    </sql>

    <!--根据部门id查询大中型提成申报信息-->
    <select id="majorEvaluateDeclarePageByDepartment" resultType="com.dayou.vo.CommissionDeclareVO">
        <include refid="majorEvaluateDeclarePageByDepartmentSql" />
    </select>

    <!--根据部门id查询大中型提成申报信息导出excel-->
    <select id="majorEvaluateDeclarePageByDepartmentExport" resultType="com.dayou.vo.CommissionDeclareVO">
        <include refid="majorEvaluateDeclarePageByDepartmentSql" />
    </select>

    <select id="detail" parameterType="java.lang.Long" resultType="com.dayou.vo.CommissionDeclareVO">
        SELECT
            cd.id,
            cd.commission_rate_id,
            cd.business_id,
            cd.production_id,
            cd.created,
            cd.declare_result,
            dd.NAME AS businessCate,
            u.NAME AS declareUser,
            bcr.business_cate_id,
            bcr.business_type,
            cd.created,
            cd.ratio,
            cd.defined_low_limit_amount,
            cd.business_type AS declareBusinessType
        FROM
            commission_declare cd
                LEFT JOIN business_commission_rate bcr ON bcr.id = cd.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN user u ON u.id = cd.declare_user_id
            where cd.deleted = 0 and bcr.deleted = 0 and cd.id =#{id}
    </select>

    <resultMap id="majorDeclareTodoCheckMap" type="com.dayou.vo.CommissionDeclareTodoCheckVO">
        <id property="id" column="id"></id>
        <result property="taskId" column="taskId"></result>
        <result property="currentNodeId" column="currentNodeId"></result>
        <result property="businessId" column="business_id"></result>
        <result property="businessType" column="business_type"></result>
        <result property="orderName" column="orderName"></result>
        <result property="name" column="name"></result>
        <result property="reportNo" column="report_no"></result>
        <result property="orderId" column="order_id"></result>
        <result property="nodeName" column="nodeName"></result>
        <result property="nodeCode" column="nodeCode"></result>
        <result property="handler" column="handler"></result>
        <result property="declareUser" column="declareUser"></result>
        <result property="businessCate" column="businessCate"></result>
        <result property="userType" column="userType"></result>
        <result property="ratio" column="ratio"></result>
        <result property="created" column="created"></result>
        <result property="prodId" column="prodId"></result>
        <result property="majorId" column="majorId"></result>
        <association property="majorSimple" column="{majorId=majorId,prodId=prodId}" javaType="com.dayou.dto.MajorDeclareSimpleDTO"
                     select="selectMajorSimple" ></association>
    </resultMap>
    <sql id="majorDeclareTodoCheck">
        SELECT
            t.*,
            u.NAME AS HANDLER,
            u1.NAME AS declareUser,
            dd.NAME AS businessCate,
            ( CASE bcr.user_type WHEN 'MARKETER' THEN '市场人员' WHEN 'EVALUATOR' THEN '评估人员' ELSE '--' END ) AS userType,
            bcr.min_ratio AS ratio
        FROM
            (
                SELECT
                    wfni.id,
                    wtr.id AS taskId,
                    wfni.id AS currentNodeId,
                    wfni.business_id,
                    wfni.business_type,
                    wfni.NAME AS nodeName,
                    wfni.CODE AS nodeCode,
                    cd.majorId,
                    cd.production_id as prodId,
                    cd.id AS cId,
                    wtr.handler_id,
                    cd.commission_rate_id,
                    cd.declare_user_id,
                    cd.created
                FROM
                    work_task_record wtr
                        INNER JOIN (
                        SELECT
                            wn.NAME,
                            wn.CODE,
                            wfni.id,
                            wfni.business_id,
                            wfni.state,
                            wfni.business_type
                        FROM
                            work_flow_node_instance wfni
                                LEFT JOIN work_node wn ON wn.id = wfni.node_id
                        WHERE
                                wfni.business_type IN ( 'COMMISSION_DECLARE_MAJOR_MARKET', 'COMMISSION_DECLARE_MAJOR_EVALUATE' )
                          AND wfni.state = 'PENDING'
                          AND wfni.deleted = 0
                          AND wn.deleted = 0
                    ) wfni ON wtr.instance_id = wfni.id
                        INNER JOIN ( SELECT id, production_id, business_id AS majorId, commission_rate_id, declare_result, declare_user_id, created FROM commission_declare WHERE deleted = 0 AND declare_result = '审核中' ) cd ON cd.id = wfni.business_id
                WHERE
                    wtr.handler_id = #{checkVO.handlerId}
            ) t
                LEFT JOIN user u ON u.id = t.handler_id
                LEFT JOIN business_commission_rate bcr ON bcr.id = t.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN user u1 ON u1.id = t.declare_user_id
        <where>
            <if test="checkVO!=null and checkVO.businessCate!=null and checkVO.businessCate!='' ">
                and dd.NAME = #{checkVO.businessCate}
            </if>
        </where>
    </sql>

    <select id="selectMajorSimple" resultType="com.dayou.dto.MajorDeclareSimpleDTO">
        <if test="majorId!=null and prodId!=null">
            SELECT
            m.id as majorId,
            m.NAME AS orderName,
            m.order_id as orderId,
            mp.NAME as name,
            mp.report_no as reportNo,
            mp.id as prodId
            FROM
            major m
            LEFT JOIN ( SELECT id, name, major_id, report_no FROM major_production WHERE deleted = 0 ) mp ON
            m.id = mp.major_id
            where m.id = #{majorId} and mp.id= #{prodId}
            <if test="checkVO!=null and checkVO.orderId!=null and checkVO.orderId!='' ">
                and m.order_id like concat('%',#{checkVO.orderId},'%')
            </if>
            <if test="checkVO!=null and checkVO.orderName!=null and checkVO.orderName!='' ">
                and m.NAME like concat('%',#{checkVO.orderName},'%')
            </if>
            <if test="checkVO!=null and checkVO.reportNo!=null and checkVO.reportNo!='' ">
                and mp.report_no like concat('%',#{checkVO.reportNo},'%')
            </if>
        </if>
        <if test="majorId!=null and prodId==null">
            SELECT
            m.id as majorId,
            m.NAME AS orderName,
            m.order_id as orderId
            FROM
            major m
            where m.id = #{majorId}
            <if test="checkVO!=null and checkVO.orderId!=null and checkVO.orderId!='' ">
                and m.order_id like concat('%',#{checkVO.orderId},'%')
            </if>
            <if test="checkVO!=null and checkVO.orderName!=null and checkVO.orderName!='' ">
                and m.NAME like concat('%',#{checkVO.orderName},'%')
            </if>

        </if>
    </select>
    <select id="todoCheckPage" parameterType="com.dayou.vo.CommissionDeclareTodoCheckVO"  resultMap="majorDeclareTodoCheckMap">
        <include refid="majorDeclareTodoCheck"/>
    </select>

    <select id="todoCheckList" resultMap="majorDeclareTodoCheckMap">
        <include refid="majorDeclareTodoCheck"/>
        order by t.created DESC
        limit 8
    </select>

    <!--更新大中型绩效申报-->
    <update id="updateMajorCommissionDeclare">
        UPDATE commission_declare
        SET commission_rate_id = #{declareDTO.commissionRateId}
        WHERE id = #{declareDTO.id}
    </update>

    <!--获取资产评估人员提成申报列表-->
    <select id="assetsEvaluateDeclarePage" resultType="com.dayou.vo.CommissionDeclareVO">
        select bus.*,dd.name as commissionType from (
        SELECT
        IFNULL(
        cd.id,
        ( SELECT id FROM commission_declare WHERE deleted = 0 AND business_id = assets.businessId AND production_id IS
        NULL AND declare_user_id =#{evaluateUserId} AND business_type ='COMMISSION_DECLARE_ASSET_EVALUATE')) id,
        IFNULL(
        cd.commission_rate_id,
        ( SELECT commission_rate_id FROM commission_declare WHERE deleted = 0 AND business_id = assets.businessId AND
        production_id IS NULL AND declare_user_id =#{evaluateUserId} AND business_type
        ='COMMISSION_DECLARE_ASSET_EVALUATE')) commission_rate_id,
        IFNULL(
        cd.declare_result,
        ( SELECT declare_result FROM commission_declare WHERE deleted = 0 AND business_id = assets.businessId AND
        production_id IS NULL AND declare_user_id =#{evaluateUserId} AND business_type
        ='COMMISSION_DECLARE_ASSET_EVALUATE')) declare_result,
        assets.*
        FROM
        (
        SELECT
        assets.id AS businessId,
        ap.id AS productionId,
        ifnull( ap.id, assets.id ) AS aId,
        assets.name AS orderName,
        assets.order_id,
        assets.bailor_contact_name,
        assets.bailor_contact_tel,
        assets.bailor,
        assets.created AS orderCreated,
        u.name AS clientManager,
        u1.name AS principal,
        cc1.name AS clienteleName,
        cc2.name AS clienteleSubName,
        ap.production_no AS reportNo,
        ap.created
        FROM
        assets
        LEFT JOIN ( SELECT id, business_id, production_no, create_production_date, created FROM assets_production WHERE deleted = 0 AND production_type != 'STATEMENT' ) ap ON assets.id = ap.business_id
        LEFT JOIN `user` u ON u.id = assets.client_manager_id
        LEFT JOIN `user` u1 ON u1.id = assets.principal_id
        LEFT JOIN customer_company cc1 ON cc1.id = assets.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = assets.clientele_sub_id
        WHERE
        assets.deleted = 0
        AND assets.principal_id = #{evaluateUserId}
        ) assets
        LEFT JOIN (
        SELECT
        id,
        ifnull( production_id, business_id ) AS xId,
        commission_rate_id,
        declare_result
        FROM
        commission_declare
        WHERE
        deleted = 0
        AND declare_user_id = #{evaluateUserId}) cd ON assets.aId = cd.xId) bus
        left join business_commission_rate bcr on bus.commission_rate_id = bcr.id
        left join dict_data dd on dd.id = bcr.business_cate_id
        <where>
            <if test="declare!=null and declare.orderId!=null and declare.orderId!=''">
                and bus.order_id like concat ('%',#{declare.orderId},'%')
            </if>
            <if test="declare!=null and declare.name!=null and declare.name!=''">
                and bus.orderName like concat ('%',#{declare.name},'%')
            </if>
            <if test="declare!=null and declare.reportNo!=null and declare.reportNo!=''">
                and bus.reportNo like concat ('%',#{declare.reportNo},'%')
            </if>
            <if test="declare!=null and declare.commissionRateId!=null ">
                and bus.commission_rate_id = #{declare.commissionRateId}
            </if>
            <if test="declare!=null and declare.declareType!=null and declare.declareType==true">
                and bus.productionId is not null
            </if>
            <if test="declare!=null and declare.declareType!=null and declare.declareType==false">
                and bus.productionId is null
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='待申报'">
                and bus.declare_result is null
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核中'">
                and bus.declare_result = #{declare.declareResult}
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核通过'">
                and bus.declare_result = #{declare.declareResult}
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核拒绝'">
                and bus.declare_result = #{declare.declareResult}
            </if>
        </where>
        order by bus.created DESC ,bus.orderCreated DESC
    </select>

    <!--获取资产市场人员提成申报列表-->
    <select id="assetsMarketDeclarePage" resultType="com.dayou.vo.CommissionDeclareVO">
        SELECT
        bus.*,
        dd.name AS commissionType
        FROM
        (
        SELECT
        IFNULL( cd.id,( SELECT id FROM commission_declare WHERE deleted = 0 AND business_id = assets.businessId AND
        production_id IS NULL AND declare_user_id =#{marketUserId} AND business_type
        ='COMMISSION_DECLARE_ASSET_MARKET') ) id,
        IFNULL( cd.commission_rate_id,( SELECT commission_rate_id FROM commission_declare WHERE deleted = 0 AND
        business_id = assets.businessId AND production_id IS NULL and declare_user_id =#{marketUserId} AND business_type
        ='COMMISSION_DECLARE_ASSET_MARKET') ) commission_rate_id,
        IFNULL( cd.declare_result,( SELECT declare_result FROM commission_declare WHERE deleted = 0 AND business_id =
        assets.businessId AND production_id IS NULL and declare_user_id =#{marketUserId} AND business_type
        ='COMMISSION_DECLARE_ASSET_MARKET') ) declare_result,
        assets.*
        FROM
        (
        SELECT
        assets.id AS businessId,
        ap.id AS productionId,
        ifnull( ap.id, assets.id ) AS aId,
        assets.name AS orderName,
        assets.order_id,
        assets.bailor_contact_name,
        assets.bailor_contact_tel,
        assets.bailor,
        assets.created AS orderCreated,
        u.name AS clientManager,
        u1.name AS principal,
        cc1.name AS clienteleName,
        cc2.name AS clienteleSubName,
        ap.production_no AS reportNo,
        ap.created,
        ap.delivery
        FROM
        assets
        LEFT JOIN ( SELECT id, business_id, production_no, create_production_date, delivery,created FROM assets_production WHERE production_type!='STATEMENT' AND deleted = 0 ) ap ON assets.id = ap.business_id
        LEFT JOIN `user` u ON u.id = assets.client_manager_id
        LEFT JOIN `user` u1 ON u1.id = assets.principal_id
        LEFT JOIN customer_company cc1 ON cc1.id = assets.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = assets.clientele_sub_id
        WHERE
        assets.deleted = 0
        AND assets.client_manager_id = #{marketUserId}
        ) assets
        LEFT JOIN (
        SELECT
        id,
        ifnull( production_id, business_id ) AS xId,
        commission_rate_id,
        declare_result
        FROM
        commission_declare
        WHERE
        deleted = 0
        AND declare_user_id = #{marketUserId}) cd ON assets.aId = cd.xId) bus left join business_commission_rate bcr on
        bus.commission_rate_id = bcr.id
        LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
        <where>
            <if test="declare!=null and declare.orderId!=null and declare.orderId!=''">
                and bus.order_id like concat ('%',#{declare.orderId},'%')
            </if>
            <if test="declare!=null and declare.name!=null and declare.name!=''">
                and bus.orderName like concat ('%',#{declare.name},'%')
            </if>
            <if test="declare!=null and declare.reportNo!=null and declare.reportNo!=''">
                and bus.reportNo like concat ('%',#{declare.reportNo},'%')
            </if>
            <if test="declare!=null and declare.commissionRateId!=null ">
                and bus.commission_rate_id = #{declare.commissionRateId}
            </if>
            <if test="declare!=null and declare.declareType!=null and declare.declareType==true">
                and bus.productionId is not null
            </if>
            <if test="declare!=null and declare.declareType!=null and declare.declareType==false">
                and bus.productionId is null
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='待申报'">
                and bus.declare_result is null
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核中'">
                and bus.declare_result = #{declare.declareResult}
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核通过'">
                and bus.declare_result = #{declare.declareResult}
            </if>
            <if test="declare!=null and declare.declareResult!=null and declare.declareResult=='审核拒绝'">
                and bus.declare_result = #{declare.declareResult}
            </if>
        </where>
        order by bus.created DESC ,bus.orderCreated DESC
    </select>

    <!--资产绩效提成申报待办result-->
    <resultMap id="assetsDeclareTodoCheckMap" type="com.dayou.vo.CommissionDeclareTodoCheckVO">
        <id property="id" column="id"></id>
        <result property="taskId" column="taskId"></result>
        <result property="currentNodeId" column="currentNodeId"></result>
        <result property="businessId" column="business_id"></result>
        <result property="businessType" column="business_type"></result>
        <result property="orderName" column="orderName"></result>
        <result property="name" column="name"></result>
        <result property="reportNo" column="report_no"></result>
        <result property="orderId" column="order_id"></result>
        <result property="nodeName" column="nodeName"></result>
        <result property="nodeCode" column="nodeCode"></result>
        <result property="handler" column="handler"></result>
        <result property="declareUser" column="declareUser"></result>
        <result property="businessCate" column="businessCate"></result>
        <result property="userType" column="userType"></result>
        <result property="ratio" column="ratio"></result>
        <result property="created" column="created"></result>
        <result property="prodId" column="prodId"></result>
        <result property="assetsId" column="assetsId"></result>
        <association property="assetsSimple" column="{assetsId=assetsId,prodId=prodId}" javaType="com.dayou.dto.AssetsDeclareSimpleDTO"
                     select="selectAssetsSimple" ></association>
    </resultMap>

    <!--查询资产绩效提成申报流程信息-->
    <sql id="getAssetsTodoCheck">
        SELECT
            t.*,
            u.name AS handler,
            u1.name AS declareUser,
            dd.name AS businessCate,
            ( CASE bcr.user_type WHEN 'MARKETER' THEN '市场人员' WHEN 'EVALUATOR' THEN '评估人员' ELSE '--' END ) AS userType,
            bcr.min_ratio AS ratio
        FROM
            (
                SELECT
                    wfni.id,
                    wtr.id AS taskId,
                    wfni.id AS currentNodeId,
                    wfni.business_id,
                    wfni.business_type,
                    wfni.name AS nodeName,
                    wfni.CODE AS nodeCode,
                    cd.assetsId,
                    cd.production_id as prodId,
                    cd.id AS cId,
                    wtr.handler_id,
                    cd.commission_rate_id,
                    cd.declare_user_id,
                    cd.created
                FROM
                    work_task_record wtr
                        INNER JOIN (
                        SELECT
                            wn.name,
                            wn.CODE,
                            wfni.id,
                            wfni.business_id,
                            wfni.state,
                            wfni.business_type
                        FROM
                            work_flow_node_instance wfni
                                LEFT JOIN work_node wn ON wn.id = wfni.node_id
                        WHERE
                            wfni.business_type IN ( 'COMMISSION_DECLARE_ASSET_MARKET', 'COMMISSION_DECLARE_ASSET_EVALUATE' )
                          AND wfni.state = 'PENDING'
                          AND wfni.deleted = 0
                          AND wn.deleted = 0
                    ) wfni ON wtr.instance_id = wfni.id
                        INNER JOIN (
                        SELECT id, production_id, business_id AS assetsId, commission_rate_id, declare_result, declare_user_id, created
                        FROM commission_declare
                        WHERE deleted = 0 AND declare_result = '审核中'
                    ) cd ON cd.id = wfni.business_id
                WHERE
                    wtr.handler_id = #{checkVO.handlerId}
            ) t
                LEFT JOIN user u ON u.id = t.handler_id
                LEFT JOIN business_commission_rate bcr ON bcr.id = t.commission_rate_id
                LEFT JOIN dict_data dd ON dd.id = bcr.business_cate_id
                LEFT JOIN user u1 ON u1.id = t.declare_user_id
    </sql>

    <!--查询资产绩效提成申报关联项目信息-->
    <select id="selectAssetsSimple" resultType="com.dayou.dto.AssetsDeclareSimpleDTO">
        <if test="assetsId!=null and prodId!=null">
            SELECT
            assets.id as assetsId,
            assets.name AS orderName,
            assets.order_id as orderId,
            ap.production_no as reportNo,
            ap.id as prodId
            FROM
            assets
            LEFT JOIN ( SELECT id, business_id, production_no FROM assets_production WHERE deleted = 0 ) ap ON
            assets.id = ap.business_id
            where assets.id = #{assetsId} and ap.id= #{prodId}
        </if>
        <if test="assetsId!=null and prodId==null">
            SELECT
            assets.id as assetsId,
            assets.name AS orderName,
            assets.order_id as orderId
            FROM
            assets
            where assets.id = #{assetsId}
        </if>
    </select>

    <!--资产待办绩效申报列表-->
    <select id="assetsTodoCheckPage" resultType="com.dayou.vo.CommissionDeclareTodoCheckVO" resultMap="assetsDeclareTodoCheckMap">
        <include refid="getAssetsTodoCheck" />
    </select>

    <!--资产待办绩效申报集合-->
    <select id="assetsTodoCheckList" resultType="com.dayou.vo.CommissionDeclareTodoCheckVO" resultMap="assetsDeclareTodoCheckMap">
        <include refid="getAssetsTodoCheck" />
        order by t.created DESC
        LIMIT 8
    </select>

    <!--更新资产绩效申报提成比列与下限信息-->
    <update id="updateAssetsCommissionDeclare">
        UPDATE commission_declare
        SET ratio = #{declareDTO.ratio},
            defined_low_limit_amount = #{declareDTO.definedLowLimitAmount}
        WHERE id = #{declareDTO.id}
    </update>

</mapper>
