create view vi_pay_apply_query as SELECT LPA.RELATIVESERIALNO                                                                           AS RELATIVESERIALNO,
       LPA.SERIALNO                                                                                   AS SERIALNO,
       LPA.CONTRACT_ID                                                                                AS CONTRACT_ID,
       LPA.PROJ_ID                                                                                    AS PROJ_ID,
       LPA.FLOW_UNID                                                                                  AS FLOW_UNID,
       LPA.PHASENO                                                                                    AS PHASENO,
       LPA.FLOWNAME                                                                                   AS FLOWNAME,
       LPA.APPLYTYPE                                                                                  AS APPLYTYPE,
       LPA.PHASETYPE                                                                                  AS PHASETYPE,
       LPA.OBJECTNO                                                                                   AS OBJECTNO,
       LPA.OBJECTTYPE                                                                                 AS OBJECTTYPE,
       LPA.USERID                                                                                     AS USERID,
       LPA.FLOWNO                                                                                     AS FLOWNO,
       LPA.FLOW_KEY                                                                                   AS FLOW_KEY,
       LPA.CUSTOMER_NAME                                                                              AS CUSTOMER_NAME,
       LPA.DISTRIBUTOR_NAME                                                                           AS DISTRIBUTOR_NAME,
       LPA.PHASENAME                                                                                  AS PHASENAME,
       LPA.USERNAME                                                                                   AS USERNAME,
       LPA.BEGINTIME                                                                                  AS BEGINTIME,
       LPA.ENDTIME                                                                                    AS ENDTIME,
       (CASE WHEN (ifnull(LFI.FACT_DATE, '未放款') = LFI.FACT_DATE) THEN '已放款' ELSE '未放款' END)           AS IS_PAY,
       LFI.FACT_DATE                                                                                  AS FACT_DATE,
       (CASE WHEN (ifnull(LFA.ARCHIVING_TIME, '未完成') = LFA.ARCHIVING_TIME) THEN '已归档' ELSE '未归档' END) AS IS_ARCHIVING,
       LFA.ARCHIVING_TIME                                                                             AS ARCHIVING_TIME,
       ZT.ITEMNAME                                                                                    AS CONTRACT_STATUS,
       (CASE FLOW.FLOWSTATE WHEN 'START' THEN '发起' WHEN 'ANYBACK' THEN '已退回' END)                     AS FLOWSTATE,
       FLOW_TRIAL.USERNAME                                                                            AS USERNAME_TRIAL,
       FLOW_REVIEW.USERNAME                                                                           AS USERNAME_REVIEW
FROM ((((((APZL.VI_PAY_APPLY LPA LEFT JOIN APZL.LB_FILE_ARCHIVING_INFO LFA ON ((LPA.CONTRACT_ID = LFA.CONTRACT_ID))) LEFT JOIN APZL.LC_FUND_INCOME LFI ON ((
        (LFI.CONTRACT_ID = LPA.CONTRACT_ID) AND (LFI.FEE_TYPE = 'feetype10') AND (LFI.ROLL_BACK = 0) AND (LFI.PAY_STATUS = 'have_paid')))) LEFT JOIN (
    SELECT BS.CONTRACT_NUMBER AS CONTRACT_NUMBER, BS.INPUTTIME AS INPUTTIME, BS.BUSINESS_STATUS AS BUSINESS_STATUS, CL.ITEMNAME AS ITEMNAME
    FROM (APZL.BUSINESS_STATUS BS
    LEFT JOIN APZL.CODE_LIBRARY CL ON (((CL.ITEMNO = BS.BUSINESS_STATUS) AND (CL.CODENO = 'BusinessStatus'))))
    WHERE ((length(BS.CONTRACT_NUMBER) > 13) AND BS.INPUTTIME IN (
        SELECT max(B.INPUTTIME)
        FROM APZL.BUSINESS_STATUS B
        WHERE ((B.CONTRACT_NUMBER IS NOT NULL) AND (B.CONTRACT_NUMBER <> '') AND (length(B.CONTRACT_NUMBER) > 13))
        GROUP BY B.CONTRACT_NUMBER
    ))
    GROUP BY BS.CONTRACT_NUMBER
) ZT ON ((ZT.CONTRACT_NUMBER = LPA.FLOW_KEY))) LEFT JOIN (
    SELECT APZL.FLOW_TASK.OBJECTNO AS OBJECTNO, APZL.FLOW_TASK.FLOWSTATE AS FLOWSTATE
    FROM APZL.FLOW_TASK
    WHERE ((APZL.FLOW_TASK.PHASENO = '0010') AND (APZL.FLOW_TASK.BASEFLOWNO = 'FundPaymentCarFlow') AND (APZL.FLOW_TASK.TASKSTATE = '0') AND
           APZL.FLOW_TASK.BEGINTIME IN (
               SELECT max(APZL.FLOW_TASK.BEGINTIME)
               FROM APZL.FLOW_TASK
               WHERE ((APZL.FLOW_TASK.PHASENO = '0010') AND (APZL.FLOW_TASK.BASEFLOWNO = 'FundPaymentCarFlow') AND (APZL.FLOW_TASK.TASKSTATE = '0'))
               GROUP BY APZL.FLOW_TASK.OBJECTNO, APZL.FLOW_TASK.PHASENO
           ))
) FLOW ON ((FLOW.OBJECTNO = LPA.FLOW_UNID))) LEFT JOIN (
    SELECT FT.USERNAME AS USERNAME, FT.OBJECTNO AS OBJECTNO
    FROM APZL.FLOW_TASK FT
    WHERE ((FT.PHASENO = '0020') AND (FT.BASEFLOWNO = 'FundPaymentCarFlow') AND (FT.TASKSTATE = '1') AND FT.BEGINTIME IN (
        SELECT max(FTA.BEGINTIME)
        FROM APZL.FLOW_TASK FTA
        WHERE ((FTA.PHASENO = '0020') AND (FTA.BASEFLOWNO = 'FundPaymentCarFlow') AND (FTA.TASKSTATE = '1') AND (FT.OBJECTNO = FTA.OBJECTNO))
    ))
) FLOW_TRIAL ON ((FLOW_TRIAL.OBJECTNO = LPA.FLOW_UNID)))
LEFT JOIN (
    SELECT FT.USERNAME AS USERNAME, FT.OBJECTNO AS OBJECTNO
    FROM APZL.FLOW_TASK FT
    WHERE ((FT.PHASENO = '0030') AND (FT.BASEFLOWNO = 'FundPaymentCarFlow') AND (FT.TASKSTATE = '1') AND FT.BEGINTIME IN (
        SELECT max(FTA.BEGINTIME)
        FROM APZL.FLOW_TASK FTA
        WHERE ((FTA.PHASENO = '0030') AND (FTA.BASEFLOWNO = 'FundPaymentCarFlow') AND (FTA.TASKSTATE = '1') AND (FT.OBJECTNO = FTA.OBJECTNO))
    ))
) FLOW_REVIEW ON ((FLOW_REVIEW.OBJECTNO = LPA.FLOW_UNID)));

-- comment on column vi_pay_apply_query.fact_date not supported: 收支日期

-- comment on column vi_pay_apply_query.ARCHIVING_TIME not supported: 归档时间

-- comment on column vi_pay_apply_query.username_trial not supported: 承办人姓名

-- comment on column vi_pay_apply_query.username_review not supported: 承办人姓名

