-- 成本中心映射配置表历史数据升级
DELETE FROM t_bas_costcentersourcemap WHERE fid IN ( SELECT fcostcenterid FROM t_bas_costcentersource );
INSERT INTO t_bas_costcentersourcemap (
    fid,
    fnumber,
    flongnumber,
    flevel,
    fisleaf,
    fparentid,
    fcostcenterid,
    fstatus,
    fcreatorid,
    fmodifierid,
    fenable,
    fcreatetime,
    fmodifytime,
    fmasterid
) SELECT
    distinct  fid,
      fnumber,
      flongnumber,
      flevel,
      fisleaf,
      fparentid,
      fid,
      'A',
      fcreatorid,
      fmodifierid,
      '1',
      fcreatetime,
      fmodifytime,
      fmasterid
FROM
    t_bas_costcenter
WHERE
        fid IN (
        SELECT
            fcostcenterid
        FROM
            t_bas_costcentersource
    )
  AND fid NOT IN (
    SELECT
        fid
    FROM
        t_bas_costcentersourcemap
);

DELETE FROM t_bas_costcentersourcemap_l WHERE fid IN ( SELECT fcostcenterid FROM t_bas_costcentersource );
INSERT INTO t_bas_costcentersourcemap_l (
    fpkid,
    fid,
    flocaleid,
    ffullname,
    fname
) SELECT
      distinct fpkid,
      fid,
      flocaleid,
      ffullname,
      fname
FROM
    t_bas_costcenter_l
WHERE
        fid IN (
        SELECT
            fcostcenterid
        FROM
            t_bas_costcentersource
    );

DELETE FROM t_bas_costcentermapentry WHERE fid IN ( SELECT fcostcenterid FROM t_bas_costcentersource );
INSERT INTO t_bas_costcentermapentry (
    fid,
    fentryid,
    fseq,
    fsourcedataid,
    fsourcetype
) SELECT
      s.fcostcenterid,
      s.fid,
      s.fseq,
      s.fdataid,
      e.fbizobjectid
FROM
    t_bas_costcentersource s
        JOIN t_bas_sourcetype e ON s.fsourcetypeid = e.fid;