Postgre Sql 寫入Json欄位資料
阿新 • • 發佈:2018-12-30
INSERT INTO nash_invoice(
id,
invoice_no,
contract_no,
contract_type,
customer_name,
nash_company,
opening_type,
invoice_type,
invoice_base_info,
invoice_open_status,
invoice_other_info,
created_by,
confirm_at,
created_at,
updated_at,
invoice_at,
apply_at
)
SELECT
t.id,
invoice_no,
contract_no,
contract_type,
relate_table_name,
COALESCE(c.company_id,s.company_id) as company_id,
CASE WHEN pay_type = 1 THEN 1
WHEN pay_type = 2 THEN 2
WHEN pay_type = 3 THEN 1
WHEN pay_type = 4 THEN 2
END AS opening_type,
invoice_type AS invoice_type,
COALESCE(c.invoice_base_info,s.invoice_base_info) as invoice_base_info,
CASE WHEN state = 3 THEN 3
END AS invoice_open_status,
w.invoice_other_info,
create_user_id AS created_by,
CASE WHEN confirm_date = '' THEN NULL
WHEN confirm_date = '0000-00-00' THEN NULL
ELSE confirm_date :: TIMESTAMP
END AS confirm_at,
CASE WHEN pass_time = '' THEN NULL
WHEN pass_time = '0000-00-00 00:00:00' THEN NULL
ELSE pass_time :: TIMESTAMP
END AS created_at,
CASE WHEN update_time = '' THEN NULL
WHEN update_time = '0000-00-00' THEN NULL
ELSE update_time :: TIMESTAMP
END AS updated_at,
CASE WHEN pay_date = '' THEN NULL
WHEN pay_date = '0000-00-00' THEN NULL
ELSE pay_date :: TIMESTAMP
END AS invoice_at,
CASE WHEN pay_date = '' THEN NULL
WHEN pay_date = '0000-00-00' THEN NULL
ELSE pay_date :: TIMESTAMP
END AS apply_at
FROM mysql_nash_contract_invoice AS t
LEFT JOIN (
SELECT
id,
invoice_number,
invoice_identify_no,
t.company_id,
t.company_name,
t.invoice_title,
row_to_json(t) AS invoice_base_info
FROM (
with a as
(
SELECT
invoice.id,
invoice.contract_no,
invoice.invoice_no AS invoice_number,
invoice.identification_number AS invoice_identify_no,
invoice.invoice_title
FROM mysql_nash_contract_invoice AS invoice
WHERE invoice.contract_type = 2 AND invoice.state = 3
),b as
(
SELECT
a.id,
a.invoice_number,
a.invoice_identify_no,
a.invoice_title,
ment.id AS company_id,
ment.name AS company_name
FROM mysql_nash_contract AS contract
RIGHT JOIN mysql_nash_room AS room ON contract.room_id = room.id
RIGHT JOIN mysql_nash_department AS depart ON room.service_center_id = depart.id
RIGHT JOIN mysql_nash_department AS ment ON depart.father = ment.id
RIGHT JOIN a on contract.contract_no = a.contract_no WHERE contract.state > 0 AND contract.contract_type = 2
)
select * from b
) AS t
) AS c ON t.id=c.id and t.contract_type=2
LEFT JOIN (
SELECT
id,
invoice_number,
invoice_identify_no,
t.company_id,
t.company_name,
t.invoice_title,
row_to_json(t) AS invoice_base_info
FROM (
with i as
(
SELECT
invoice.id,
invoice.contract_no,
invoice.invoice_no AS invoice_number,
invoice.identification_number AS invoice_identify_no,
invoice.invoice_title
FROM mysql_nash_contract_invoice AS invoice
WHERE invoice.contract_type = 4 AND invoice.state=3
),p as
(
SELECT servicecenter_id,contract_id FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY contract_id) rn FROM mysql_nash_cubicle_contract) AS t
WHERE t.rn = 1 AND t.state=1 AND t.contract_id IN (
SELECT b.id FROM nash_invoice AS a LEFT JOIN mysql_nash_contract AS b ON b.contract_no=a.contract_no WHERE a.contract_type=4
)
),u as
(
SELECT
i.id,
i.invoice_number,
i.invoice_identify_no,
i.invoice_title,
ment.id AS company_id,
ment.name AS company_name,
contract.contract_type
FROM mysql_nash_contract AS contract
LEFT JOIN p AS service ON service.contract_id=contract.id
LEFT JOIN mysql_nash_department AS depart ON depart.id=service.servicecenter_id
LEFT JOIN mysql_nash_department AS ment ON ment.id = depart.father
LEFT JOIN i ON contract.contract_no = i.contract_no
WHERE ment.id is not null AND ment.name is not null
)
select * from u
) AS t
) AS s ON t.id=s.id and t.contract_type=4
LEFT JOIN (
SELECT
id,
business_licence_path,
taxpayer_certificat_path,
account_opening_license_path,
bank_id,
bank_code,
address,
telephone,
row_to_json(w) AS invoice_other_info
FROM (
SELECT
invoice.id,
CASE WHEN LEFT( invoice.business_licence_path, 4 ) = '/app' THEN
CONCAT( 'http://nami.nashwork.com:8080/data', invoice.business_licence_path )
WHEN LEFT( invoice.business_licence_path, 4 ) = 'http' THEN invoice.business_licence_path
WHEN LEFT( invoice.business_licence_path, 5 ) = '/data' THEN
CONCAT( 'http://nami.nashwork.com:8080', invoice.business_licence_path )
WHEN LEFT( invoice.business_licence_path, 4 ) = 'data' THEN
CONCAT( 'http://nami.nashwork.com:8080/', invoice.business_licence_path )
END AS business_licence_path,
CASE WHEN LEFT( invoice.taxpayer_certificat_path, 4 ) = '/app' THEN
CONCAT( 'http://nami.nashwork.com:8080/data', invoice.taxpayer_certificat_path )
WHEN LEFT( invoice.taxpayer_certificat_path, 4 ) = 'http' THEN invoice.taxpayer_certificat_path
WHEN LEFT( invoice.taxpayer_certificat_path, 5 ) = '/data' THEN
CONCAT( 'http://nami.nashwork.com:8080', invoice.taxpayer_certificat_path )
WHEN LEFT( invoice.taxpayer_certificat_path, 4 ) = 'data' THEN
CONCAT( 'http://nami.nashwork.com:8080/', invoice.taxpayer_certificat_path )
END AS taxpayer_certificat_path,
CASE WHEN LEFT( invoice.account_opening_license_path, 4 ) = '/app' THEN
CONCAT( 'http://nami.nashwork.com:8080/data', invoice.account_opening_license_path )
WHEN LEFT( invoice.account_opening_license_path, 4 ) = 'http' THEN invoice.account_opening_license_path
WHEN LEFT( invoice.account_opening_license_path, 5 ) = '/data' THEN
CONCAT( 'http://nami.nashwork.com:8080', invoice.account_opening_license_path )
WHEN LEFT( invoice.account_opening_license_path, 4 ) = 'data' THEN
CONCAT( 'http://nami.nashwork.com:8080/', invoice.account_opening_license_path)
END AS account_opening_license_path,
invoice.bank_id,
invoice.bank_code,
invoice.address,
invoice.telephone
FROM mysql_nash_contract_invoice AS invoice
) AS w
) AS w ON t.id=w.id
WHERE t.state='3'
ORDER BY t.id ASC;