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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.MajorProduction">
        <result column="id" property="id" />
        <result column="major_id" property="majorId" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="report_no" property="reportNo" />
        <result column="production" property="production" />
        <result column="signatory" property="signatory" />
        <result column="name" property="name" />
        <result column="client_name" property="clientName" />
        <result column="client_tel" property="clientTel" />
        <result column="client_address" property="clientAddress" />
        <result column="owner" property="owner" />
        <result column="value_timing" property="valueTiming" />
        <result column="evaluate_acreage" property="evaluateAcreage" />
        <result column="evaluate_amount" property="evaluateAmount" />
        <result column="evaluate_price" property="evaluatePrice" />
        <result column="comment" property="comment" />
        <result column="qr_code" property="qrCode" />
        <result column="repertory_state" property="repertoryState" />
        <result column="first_check" property="firstCheck" />
        <result column="second_check" property="secondCheck" />
        <result column="third_check" property="thirdCheck" />
        <result column="save_file_check" property="saveFileCheck" />
        <result column="save_file_second_check" property="saveFileSecondCheck" />
        <result column="if_save_file" property="ifSaveFile" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        major_id,
        deleted,
        created,
        modified,
        report_no, production,signatory, name, client_name, client_tel, client_address, owner, value_timing, evaluate_acreage, evaluate_amount, evaluate_price
            , comment,qr_code,repertory_state,first_check,second_check,third_check,save_file_check,save_file_second_check,if_save_file
    </sql>

    <select id="getList" parameterType="com.dayou.entity.MajorProduction" resultType="com.dayou.entity.MajorProduction">
    select * from (
        SELECT
        mp.id,
        mp.major_id,
        (SELECT id FROM work_flow_node_instance
                   WHERE business_id = #{production.majorId}
                         AND (business_sub_id = mp.report_no or business_min_id = mp.report_no ) and  deleted = 0
                                                    and sequence = ( #{production.sequence} + 1) ) AS nextInstanceNodeId ,
        mp.report_no,
        mp.production,
        mp.signatory,
        mp.`name`,
        mp.client_name,
        mp.client_tel,
        mp.client_address,
        mp.`owner`,
        mp.value_timing,
        mp.evaluate_acreage,
        (mp.evaluate_amount/10000) as evaluateAmount,
        mp.evaluate_price,
        mp.`comment`,
        mp.qr_code,
        mp.repertory_state,
        mp.first_check,
        mp.second_check,
        mp.third_check,
        mp.save_file_check,
        mp.save_file_second_check,
        mp.if_save_file,
        mp.file_path,
        m.`name` AS majorName,
        mp.checked_file_path,
        mp.validate_code,
        mp.is_submit,
        mp.is_record,
        mp.main_worker,
        mp.main_worker_no,
        mp.minor_worker,
        mp.minor_worker_no,
        mp.small_worker,
        mp.small_worker_no,
        mp.work_start_date,
        mp.work_end_date,
        mp.work_purpose,
        mp.not_record_reason,
        mp.not_submit_reason
        FROM
            major_production mp
                LEFT JOIN major m ON m.id = mp.major_id
        WHERE
            mp.major_id = #{production.majorId}
          <if test="production!=null and production.afterProduction==null
            and production.statementNo==null and production.reportNo!=null and production.reportNo!=''  ">
              and mp.report_no = #{production.reportNo}
          </if>
        <if test="production!=null and production.afterProduction==null
            and production.statementNo!=null and production.statementNo!='' and production.reportNo==null  ">
            and mp.report_no = #{production.statementNo}
        </if>
        <if test="production!=null and production.afterProduction!=null and  production.afterProduction=='REPORT' and production.reportNo!=null and production.reportNo!=''  ">
            and mp.report_no in (select report_no from major_target where major_id =  #{production.majorId} and statement_no =#{production.reportNo} and deleted = 0)
        </if>
        <if test="production!=null and production.afterProduction!=null and  production.afterProduction=='LETTER' and production.reportNo!=null and production.reportNo!=''  ">
            and mp.report_no in (select letter_no from major_target where major_id =  #{production.majorId} and statement_no =#{production.reportNo} and deleted = 0)
        </if>
          <if test="production!=null and production.production!=null and production.production!=''  ">
              and mp.production = #{production.production}
          </if>
          <if test="production!=null and production.afterProduction==null and production.statementNo!=null
                    and production.statementNo!='' and production.reportNo!=null and production.reportNo!=''">
              and mp.report_no in (#{production.statementNo},#{production.reportNo})
          </if>
          and mp.deleted = 0 and m.deleted = 0
        order by mp.created ASC ) t where nextInstanceNodeId is null
    </select>

    <select id="getPendingList" parameterType="com.dayou.entity.MajorProduction" resultType="com.dayou.entity.MajorProduction">
        SELECT
        mp.id,
        mp.major_id,
        mp.report_no,
        mp.production,
        mp.signatory,
        mp.`name`,
        mp.client_name,
        mp.client_tel,
        mp.client_address,
        mp.`owner`,
        mp.value_timing,
        mp.evaluate_acreage,
        (mp.evaluate_amount/10000) as evaluateAmount,
        mp.evaluate_price,
        mp.`comment`,
        mp.qr_code,
        mp.repertory_state,
        mp.first_check,
        mp.second_check,
        mp.third_check,
        mp.save_file_check,
        mp.save_file_second_check,
        mp.if_save_file,
        mp.file_path,
        m.`name` AS majorName,
        mp.checked_file_path,
        mp.validate_code,
        mp.is_submit,
        mp.is_record,
        mp.main_worker,
        mp.main_worker_no,
        mp.minor_worker,
        mp.minor_worker_no,
        mp.small_worker,
        mp.small_worker_no,
        mp.work_start_date,
        mp.work_end_date,
        mp.work_purpose
        FROM
        major_production mp
        LEFT JOIN major m ON m.id = mp.major_id
        WHERE
        mp.major_id = #{production.majorId}
        <if test="production!=null and production.afterProduction==null
            and production.statementNo==null and production.reportNo!=null and production.reportNo!=''  ">
            and mp.report_no = #{production.reportNo}
        </if>
        <if test="production!=null and production.afterProduction==null
            and production.statementNo!=null and production.statementNo!='' and production.reportNo==null  ">
            and mp.report_no = #{production.statementNo}
        </if>
        <if test="production!=null and production.afterProduction==null and production.statementNo!=null
                    and production.statementNo!='' and production.reportNo!=null and production.reportNo!=''">
            and mp.report_no in (#{production.statementNo},#{production.reportNo})
        </if>
        and mp.deleted = 0 and m.deleted = 0
        order by mp.created ASC
    </select>

    <select id="saveDonePage" parameterType="com.dayou.vo.MajorProductionSaveDoneVO" resultType="com.dayou.vo.MajorProductionSaveDoneVO">
        SELECT
            mp.id,
            mp.name,
            mp.major_id,
            mp.report_no,
            m.order_id,
            m.name as orderName,
            u1.name as clientManager,
            u2.name as principal,
            m.business_object_type,
            mp.production,
            mp.repertory_state,
            mp.save_file_date,
            pf.real_amount
        FROM
            major_production mp
                LEFT JOIN major m ON m.id = mp.major_id
                LEFT JOIN user u1 ON u1.id = m.client_manager_id
                LEFT JOIN user u2 ON u2.id = m.principal_id
                left join (select business_id,production_no,real_amount, production_type from production_fund where business_type='MAJOR_BUSINESS' and deleted = 0) pf
                on (pf.business_id = mp.major_id and pf.production_no =mp.report_no and mp.production = pf.production_type)
        where mp.deleted = 0 and  m.deleted = 0 and mp.if_save_file= 1
        <if test="keyword!=null and keyword!='' ">
            and (
            mp.name like concat('%',#{keyword},'%')
            or m.order_id like concat('%',#{keyword},'%')
            or m.name like concat('%',#{keyword},'%')
            or u1.NAME like concat('%',#{keyword},'%')
            or u2.NAME like concat('%',#{keyword},'%')
            or mp.report_no like concat('%',#{keyword},'%')
            )
        </if>
        <if test="done!=null and done.production!=null and done.production!=''">
            and mp.production =#{done.production}
        </if>
        <if test="done!=null and done.startDate!=null and done.startDate!=''">
            and mp.save_file_date &gt;= #{done.startDate}
        </if>
        <if test="done!=null and done.endDate!=null and done.endDate!=''">
            and mp.save_file_date &lt;= #{done.endDate}
        </if>
        <if test="done!=null and done.hasCommission == true">
            and pf.real_amount > 0
        </if>
        <if test="done!=null and done.hasCommission == false">
            and (pf.real_amount IS NULL OR pf.real_amount = 0)
        </if>
        order by mp.save_file_date desc
    </select>

    <select id="getHouse" parameterType="com.dayou.vo.MajorProductionVO" resultType="com.dayou.vo.MajorProductionVO">
        select
        (@i :=  @i + 1) AS id,
        temp.* from (SELECT
        '大中型' AS businessType,
        mp.report_no AS reportNo,
        ( CASE mp.production WHEN 'STATEMENT' THEN '价值意见书' WHEN 'REPORT' THEN '报告' ELSE '复评函' END ) production,
        mp.repertory_state,
        mp.repertory_in_time,
        u.name as clientManager
        FROM
        major_production mp left join major m on m.id = mp.major_id
        left join user u on u.id = m.client_manager_id
        WHERE
        mp.deleted = 0
        AND mp.repertory_state = 0 UNION ALL
        SELECT
        '资产' AS business_type,
        ap.production_no AS reportNo,
        ( CASE ap.production_type WHEN 'STATEMENT' THEN '价值意见书' WHEN 'REPORT' THEN '报告' WHEN 'CONSULT' THEN '咨询报告' WHEN 'TECHNIC' THEN '技术报告' WHEN 'FINAL' THEN '结果报告' ELSE '复评函' END ) production,
        ap.repertory_state,
        ap.repertory_in_time,
        u.name as clientManager
        FROM
        assets_production ap left join assets a on a.id = ap.business_id
        left join user u on u.id = a.client_manager_id
        WHERE
        ap.deleted = 0
        AND ap.repertory_state = 0 UNION ALL
        SELECT
        '个贷' AS business_type,
        p.order_id AS reportNo,
        ( CASE pp.production WHEN 'STATEMENT' THEN '价值意见书' WHEN 'REPORT' THEN '报告' WHEN 'CONSULT' THEN '咨询报告' WHEN 'TECHNIC' THEN '技术报告' WHEN 'FINAL' THEN '结果报告' ELSE '复评函' END ) production,
        pp.repertory_state,
        pp.repertory_in_time,
        u.name as clientManager
        FROM
        personal_production pp
        LEFT JOIN personal_target pt ON pt.id = pp.target_id
        LEFT JOIN personal p ON p.id = pt.personal_id
        left join user u on u.id = p.client_manager_id
        WHERE
        p.deleted = 0
        AND pp.repertory_state = 0
        AND pt.deleted = 0
        AND pp.deleted = 0) temp
        <where>
            <if test="vo!=null and vo.reportNo!=null and vo.reportNo!=''">
                and temp.reportNo =#{vo.reportNo}
            </if>
            <if test="vo!=null and vo.production!=null and vo.production!=''">
                and temp.production =#{vo.production}
            </if>
            <if test="vo!=null and vo.startDate!=null and vo.startDate!=''">
                and temp.repertory_in_time &gt;= #{vo.startDate}
            </if>
            <if test="vo!=null and vo.endDate!=null and vo.endDate!=''">
                and temp.repertory_in_time &lt;= #{vo.endDate}
            </if>
        </where>
        order by temp.repertory_in_time desc
    </select>

    <resultMap id="myOrderMap" type="com.dayou.vo.MajorOrderVO">
        <result column="id" property="id" />
        <result column="businessId" property="businessId" />
        <result column="order_id" property="orderId" />
        <result column="orderName" property="orderName" />
        <result column="production" property="production" />
        <result column="productionNo" property="productionNo" />
        <result column="evaluate_amount" property="evaluateAmount" />
        <result column="if_save_file" property="ifSaveFile" />
        <result column="principal" property="principal" />
        <result column="clientManager" property="clientManager" />
        <result column="clienteleName" property="clienteleName" />
        <result column="clienteleSubName" property="clienteleSubName" />
        <result column="bailor" property="bailor" />
        <result column="clienteleContactName" property="clienteleContactName" />
        <result column="owner" property="owner" />
        <result column="orderFundId" property="orderFundId" />
        <result column="should_amount" property="shouldAmount" />
        <result column="real_amount" property="realAmount" />
        <result column="shouldAmount" property="shouldAmount" />
        <result column="orderShouldAmount" property="orderShouldAmount" />
        <result column="orderRealAmount" property="orderRealAmount" />
        <result column="productionFundId" property="productionFundId" />
        <result column="created" property="created" />
        <result column="remark" property="remark" />
        <result column="invoiceAmount" property="invoiceAmount" />
        <result column="delivery" property="delivery" />
        <result column="mDeclareResult" property="mDeclareResult" />
        <result column="eDeclareResult" property="eDeclareResult" />
        <association property="currentNodeName" javaType="java.lang.String"
                     select="queryCurrentNodeName" column="{businessId=businessId,productionNo=productionNo}"/>
    </resultMap>

    <select id="queryCurrentNodeName" resultType="java.lang.String">
        select name from work_node where id = (
        select node_id from work_flow_node_instance where
        business_type='MAJOR_BUSINESS'
        and state= 'PENDING'
        AND deleted = 0
        and business_id = #{businessId}
            <if test="productionNo!=null and productionNo!=''">
              and (business_min_id = #{productionNo} or business_sub_id = #{productionNo}) order by id desc limit 1
            </if>
            <if test="productionNo==null">
                and business_sub_id is null and business_min_id is null
            </if>
        )
    </select>

    <sql id="myMajorOrder">
        SELECT
            mp.id AS id,
            m.id as businessId,
            m.order_id,
            m.name as orderName,
            mp.report_no as productionNo,
            mp.production,
            mp.if_save_file,
            mp.evaluate_amount,
            u.name as principal,
            u1.name as clientManager,
            cc.name as clienteleName,
            cc1.name as clienteleSubName,
            cl.name as clienteleContactName,
            m.bailor,
            m.owner,
            m.created,
            orf.id as orderFundId,
            orf.should_amount as orderShouldAmount,
            orf.real_amount as orderRealAmount,
            pf.remark,
            pf.id as productionFundId,
            pf.real_amount,
            pf.production_should_amount as shouldAmount,
            fi.invoiceAmount,
            mp.delivery,
            ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) as mDeclareResult,
            ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) as eDeclareResult
        FROM
            major m
                left join (select id,major_id,report_no,evaluate_amount,production,if_save_file,delivery from major_production where deleted = 0 ) mp on mp.major_id = m.id
                left join user u on u.id = m.principal_id
                left join user u1 on u1.id = m.client_manager_id
                left join customer_company cc on cc.id = m.clientele_id
                left join customer_company cc1 on cc1.id = m.clientele_sub_id
                left join customer_linkman cl on  cl.id = m.clientele_contact_id
                left join (select * from order_fund where business_type ='MAJOR_BUSINESS' and deleted = 0) orf on orf.business_id = m.id
                left join (select * from production_fund where business_type = 'MAJOR_BUSINESS' and deleted = 0) pf on (pf.business_id = m.id and pf.production_no = mp.report_no and mp.production = pf.production_type)
                left join (select sum(real_amount) as invoiceAmount,order_fund_id,production_fund_id from finance_invoice where state='已开票' and deleted = 0 group by production_fund_id) fi on (fi.order_fund_id = orf.id and fi.production_fund_id = pf.id)

    </sql>

    <sql id="myOrderSql">
        <include refid="myMajorOrder" />
        where m.deleted = 0
        and m.client_manager_id = #{vo.clientManagerId}
        <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 cc.name like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cl.name like concat('%',#{keyword},'%')
            or m.bailor like concat('%',#{keyword},'%')
            or m.owner like concat('%',#{keyword},'%')
            or orf.should_amount like concat('%',#{keyword},'%')
            or orf.real_amount like concat('%',#{keyword},'%')
            or orf.remark like concat('%',#{keyword},'%')
            or mp.report_no like concat('%',#{keyword},'%')
            )
        </if>
        <if test="vo!=null and vo.principalId!=null and vo.principalId!=''">
            AND m.principal_id like concat('%',#{vo.principalId},'%')
        </if>
        <if test="vo!=null and vo.orderId!=null and vo.orderId!=''">
            AND m.order_id like concat('%',#{vo.orderId},'%')
        </if>
        <if test="vo!=null and vo.productionNo!=null and vo.productionNo!=''">
            AND mp.report_no like concat('%',#{vo.productionNo},'%')
        </if>
        <if test="vo!=null and vo.clienteleName!=null and vo.clienteleName!=''">
            AND cc.name like concat('%',#{vo.clienteleName},'%')
        </if>
        <if test="vo!=null and vo.clienteleSubName!=null and vo.clienteleSubName!=''">
            AND cc1.name like concat('%',#{vo.clienteleSubName},'%')
        </if>
        <if test="vo!=null and vo.startDate!=null and vo.startDate!=''">
            and m.created &gt;= #{vo.startDate}
        </if>
        <if test="vo!=null and vo.endDate!=null and vo.endDate!=''">
            and m.created &lt;= #{vo.endDate}
        </if>
        <if test="vo!=null and vo.production!=null and vo.production!='ORDER'">
            AND mp.production = #{vo.production}
        </if>
        <if test="vo!=null and vo.production!=null and vo.production=='ORDER'">
            AND mp.id is null
        </if>
        <if test="vo!=null and vo.delivery == true">
            AND mp.delivery = #{vo.delivery}
        </if>
        <if test="vo!=null and vo.delivery == false">
            AND (mp.delivery = #{vo.delivery} OR mp.delivery IS NULL)
        </if>
        <if test="vo!=null and vo.hasRealAmount == true">
            AND (pf.real_amount IS NOT NULL OR pf.real_amount > 0)
        </if>
        <if test="vo!=null and vo.hasRealAmount == false">
            AND (pf.real_amount IS NULL OR pf.real_amount = 0)
        </if>
        <if test="vo!=null and vo.hasShouldAmount == true">
            AND (orf.should_amount IS NOT NULL OR orf.should_amount > 0 OR pf.production_should_amount IS NOT NULL OR pf.production_should_amount > 0)
        </if>
        <if test="vo!=null and vo.hasShouldAmount == false">
            AND (orf.should_amount IS NULL OR orf.should_amount = 0)
            AND (pf.production_should_amount IS NULL OR pf.production_should_amount = 0)
        </if>
        order by m.created DESC
    </sql>

    <select id="myOrder" parameterType="com.dayou.vo.MajorOrderVO" resultMap="myOrderMap">
        <include refid="myOrderSql" />
    </select>

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

    <select id="waitingClaim" parameterType="com.dayou.vo.MajorOrderVO" resultType="com.dayou.vo.MajorOrderVO">
        <include refid="myMajorOrder" />
        where m.deleted = 0
        and m.client_manager_id = #{vo.clientManagerId} and orf.id is not null
        <if test="vo!=null and vo.name!=null and vo.name !=''">
            and m.name like concat ('%',#{vo.name},'%')
        </if>
        <if test="vo!=null and vo.orderId!=null and vo.orderId !=''">
            and m.order_id like concat ('%',#{vo.orderId},'%')
        </if>
        <if test="vo!=null and vo.mDeclareResult!=null and vo.mDeclareResult =='审核通过'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) = '审核通过'
        </if>
        <if test="vo!=null and vo.mDeclareResult!=null and vo.mDeclareResult =='审核拒绝'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) = '审核拒绝'
        </if>
        <if test="vo!=null and vo.mDeclareResult!=null and vo.mDeclareResult =='审核中'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) = '审核中'
        </if>
        <if test="vo!=null and vo.mDeclareResult!=null and vo.mDeclareResult =='未申报'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) is null
        </if>
        <if test="vo!=null and vo.eDeclareResult!=null and vo.eDeclareResult =='审核通过'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) = '审核通过'
        </if>
        <if test="vo!=null and vo.eDeclareResult!=null and vo.eDeclareResult =='审核拒绝'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) = '审核拒绝'
        </if>
        <if test="vo!=null and vo.eDeclareResult!=null and vo.eDeclareResult =='审核中'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) = '审核中'
        </if>
        <if test="vo!=null and vo.eDeclareResult!=null and vo.eDeclareResult =='未申报'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id IS NULL AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) is null
        </if>
        order by m.created DESC
    </select>

    <select id="getMajorProductionAboutFundId" parameterType="java.lang.Long" resultType="com.dayou.entity.MajorProduction">
        SELECT
            mp.id,
            pf.id AS productionFundId,
            sum(pf.real_amount) as realAmount,
            mp.production,
            mp.NAME,
            mp.client_name,
            mp.OWNER,
            mp.evaluate_acreage,
            mp.evaluate_amount,
            mp.evaluate_price,
            mp.report_no,
            mp.repertory_state,
            mp.repertory_in_time,
            mp.repertory_out_time,
            mp.if_save_file,
            mp.save_file_date,
            mp.delivery,
            `of`.should_amount AS orderShouldAmount,
            pf.production_should_amount as shouldAmount,
            m.order_id,
            m.name as orderName,
            m.id as businessId,
            `of`.id as orderFundId
        FROM
            major_production mp
                left join major m on m.id =mp.major_id
                LEFT JOIN ( SELECT id, business_id, production_no, real_amount, production_type,production_should_amount FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf ON ( mp.major_id = pf.business_id AND mp.report_no = pf.production_no AND mp.production = pf.production_type )
        LEFT JOIN (SELECT id,business_id,should_amount FROM order_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0) AS `of` ON mp.major_id = `of`.business_id
        WHERE
            mp.major_id = #{majorId} and mp.deleted = 0

          AND mp.NAME IS NOT NULL
        GROUP BY mp.id
        ORDER BY mp.id DESC
    </select>

    <select id="todoSaveFilePage" parameterType="com.dayou.vo.TaskTodoVO" resultType="com.dayou.vo.TaskTodoVO">
        SELECT
        wfni.id,
        mp.id AS productionId,
        mp.NAME,
        mp.repertory_state,
        wtr.id AS taskId,
        wfni.id AS currentNodeId,
        wfni.business_min_id,
        wfni.business_sub_id,
        mp.report_no,
        wfni.business_id,
        m.NAME AS orderName,
        m.order_id,
        u1.NAME AS clientManager,
        u2.NAME AS principal,
        d.NAME AS departmentName,
        wfni.NAME AS nodeName,
        wfni.CODE AS nodeCode,
        u.NAME AS HANDLER,
        pf.real_amount AS realAmount,
        wtr.handler_id,
        wtr.finished AS taskFinished,
        DATE_FORMAT( m.created, '%Y-%m-%d' ) AS created,
        wtr.created AS taskCreated,
        mp.production
        FROM
        work_task_record wtr
        LEFT JOIN work_node_task wnt ON wnt.id = wtr.task_id
        INNER JOIN (
        SELECT
        wn.NAME,
        wn.CODE,
        wfni.id,
        wfni.business_id,
        wfni.business_sub_id,
        wfni.business_min_id,
        wfni.state
        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
        AND wn.CODE = 'BUSINESS_ARCHIVING'
        ) wfni ON wtr.instance_id = wfni.id
        LEFT JOIN major m ON m.id = wfni.business_id
        LEFT JOIN user u ON u.id = wtr.handler_id
        LEFT JOIN user u1 ON u1.id = m.client_manager_id
        LEFT JOIN user u2 ON u2.id = m.principal_id
        LEFT JOIN department d ON d.id = m.department_id
        LEFT JOIN ( SELECT id, major_id, report_no, NAME, repertory_state, production FROM major_production WHERE deleted = 0 ) mp
        ON ( mp.major_id = m.id AND (mp.report_no =  wfni.business_sub_id or mp.report_no = wfni.business_min_id))
        LEFT JOIN ( SELECT business_id, production_no, real_amount FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) pf
        ON ( pf.business_id = m.id AND pf.production_no = mp.report_no  )
        WHERE u.deleted = 0
          and  wtr.handler_id =#{todoVO.handlerId}
          AND wnt.deleted = 0
          AND wtr.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and (
            mp.name like concat('%',#{keyword},'%')
            or m.order_id like concat('%',#{keyword},'%')
            or m.name like concat('%',#{keyword},'%')
            or wfni.business_min_id like concat('%',#{keyword},'%')
            or u1.NAME like concat('%',#{keyword},'%')
            or u2.NAME like concat('%',#{keyword},'%')
            )
        </if>
        <if test="todoVO!=null and todoVO.production!=null and todoVO.production!=''">
            and mp.production =#{todoVO.production}
        </if>
        ORDER BY
            mp.report_no ASC
    </select>

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

    <select id="performanceAllotPage" parameterType="com.dayou.vo.MajorProductionPerformanceVO" resultType="com.dayou.vo.MajorProductionPerformanceVO">
        SELECT
        mp.id,
        m.order_id,
        m.id as majorId,
        mp.NAME,
        mp.report_no,
        (case mp.production when 'REPORT' then '报告' when 'STATEMENT' THEN '价值意见书' else '复评函' end ) as production,
        pf.real_amount AS claimAmount,
        allot.major_production_id AS isAllot
        FROM
        major_production mp
        left join (select business_id,production_no,production_type,real_amount from production_fund where business_type = 'MAJOR_BUSINESS' and deleted = 0 ) pf on (mp.major_id = pf.business_id and mp.report_no = pf.production_no and mp.production = pf.production_type)
        LEFT JOIN major m ON m.id = mp.major_id
        LEFT JOIN (SELECT major_production_id FROM major_production_allot WHERE deleted = 0 and user_type = 'EVALUATE' GROUP BY major_production_id) AS allot ON major_production_id = mp.id
        WHERE
        m.deleted = 0
        AND mp.deleted = 0
        AND mp.NAME IS NOT NULL
          and m.department_id in (
        <foreach collection="vo.departmentIds" open="(" close=")" separator="," item="departmentId">
            #{departmentId}
        </foreach>
        )
        <if test="keyword!=null and keyword!='' ">
            and (
            mp.name like concat('%',#{keyword},'%')
            or m.order_id like concat('%',#{keyword},'%')
            or mp.report_no like concat('%',#{keyword},'%')
            )
        </if>
        <if test="vo!=null and vo.production != null">
            AND mp.production = #{vo.production}
        </if>
        <if test="vo!=null and vo.hasRealAmount == true">
            AND (pf.real_amount IS NOT NULL OR pf.real_amount > 0)
        </if>
        <if test="vo!=null and vo.hasRealAmount == false">
            AND (pf.real_amount IS NULL OR pf.real_amount = 0)
        </if>
        <if test="vo!=null and vo.hasAllot == true">
            AND allot.major_production_id IS NOT NULL
        </if>
        <if test="vo!=null and vo.hasAllot == false">
            AND allot.major_production_id IS NULL
        </if>

        order by mp.id DESC, isAllot ASC
    </select>

    <select id="validateProd" parameterType="java.lang.Long" resultType="com.dayou.vo.ProdValidateVO">
        SELECT
            mp.evaluate_acreage as acreage,
            ROUND(mp.evaluate_amount/10000,2) as amount,
            mp.name AS productionName,
            mp.report_no AS productionNo ,
            mp.client_name as bailor
        FROM
            major_production mp
        WHERE
            mp.id = #{id}
          AND mp.deleted = 0
    </select>

    <select id="waitingClaimProd" parameterType="com.dayou.vo.MajorProductionVO" resultType="com.dayou.vo.MajorProductionVO">
        SELECT
            m.id as majorId,
            m.order_id,
            m.financial,
            mp.id,
            mp.name,
            mp.report_no,
            mp.standard_amount,
            (case mp.production when 'STATEMENT' THEN '意见书' when 'REPORT' THEN '报告' when 'LETTER' THEN '复评函' else '' end) production,
            u.NAME AS principal,
            u1.NAME AS clientManager,
            cc.NAME AS clienteleName,
            orf.id AS orderFundId,
            orf.should_amount as orderShouldAmount,
            off.production_should_amount as shouldAmount,
            off.id as productionFundId,
            off.real_amount,
            fi.invoiceAmount,
        ifnull(( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ),
        ( SELECT declare_result FROM commission_declare WHERE business_id = m.id  and production_id is null AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 )) as mDeclareResult,
        ifnull(( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ),
        ( SELECT declare_result FROM commission_declare WHERE business_id = m.id  and production_id is null AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 )) as eDeclareResult
        FROM
            major_production mp
                left join major m on m.id = mp.major_id
                INNER JOIN ( SELECT id,business_id,should_amount FROM order_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) orf ON orf.business_id = m.id
                LEFT JOIN ( SELECT id,business_id,production_no,real_amount,standard_amount,production_should_amount,production_type FROM production_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 ) off  ON (off.business_id = m.id and off.production_no = mp.report_no and off.production_type = mp.production)
                LEFT JOIN user u ON u.id = m.principal_id
                LEFT JOIN user u1 ON u1.id = m.client_manager_id
                LEFT JOIN customer_company cc ON cc.id = m.clientele_id
        left join (select sum(real_amount) as invoiceAmount,order_fund_id,production_fund_id from finance_invoice where state='已开票' and deleted = 0 GROUP BY production_fund_id) fi on (fi.order_fund_id = orf.id and fi.production_fund_id = off.id)
        WHERE
            m.deleted = 0
          AND	mp.deleted = 0
          AND m.client_manager_id = #{vo.clientManagerId}
        <if test="vo!=null and vo.hasRealAmount!=null and vo.hasRealAmount==true">
            and off.real_amount IS NOT NULL
        </if>
        <if test="vo!=null and vo.hasRealAmount!=null and vo.hasRealAmount==false">
            and off.real_amount IS NULL
        </if>
        <if test="vo!=null and vo.name!=null and vo.name !=''">
            and mp.name like concat ('%',#{vo.name},'%')
        </if>
        <if test="vo!=null and vo.orderId!=null and vo.orderId !=''">
            and m.order_id like concat ('%',#{vo.orderId},'%')
        </if>
        <if test="vo!=null and vo.reportNo!=null and vo.reportNo !=''">
            and mp.report_no like concat ('%',#{vo.reportNo},'%')
        </if>
        <if test="vo!=null and vo.mDeclareResult!=null and vo.mDeclareResult =='审核通过'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id  AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) = '审核通过'
        </if>
        <if test="vo!=null and vo.mDeclareResult!=null and vo.mDeclareResult =='审核拒绝'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id  AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) = '审核拒绝'
        </if>
        <if test="vo!=null and vo.mDeclareResult!=null and vo.mDeclareResult =='审核中'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id  AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) = '审核中'
        </if>
        <if test="vo!=null and vo.mDeclareResult!=null and vo.mDeclareResult =='未申报'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id  AND business_type = 'COMMISSION_DECLARE_MAJOR_MARKET' AND deleted = 0 ) is null
        </if>
        <if test="vo!=null and vo.eDeclareResult!=null and vo.eDeclareResult =='审核通过'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id  AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) = '审核通过'
        </if>
        <if test="vo!=null and vo.eDeclareResult!=null and vo.eDeclareResult =='审核拒绝'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id  AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) = '审核拒绝'
        </if>
        <if test="vo!=null and vo.eDeclareResult!=null and vo.eDeclareResult =='审核中'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id  AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) = '审核中'
        </if>
        <if test="vo!=null and vo.eDeclareResult!=null and vo.eDeclareResult =='未申报'">
            and ( SELECT declare_result FROM commission_declare WHERE business_id = m.id AND production_id = mp.id  AND business_type = 'COMMISSION_DECLARE_MAJOR_EVALUATE' AND deleted = 0 ) is null
        </if>
        ORDER BY
            <if test="vo!=null and vo.invoiceAmount!=null">
                fi.invoiceAmount=${vo.invoiceAmount} desc,
            </if>
                mp.created DESC
    </select>

    <update id="cancelDelivery">
        update major_production set delivery = 0 ,delivery_date = null where major_id = #{businessId} and report_no = #{businessSubId} and production = #{productionType}
    </update>

    <select id="getMajorProductionById" parameterType="java.lang.Long" resultType="com.dayou.vo.MajorProductionVO">
        SELECT
            mp.id,
            mp.major_id,
            m.order_id,
            mp.report_no,
            mp.production,
            m.client_manager_id,
            m.department_id,
            d.leader_id as departmentLeaderId
        FROM
            major_production mp
                LEFT JOIN major m ON m.id = mp.major_id
                left join department d on d.id = m.department_id
        WHERE
            mp.id = #{productionId} and mp.deleted = 0 and m.deleted = 0
    </select>

    <select id="getProductionNosByMajorId" parameterType="java.lang.Long" resultType="java.lang.String">
        select report_no from major_production where major_id = #{majorId} and deleted = 0
    </select>


    <select id="getSecondCheckId" resultType="java.lang.Long">
        select second_check_id from major_production where major_id =#{businessId} and report_no = #{businessMinId} and deleted = 0
    </select>

    <select id="getEvaluateAmountByProductionNo" parameterType="java.lang.String" resultType="com.dayou.entity.MajorProduction">
        SELECT
            mp.id,
            mp.work_purpose,
            mp.evaluate_amount,
            m.business_object_type,
            mp.production,
            mp.is_record
        FROM
            major_production mp
                LEFT JOIN major m ON m.id = mp.major_id
        WHERE
            mp.report_no =  #{toMergeNumber} and mp.deleted = 0

    </select>

    <select id="findMaxLetterNoByBusinessId" parameterType="java.lang.Long" resultType="java.lang.String">
        SELECT
            max( letter_no )
        FROM
            major_target
        WHERE
            major_id = #{businessId}
          AND deleted = 0
    </select>
</mapper>
