IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[Proj_QSY_CalculateDynamicDataEveryDay]') AND type = 'P')
DROP PROCEDURE [dbo].[Proj_QSY_CalculateDynamicDataEveryDay]
GO

-- =============================================
-- Author: zhangxiaomeng
-- Create date: 2017-12-29
-- Description: 每天定时计算动态数据，如：员工工龄
-- =============================================
CREATE PROCEDURE [dbo].[Proj_QSY_CalculateDynamicDataEveryDay]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


--Region #1工龄
declare @tmp table(
id bigint primary key ,
starttime datetime,
y int,m int,d int,
timeaddyear datetime,
timeaddmonth datetime,
tempdate datetime)

insert into @tmp(id,starttime,y,m)
select ord_id as id,
ord_rzsj ,
datediff(month , ord_rzsj , getdate())/12 y,
datediff(month , ord_rzsj , getdate())%12 m
from order_40
where ord_zzzk='在职'

update @tmp set tempdate = DATEADD(MONTH,m,DATEADD(YEAR,y,starttime)) ,
timeaddyear = DATEADD(YEAR,y,starttime)

update @tmp set timeaddmonth = DATEADD(MONTH,m,timeaddyear)

update @tmp set d = DATEDIFF(day,tempdate,GETDATE())+DATEDIFF(day,dateadd(MONTH,-1,GETDATE()),GETDATE())-1,
m = case when m>0 then m-1 else 11 end,
y = case when m>0 then y when m<=0 and y>0 then y-1 end
where DATEDIFF(day,tempdate,GETDATE()) <0

update @tmp set d = DATEDIFF(day,tempdate,GETDATE())-1
where DATEDIFF(day,tempdate,GETDATE()) >0

update @tmp 
set m = m-1,
d = DATEDIFF(day,dateadd(MONTH,-1,GETDATE()),GETDATE())
where DATEDIFF(day,tempdate,GETDATE()) =0
and m>0

update @tmp 
set d = 0
where  d is null

/*
select ord_gh x,ord_xm xm,*,datepart(DAY,starttime) from @tmp,order_40 where ord_id = id
-- and ord_gh = '00001'
and ord_gh not like '%A'
order by ord_gh asc
*/
--更新工龄
update a
set a.ord_gl = convert(varchar(100),b.y)+'年'+convert(varchar(100),b.m)+'月'+convert(varchar(100),b.d)+'日'
,a.ord_sjczlx = '只更新工龄'+convert(nvarchar(100),getdate(),120)
from order_40 as a,@tmp as b
where a.ord_id = b.id

--删除临时表
delete @tmp
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

--Region #2试用到期天数 ord_sydqts
update a
set a.ord_sydqts = ''
,a.ord_sjczlx = '只更新到期天数'+convert(nvarchar(100),getdate(),120)
from order_40 a
where a.ord_jsrq<convert(datetime,convert(varchar(100),getdate(),23)+' 00:00:01',120)

