## 物料编码
~~~sql
-- TKNCC.V_UAPBD_QUERYSYNC_MATERIAL source
create view V_UAPBD_QUERYSYNC_MATERIAL as
SELECT
    t."PK_MATERIAL",t."CODE",t."NAME",t."MATERIALSPEC",t."ENABLESTATE",t."DW_CODE",t."DW_NAME",t."PK_MEASDOC",t."CHKFREEFLAG",t."STOCKORG_CODE",t."PK_STORDOC",t."STOCKCODE",t."STOCKNAME",t."PK_ORG",t."REAL_ORG_CODE",t."MTS",t."MKTS",
    'C022' AS org_code,
    t1.materstate,
    t.PK_MARBASCLASS,
    CASE
        WHEN t.mTS IS NULL
            AND t.mkTS IS NULL
            AND t1.mdTS IS NULL THEN to_char(TO_DATE(sysdate, 'YYYY-MM-DD HH24:MI:SS'))
        ELSE GREATEST(
                COALESCE(t.mTS, '2000-01-01 00:00:00'),
                COALESCE(t.mkTS, '2000-01-01 00:00:00'),
                COALESCE(t1.mdTS, '2000-01-01 00:00:00'))
        END AS ts
FROM
    (
        SELECT
            m.PK_MATERIAL,
            m.CODE,
            m.NAME,
            m.MATERIALSPEC,
            m.PK_MARBASCLASS,
            m.ENABLESTATE,
            mc.code AS dw_code,
            mc.name AS dw_name,
            mc.pk_measdoc AS pk_measdoc,
            mk.chkfreeflag,
            os.code AS stockorg_code,
            bdstoc.pk_stordoc,
            bdstoc.code AS stockcode,
            bdstoc.name AS stockname,
            oo.PK_ORG,
            oo.code AS real_org_code,
            m.ts AS mts,
            mk.ts AS mkts
        FROM
            bd_material m
                INNER JOIN bd_materialstock mk ON
                m.PK_MATERIAL = mk.pk_material
                INNER JOIN org_stockorg os ON
                        mk.pk_org = os.pk_stockorg
                    AND os.code = 'C022'
                LEFT JOIN bd_stordoc bdstoc ON
                mk.pk_stordoc = bdstoc.pk_stordoc
                LEFT JOIN bd_measdoc mc ON
                m.pk_measdoc = mc.pk_measdoc
                LEFT JOIN org_orgs oo ON
                m.pk_org = oo.pk_org
        WHERE
            m.dr = 0
        --		AND (oo.code = '30401'
--			OR m.CODE LIKE '1%'
--			OR m.CODE LIKE '2%'
--			OR m.CODE LIKE '4%')
        ORDER BY
            m.pk_material DESC
    )t
        LEFT JOIN (
        SELECT
            md.pk_material,
            md.materstate,
            orgf.code AS orgf_code,
            md.ts AS mdts
        FROM
            bd_materialprod md
                INNER JOIN org_factory orgf ON
                        md.pk_org = orgf.pk_factory
                    AND orgf.code = 'C022'
    )t1 ON
            t.PK_MATERIAL = t1.PK_MATERIAL


~~~

## 物料分类
~~~sql
create view V_UAPBD_QUERYSYNC_MATCLASS as
SELECT
    m.code,
    m.name,
    m.pk_marbasclass ,
    p.name AS parent_name,
    p.code AS parent_code,
    p.pk_marbasclass pk_parent,
    oo.CODE AS real_org_code,
    m.enablestate,
    CASE
        WHEN p.TS IS NULL
            AND m.TS IS NULL THEN to_char(TO_DATE(sysdate, 'YYYY-MM-DD HH24:MI:SS'))
        ELSE GREATEST(
                COALESCE(p.TS, '2000-01-01 00:00:00'),
                COALESCE(m.TS, '2000-01-01 00:00:00'))
        END AS ts
FROM
    bd_marbasclass m
        LEFT JOIN bd_marbasclass p ON m.pk_parent = p.pk_marbasclass
        LEFT JOIN ORG_ORGS oo ON m.PK_ORG = oo.PK_ORG
WHERE
    m.dr = 0
~~~

## 人员
~~~sql
create view V_UAPBD_QUERYSYNC_PSNDOC as
SELECT
    p.pk_psndoc,
    p.CODE,
    p.NAME,
    dept.code AS dept_code,
    dept.name AS dept_name,
    p.ENABLESTATE,
    org.code as org_code,
    CASE
        WHEN p.TS IS NULL
            AND j.TS IS NULL THEN to_char(TO_DATE(sysdate, 'YYYY-MM-DD HH24:MI:SS'))
        ELSE GREATEST(
                COALESCE(p.TS, '2000-01-01 00:00:00'),
                COALESCE(j.TS, '2000-01-01 00:00:00'))
        END AS ts
