#1.头条-全量素材基本信息表
#创建表
CREATE TABLE `app_bytedance_material_info` (
  `material_id` varchar(50) NOT NULL COMMENT '素材id',
  `company_id` varchar(50) DEFAULT NULL COMMENT '分公司id',
  `company_name` varchar(100) DEFAULT NULL COMMENT '分公司名称',
  `help_time` bigint(20) DEFAULT NULL COMMENT '上传助手时间',
  `media_time` bigint(20) DEFAULT NULL COMMENT '上传媒体时间',
  `confirm_time` timestamp NULL DEFAULT NULL COMMENT '通过审核时间',
  `farst_cost_time` bigint(20) DEFAULT NULL COMMENT '首次消耗时间',
  `channel_type` bigint(20) DEFAULT NULL COMMENT '素材渠道',
  `material_type` bigint(20) DEFAULT NULL COMMENT '视频类型',
  `material_name` varchar(1000) DEFAULT NULL COMMENT '视频名称',
  `video_url` varchar(1000) DEFAULT NULL COMMENT '视频链接',
  `cover_url` varchar(1000) DEFAULT NULL COMMENT '首页链接',
  `material_innovate` bigint(20) DEFAULT NULL COMMENT '是否创新素材',
  `status` bigint(20) DEFAULT NULL COMMENT '当日审核状态',
  `sync_kuaishou` bigint(20) DEFAULT NULL COMMENT '是否同步快手',
  `sync_bytedance` bigint(20) DEFAULT NULL COMMENT '是否同步头条',
  `effect_flag` bigint(20) DEFAULT NULL COMMENT '是否有效素材',
  `effect_date` bigint(20) DEFAULT NULL COMMENT '有效时间',
  `faddish_flag` bigint(20) DEFAULT NULL COMMENT '是否爆款素材',
  `faddish_date` bigint(20) DEFAULT NULL COMMENT '爆款时间',
  `auditor_id` varchar(100) DEFAULT NULL COMMENT '审核人',
  `auditor_name` varchar(100) DEFAULT NULL COMMENT '审核人名称',
  `dept_id` varchar(100) DEFAULT NULL COMMENT '设计部门id',
  `dept_name` varchar(100) DEFAULT NULL COMMENT '设计部门名称',
  `clip_id` varchar(100) DEFAULT NULL COMMENT '剪辑id',
  `clip_name` varchar(100) DEFAULT NULL COMMENT '剪辑名称',
  `shot_id` varchar(100) DEFAULT NULL COMMENT '拍摄',
  `shot_name` varchar(100) DEFAULT NULL COMMENT '拍摄名称',
  `plan_id` varchar(100) DEFAULT NULL COMMENT '策划id',
  `plan_name` varchar(100) DEFAULT NULL COMMENT '策划名称',
  `leader_id` varchar(100) DEFAULT NULL COMMENT '设计负责人id',
  `leader_name` varchar(100) DEFAULT NULL COMMENT '设计负责人名称',
  `data_create_time` timestamp DEFAULT CURRENT_TIMESTAMP,
  `data_update_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`material_id`) /*T![clustered_index] NONCLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='头条-素材基本信息(日新)';
#插入头条素材明细数据（前置sql）
REPLACE INTO application.`app_bytedance_material_info` (material_id, company_id, company_name, help_time, media_time, confirm_time, farst_cost_time, channel_type, material_type, material_name, video_url, cover_url, material_innovate, status, sync_kuaishou, sync_bytedance, effect_flag, effect_date, faddish_flag, faddish_date, auditor_id, auditor_name, dept_id, dept_name, clip_id, clip_name, shot_id, shot_name, plan_id, plan_name, leader_id, leader_name)
select a.signature,com.company_id,
(CASE
WHEN com.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
	'北京汇创思拓数字科技有限公司'
WHEN com.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
	'广州汇创思拓数字科技有限公司'
WHEN com.company_id = '4b10089775c040119e139087517aed88' THEN
	'上海汇创思拓数字科技有限公司'
WHEN com.company_id = '5e46b5dea3d740eeb8ff1a2895015a4b' THEN
	'北京骄阳数字科技有限公司'
WHEN com.company_id = '1648c7ceb4d449a9a13fbed7a8d9976c' THEN
	'杭州妙构思数字科技有限公司'
WHEN com.company_id = '3321d5893bcd430cab1238a99bc7db11' THEN
	'北京次元像素'
	else com.company_name
END) company_name,
date_format(b.create_time,'%Y%m%d') create_time,date_format(c.stat_date,'%Y%m%d') stat_date,d.confirm_time,
a.frast_cost_time,0 channel_type,c.material_type,
CASE WHEN d.material_name IS NOT NULL THEN d.material_name
     ELSE c.photo_name END AS material_name,
c.video_url,d.cover_url cover_url,d.material_innovate,d.status,
d.sync_kuaishou,d.sync_bytedance,
case when eff.effect_date  is not null then 1 else 0 end effect_flag,
	case when eff.effect_date  is not null then eff.effect_date else 0 end effect_date,
	case when hot.hot_date  is not null then 1 else 0 end faddish_flag,
	case when hot.hot_date  is not null then  hot.hot_date  else 0 end faddish_date,
d.auditor_id,audi.realname auditor_name,ur.org_code dept_id,ur.depart_name dept_name,b.clip_id,userd.realname clip_name,
b.shot_id,shot.realname shot_name,b.plan_id,plan.realname plan_name,b.leader_id,leader.realname leader_name
from application.bytedance_material_farst_cost_time a
left join (select material_id,create_time,clip_id,shot_id,plan_id,leader_id from (
select material_id,create_time,clip_id,shot_id,plan_id,leader_id,row_number() over (partition by material_id order by create_time ) rn
from `jeecg-boot`.ctop_material_ascription) er where rn=1) b
on a.signature=b.material_id
left join
(select  * from
(select signature,
case when size>height then 2
     when size<height then 1
else 0 end material_type,video_url,date_format(create_time,'%Y%m%d') stat_date ,filename photo_name,row_number() over(partition by signature order by create_time) rm  from `media_api`.bytedance_file_video_get
where date_format(create_time,'%Y%m%d')>= date_format(date_sub(${stat_date}, INTERVAL 13 DAY),'%Y%m%d')) m where rm=1) c
on  a.signature=c.signature
left join
(select * from
(select  code,confirm_time,auditor_id,user_id,material_innovate,status,sync_kuaishou,sync_bytedance,material_name,cover_url,row_number() over (partition by code order by confirm_time) rm
from `jeecg-boot`.ctop_material_info ) v
where rm=1) d
on a.signature=d.code
left join `jeecg-boot`.user_company com on b.clip_id=com.user_id
left join `jeecg-boot`.sys_user audi on  d.auditor_id=audi.id
left join `jeecg-boot`.sys_user userd on   b.clip_id=userd.id
left join `jeecg-boot`.sys_user shot on  b.shot_id=shot.id
left join `jeecg-boot`.sys_user plan on  b.plan_id=plan.id
left join `jeecg-boot`.sys_user leader on  b.leader_id=leader.id
LEFT JOIN `jeecg-boot`.sys_user_depart de ON b.leader_id = de.user_id
LEFT JOIN `jeecg-boot`.sys_depart ur ON de.dep_id = ur.id
left join application.effect_material eff
on a.signature=eff.signature
left join
application.hot_material hot
on a.signature=hot.signature
where a.frast_cost_time=${stat_date};
#更新头条素材有效和爆款（后置sql）
replace into application.`app_bytedance_material_info` (material_id, company_id, company_name, help_time, media_time, confirm_time, farst_cost_time, channel_type, material_type, material_name, video_url, cover_url, material_innovate, status, sync_kuaishou, sync_bytedance, effect_flag, effect_date, faddish_flag, faddish_date, auditor_id, auditor_name, dept_id, dept_name, clip_id, clip_name, shot_id, shot_name, plan_id, plan_name, leader_id, leader_name)
select
new.material_id,
new.company_id,
new.company_name,
new.help_time,
new.media_time,
new.confirm_time,
new.farst_cost_time,
new.channel_type,
new.material_type,
new.material_name,
new.video_url,
new.cover_url,
new.material_innovate,
new.status,
new.sync_kuaishou,
new.sync_bytedance,
case when  eff.signature is not null then 1 else 0 end effect_flag,
case when  eff.signature is not null then eff.effect_date else 0 end effect_date,
case when  hot.signature is not null then 1 else 0 end  faddish_flag,
case when  hot.signature is not null then hot.hot_date else 0 end faddish_date,
new.auditor_id,
new.auditor_name,
new.dept_id,
new.dept_name,
new.clip_id,
new.clip_name,
new.shot_id,
new.shot_name,
new.plan_id,
new.plan_name,
new.leader_id,
new.leader_name
from application.`app_bytedance_material_info` new
left JOIN
(select signature,effect_date from application.effect_material  where media_id=1)  eff
on new.material_id=eff.signature
left join
(select signature,hot_date from application.hot_material  where media_id=1) hot
on new.material_id=hot.signature
where new.farst_cost_time>=date_sub(${stat_date}, INTERVAL 20 DAY);

#2.快手-全量素材基本信息表
#创建表
CREATE TABLE `app_kuaishou_material_info` (
  `material_id` varchar(50) NOT NULL COMMENT '素材id',
  `company_id` varchar(50) DEFAULT NULL COMMENT '分公司id',
  `company_name` varchar(100) DEFAULT NULL COMMENT '分公司名称',
  `help_time` bigint(20) DEFAULT NULL COMMENT '上传助手时间',
  `media_time` bigint(20) DEFAULT NULL COMMENT '上传媒体时间',
  `confirm_time` timestamp NULL DEFAULT NULL COMMENT '通过审核时间',
  `farst_cost_time` bigint(20) DEFAULT NULL COMMENT '首次消耗时间',
  `channel_type` bigint(20) DEFAULT NULL COMMENT '素材渠道',
  `material_type` bigint(20) DEFAULT NULL COMMENT '视频类型',
  `material_name` varchar(1000) DEFAULT NULL COMMENT '视频名称',
  `video_url` varchar(1000) DEFAULT NULL COMMENT '视频链接',
  `cover_url` varchar(1000) DEFAULT NULL COMMENT '首页链接',
  `material_innovate` bigint(20) DEFAULT NULL COMMENT '是否创新素材',
  `status` bigint(20) DEFAULT NULL COMMENT '当日审核状态',
  `sync_kuaishou` bigint(20) DEFAULT NULL COMMENT '是否同步快手',
  `sync_bytedance` bigint(20) DEFAULT NULL COMMENT '是否同步头条',
  `effect_flag` bigint(20) DEFAULT NULL COMMENT '是否有效素材',
  `effect_date` bigint(20) DEFAULT NULL COMMENT '有效时间',
  `faddish_flag` bigint(20) DEFAULT NULL COMMENT '是否爆款素材',
  `faddish_date` bigint(20) DEFAULT NULL COMMENT '爆款时间',
  `auditor_id` varchar(100) DEFAULT NULL COMMENT '审核人',
  `auditor_name` varchar(100) DEFAULT NULL COMMENT '审核人名称',
  `dept_id` varchar(100) DEFAULT NULL COMMENT '设计部门id',
  `dept_name` varchar(100) DEFAULT NULL COMMENT '设计部门名称',
  `clip_id` varchar(100) DEFAULT NULL COMMENT '剪辑id',
  `clip_name` varchar(100) DEFAULT NULL COMMENT '剪辑名称',
  `shot_id` varchar(100) DEFAULT NULL COMMENT '拍摄',
  `shot_name` varchar(100) DEFAULT NULL COMMENT '拍摄名称',
  `plan_id` varchar(100) DEFAULT NULL COMMENT '策划id',
  `plan_name` varchar(100) DEFAULT NULL COMMENT '策划名称',
  `leader_id` varchar(100) DEFAULT NULL COMMENT '设计负责人id',
  `leader_name` varchar(100) DEFAULT NULL COMMENT '设计负责人名称',
  `data_create_time` timestamp DEFAULT CURRENT_TIMESTAMP,
  `data_update_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`material_id`) /*T![clustered_index] NONCLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='快手-素材基本信息(日新)';
##插入快手素材明细数据（前置sql）
replace into application.`app_kuaishou_material_info` (material_id, company_id, company_name, help_time, media_time, confirm_time, farst_cost_time, channel_type, material_type, material_name, video_url, cover_url, material_innovate, status, sync_kuaishou, sync_bytedance, effect_flag, effect_date, faddish_flag, faddish_date, auditor_id, auditor_name, dept_id, dept_name, clip_id, clip_name, shot_id, shot_name, plan_id, plan_name, leader_id, leader_name)
SELECT
	a.signature,
	com.company_id,
	(
		CASE
		WHEN com.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
			'北京汇创思拓数字科技有限公司'
		WHEN com.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
			'广州汇创思拓数字科技有限公司'
		WHEN com.company_id = '4b10089775c040119e139087517aed88' THEN
			'上海汇创思拓数字科技有限公司'
		WHEN com.company_id = '5e46b5dea3d740eeb8ff1a2895015a4b' THEN
			'北京骄阳数字科技有限公司'
		WHEN com.company_id = '1648c7ceb4d449a9a13fbed7a8d9976c' THEN
			'杭州妙构思数字科技有限公司'
		WHEN com.company_id = '3321d5893bcd430cab1238a99bc7db11' THEN
			'北京次元像素'
		ELSE
			com.company_name
		END
	) company_name,
	date_format(b.create_time, '%Y%m%d') help_time,
	date_format(c.stat_date, '%Y%m%d') media_time,
	d.confirm_time,
	a.frast_cost_time,
	c.channel_type,
	c.material_type,
  CASE WHEN d.material_name IS NOT NULL THEN d.material_name
  ELSE c.photo_name END AS material_name,
	c.url,
  c.cover_url,
	d.material_innovate,
	d.STATUS,
	d.sync_kuaishou,
	d.sync_bytedance,
	0 effect_flag,
	0 effect_date,
	0 faddish_flag,
	0 faddish_date,
	d.auditor_id,
	audi.realname auditor_name,
	ur.org_code dept_id,
	ur.depart_name dept_name,
	b.clip_id,
	userd.realname clip_name,
	b.shot_id,
	shot.realname shot_name,
	b.plan_id,
	plan.realname plan_name,
	b.leader_id,
	leader.realname leader_name
FROM application.material_farst_cost_time a
LEFT JOIN (select material_id,create_time,clip_id,shot_id,plan_id,leader_id from (
select material_id,create_time,clip_id,shot_id,plan_id,leader_id,row_number() over (partition by material_id order by create_time ) rn
from `jeecg-boot`.ctop_material_ascription) er where rn=1) b
ON a.signature = b.material_id
LEFT JOIN (select * from (
SELECT signature,channel_type,material_type,url,cover_url,stat_date,photo_name,row_number() over(partition by signature order by stat_date) rm  FROM `media_api`.kuaishou_video_list
where date_format(stat_date,'%Y%m%d')>= date_format(date_sub(${stat_date}, INTERVAL 13 DAY),'%Y%m%d'))m where rm=1) c ON a.signature = c.signature
LEFT JOIN
(select code,confirm_time,auditor_id,user_id,material_innovate,status,sync_kuaishou,sync_bytedance,material_name from (
select  code,confirm_time,auditor_id,user_id,material_innovate,status,sync_kuaishou,sync_bytedance,material_name,row_number() over (partition by code order by confirm_time) rm
from `jeecg-boot`.ctop_material_info ) v
where rm=1) d
ON a.signature = d.CODE
LEFT JOIN `jeecg-boot`.user_company com ON b.clip_id = com.user_id
LEFT JOIN `jeecg-boot`.sys_user audi ON d.auditor_id = audi.id
LEFT JOIN `jeecg-boot`.sys_user userd ON b.clip_id = userd.id
LEFT JOIN `jeecg-boot`.sys_user shot ON b.shot_id = shot.id
LEFT JOIN `jeecg-boot`.sys_user plan ON b.plan_id = plan.id
LEFT JOIN `jeecg-boot`.sys_user leader ON b.plan_id = leader.id
LEFT JOIN `jeecg-boot`.sys_user_depart de ON b.leader_id = de.user_id
LEFT JOIN `jeecg-boot`.sys_depart ur ON de.dep_id = ur.id
where a.frast_cost_time=${stat_date};
#更新快手素材有效和爆款（后置sql）
replace into application.`app_kuaishou_material_info` (material_id, company_id, company_name, help_time, media_time, confirm_time, farst_cost_time, channel_type, material_type, material_name, video_url, cover_url, material_innovate, status, sync_kuaishou, sync_bytedance, effect_flag, effect_date, faddish_flag, faddish_date, auditor_id, auditor_name, dept_id, dept_name, clip_id, clip_name, shot_id, shot_name, plan_id, plan_name, leader_id, leader_name)
select
new.material_id,
new.company_id,
new.company_name,
new.help_time,
new.media_time,
new.confirm_time,
new.farst_cost_time,
new.channel_type,
new.material_type,
new.material_name,
new.video_url,
new.cover_url,
new.material_innovate,
new.status,
new.sync_kuaishou,
new.sync_bytedance,
case when  eff.signature is not null then 1 else 0 end effect_flag,
case when  eff.signature is not null then eff.effect_date else 0 end effect_date,
case when  hot.signature is not null then 1 else 0 end  faddish_flag,
case when  hot.signature is not null then hot.hot_date else 0 end faddish_date,
new.auditor_id,
new.auditor_name,
new.dept_id,
new.dept_name,
new.clip_id,
new.clip_name,
new.shot_id,
new.shot_name,
new.plan_id,
new.plan_name,
new.leader_id,
new.leader_name
from application.`app_kuaishou_material_info` new
LEFT JOIN
(select signature,effect_date from application.effect_material  where media_id=2)  eff
on new.material_id=eff.signature
left join
(select signature,hot_date from application.hot_material  where media_id=2) hot
on new.material_id=hot.signature
where new.farst_cost_time>=date_sub(${stat_date}, INTERVAL 20 DAY);

#3.素材标签基本信息表（日更新）
#创建表
CREATE TABLE `app_material_tag_busi_info` (
  `material_id` varchar(100) NOT NULL COMMENT '素材id',
  `company_id` varchar(50) DEFAULT NULL COMMENT '分公司id',
  `company_name` varchar(100) DEFAULT NULL COMMENT '分公司名称',
  `create_date` bigint(20) DEFAULT NULL COMMENT '创建时间',
  `updt_date` bigint(20) DEFAULT NULL COMMENT '更新时间',
  `leader_id` varchar(100) DEFAULT NULL COMMENT '设计id',
  `leader_name` varchar(100) DEFAULT NULL COMMENT '设计负责人名称',
  `tag_category_id` varchar(100) DEFAULT NULL COMMENT '一级标签',
  `tag_category_name` varchar(100) DEFAULT NULL COMMENT '一级标签名称',
  `parent_id` varchar(100) DEFAULT NULL COMMENT '二级标签',
  `parent_name` varchar(100) DEFAULT NULL COMMENT '二级标签名称',
  `tag_code` varchar(100) NOT NULL COMMENT '三级标签',
  `tag_name` varchar(100) DEFAULT NULL COMMENT '三级标签名称',
  PRIMARY KEY (`material_id`,`tag_code`) /*T![clustered_index] NONCLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='素材标签明细(新)';
#插入数据
REPLACE INTO application.`app_material_tag_busi_info`
select m.material_id,m.company_id,m.company_name,m.create_date,m.updt_date,m.leader_id,m.leader_name,m.tag_category_id,m.tag_category_name,m.parent_id,m.parent_name,
ifnull(m.tag_code,0) tag_code,m.tag_name from
(select a.code material_id,com.company_id,
(CASE
WHEN com.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
	'北京汇创思拓数字科技有限公司'
WHEN com.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
	'广州汇创思拓数字科技有限公司'
WHEN com.company_id = '4b10089775c040119e139087517aed88' THEN
	'上海汇创思拓数字科技有限公司'
WHEN com.company_id = '5e46b5dea3d740eeb8ff1a2895015a4b' THEN
	'北京骄阳数字科技有限公司'
WHEN com.company_id = '1648c7ceb4d449a9a13fbed7a8d9976c' THEN
	'杭州妙构思数字科技有限公司'
WHEN com.company_id = '3321d5893bcd430cab1238a99bc7db11' THEN
	'北京次元像素'
	else com.company_name
END) company_name,
date_format(a.create_time,'%Y%m%d') create_date,date_format(a.update_time,'%Y%m%d') updt_date,c.leader_id,us.realname leader_name,b.tag_category_id,b.tag_category_name,b.parent_id,b.parent_name,b.tag_code,b.tag_name
from `jeecg-boot`.ctop_material_tag_info  a
left join
(select tag_category_id,tag_category_name,
      case when parent_id is not null then parent_id
           when tag_category_id=2 then '' else tag_code end parent_id,
      case when parent_id=60155 then '真人'
           when parent_id=60156 then '制作'
           when parent_id=60160 then '外拍场景'
           when parent_id=60160 then '公司内景'
           when tag_category_id=2 then '' else '其他' end parent_name,
       tag_code,tag_name
 from `jeecg-boot`.ctop_tag_info )b
on a.tag_code=b.tag_code
inner join `jeecg-boot`.ctop_material_ascription c
on a.code=c.material_id
left join `jeecg-boot`.user_company com on c.clip_id=com.user_id
left join `jeecg-boot`.sys_user us on c.leader_id=us.id
WHERE date_format(a.create_time,'%Y%m%d') between date_sub(${stat_date},INTERVAL 4 DAY) AND ${stat_date}) m;