update a
set a.ord_sydqts = DATEDIFF(DAY,GETDATE(),isnull(ord_jsrq,'1900-01-01'))
,a.ord_sjczlx = '只更新到期天数'+convert(nvarchar(100),getdate(),120)
from order_40 a
where a.ord_jsrq>convert(datetime,convert(varchar(100),getdate(),23)+' 00:00:01',120)

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--Region #3转换后生日
/*
唐晗：
比如我，就是过农历，所以我在入职的时候告诉人事组我的生日是农历的12月28。然后人事组就在系统上登记，农，12-28。然后转换生日是当年的阳历，因为系统要发邮件
再比如罗益越，她的生日是阳历的11月11日，在办理入职的时候，我会问她的实际生日，是过农历还是阳历，她说是阳历的11月11日，我录档案的时候就会选阳历，11-11，
然后转换生日就是每年的11月11日。我是农历12月28，那么转换成阳历生日就是2018年2月13日。
*/
--农历
--1.生日过了 （设置明年的年+生日月日）
--2.生日没有过（设置今年的年+生日月日）
--3.今天过生
--substring(CONVERT(varchar(30),dbo.fn_GetSolarDate(convert(nvarchar(30),ord_sr,23),default),120),5,50)
-- substring(CONVERT(varchar(30),dbo.fn_GetSolarDate(convert(nvarchar(30),ord_sr,23),default),120),5,50) 
update a
set a.ord_zhhsr =  
convert( datetime,
(dbo.fn_GetSolarDate(
 convert(varchar(10),convert(int,substring(dbo.fn_GetLunar(convert(nvarchar(30),GETDATE(),23),default),1,4))+1)
 +substring(convert(nvarchar(30),ord_sr,23),5,50),default)
),120) 
,a.ord_sjczlx = '转换后生日'+convert(nvarchar(100),getdate(),120)
from 
order_40 a
where 
a.ord_sr >'1900-01-01'
and a.ord_sr<'2049-01-01'
and a.ord_srlx like '农%'
--and a.ord_gh = '00536'
and datediff(day, 
dbo.fn_GetSolarDate(substring(dbo.fn_GetLunar(convert(nvarchar(30),GETDATE(),23),default),1,4)+substring(convert(nvarchar(30),ord_sr,23),5,50),DEFAULT)
    ,GETDATE())>0  --生日过了
and a.ord_zzzk ='在职'

update a
set a.ord_zhhsr = convert(datetime,
(dbo.fn_GetSolarDate(
convert(varchar(10),convert(int,substring(dbo.fn_GetLunar(convert(nvarchar(30),GETDATE(),23),default),1,4)))
+substring(convert(nvarchar(30),ord_sr,23),5,50),default)
),120) 
,a.ord_sjczlx = '转换后生日'+convert(nvarchar(100),getdate(),120)
from 
order_40 a
where 
a.ord_sr >'1900-01-01'
and a.ord_sr<'2049-01-01'
and a.ord_srlx like '农%'
and datediff(day
,dbo.fn_GetSolarDate(
substring(dbo.fn_GetLunar(convert(nvarchar(30),GETDATE(),23),default),1,4)--现在农历哪年？
+substring(convert(nvarchar(30),ord_sr,23),5,50)--农历生日“月-日”部分
,DEFAULT)
    ,GETDATE())<=0 --未过生日的 
and a.ord_zzzk ='在职'


update a
set a.ord_zhhsr = (substring(convert(nvarchar(30),GETDATE(),23),1,4)
+substring(convert(nvarchar(30),ord_sr,23),5,50))
,a.ord_sjczlx = '转换后生日'+convert(nvarchar(100),getdate(),120)
from order_40 a
where a.ord_sr >'1900-01-01'
and a.ord_sr<'2049-01-01'
and a.ord_srlx like '阳%'
and datediff(day
,substring(convert(nvarchar(30),GETDATE(),23),1,4)--现在阳历哪年？
+substring(convert(nvarchar(30),ord_sr,23),5,50)--阳历生日“月-日”部分
    ,GETDATE())<=0 --未过生日的
     
update a
set a.ord_zhhsr = substring(convert(nvarchar(30),dateadd(year,1,GETDATE()),23),1,4)+substring(convert(nvarchar(30),ord_sr,23),5,50)
,a.ord_sjczlx = '转换后生日'+convert(nvarchar(100),getdate(),120)
from order_40 a
where a.ord_sr >'1900-01-01'
and a.ord_sr<'2049-01-01'
and a.ord_srlx like '阳%'
and datediff(day
,substring(convert(nvarchar(30),GETDATE(),23),1,4)--现在阳历哪年？
+substring(convert(nvarchar(30),ord_sr,23),5,50)--阳历生日“月-日”部分
    ,GETDATE())>0 --过了生日的
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------        

SET NOCOUNT OFF;
END