FROM
    bd_psndoc p
        LEFT JOIN bd_psnjob j ON p.pk_psndoc = j.pk_psndoc AND (j.enddutydate IS NULL OR TO_DATE(j.enddutydate, 'YYYY-MM-DD HH24:MI:SS') > SYSDATE)
        AND j.ismainjob = 'Y'
        AND j.dr = 0
        LEFT JOIN org_orgs org ON p.pk_org = org.pk_org
        LEFT JOIN org_dept dept ON j.pk_dept = dept.pk_dept
WHERE
        p.dr = 0
~~~

## 客户
~~~sql
create view V_UAPBD_QUERYSYNC_CUSTOMER as
SELECT t.PK_CUSTOMER,
       t.NAME,
       t.CODE,
       t.ENABLESTATE,
       t.SHORTNAME,
       t.REAL_ORG_CODE,
       t.ORG_NAME,
       t.GROUP_CODE,
       t.GROUP_NAME,
       NVL(t.TS, SYSDATE) AS ts,
       t.RN,
       CEIL(t.rn / 10000) AS org_code
FROM (SELECT c.pk_customer,
             c.name,
             c.code,
             c.enablestate,
             c.shortname,
             oo.code                                    AS real_org_code,
             oo.name                                    AS org_name,
             og.code                                    AS group_code,
             og.name                                    AS group_name,
             c.ts                                       AS TS,
             ROW_NUMBER() OVER (ORDER BY c.pk_customer) AS rn
      FROM bd_customer c
               LEFT JOIN org_orgs oo ON c.pk_org = oo.pk_org
               LEFT JOIN org_group og ON c.pk_group = og.pk_group
      WHERE c.dr = 0) t
~~~

## 供应商
~~~sql
create view V_UAPBD_QUERYSYNC_SUPPLIER as
SELECT
    t."CODE",t."PK_SUPPLIER",t."NAME",t."ENABLESTATE",t."SHORTNAME",t."REAL_ORG_CODE",t."ORG_NAME",t."GROUP_CODE",t."GROUP_NAME",t."TS",t."RN",
    CEIL(t.rn / 10000) AS org_code
FROM (
         SELECT
             s.CODE,
             s.PK_SUPPLIER,
             s.NAME,
             s.ENABLESTATE,
             s.shortname,
             oo.code AS real_org_code,
             oo.name AS org_name,
             og.code AS group_code,
             og.name AS group_name,
             s.ts AS TS,
             ROW_NUMBER() OVER (ORDER BY s.PK_SUPPLIER) AS rn
         FROM
             bd_supplier s
                 LEFT JOIN org_orgs oo ON s.pk_org = oo.pk_org
                 LEFT JOIN org_group og ON s.pk_group = og.pk_group
         WHERE
             s.dr = 0
     ) t
~~~

## 部门
~~~sql
create view V_UAPBD_QUERYSYNC_DEPT as
SELECT
    d.CODE,
    d.NAME,
    d.ENABLESTATE,
    d.PK_DEPT,
    oo.code AS org_code,
    oo.name AS org_name,
    og.code AS group_code,
    og.name AS group_name,
    CASE
        WHEN d.TS IS NULL
            AND d.MODIFIEDTIME IS NULL THEN to_char(TO_DATE(sysdate, 'YYYY-MM-DD HH24:MI:SS'))
        ELSE GREATEST(
                COALESCE(d.TS, '2000-01-01 00:00:00'),
                COALESCE(d.MODIFIEDTIME, '2000-01-01 00:00:00'))
        END AS ts
FROM
    org_dept d
        LEFT JOIN org_orgs oo ON d.pk_org = oo.pk_org
        LEFT JOIN org_group og ON d.pk_group = og.pk_group
WHERE
    d.dr = 0
~~~

## 仓库
~~~sql
create view V_UAPBD_QUERYSYNC_STORDOC as
SELECT
    sd.CODE,
    sd.name,
    sd.ENABLESTATE,
    sd.PK_STORDOC,
    so.code AS org_code,
    so.name AS org_name,
    og.code AS group_code,
    og.name AS group_name,
    sd.ts AS TS
FROM
    bd_stordoc sd
        LEFT JOIN org_stockorg so ON sd.pk_org = so.pk_stockorg
        LEFT JOIN org_group og ON sd.pk_group = og.pk_group
WHERE
    sd.dr = 0
~~~