kettle中的同步程序

SELECT ci.verified_name AS 客户名称
,o.order_code AS 订单编号
,o.shipping_address AS 订单地址
,os.name AS 订单状态
,o.create_time AS 建单时间
,o.order_price AS 订单金额
,bo.back_time AS 退单时间
,bo.back_remark AS 退单说明
,bo.back_price AS 申请金额
FROM `kstore`.np_order o
LEFT JOIN kstore.np_order_receivables r
ON r.`order_code` = o.order_code
INNER JOIN `tplus`.`syn_customer` c
ON o.`customer_id` = c.`code`
LEFT JOIN tplus.order_status os ON o.order_status = os.code
LEFT JOIN kstore.np_back_order bo ON o.order_code = bo.order_code
LEFT JOIN kstore.np_customer_info ci ON o.customer_id = ci.customer_id
WHERE ( o.order_status = '4' OR bo.back_order_id IS NOT NULL)--状态为4 说明作废
AND o.`create_time` > DATE_ADD(NOW(),INTERVAL -24 HOUR)
ORDER BY o.create_time desc

---------------------------------------------------------------------------------------------------------------------------------

SELECT a.address_id
,a.address_name
,t.id AS customer_id
,CONCAT(d.district_name,' ',a.address_detail) AS addr
,a.address_moblie
,a.is_default
,a.create_time
FROM np_customer_address a INNER JOIN np_customer c ON a.customer_id = c.customer_id
LEFT JOIN np_sys_district d ON a.address_county = d.district_id
LEFT JOIN tplus.syn_customer t ON a.customer_id = t.code
WHERE a.del_flag = '0'--0表示删除标记中的未删除
AND (a.create_time > DATE_ADD(NOW(),INTERVAL -12 hour) OR a.modified_time > DATE_ADD(NOW(),INTERVAL -12 hour))--现在时间的十二小时之前的时间

-------------------------------------------------------------------------------------------------------------------------------------------------------------

-----------------------预存款明细-------------------------
SELECT t.id
,c.customer_id AS 客户编号
,c.verified_name AS 客户名称
,ot.name AS 交易类型
,os.name AS 交易状态
,t.create_time AS 交易时间
,t.order_price AS 交易金额
,t.current_price AS 当前金额
,t.order_code AS 交易订单编号
FROM kstore.np_trade_info t
LEFT JOIN tplus.app_order_type ot ON t.order_type = ot.code
LEFT JOIN tplus.app_order_status os ON t.order_status = os.code
LEFT JOIN kstore.np_customer_info c ON t.customer_id = c.customer_id AND c.del_flag = '0'--(0表示未删除标记)
where t.create_time > '2018-09-01'
-- DATE_ADD(NOW(),INTERVAL -4 hour)

--------------------------------------------------------同步订单信息中的-------------------------------------------------------------------

SELECT o.customer_id
,o.order_status
,order_line_pay
,c.id AS idcustomer
,c.idpartnerclass as customertype
,o.order_id
,o.`order_old_code`--主单号
,o.order_code
,o.order_price
,o.`express_price` --运费
,o.jf_price -- 积分兑换金额
,o.modify_price -- 订单修改金额
,o.promotions_price -- 促销金额
,o.discount_price -- 会员折扣金额
,o.coupon_price -- 优惠券金额
,o.order_old_price --订单原始总金额
,o.`pay_time`
,r.`pay_mode`
,customer_remark AS remark
,r.`pay_money`
,IF(o.invoice_type='0','203','204') AS invoicetype -- 发票类型
,o.`shipping_person`
,o.`shipping_mobile`
,CONCAT(o.`shipping_county`,' ',o.`shipping_address`) AS addr
,og.`goods_id` -- 商品编号
,og.goods_info_id -- 货品编号
,og.goods_info_num -- 货品个数
,og.goods_info_old_price -- 货品原始价格
,og.goods_info_price -- 货品交易价格
,og.goods_coupon_price -- 货品优惠金额
,og.goods_info_old_price *og.goods_info_num AS old_sum_price --货品原始总金额
,og.goods_info_sum_price -- 小计金额
,p.`changeRate` -- 变化率(针对于产品的箱 袋 一箱几袋就是这样的转化率)
,p.`idunit` --
,p.`idbaseunit` --
,CONCAT(og.goods_info_num,p.`danwei`) AS compositionquantity -- 数量跟单位连起来合成 一箱 一袋等
,og.have_gift_status -- 是否有赠品
,og.have_coupon_status -- 是否有优惠券
,og.`goods_marketing_id` --商品促销ID
,m.marketing_type -- 促销类型 0:满减 1:满折 2:套装 3:抢购 4:满赠 5:包邮 7:支付返利
,m.`marketing_name` -- 促销名称
,mr.`rush_discount` -- 抢购折扣
,og.is_present AS is_gift -- 订单商品类型 普通商品 赠品
,NOW() AS deal_time -- 将现在时间作为删除时间
FROM `kstore`.np_order o
LEFT JOIN kstore.np_order_receivables r
ON r.`order_code` = o.order_code
INNER JOIN `tplus`.`syn_customer` c
ON o.`customer_id` = c.`code`
INNER JOIN `np_order_goods` og
ON o.order_id = og.`order_id`
LEFT JOIN `qm_marketing` m
ON m.marketing_id = og.goods_marketing_id
LEFT JOIN `qm_marketing_rush` mr
ON m.marketing_id = mr.marketing_id
LEFT JOIN tplus.`product_info` p ON og.goods_info_id = p.goods_info_id
WHERE (o.order_status IN ('1','2','3','7') or (order_line_pay = '0' and o.order_status = '0'))-- 状态是在1.已付款未发货2.已发货3,已经收货7.申请退货中的时候
AND o.`create_time` > DATE_ADD(NOW(),INTERVAL -1 DAY) -- 一天前的
and o.create_time > '2018-03-01'
-- and o.order_code in ('1802251552377826')
ORDER BY order_id desc

------------------------------------------------------------得到需要创建的销货单----------------------------------------------------------

select row_number() over (PARTITION by order_code order by idunit desc) v, '1034' + 'ǐ'+'' + 'ǐ'+'2' + 'ǐ'+CONVERT(varchar(100), isnull(pay_time,[deal_time]), 23)+ 'ǐ'+''+ 'ǐ'+''+ 'ǐ'+cast(isnull(idsaleman,3) as varchar(10)) + 'ǐ'+cast(idcustomer as varchar(20))+ 'ǐ'+'7' + 'ǐ'+'' + 'ǐ'+'' + 'ǐ'+isnull(remark,'') + 'ǐ'+'' + 'ǐ'+'' + 'ǐ'+'1' + 'ǐ'+case priuserdefnvc1 when '专用发票' then '281' when '普通发票' then '210' else '203' end + 'ǐ'+'' + 'ǐ'+'' +'ǐ'+order_code + 'ǐ'+'' maintable
,''+ 'ǐ'+cast(goods_info_num as varchar(20))+ 'ǐ'+cast(is_gift as varchar(20))+ 'ǐ'+cast(goods_id as varchar(20))+ 'ǐ'+cast((1-is_gift)*isnull(cagreementPrice,goods_info_price) as varchar(20))+ 'ǐ'+cast(idunit as varchar(20))+ 'ǐ'+''+ 'ǐ'+''+ 'ǐ'+''+ 'ǐ'+'5'+ 'ǐ'+''+ 'ǐ'+'0.0'+ 'ǐ'+'0.0'+ 'ǐ'+''+ 'ǐ'+cast(isnull(idinvlocation,38) as varchar(20))+ 'ǐ'+''+ 'ǐ'+''+ 'ǐ'+'0.0'+ 'ǐ'+''+ 'ǐ'+''+ 'ǐ'+''+ 'ǐ'+''+ 'ǐ'+''+ 'ǐ'+isnull(Benzhu,'') as detailtable
from (
select order_code
,pay_time -- 支付时间
,[deal_time]
,p.idsaleman
,o.idcustomer
,remark --备注
,goods_info_num -- 货品数量
,o.is_gift -- 赠品
,goods_id
,cp.cagreementPrice
,goods_info_price -- 货品价格
,o.idunit
,i.idinvlocation
,[rush_discount]
,isnull(case when o.is_gift = '0' then Cast([rush_discount]*10 AS VARCHAR(10))+ '折' + Isnull([marketing_value], '') when o.is_gift = '1' then '赠品' end,'') as Benzhu
,p.priuserdefnvc1
FROM mcyg..app_ordery o
left join AA_Partner p on o.idcustomer = p.id
INNER JOIN aa_inventory i -- T+的表数据信息
ON o.goods_id = i.id
left join (select cast(agreementPrice as decimal(32,2)) as cagreementPrice,idcustomer,idinventory,idunit from AA_CustomerInventoryPrice) cp
on cp.idcustomer = o.idcustomer
and cp.idinventory = o.goods_id
and cp.idunit = o.idunit
WHERE not EXISTS (SELECT 1
FROM sa_saledelivery
WHERE code = o.order_code)
union all
select app.order_code
,app.[deal_time]
,app.[deal_time]
,p.idsaleman
,app.idcustomer
,app.remark
,app.num
,0
,t.idinventory
,null
,app.price
,t.idunit
,i.idinvlocation
,null
,t.detailmemo
,p.priuserdefnvc1
from mcyg..app_youhui t inner join(
select distinct order_code,1 num ,express_price price,'YF' as DetailMemo,[deal_time],idcustomer,remark
from mcyg..app_ordery where express_price > 0 and customertype <> 25
union all
select distinct order_code,-1 as num,jf_price,'JF' as DetailMemo,[deal_time],idcustomer,remark
from mcyg..app_ordery where jf_price > 0
union all
select distinct order_code,-1 as num,isnull(modify_price,0)+isnull(promotions_price,0)+ isnull(discount_price,0)+isnull(coupon_price,0) ,'HD' as DetailMemo,[deal_time],idcustomer,remark
from mcyg..app_ordery
where isnull(modify_price,0)+isnull(promotions_price,0)+ isnull(discount_price,0)+isnull(coupon_price,0)> 0) app on t.DetailMemo = app.DetailMemo
left join AA_Partner p on app.idcustomer = p.id
left JOIN aa_inventory i
ON t.idinventory = i.id
WHERE NOT EXISTS (SELECT 1
FROM sa_saledelivery
WHERE code = app.order_code)) app

---------------------------------------------------------------------检查上架货品信息---------------------------------------------------------------

select
i.priuserdefnvc4 as APP分类
,case when i.productInfo is null then '品牌为空' else '' end as 品牌
,case when p.invSCost1 < '1' or p.invSCost1 is null then 'APP销售价异常' else '' end as APP售价
,i.code as 编号
,i.name as 存货名
,i.specification as 规格
-- ,i.name+'('+u.name+')' as goods_info_name
,u.name as 单位
,u.changeRate as 转换率
,getdate() as 检查时间
from AA_InventoryPrice p right join dbo.AA_Inventory i on i.id = p.idinventory
left join [eap_userdefinearticledto_0001] appc on appc.name = i.priuserdefnvc4
left join dbo.AA_Unit u on p.idunit = u.id
left join (select idbaseunit,idinventory,sum(baseQuantity) baseQuantity from ST_CurrentStock where idwarehouse in (2,3) group by idbaseunit,idinventory) cs on i.id = cs.idinventory
left join (select sob.idinventory,sum(baseQuantity) as saleQuantity from SA_SaleOrder_b sob inner join SA_SaleOrder so on sob.idSaleOrderDTO = so.id left join [SA_SaleDelivery] sd on so.code = sd.SourceVoucherCode
where sd.code is null group by sob.idinventory) ss on ss.idinventory = i.id
left join eap_enumItem b on b.idEnum= N'32' and i.productInfo = b.id
where -- i.disabled = 0 and p.invSCost1 is not null and
i.priuserdefnvc4 is not null and i.priuserdefnvc4 > '' -- APP分类已设置
and( p.invSCost1 <'1' or p.invSCost1 is null -- 有APP销售价
or i.productInfo is null or i.productInfo ='' ) -- 品牌不为空
and i.disabled = 0 -- 非停用商品
-- and i.code= '010406'
-- and p.id = 281
-- and appc.code is null
order by i.code,u.name

猜你喜欢

转载自www.cnblogs.com/danJuly/p/10233329.html