init.lua 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. local skynet = require "skynet"
  2. local s = require "service"
  3. local mysql = require "skynet.db.mysql"
  4. local runconfig = require("run_config")
  5. local db = nil
  6. local cjson = require "cjson"
  7. local tools = require "tools"
  8. s.resp.parse_template_info = function(info,template_info)
  9. --片头
  10. local head_video = {}
  11. local type = 1 -- 类型 1=片头视频,2=视频,3=片尾视频,4=bgm音频,5=片尾音频,6=图片
  12. local video_crop_time = info.video_crop_time
  13. local ratio = info.ratio
  14. local user_id = info.user_id
  15. local video_stype = info.video_stype
  16. -- skynet.error("(info.template_info",info.template_info)
  17. -- if template_info.piantou_video_data.is_random then --说明选择的是随机
  18. -- local folder_list = template_info.piantou_video_data.random_folder_list
  19. -- if folder_list ==nil or #folder_list ==0 then
  20. -- skynet.error("开始随机生成全部片头")
  21. -- --说明选择的全部随机
  22. -- local isOk,list = s.resp.get_random_res_by_type(user_id,1,ratio,0)
  23. -- if not isOk then
  24. -- skynet.error("缺少片头资源!")
  25. -- else
  26. -- if #list>=1 then
  27. -- skynet.error("获取全部随机的一个指定片头")
  28. -- table.insert(head_video,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  29. -- tools.dump(head_video)
  30. -- end
  31. -- end
  32. -- else -- 说明选择了指定文件夹随机
  33. -- local isOk,list = s.resp.get_random_res_by_folder_list(1,ratio,folder_list,0,user_id)
  34. -- if not isOk then
  35. -- skynet.error("缺少片头资源!")
  36. -- else
  37. -- if #list>=1 then
  38. -- table.insert(head_video,1,list[tools.getRandomIndex(list)])
  39. -- end
  40. -- end
  41. -- end
  42. -- else
  43. -- if template_info.piantou_video_data.is_specified then --说明是指定了文件
  44. -- local isok,item = s.resp.get_res_file_by_id(template_info.piantou_video_data.specified_res_list[1].file_id)
  45. -- if isok then
  46. -- table.insert(head_video,1,item)
  47. -- end
  48. -- else
  49. -- skynet.error("参数错误!没有指定随机也没指定固定文件")
  50. -- end
  51. -- end
  52. if video_stype ==1 then
  53. if template_info.piantou_video_data.select_video_file~=nil then
  54. local data = template_info.piantou_video_data.select_video_file
  55. table.insert(head_video,1,{video_id =data.info.id,path =data.info.path,duration=data.info.duration,info=data })
  56. -- table.insert(head_video,1,data)
  57. end
  58. end
  59. if #head_video <= 0 then
  60. skynet.error("片头是空的")
  61. end
  62. --内容
  63. local content_video = {}
  64. if video_stype ==1 then
  65. local json_info = cjson.decode(info.template_info)
  66. json_info = tools.base64decode(json_info.data)
  67. json_info = cjson.decode(json_info)
  68. local need_num = math.floor((json_info.subtitles_audio_duration - head_video[1].duration) / (video_crop_time)) +1
  69. if template_info.content_video_data.is_random then --说明选择的是随机
  70. local folder_list = template_info.content_video_data.random_folder_list
  71. if folder_list ==nil or #folder_list ==0 then
  72. --说明选择的全部随机
  73. local isOk,list = s.resp.get_random_res_by_type(user_id,2,ratio,(video_crop_time+5))
  74. if not isOk then
  75. return skynet.error("缺少内容资源!")
  76. else
  77. skynet.error("need_num",need_num)
  78. if need_num>#list then
  79. skynet.error("缺少内容资源!")
  80. end
  81. local random_array = tools.getRandomArray(list)
  82. -- skynet.error("random_array")
  83. -- tools.dump(random_array)
  84. for i = 1, need_num, 1 do
  85. local random_index = random_array[i]
  86. table.insert(content_video,i,{video_id =list[random_index].id,path =cjson.decode( list[random_index].file_info).path,duration=list[random_index].duration,info = list[random_index].file_info })
  87. -- table.insert(content_video,i,list[random_index].file_info)
  88. end
  89. end
  90. else -- 说明选择了指定文件夹随机
  91. local isOk,list = s.resp.get_random_res_by_folder_list(2,ratio,folder_list,(video_crop_time+5),user_id)
  92. if not isOk then
  93. return skynet.error("缺少内容资源!")
  94. else
  95. if need_num>#list then
  96. skynet.error("缺少内容资源!")
  97. end
  98. local random_array = tools.getRandomArray(list)
  99. for i = 1, need_num, 1 do
  100. local random_index = random_array[i]
  101. table.insert(content_video,i,{video_id =list[random_index].id,path =cjson.decode( list[random_index].file_info).path,duration=list[random_index].duration ,info = list[random_index].file_info})
  102. -- table.insert(content_video,i,list[random_index].file_info)
  103. end
  104. end
  105. end
  106. else
  107. if template_info.content_video_data.is_specified then --说明是指定了文件
  108. local specified_res_list = template_info.content_video_data.specified_res_list
  109. if #specified_res_list<need_num then
  110. skynet.error("指定文件缺少",need_num)
  111. else
  112. for i = 1, #specified_res_list, 1 do
  113. local isok,item = s.resp.get_res_file_by_id(specified_res_list[1].file_id)
  114. if isok then
  115. -- table.insert(content_video,i, item.file_info)
  116. table.insert(head_video,i,{video_id =item.id,path =cjson.decode( item.file_info).path,duration=item.duration,info = item.file_info})
  117. end
  118. end
  119. end
  120. else
  121. skynet.error("参数错误!没有指定随机也没指定固定文件")
  122. end
  123. end
  124. end
  125. --片尾
  126. local end_video = {}
  127. -- if template_info.pianwei_video_data.is_random then --说明选择的是随机
  128. -- local folder_list = template_info.pianwei_video_data.random_folder_list
  129. -- if folder_list ==nil or #folder_list ==0 then
  130. -- --说明选择的全部随机
  131. -- local isOk,list = s.resp.get_random_res_by_type(user_id,3,ratio,0)
  132. -- if not isOk then
  133. -- return skynet.error("缺少片尾资源!")
  134. -- else
  135. -- if #list>=1 then
  136. -- table.insert(end_video,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  137. -- end
  138. -- end
  139. -- else -- 说明选择了指定文件夹随机
  140. -- local isOk,list = s.resp.get_random_res_by_folder_list(3,ratio,folder_list,0,user_id)
  141. -- if not isOk then
  142. -- return skynet.error("缺少片尾资源!")
  143. -- else
  144. -- if #list>=1 then
  145. -- table.insert(end_video,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  146. -- end
  147. -- end
  148. -- end
  149. -- else
  150. -- if template_info.pianwei_video_data.is_specified then --说明是指定了文件
  151. -- local isok,item = s.resp.get_res_file_by_id(template_info.pianwei_video_data.specified_res_list[1].file_id)
  152. -- if isok then
  153. -- table.insert(end_video,1,item)
  154. -- end
  155. -- else
  156. -- skynet.error("参数错误!没有指定随机也没指定固定文件")
  157. -- end
  158. -- end
  159. if template_info.pianwei_video_data.select_video_file~=nil then
  160. local data = template_info.pianwei_video_data.select_video_file
  161. table.insert(end_video,1,{video_id =data.info.id,path =data.info.path,duration=data.info.duration,info = data })
  162. -- table.insert(end_video,1,data)
  163. end
  164. if #end_video<=0 then
  165. skynet.error("添加片尾资源失败")
  166. end
  167. --BGM音效
  168. local bgm_audio = {}
  169. -- if template_info.bgm_audio_data.is_random then --说明选择的是随机
  170. -- local folder_list = template_info.bgm_audio_data.random_folder_list
  171. -- if template_info.bgm_audio_data.random_folder_list ==nil or #template_info.bgm_audio_data.random_folder_list ==0 then
  172. -- --说明选择的全部随机
  173. -- local isOk,list = s.resp.get_random_res_by_type(user_id,4,ratio,0)
  174. -- if not isOk then
  175. -- skynet.error("缺少BGM音效资源!")
  176. -- else
  177. -- if #list>=1 then
  178. -- table.insert(bgm_audio,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  179. -- end
  180. -- end
  181. -- else -- 说明选择了指定文件夹随机
  182. -- local isOk,list = s.resp.get_random_res_by_folder_list(4,ratio,folder_list,0,user_id)
  183. -- if not isOk then
  184. -- skynet.error("缺少BGM音效资源!")
  185. -- else
  186. -- if #list>=1 then
  187. -- table.insert(bgm_audio,1,{video_id =list[1].id,path =cjson.decode( list[tools.getRandomIndex(list)].file_info).path,duration=list[1].duration })
  188. -- end
  189. -- end
  190. -- end
  191. -- else
  192. -- if template_info.bgm_audio_data.is_specified then --说明是指定了文件
  193. -- local isok,item = s.resp.get_res_file_by_id(template_info.bgm_audio_data.specified_res_list[1].file_id)
  194. -- if isok then
  195. -- table.insert(bgm_audio,1,item)
  196. -- end
  197. -- else
  198. -- skynet.error("参数错误!没有指定随机也没指定固定文件")
  199. -- end
  200. -- end
  201. if template_info.bgm_audio_data.select_video_file~=nil then
  202. local data = template_info.bgm_audio_data.select_video_file
  203. table.insert(bgm_audio,1,{video_id =data.info.id,path =data.info.path,duration=data.info.duration,info = data })
  204. -- table.insert(bgm_audio,1,data)
  205. end
  206. --底部
  207. local bottom = {}
  208. if template_info.bottom_text_image_data.select_video_file~=nil then
  209. local data = template_info.bottom_text_image_data.select_video_file
  210. table.insert(bottom,1,{info = data })
  211. -- table.insert(bgm_audio,1,data)
  212. end
  213. ---
  214. local corner = {}
  215. if template_info.corner_mark_image_data.select_video_file~=nil then
  216. local data = template_info.corner_mark_image_data.select_video_file
  217. table.insert(corner,1,{info = data })
  218. -- table.insert(bgm_audio,1,data)
  219. end
  220. local background_image = {}
  221. if template_info.background_image_data.select_video_file~=nil then
  222. local data = template_info.background_image_data.select_video_file
  223. table.insert(background_image,1,{info = data })
  224. -- table.insert(bgm_audio,1,data)
  225. end
  226. return head_video,content_video,end_video,bgm_audio,bottom,corner,background_image
  227. end
  228. --根据部门id获取部门列表
  229. s.resp.get_group_list = function(group_id,call_back)
  230. skynet.fork(function()
  231. local sql = string.format("select * from users where group_id = %d ",group_id)
  232. local res = db:query(sql)
  233. if res and #res > 0 then
  234. local tab = {}
  235. if #res == 1 then
  236. tab[1] = tools.getDbResData(res)
  237. else
  238. tab = tools.getDbResData(res)
  239. end
  240. call_back(tab)
  241. else
  242. call_back({})
  243. skynet.error("No data found.")
  244. end
  245. end)
  246. end
  247. s.resp.getUserById = function(id)
  248. local sql = string.format("select * from users where id = %d ",id)
  249. skynet.error("sql",sql)
  250. local res = db:query(sql)
  251. -- 判断是否找到数据
  252. if res and #res > 0 then
  253. local tab = tools.getDbResData(res)
  254. skynet.error("Found data:",tab)
  255. return true,tab
  256. else
  257. skynet.error("No data found.")
  258. return false,nil
  259. end
  260. end
  261. s.resp.updateUserInfoById = function(id)
  262. end
  263. s.resp.createUser = function()
  264. end
  265. s.resp.deleteUserById = function(id)
  266. end
  267. s.resp.verify = function(msg_body)
  268. local isok = false
  269. local user_data = nil
  270. isok,user_data = s.resp.select_user_by_account(msg_body.account)
  271. if not isok or user_data==nil then
  272. return isok,user_data
  273. end
  274. isok = false
  275. if msg_body.password==user_data.password then
  276. isok = true
  277. -- user_data = {user_id=1,user_name="小丑"}
  278. end
  279. return isok,user_data
  280. end
  281. s.resp.select_user_by_account = function(account)
  282. local sql = string.format("select * from users where account = '%s'",account)
  283. skynet.error("sql",sql)
  284. local res = db:query(sql)
  285. -- 判断是否找到数据
  286. if res and #res > 0 then
  287. local tab = tools.getDbResData(res)
  288. skynet.error("Found data:",tab)
  289. return true,tab
  290. else
  291. skynet.error("No data found.")
  292. return false,nil
  293. end
  294. -- tools.dump(res)
  295. end
  296. s.resp.select_user_by_user_id = function(user_id)
  297. end
  298. s.resp.add_new_user = function(msg_body)
  299. -- skynet.error("msg_body",msg_body.group_type)
  300. local sql = string.format("INSERT INTO users (name,account, password,permit_id,group_id) VALUES ('%s', '%s','%s',%d,%d)",msg_body.user_name,msg_body.account,msg_body.password,msg_body.permit_id,msg_body.group_id)
  301. local res = db:query(sql)
  302. return true
  303. end
  304. --- 新建文件夹
  305. s.resp.new_folder = function(msg_body,fd)
  306. skynet.fork(function(_fd)
  307. local isOk,folder_info = s.resp.get_folder_by_name(msg_body)
  308. if isOk then
  309. return tools.response_db_new_folder(_fd,false,string.format("创建失败,已存在 %s !",msg_body.folder_name))
  310. end
  311. local sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,is_public,classification_id) VALUES (%d, '%s',%d,%d,%d)",msg_body.user_id,msg_body.folder_name,msg_body.folder_type,msg_body.is_public,msg_body.classification_id)
  312. -- skynet.error(sql)
  313. local res = db:query(sql)
  314. tools.response_db_new_folder(_fd,true)
  315. end,fd)
  316. return true
  317. end
  318. --获取资源文件夹列表
  319. s.resp.folder_list = function(msg_body,fd)
  320. skynet.fork(function(_fd)
  321. local sql = string.format("select * from folder_list_tab where user_id = %d AND folder_type = %d AND classification_id = %d",msg_body.user_id,msg_body.type,msg_body.classification_id)
  322. local res = db:query(sql)
  323. -- skynet.error('folder_list',sql)
  324. -- 判断是否找到数据
  325. if res and #res > 0 then
  326. local tab = {}
  327. if #res == 1 then
  328. tab[1] = tools.getDbResData(res)
  329. else
  330. tab = tools.getDbResData(res)
  331. end
  332. skynet.error("Found data:")
  333. -- tools.dump(tab)
  334. -- return true,tab
  335. tools.response_db_folder_list(_fd,msg_body,true,tab)
  336. else
  337. skynet.error("No data found.")
  338. tools.response_db_folder_list(_fd,msg_body,false,nil)
  339. -- return false,nil
  340. end
  341. end,fd)
  342. return true
  343. end
  344. --获取文件夹列表每个文件夹的文件数量
  345. s.resp.folder_res_list_nums = function(folder_list,fd,total_count)
  346. skynet.fork(function(_fd)
  347. local temp ={}
  348. for i = 1, #folder_list, 1 do
  349. local msg_body = folder_list[i]
  350. -- tools.dump(msg_body)
  351. local sql = string.format("select * from res_list_tab where user_id = %d AND folder_id = %d AND is_public = %d AND is_delete = 0",msg_body.user_id,msg_body.id,msg_body.is_public)
  352. local res = db:query(sql)
  353. local count = 0
  354. if res and #res > 0 then
  355. count = #res
  356. skynet.error("Found data:")
  357. -- tools.dump(tab)
  358. -- return true,tab
  359. else
  360. skynet.error("No data found.",msg_body.id)
  361. end
  362. temp[msg_body.id] = count
  363. -- table.insert(temp,msg_body.id,count)
  364. end
  365. tools.response_db_folder_list_nums(_fd,folder_list,temp,total_count)
  366. end,fd)
  367. end
  368. --获取指定文件夹内的资源列表
  369. s.resp.folder_res_list = function(msg_body,fd,call_back)
  370. skynet.fork(function(_fd)
  371. local sql_duration = ""
  372. if msg_body.duration~=nil then
  373. sql_duration = string.format(" AND duration >= %d",msg_body.duration)
  374. end
  375. local sql = string.format("select * from res_list_tab where user_id = %d AND folder_id = %d AND is_public = %d AND is_delete = 0"..sql_duration,msg_body.user_id,msg_body.folder_id,msg_body.is_public)
  376. local res = db:query(sql)
  377. skynet.error('获取指定文件夹内的资源列表',sql)
  378. -- 判断是否找到数据
  379. if res and #res > 0 then
  380. local tab = {}
  381. if #res == 1 then
  382. tab[1] = tools.getDbResData(res)
  383. else
  384. tab = tools.getDbResData(res)
  385. end
  386. skynet.error("Found data:")
  387. if not call_back and _fd~=nil then
  388. tools.response_db_folder_res_list(_fd,msg_body,true,tab)
  389. else
  390. if call_back ~=nil then
  391. call_back(tab)
  392. end
  393. end
  394. -- tools.dump(tab)
  395. -- return true,tab
  396. else
  397. skynet.error("No data found.",call_back)
  398. if call_back == nil and _fd~=nil then
  399. tools.response_db_folder_res_list(_fd,msg_body,false,nil)
  400. else
  401. if call_back ~=nil then
  402. call_back({})
  403. end
  404. end
  405. -- return false,nil
  406. end
  407. end,fd)
  408. return true
  409. end
  410. s.resp.on_recv = function (source, msg_id, msg_body,fd,...)
  411. if db==nil then
  412. return false,nil
  413. end
  414. if s.resp[msg_id]==nil then
  415. return false,nil
  416. end
  417. return s.resp[msg_id](msg_body,fd,...)
  418. end
  419. --添加资源数据到表里
  420. s.resp.add_res_file = function(msg_body)
  421. skynet.fork(function()
  422. local temp = '{"width": 100, "height": 100, "duration": 123}'
  423. temp = cjson.encode(msg_body)
  424. local duration = 0
  425. if msg_body.duration~=nil then
  426. duration = tonumber(msg_body.duration)
  427. end
  428. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name, is_public,file_info,duration,classification_id) VALUES (%d, %d, %d, '%s',%d,'%s',%f,%d)",msg_body.user_id,msg_body.folder_id,msg_body.stype,msg_body.file_name,msg_body.is_public,temp,duration,msg_body.classification_id)
  429. local res = db:query(sql)
  430. end)
  431. return true
  432. end
  433. --更改文件夹名
  434. s.resp.reset_folder_name = function(msg_body)
  435. local isOk = s.resp.get_res_folder_by_id(msg_body.folder_id)
  436. if not isOk then
  437. return false
  438. end
  439. local sql = string.format("UPDATE folder_list_tab SET folder_name ='%s' WHERE id = %d AND is_public = %d",msg_body.name,msg_body.folder_id,msg_body.is_public)
  440. db:query(sql)
  441. return true
  442. end
  443. --更改资源文件名
  444. s.resp.reset_res_name = function(msg_body)
  445. local isOk = s.resp.get_res_file_by_id(msg_body.file_id)
  446. if not isOk then
  447. return false
  448. end
  449. local sql = string.format("UPDATE res_list_tab SET file_name ='%s' WHERE id = %d AND folder_id = %d AND is_public = %d AND is_delete = 0",msg_body.name,msg_body.file_id,msg_body.folder_id,msg_body.is_public)
  450. db:query(sql)
  451. return true
  452. end
  453. --根据id获取资源
  454. s.resp.get_res_file_by_id = function(file_id)
  455. local sql = string.format("select * from res_list_tab where id = %d AND is_delete = 0",file_id)
  456. -- skynet.error("sql",sql)
  457. local res = db:query(sql)
  458. -- 判断是否找到数据
  459. if res and #res > 0 then
  460. local tab = tools.getDbResData(res)
  461. skynet.error("Found data:",tab)
  462. return true,tab
  463. else
  464. skynet.error("No data found.")
  465. return false,nil
  466. end
  467. end
  468. --根据id获取文件夹
  469. s.resp.get_res_folder_by_id = function(folder_id)
  470. local sql = string.format("select * from folder_list_tab where id = %d ",folder_id)
  471. -- skynet.error("sql",sql)
  472. local res = db:query(sql)
  473. -- 判断是否找到数据
  474. if res and #res > 0 then
  475. local tab = tools.getDbResData(res)
  476. skynet.error("Found data:",tab)
  477. return true,tab
  478. else
  479. skynet.error("No data found.")
  480. return false,nil
  481. end
  482. end
  483. --搜索资源
  484. s.resp.search_res = function(msg_body,fd)
  485. skynet.fork(function(_fd)
  486. local sql_duration = ""
  487. if msg_body.duration~=nil then
  488. sql_duration = string.format(" AND duration >= %f",msg_body.duration)
  489. end
  490. local sql = string.format("SELECT * FROM res_list_tab WHERE file_type = %d AND file_name LIKE '%%%s%%' AND is_delete = 0 AND classification_id = %d AND user_id = %d"..sql_duration,msg_body.type ,msg_body.search_content,msg_body.classification_id,msg_body.user_id)
  491. -- skynet.error("sql",sql)
  492. local res = db:query(sql)
  493. if res and #res > 0 then
  494. local tab = {}
  495. if #res == 1 then
  496. tab[1] = tools.getDbResData(res)
  497. else
  498. tab = tools.getDbResData(res)
  499. end
  500. skynet.error("Found data:")
  501. -- tools.dump(tab)
  502. -- return true,tab
  503. tools.response_db_search_res(_fd,msg_body,true,tab)
  504. else
  505. skynet.error("No data found.")
  506. -- return false,nil
  507. tools.response_db_search_res(_fd,msg_body,false,nil)
  508. end
  509. end,fd)
  510. return true
  511. end
  512. --根据所有文件id查到所有文件的url并删除
  513. local fork_get_file_list_url_and_delete = function(deleteString,call_back)
  514. local sql = string.format("SELECT * FROM res_list_tab WHERE id IN (%s)", deleteString)
  515. local res = db:query(sql)
  516. if res and #res > 0 then
  517. local tab = {}
  518. if #res == 1 then
  519. tab[1] = tools.getDbResData(res)
  520. else
  521. tab = tools.getDbResData(res)
  522. end
  523. skynet.error("Found data:fork_get_file_list_url_and_delete")
  524. local url_list = {}
  525. local count = 1
  526. for i = 1, #tab, 1 do
  527. if tab[i].ref_id==0 then
  528. local file_info = cjson.decode(tab[i].file_info)
  529. table.insert(url_list,count,file_info.path)
  530. count = count + 1
  531. end
  532. end
  533. skynet.send("tools_work","lua","delete_file",url_list)
  534. -- tools.dump(url_list)
  535. -- return true,tab
  536. else
  537. skynet.error("No data found.")
  538. -- return false,nil
  539. end
  540. if call_back~=nil then
  541. call_back()
  542. end
  543. end
  544. --执行删除文件操作
  545. local fork_delete_file = function(msg_body)
  546. -- tools.dump(msg_body)
  547. local listToDelete = {}
  548. for i = 1, #msg_body.file_id_list, 1 do
  549. table.insert(listToDelete,i,msg_body.file_id_list[i])
  550. end
  551. local deleteString = table.concat(listToDelete, ',')
  552. local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  553. db:query(sql)
  554. skynet.fork(fork_get_file_list_url_and_delete,deleteString,function()
  555. local delete_sql = string.format("DELETE FROM res_list_tab WHERE id IN (%s) AND is_public = %d AND user_id = %d", deleteString,msg_body.is_public,msg_body.user_id)
  556. db:query(delete_sql)
  557. end)
  558. end
  559. --执行删除文件夹操作
  560. local fork_delete_folder = function(msg)
  561. for i = 1, #msg.folder_id_list, 1 do
  562. local isok = s.resp.folder_res_list({
  563. folder_id = msg.folder_id_list[i],
  564. user_id = msg.user_id,
  565. is_public=msg.is_public},nil,function(list)
  566. -- skynet.error("执行删除文件夹操作",#list)
  567. if #list>0 then
  568. local temp = {}
  569. for i = 1, #list, 1 do
  570. table.insert(temp,i,list[i].id)
  571. end
  572. skynet.fork(fork_delete_file,{file_id_list=temp,user_id=msg.user_id,is_public=msg.is_public})
  573. end
  574. end)
  575. end
  576. end
  577. --删除资源文件
  578. s.resp.delete_res = function(msg_body)
  579. skynet.fork(fork_delete_file,msg_body)
  580. return true
  581. end
  582. --删除文件夹
  583. s.resp.delete_folder =function(msg_body)
  584. local listToDelete = {}
  585. for i = 1, #msg_body.folder_id_list, 1 do
  586. table.insert(listToDelete,i,msg_body.folder_id_list[i])
  587. end
  588. local deleteString = table.concat(listToDelete, ',')
  589. local sql = string.format("DELETE FROM folder_list_tab WHERE id IN (%s) AND is_public = %d AND user_id = %d", deleteString,msg_body.is_public,msg_body.user_id)
  590. db:query(sql)
  591. skynet.fork(fork_delete_folder,msg_body)
  592. return true
  593. end
  594. --新建模板
  595. s.resp.create_template = function(msg_body,fd)
  596. local template_info = "{}"
  597. if msg_body.template_info~=nil and msg_body.template_info~="" then
  598. template_info = msg_body.template_info
  599. end
  600. local sql = string.format("INSERT INTO template_list_tab (user_id,project_name, book_name,ratio,video_stype,subtitles,subtitles_audio,video_nums,video_crop_time,template_info) VALUES (%d, '%s','%s',%d,%d,'%s','%s',%d,%d,'%s')",msg_body.user_id,msg_body.project_name,msg_body.book_name,msg_body.ratio,msg_body.video_stype,msg_body.subtitles,msg_body.subtitles_audio,msg_body.video_nums,msg_body.video_crop_time,template_info)
  601. local res = db:query(sql)
  602. -- skynet.error("sql",sql)
  603. return true
  604. end
  605. --获取模板列表
  606. s.resp.get_template_list = function(msg_body,fd)
  607. skynet.fork(function(_fd)
  608. local sql = string.format("select * from template_list_tab where user_id = %d AND video_stype = %d",msg_body.user_id,msg_body.video_stype)
  609. skynet.error("sql",sql)
  610. local res = db:query(sql)
  611. -- 判断是否找到数据
  612. if res and #res > 0 then
  613. local tab = {}
  614. if #res == 1 then
  615. tab[1] = tools.getDbResData(res)
  616. else
  617. tab = tools.getDbResData(res)
  618. end
  619. skynet.error("Found data:",tab)
  620. -- return true,tab
  621. tools.response_db_get_template_list(_fd,msg_body,true,tab)
  622. else
  623. skynet.error("No data found.")
  624. -- return false,nil
  625. tools.response_db_get_template_list(_fd,msg_body,false,nil)
  626. end
  627. end,fd)
  628. return true
  629. end
  630. --删除模板
  631. s.resp.delete_template = function(msg_body)
  632. local listToDelete = {}
  633. for i = 1, #msg_body.template_id_list, 1 do
  634. table.insert(listToDelete,i,msg_body.template_id_list[i])
  635. end
  636. local deleteString = table.concat(listToDelete, ',')
  637. local sql = string.format("DELETE FROM template_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  638. db:query(sql)
  639. skynet.error(sql)
  640. return true
  641. end
  642. --保存/修改模板
  643. s.resp.save_template = function(msg_body,fd)
  644. local template_info = "{}"
  645. -- tools.dump(msg_body)
  646. if msg_body.template_info~="" and msg_body.template_info~=nil then
  647. template_info = msg_body.template_info
  648. end
  649. local sql = string.format("UPDATE template_list_tab SET project_name = '%s' , book_name = '%s' ,ratio = %d, video_stype = %d, subtitles = '%s', subtitles_audio = '%s' ,video_nums = %d, video_crop_time = %d, template_info = '%s' WHERE id = %d ",msg_body.project_name,msg_body.book_name,msg_body.ratio,msg_body.video_stype,msg_body.subtitles,msg_body.subtitles_audio,msg_body.video_nums,msg_body.video_crop_time,template_info,msg_body.template_id)
  650. -- skynet.error("sql",sql)
  651. db:query(sql)
  652. return true
  653. end
  654. --搜索模板
  655. s.resp.search_template = function(msg_body,fd)
  656. skynet.fork(function(_fd)
  657. local sql = string.format("SELECT * FROM template_list_tab WHERE user_id = %d AND video_stype = %d AND book_name LIKE '%%%s%%'",msg_body.user_id ,msg_body.video_stype,msg_body.search_content)
  658. -- local sql = string.format("select * from template_list_tab where user_id = %d AND video_stype = %d",msg_body.user_id,msg_body.video_stype)
  659. skynet.error("sql",sql)
  660. local res = db:query(sql)
  661. -- 判断是否找到数据
  662. if res and #res > 0 then
  663. local tab = {}
  664. if #res == 1 then
  665. tab[1] = tools.getDbResData(res)
  666. else
  667. tab = tools.getDbResData(res)
  668. end
  669. skynet.error("Found data:",tab)
  670. -- return true,tab
  671. tools.response_db_search_template(_fd,msg_body,true,tab)
  672. else
  673. skynet.error("No data found.")
  674. -- return false,nil
  675. tools.response_db_search_template(_fd,msg_body,false,nil)
  676. end
  677. end,fd)
  678. return true
  679. end
  680. --根据模板ID获取模板信息
  681. s.resp.get_template_info_by_id = function(msg_body,fd,call_back)
  682. skynet.fork(function(_fd)
  683. local sql = string.format("SELECT * FROM template_list_tab WHERE user_id = %d AND id = %d",msg_body.user_id ,msg_body.template_id)
  684. -- local sql = string.format("select * from template_list_tab where user_id = %d AND video_stype = %d",msg_body.user_id,msg_body.video_stype)
  685. skynet.error("sql",sql)
  686. local res = db:query(sql)
  687. -- 判断是否找到数据
  688. if res and #res > 0 then
  689. local tab = tools.getDbResData(res)
  690. skynet.error("Found data:",tab)
  691. -- return true,tab
  692. if call_back~=nil and fd ==nil then
  693. call_back(tab)
  694. else
  695. tools.response_db_get_template_info_by_id(_fd,msg_body,true,tab)
  696. end
  697. else
  698. skynet.error("No data found.")
  699. -- return false,nil
  700. if call_back~=nil and fd ==nil then
  701. call_back(nil)
  702. else
  703. tools.response_db_search_template(_fd,msg_body,false,nil)
  704. end
  705. end
  706. end,fd)
  707. return true
  708. end
  709. --获取指定类型所有文件
  710. s.resp.get_file_list_by_type = function(msg_body,fd)
  711. skynet.fork(function(_fd,_msg_body)
  712. local sql_duration = ""
  713. if msg_body.duration~=nil then
  714. sql_duration = string.format(" AND duration >= %f",msg_body.duration)
  715. end
  716. local sql = string.format("SELECT * FROM res_list_tab WHERE file_type = %d AND user_id = %d AND is_delete = 0"..sql_duration,msg_body.type,msg_body.user_id)
  717. local res = db:query(sql)
  718. -- skynet.error(sql)
  719. if res and #res > 0 then
  720. local tab = {}
  721. if #res == 1 then
  722. tab[1] = tools.getDbResData(res)
  723. else
  724. tab = tools.getDbResData(res)
  725. end
  726. tools.response_db_get_file_list_by_type(_fd,_msg_body,true,tab)
  727. -- return true,tab
  728. else
  729. skynet.error("No data found.")
  730. -- return false,nil
  731. tools.response_db_get_file_list_by_type(_fd,_msg_body,false,nil)
  732. end
  733. end,fd,msg_body)
  734. end
  735. --随机一个类型,获取相应数量的资源列表
  736. s.resp.get_random_res_by_type = function(user_id,file_type,classification_id,duration)
  737. local sql = string.format("SELECT *FROM res_list_tab WHERE file_type = %d AND is_delete = 0 AND classification_id = %d AND duration>= %f AND user_id = %d", file_type,classification_id,duration,user_id)
  738. local res = db:query(sql)
  739. if res and #res > 0 then
  740. local tab = {}
  741. if #res == 1 then
  742. tab[1] = tools.getDbResData(res)
  743. else
  744. tab = tools.getDbResData(res)
  745. end
  746. -- local test = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  747. -- tools.shuffle(tab)
  748. -- tools.dump(test)
  749. -- tools.dump(tab)
  750. return true,tab
  751. else
  752. skynet.error("No data found.")
  753. return false,nil
  754. end
  755. end
  756. --获取一个类型,指定在文件夹列表里随机
  757. s.resp.get_random_res_by_folder_list = function(file_type,classification_id,folder_list,duration,user_id)
  758. local listToSelect = {}
  759. for i = 1, #folder_list, 1 do
  760. table.insert(listToSelect,i,folder_list[i].folder_id)
  761. end
  762. local selectString = table.concat(listToSelect, ',')
  763. skynet.error("file_type",file_type)
  764. skynet.error("classification_id",classification_id)
  765. skynet.error("folder_list")
  766. tools.dump(folder_list)
  767. skynet.error("user_id",user_id)
  768. local sql = string.format("SELECT* FROM res_list_tab WHERE file_type = %d AND is_delete = 0 AND classification_id = %d AND duration>= %f AND user_id = %d AND folder_id in(%s)", file_type,classification_id,duration,user_id,selectString)
  769. local res = db:query(sql)
  770. if res and #res > 0 then
  771. local tab = {}
  772. if #res == 1 then
  773. tab[1] = tools.getDbResData(res)
  774. else
  775. tab = tools.getDbResData(res)
  776. end
  777. -- tools.dump(tab)
  778. return true,tab
  779. else
  780. skynet.error("No data found.")
  781. return false,nil
  782. end
  783. end
  784. --生成滚动视频
  785. s.resp.generate_scroll_video = function(template_tab,fd)
  786. local isOk,error_info = s.resp.check_template_scroll_isRight(template_tab)
  787. return isOk,error_info
  788. end
  789. --生成解压视频数据库
  790. s.resp.generate_video = function(_msg_body,_fd)
  791. skynet.fork(function(msg_body,fd)
  792. s.resp.get_template_info_by_id(msg_body,nil,function(tab)
  793. -- tools.dump(tab)
  794. if tab ~=nil then
  795. local isOk,error_info = s.resp.check_template_isRight(tab)
  796. if tab.video_stype == 1 then --解压
  797. if not isOk then
  798. return tools.response(fd,200,error_info)
  799. end
  800. else
  801. isOk,error_info = s.resp.check_template_scroll_isRight(tab)
  802. if not isOk then
  803. return tools.response(fd,200,error_info)
  804. end
  805. end
  806. -- tools.dump(tab)
  807. local user_id = msg_body.user_id
  808. local template_id = msg_body.template_id
  809. local file_name = tab.book_name
  810. local video_stype = tab.video_stype
  811. --先查找是否有这个名的文件夹
  812. local sql = string.format("SELECT * FROM generate_video_folder_list_tab WHERE user_id = %d AND book_name = '%s' AND video_stype = %d ",user_id,file_name,video_stype)
  813. local res = db:query(sql)
  814. local info = tools.getDbResData(res)
  815. local write_video_db = function(folder_id)
  816. skynet.error("开始生成",folder_id)
  817. local template_info = cjson.decode(tab.template_info)
  818. if template_info.data~=nil and template_info.data~="" then
  819. template_info = tools.base64decode(template_info.data)
  820. template_info = cjson.decode(template_info)
  821. end
  822. -- tools.dump(template_info)
  823. local gen_num = 0
  824. if template_info.creat_video_list~=nil then
  825. gen_num = #template_info.creat_video_list
  826. end
  827. local custom = {}
  828. if template_info.custom~=nil and template_info.custom~="" then
  829. custom = template_info.custom
  830. end
  831. skynet.error("gen_num",gen_num)
  832. -- tools.dump(custom)
  833. for i = 1, gen_num, 1 do -- 模拟数据每次生成5条
  834. local item_info = template_info.creat_video_list[i]
  835. local head_video,content_video,end_video,bgm_audio,bottom,corner,background_image = s.resp.parse_template_info(tab,item_info)
  836. -- tools.dump(head_video)
  837. -- tools.dump(content_video)
  838. -- tools.dump(end_video)
  839. -- tools.dump(bgm_audio)
  840. local is_corner_mark = item_info.is_corner_mark
  841. local is_video_bottom_text = item_info.is_video_bottom_text
  842. local hot_tag_type = 0
  843. local item_custom = {}
  844. if item_info.custom~=nil and item_info.custom~="" then
  845. item_custom = item_info.custom
  846. end
  847. if is_corner_mark then
  848. hot_tag_type = 1
  849. end
  850. local fictitious_tag_type = 0
  851. if is_video_bottom_text then
  852. fictitious_tag_type = 1
  853. end
  854. local gen_info = {user_id=0,id=0,project_name=tab.project_name,
  855. book_name=tab.book_name,
  856. ratio=tab.ratio,
  857. video_stype=tab.video_stype,
  858. crop_time=tab.video_crop_time,
  859. marketing_title_info={marketing_title_top_h = 0,stroke_h=0,marketing_title=item_info.title_html},
  860. video_list = content_video,
  861. subtitles = {stype=0,subtitle_font="",subtitles_path=template_info.subtitles_path},
  862. subtitles_audio={path = template_info.subtitles_audio_path,duration=template_info.subtitles_audio_duration },
  863. video_last_frame =end_video[1] or {},
  864. video_first_frame = head_video[1] or {},
  865. bgm_list = bgm_audio[1],
  866. corner_mark_image = corner[1] or {},
  867. bottom_text_image = bottom[1] or {},
  868. background_image = background_image[1] or {},
  869. hot_tag = {type=hot_tag_type,path=""}, --上边
  870. fictitious_tag = {type=fictitious_tag_type,path=""}, --下边
  871. custom = custom,
  872. video_custom = item_custom,
  873. }
  874. local video_name = gen_info.book_name.."__视频"..i
  875. local book_name = gen_info.book_name
  876. local ratio = gen_info.ratio
  877. local video_stype = gen_info.video_stype
  878. local video_url = ""
  879. local generate_video_info = {}
  880. generate_video_info =item_info
  881. generate_video_info.book_name = tab.book_name
  882. generate_video_info.project_name = tab.project_name
  883. generate_video_info.ratio = tab.ratio
  884. generate_video_info.video_stype = tab.video_stype
  885. generate_video_info = cjson.encode(generate_video_info)
  886. local video_state = 0
  887. sql = string.format("INSERT INTO generate_video_list_tab (user_id,template_id, folder_id,video_name,book_name,ratio,video_stype,video_url,video_state,generate_video_info) VALUES (%d,%d,%d,'%s','%s',%d,%d,'%s',%d,'%s')",user_id,
  888. template_id,
  889. folder_id,
  890. video_name,
  891. book_name,
  892. ratio,
  893. video_stype,
  894. video_url,video_state,cjson.encode({data=tools.base64encode(generate_video_info)}))
  895. -- skynet.error("123sss")
  896. -- tools.dump(gen_info)
  897. db:query(sql)
  898. -- skynet.error(sql)
  899. sql = string.format("SELECT * FROM generate_video_list_tab WHERE user_id = %d and folder_id = %d and is_use = 0 LIMIT 1",user_id,folder_id)
  900. res = db:query(sql)
  901. if res and #res > 0 then
  902. local tab = {}
  903. if #res > 0 then
  904. tab = tools.getDbResData(res)
  905. gen_info.id = tab.id
  906. gen_info.user_id = user_id
  907. sql = string.format("UPDATE generate_video_list_tab SET is_use = %d WHERE id =%d",1,gen_info.id)
  908. db:query(sql)
  909. skynet.send("tools_work","lua","generate_video",gen_info)
  910. end
  911. -- return true,tab
  912. else
  913. skynet.error("No data found.")
  914. end
  915. end
  916. --获取5个未使用的生成视频文件
  917. tools.response_db_generate_video(fd,true)
  918. end
  919. if res and #res > 0 then --存在这个文件夹,直接写入到这个文件夹
  920. local folder_id = info.id
  921. write_video_db(folder_id)
  922. else -- 没有这个文件夹则创建一个
  923. sql = string.format("INSERT INTO generate_video_folder_list_tab (user_id,book_name, generate_video_info,video_stype) VALUES (%d, '%s','%s' ,%d)",user_id,file_name,"{}",video_stype)
  924. db:query(sql)
  925. --查找新建的文件夹id
  926. sql = string.format("SELECT * FROM generate_video_folder_list_tab WHERE user_id = %d AND book_name = '%s' AND video_stype = %d ",user_id,file_name,video_stype)
  927. res = db:query(sql)
  928. info = tools.getDbResData(res)
  929. local folder_id = info.id
  930. write_video_db(folder_id)
  931. end
  932. end
  933. end)
  934. end,_msg_body,_fd)
  935. return true
  936. end
  937. --获取生成视频文件夹列表
  938. s.resp.get_generate_video_folder_list = function(msg_body,fd)
  939. skynet.fork(function(_fd,_msg_body)
  940. -- local count = msg_body.count
  941. -- local page = (msg_body.page - 1) * count
  942. local sql = string.format("select * from generate_video_folder_list_tab where user_id = %d AND video_stype = %d",_msg_body.user_id,_msg_body.video_stype)
  943. -- local sql = string.format("select * from generate_video_folder_list_tab where user_id = %d AND video_stype = %d limit %d,%d",_msg_body.user_id,_msg_body.video_stype,page,count)
  944. local res = db:query(sql)
  945. -- skynet.error('folder_list',sql)
  946. -- 判断是否找到数据
  947. if res and #res > 0 then
  948. local tab = {}
  949. if #res == 1 then
  950. tab[1] = tools.getDbResData(res)
  951. else
  952. tab = tools.getDbResData(res)
  953. end
  954. skynet.error("Found data:")
  955. -- tools.dump(tab)
  956. -- return true,tab
  957. tools.response_db_generate_video_folder_list(_fd,msg_body,true,tab)
  958. else
  959. skynet.error("No data found.")
  960. tools.response_db_folder_list(_fd,msg_body,false,nil)
  961. -- return false,nil
  962. end
  963. end,fd,msg_body)
  964. return true
  965. end
  966. --获取文件夹内的生成视频
  967. s.resp.get_video_list_by_folder_id = function(msg_body,fd,call_back)
  968. skynet.fork(function(_fd)
  969. local sql = string.format("select * from generate_video_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0",msg_body.user_id,msg_body.folder_id)
  970. local res = db:query(sql)
  971. -- skynet.error('folder_res_list',sql)
  972. -- 判断是否找到数据
  973. if res and #res > 0 then
  974. local tab = {}
  975. if #res == 1 then
  976. tab[1] = tools.getDbResData(res)
  977. else
  978. tab = tools.getDbResData(res)
  979. end
  980. skynet.error("Found data:")
  981. if not call_back and _fd~=nil then
  982. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,true,tab)
  983. else
  984. if call_back ~=nil then
  985. call_back(tab)
  986. end
  987. end
  988. -- tools.dump(tab)
  989. -- return true,tab
  990. else
  991. skynet.error("No data found.",call_back)
  992. if call_back == nil and _fd~=nil then
  993. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,false,nil)
  994. else
  995. if call_back ~=nil then
  996. call_back({})
  997. end
  998. end
  999. -- return false,nil
  1000. end
  1001. end,fd)
  1002. return true
  1003. end
  1004. --根据所有生成视频文件id查到所有文件的url并删除
  1005. local fork_get_gen_video_file_list_url_and_delete = function(delete_list,call_back)
  1006. local url_list = {}
  1007. for i = 1, #delete_list, 1 do
  1008. table.insert(url_list,i, delete_list[i].path)
  1009. end
  1010. skynet.send("tools_work","lua","delete_file",url_list)
  1011. if call_back~=nil then
  1012. call_back()
  1013. end
  1014. end
  1015. --执行删除生成视频文件操作
  1016. local fork_delete_gen_video_file = function(msg_body)
  1017. -- tools.dump(msg_body)
  1018. local listToDelete = {}
  1019. for i = 1, #msg_body.file_list, 1 do
  1020. table.insert(listToDelete,i,msg_body.file_list[i].id)
  1021. end
  1022. local deleteString = table.concat(listToDelete, ',')
  1023. local sql = string.format("UPDATE generate_video_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  1024. db:query(sql)
  1025. skynet.fork(fork_get_gen_video_file_list_url_and_delete,msg_body.file_list,function()
  1026. local delete_sql = string.format("DELETE FROM generate_video_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  1027. db:query(delete_sql)
  1028. skynet.error(delete_sql)
  1029. end)
  1030. end
  1031. --执行删除生成视频文件夹操作
  1032. local fork_delete_gen_video_folder = function(msg)
  1033. for i = 1, #msg.folder_id_list, 1 do
  1034. local isok = s.resp.get_video_list_by_folder_id({
  1035. folder_id = msg.folder_id_list[i],
  1036. user_id = msg.user_id},nil,function(list)
  1037. skynet.error("执行删除生成视频文件夹操作",#list)
  1038. if #list>0 then
  1039. local temp = {}
  1040. for i = 1, #list, 1 do
  1041. table.insert(temp,i,list[i])
  1042. end
  1043. skynet.fork(fork_delete_gen_video_file,{file_list=temp,user_id=msg.user_id})
  1044. end
  1045. end)
  1046. local delete_sql = string.format("DELETE FROM generate_video_folder_list_tab WHERE id = %d", msg.folder_id_list[i])
  1047. db:query(delete_sql)
  1048. end
  1049. end
  1050. --删除生成视频文件夹
  1051. s.resp.delete_generate_video_folder = function(msg_body,fd)
  1052. skynet.fork(fork_delete_gen_video_folder,msg_body,fd)
  1053. return true
  1054. end
  1055. --删除生成视频
  1056. s.resp.delete_generate_video = function(msg_body,fd)
  1057. local temp = {}
  1058. msg_body.file_list = {}
  1059. local listToDelete = {}
  1060. for i = 1, #msg_body.file_id_list, 1 do
  1061. table.insert(listToDelete,i,msg_body.file_id_list[i])
  1062. end
  1063. local deleteString = table.concat(listToDelete, ',')
  1064. local sql = string.format("select * from generate_video_list_tab WHERE id IN (%s) AND is_delete = 0",deleteString)
  1065. local res = db:query(sql)
  1066. skynet.error(sql)
  1067. if res and #res > 0 then
  1068. local tab = {}
  1069. if #res == 1 then
  1070. tab[1] = tools.getDbResData(res)
  1071. else
  1072. tab = tools.getDbResData(res)
  1073. end
  1074. for i = 1, #tab, 1 do
  1075. table.insert(msg_body.file_list,i,tab[i])
  1076. end
  1077. skynet.fork(fork_delete_gen_video_file,msg_body,fd)
  1078. skynet.error("Found data:")
  1079. else
  1080. skynet.error("No data found.")
  1081. end
  1082. return true
  1083. end
  1084. --重命名生成视频文件夹的名字
  1085. s.resp.reset_generate_video_folder_name = function(msg_body,fd)
  1086. local sql = string.format("UPDATE generate_video_list_tab SET video_name ='%s' WHERE id = %d ",msg_body.name,msg_body.folder_id)
  1087. db:query(sql)
  1088. return true
  1089. end
  1090. --生成视频搜索
  1091. s.resp.search_generate_video_file = function(msg_body,fd)
  1092. skynet.fork(function(_fd)
  1093. -- tools.dump(msg_body)
  1094. local sql = string.format("SELECT * FROM generate_video_list_tab WHERE book_name LIKE '%%%s%%' AND is_delete = 0 AND video_stype = %d AND user_id = %d" ,msg_body.search_content,msg_body.video_stype,msg_body.user_id)
  1095. -- skynet.error("sql",sql)
  1096. local res = db:query(sql)
  1097. if res and #res > 0 then
  1098. local tab = {}
  1099. if #res == 1 then
  1100. tab[1] = tools.getDbResData(res)
  1101. else
  1102. tab = tools.getDbResData(res)
  1103. end
  1104. skynet.error("Found data:")
  1105. -- tools.dump(tab)
  1106. -- return true,tab
  1107. tools.response_db_search_generate_video_file(_fd,msg_body,true,tab)
  1108. else
  1109. skynet.error("No data found.")
  1110. -- return false,nil
  1111. tools.response_db_search_generate_video_file(_fd,msg_body,false,nil)
  1112. end
  1113. end,fd)
  1114. return true
  1115. end
  1116. --获取生成视频文件夹列表每个文件夹的文件数量
  1117. s.resp.folder_gen_video_list_nums = function(folder_list,fd,total_count)
  1118. skynet.fork(function(_fd)
  1119. local temp ={}
  1120. for i = 1, #folder_list, 1 do
  1121. local msg_body = folder_list[i]
  1122. -- tools.dump(msg_body)
  1123. local sql = string.format("select * from generate_video_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0",msg_body.user_id,msg_body.id)
  1124. local res = db:query(sql)
  1125. local count = 0
  1126. if res and #res > 0 then
  1127. count = #res
  1128. skynet.error("Found data:")
  1129. -- tools.dump(tab)
  1130. -- return true,tab
  1131. else
  1132. skynet.error("No data found.",msg_body.id)
  1133. end
  1134. temp[msg_body.id] = count
  1135. -- table.insert(temp,msg_body.id,count)
  1136. end
  1137. tools.response_db_folder_generate_video_list_nums(_fd,folder_list,temp,total_count)
  1138. end,fd)
  1139. end
  1140. --更新生成视频的状态
  1141. s.resp.update_gen_video_file_status = function(msg_body)
  1142. local custom = "{}"
  1143. if msg_body.custom~=nil and msg_body.custom~="" then
  1144. custom = msg_body.custom
  1145. end
  1146. local sql = string.format("UPDATE generate_video_list_tab SET video_url = '%s', path = '%s' , custom = '%s', video_state = %d WHERE id = %d and user_id = %d",msg_body.surl,msg_body.path,custom,msg_body.state,msg_body.id,msg_body.user_id)
  1147. db:query(sql)
  1148. skynet.error(sql)
  1149. return true
  1150. end
  1151. --检测指定类型文件是否够数量
  1152. s.resp.checkNumsByType = function(msg_body)
  1153. local duration = 0
  1154. if msg_body.duration~= nil then
  1155. duration = msg_body.duration
  1156. end
  1157. local limit = msg_body.limit
  1158. local type = msg_body.type
  1159. local sql = string.format("select *from res_list_tab where duration >= %f AND file_type= %d AND user_id = %d LIMIT %d",duration,type,msg_body.user_id,limit)
  1160. -- skynet.error("sql",sql)
  1161. local res = db:query(sql)
  1162. if res and #res > 0 then
  1163. if #res >= tonumber(limit) then
  1164. return true
  1165. end
  1166. return false
  1167. else
  1168. return false
  1169. end
  1170. end
  1171. --修改密码
  1172. s.resp.reset_password = function(msg_body)
  1173. local sql = string.format("UPDATE users SET password = '%s' WHERE id = %d ",msg_body.new_pw,msg_body.user_id)
  1174. db:query(sql)
  1175. return true
  1176. end
  1177. --检测是否有此文件
  1178. s.resp.check_have_file = function(msg_body)
  1179. local sql = string.format("select * from res_list_tab where ref_id = %d AND is_delete = 0 AND user_id = %d LIMIT 1",msg_body.file_id,msg_body.user_id)
  1180. -- skynet.error("sql",sql)
  1181. local res = db:query(sql)
  1182. -- 判断是否找到数据
  1183. if res and #res > 0 then
  1184. local tab = tools.getDbResData(res)
  1185. skynet.error("Found data:",tab)
  1186. return true,tab
  1187. else
  1188. skynet.error("No data found.")
  1189. return false,nil
  1190. end
  1191. end
  1192. --检测是否有此文件夹
  1193. s.resp.check_have_folder = function(msg_body)
  1194. local sql = string.format("select * from folder_list_tab where ref_id = %d AND user_id = %d LIMIT 1",msg_body.folder_id,msg_body.user_id)
  1195. -- skynet.error("sql",sql)
  1196. local res = db:query(sql)
  1197. -- 判断是否找到数据
  1198. if res and #res > 0 then
  1199. local tab = tools.getDbResData(res)
  1200. skynet.error("Found data:",tab)
  1201. return true,tab
  1202. else
  1203. skynet.error("No data found.")
  1204. return false,nil
  1205. end
  1206. end
  1207. --检测滚屏模板是否正常
  1208. s.resp.check_template_scroll_isRight = function(info)
  1209. local isok = s.resp.checkNumsByType({
  1210. duration = 0,
  1211. type = 6,
  1212. limit = 1,
  1213. user_id = info.user_id})
  1214. if not isok then
  1215. return false,cjson.encode({code=10001,msg = "新建模板失败,图片资源不足!"})
  1216. end
  1217. isok = s.resp.checkNumsByType({
  1218. duration = 0,
  1219. type = 4,
  1220. limit = 1,
  1221. user_id = info.user_id}
  1222. )
  1223. if not isok then
  1224. return false,cjson.encode({code=10001,msg = "新建模板失败,BGM音频不足!"})
  1225. end
  1226. isok = s.resp.checkNumsByType({
  1227. duration = 0,
  1228. type = 3,
  1229. limit = 1,
  1230. user_id = info.user_id}
  1231. )
  1232. if not isok then
  1233. return false,cjson.encode({code=10001,msg = "新建模板失败,片尾资源不足!"})
  1234. end
  1235. return true,nil
  1236. end
  1237. --检查模板是否正常
  1238. s.resp.check_template_isRight = function(info)
  1239. local isok = s.resp.checkNumsByType({
  1240. duration = 0,
  1241. type = 1,
  1242. limit = 1,
  1243. user_id = info.user_id})
  1244. if not isok then
  1245. return false,cjson.encode({code=10001,msg = "新建模板失败,片头资源不足!"})
  1246. end
  1247. isok = s.resp.checkNumsByType({
  1248. duration = 0,
  1249. type = 3,
  1250. limit = 1,
  1251. user_id = info.user_id}
  1252. )
  1253. if not isok then
  1254. return false,cjson.encode({code=10001,msg = "新建模板失败,片尾资源不足"})
  1255. end
  1256. local subtitles_audio_duration = 0
  1257. if info.template_info ~=nil and info.template_info~="" then
  1258. local template_info = cjson.decode(info.template_info)
  1259. template_info = tools.base64decode(template_info.data)
  1260. template_info = cjson.decode(template_info)
  1261. if template_info.subtitles_audio_duration~=nil then
  1262. subtitles_audio_duration = tonumber(template_info.subtitles_audio_duration)
  1263. end
  1264. end
  1265. local duration = info.video_crop_time + 5
  1266. local limit = math.floor(subtitles_audio_duration/duration)
  1267. skynet.error("limit",limit)
  1268. skynet.error("subtitles_audio_duration",subtitles_audio_duration)
  1269. skynet.error("duration",duration)
  1270. isok = s.resp.checkNumsByType({
  1271. duration = duration,
  1272. type = 2,
  1273. limit =limit+1 ,
  1274. user_id = info.user_id}
  1275. )
  1276. if not isok then
  1277. return false,cjson.encode({code=10001,msg = "正片资源不足"})
  1278. end
  1279. isok = s.resp.checkNumsByType({
  1280. duration = 0,
  1281. type = 4,
  1282. limit = 1,
  1283. user_id = info.user_id}
  1284. )
  1285. if not isok then
  1286. return false,cjson.encode({code=10001,msg = "BGM音效资源不足!"})
  1287. end
  1288. return true,nil
  1289. end
  1290. --接收到删除引用的文件列表信息
  1291. s.resp.recv_ref_file_list_del_info = function(msg_body)
  1292. skynet.error("接收到删除引用的文件列表信息")
  1293. local listToDelete = {}
  1294. for i = 1, #msg_body.file_id_list, 1 do
  1295. table.insert(listToDelete,i,msg_body.file_id_list[i])
  1296. end
  1297. local deleteString = table.concat(listToDelete, ',')
  1298. -- local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE ref_id IN (%s)",1,deleteString)
  1299. -- db:query(sql)
  1300. local delete_sql = string.format("DELETE FROM res_list_tab WHERE ref_id IN (%s)", deleteString)
  1301. db:query(delete_sql)
  1302. skynet.error(delete_sql)
  1303. return true
  1304. end
  1305. --检测引用的文件夹是否有自己上传的文件
  1306. s.resp.check_ref_folder_have_self_file = function(msg_body)
  1307. local check_user_folder = function(info) --检测用户的文件夹
  1308. local user_id = info.user_id
  1309. local isHave = false
  1310. local folder_id = info.id
  1311. local sql = string.format("select * from res_list_tab where folder_id = %d AND is_delete = 0 AND user_id = %d ",folder_id,user_id)
  1312. local res = db:query(sql)
  1313. local delete_tab = {}
  1314. -- 判断是否找到数据
  1315. if res and #res > 0 then
  1316. local tab = {}
  1317. if #res == 1 then
  1318. tab[1] = tools.getDbResData(res)
  1319. else
  1320. tab = tools.getDbResData(res)
  1321. end
  1322. local count = 1
  1323. for i = 1, #tab, 1 do
  1324. if tab[i].ref_id==0 then
  1325. isHave = true
  1326. else
  1327. table.insert(delete_tab,count,tab[i])
  1328. count = count+1
  1329. end
  1330. end
  1331. return isHave,delete_tab,info
  1332. else
  1333. return isHave,delete_tab,info
  1334. end
  1335. end
  1336. local sql = ""
  1337. local folder_id = msg_body.folder_id
  1338. --获取当前引用的文件夹
  1339. sql = string.format("select * from folder_list_tab where ref_id = %d ",folder_id)
  1340. local res = db:query(sql)
  1341. local tab_list = {}
  1342. if res and #res > 0 then
  1343. local tab = {}
  1344. if #res == 1 then
  1345. tab[1] = tools.getDbResData(res)
  1346. else
  1347. tab = tools.getDbResData(res)
  1348. end
  1349. for i = 1, #tab, 1 do
  1350. local isHave,delete_tab,info = check_user_folder(tab[i])
  1351. table.insert(tab_list,i,{isHave = isHave,delete_tab=delete_tab,info=info})
  1352. end
  1353. return tab_list
  1354. else
  1355. return tab_list
  1356. end
  1357. end
  1358. --接收到删除引用的文件夹列表信息
  1359. s.resp.recv_ref_folder_list_del_info = function(msg_body)
  1360. local delete_file_list = function(delete_tab)
  1361. local listToDelete = {}
  1362. for i = 1, #delete_tab, 1 do
  1363. table.insert(listToDelete,i,delete_tab[i].ref_id)
  1364. end
  1365. s.resp.recv_ref_file_list_del_info({file_id_list=listToDelete})
  1366. end
  1367. -- skynet.error("接收到删除引用的文件夹列表信息")
  1368. -- tools.dump(msg_body)
  1369. for i = 1, #msg_body.folder_id_list, 1 do
  1370. local folder_id = msg_body.folder_id_list[i]
  1371. local list = s.resp.check_ref_folder_have_self_file({folder_id=folder_id})
  1372. for i = 1, #list, 1 do
  1373. tools.dump(list[i])
  1374. delete_file_list(list[i].delete_tab)
  1375. local isHave = list[i].isHave
  1376. local folder_info = list[i].info
  1377. if isHave then --有自定义的文件不删除这个文件夹,但是要将其引用设置为0
  1378. skynet.error("有自定义的文件不删除这个文件夹,但是要将其引用设置为0")
  1379. local sql = string.format("UPDATE folder_list_tab SET ref_id = %d WHERE id = %d AND user_id = %d",0,folder_info.id,folder_info.user_id)
  1380. db:query(sql)
  1381. skynet.error(sql)
  1382. else --没有自己上传的文件夹,将其文件夹删除
  1383. skynet.error("没有自己上传的文件夹,将其文件夹删除")
  1384. local sql = string.format("DELETE FROM folder_list_tab WHERE id = %d AND user_id = %d ",folder_info.id,folder_info.user_id)
  1385. db:query(sql)
  1386. skynet.error(sql)
  1387. end
  1388. end
  1389. end
  1390. return true
  1391. end
  1392. --接收到修改引用的文件信息
  1393. s.resp.recv_ref_file_modify_info = function(msg_body)
  1394. local file_id = msg_body.file_id
  1395. local name = msg_body.name
  1396. local sql = string.format("UPDATE res_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,file_id)
  1397. db:query(sql)
  1398. return true
  1399. end
  1400. s.resp.update_generate_video_custom = function(msg_body)
  1401. local list = msg_body.data
  1402. skynet.fork(function()
  1403. for i = 1, #list, 1 do
  1404. local video_id = list[i].video_id
  1405. local custom = "{}"
  1406. if list[i].custom~=nil and list[i].custom~="" then
  1407. custom = list[i].custom
  1408. end
  1409. local sql = string.format("UPDATE generate_video_list_tab SET custom = '%s' WHERE id = %d and user_id = %d",custom,video_id,msg_body.user_id)
  1410. db:query(sql)
  1411. -- skynet.error(sql)
  1412. end
  1413. end)
  1414. -- skynet.error(sql)
  1415. return true
  1416. end
  1417. --接收到修改引用的文件夹信息
  1418. s.resp.recv_ref_folder_modify_info = function(msg_body)
  1419. local folder_id = msg_body.folder_id
  1420. local name = msg_body.name
  1421. local sql = string.format("UPDATE folder_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,folder_id)
  1422. db:query(sql)
  1423. return true
  1424. end
  1425. --获取用户所有的文件数
  1426. s.resp.get_user_folder_list_nums = function(msg_body)
  1427. local sql = string.format("select COUNT(user_id) AS nums from folder_list_tab where user_id = %d ",msg_body.user_id)
  1428. local res = db:query(sql)
  1429. local count = 0
  1430. if res and #res > 0 then
  1431. count = res.nums
  1432. end
  1433. return count
  1434. end
  1435. --获取用户指定类型的数量
  1436. s.resp.get_user_file_count_by_type = function(msg_body)
  1437. local sql = string.format("select COUNT(user_id) AS nums from res_list_tab where user_id = %d AND file_type = %d",msg_body.user_id,msg_body.file_type)
  1438. local res = db:query(sql)
  1439. local count = 0
  1440. if res and #res > 0 then
  1441. count = res.nums
  1442. end
  1443. return count
  1444. end
  1445. s.resp.check_file_name_list_is_repeat_by_folder_id = function(msg_body,fd,call_back)
  1446. -- tools.dump(msg_body)
  1447. skynet.fork(function()
  1448. local nameString = ""
  1449. for i = 1, #msg_body.file_name_list, 1 do
  1450. nameString = nameString.."'"..msg_body.file_name_list[i].."'"
  1451. if i<#msg_body.file_name_list then
  1452. nameString = nameString..","
  1453. end
  1454. end
  1455. local sql = string.format("SELECT file_name FROM res_list_tab WHERE file_name IN (%s) AND folder_id = %d AND user_id = %d ",
  1456. nameString,
  1457. msg_body.folder_id,
  1458. msg_body.user_id)
  1459. local res = db:query(sql)
  1460. local tab = {}
  1461. -- skynet.error("sql",sql)
  1462. if #res == 1 then
  1463. tab[1] = tools.getDbResData(res)
  1464. else
  1465. if #res>0 then
  1466. tab = tools.getDbResData(res)
  1467. end
  1468. end
  1469. -- tools.dump(tab)
  1470. local normal_file_name_list ={}
  1471. local repeat_file_name_list ={}
  1472. for i = 1, #tab, 1 do
  1473. table.insert(repeat_file_name_list,i,tab[i].file_name)
  1474. end
  1475. function check(file_name,list)
  1476. for i = 1, #list, 1 do
  1477. if list[i]==file_name then
  1478. return false
  1479. end
  1480. end
  1481. return true
  1482. end
  1483. local count = 1
  1484. for i = 1, #msg_body.file_name_list, 1 do
  1485. if check(msg_body.file_name_list[i],repeat_file_name_list) then
  1486. table.insert(normal_file_name_list,count,msg_body.file_name_list[i])
  1487. count = count+1
  1488. end
  1489. end
  1490. if call_back~=nil then
  1491. call_back({normal_file_name_list=normal_file_name_list,repeat_file_name_list=repeat_file_name_list})
  1492. else
  1493. tools.response_db_check_folder_repeat_file(fd,msg_body,true,{normal_file_name_list=normal_file_name_list,repeat_file_name_list=repeat_file_name_list})
  1494. end
  1495. end)
  1496. return true
  1497. end
  1498. --获取用户的分享用户列表
  1499. s.resp.get_share_user_list = function(msg_body,fd)
  1500. local isOk,user_data = s.resp.getUserById(msg_body.user_id)
  1501. local temp = {}
  1502. if isOk then
  1503. local select_share = nil
  1504. if user_data.user_info~=nil and user_data.user_info~="" then
  1505. local info = cjson.decode(user_data.user_info)
  1506. select_share = info.select_share
  1507. end
  1508. local count = 1
  1509. skynet.fork(function()
  1510. function action()
  1511. tools.response_db_get_share_user_list(fd,msg_body,true,temp)
  1512. end
  1513. if select_share~=nil and select_share~="" then
  1514. for i = 1, #select_share, 1 do
  1515. local user_id = select_share[i]
  1516. local _,ud = s.resp.getUserById(user_id)
  1517. if _ then
  1518. if ud.id~=msg_body.user_id then
  1519. table.insert(temp,count,ud)
  1520. count = count +1
  1521. end
  1522. end
  1523. end
  1524. end
  1525. if user_data.permit_id == 2 then --说明是管理
  1526. s.resp.get_group_list(user_data.group_id,function(list)
  1527. -- skynet.error("user_data.group_id",user_data.group_id)
  1528. -- tools.dump(list)
  1529. for i = 1, #list, 1 do
  1530. if list[i].permit_id~=2 and list[i].id~=user_data.id then
  1531. table.insert(temp,count,list[i])
  1532. count = count +1
  1533. end
  1534. end
  1535. action()
  1536. end)
  1537. else
  1538. action()
  1539. end
  1540. end)
  1541. return true
  1542. end
  1543. return false
  1544. end
  1545. --根据名字获取文件夹
  1546. s.resp.get_folder_by_name = function(msg_body,fd)
  1547. local sql = string.format("SELECT * FROM folder_list_tab WHERE folder_name = '%s' AND classification_id = %d AND folder_type = %d AND user_id = %d LIMit 1", msg_body.folder_name,msg_body.classification_id,msg_body.folder_type,msg_body.user_id)
  1548. local res = db:query(sql)
  1549. local tab = {}
  1550. tab = tools.getDbResData(res)
  1551. if #res <=0 then
  1552. return false,nil
  1553. end
  1554. return true,tab
  1555. end
  1556. s.resp.get_folder_have_file_name = function(user_id,folder_id,file_name)
  1557. local sql = string.format("SELECT * FROM res_list_tab WHERE file_name = '%s' AND user_id = %d AND folder_id = %d LIMit 1", file_name,user_id,folder_id)
  1558. local res = db:query(sql)
  1559. local tab = {}
  1560. tab = tools.getDbResData(res)
  1561. skynet.error("#res",#res)
  1562. if #res <=0 then
  1563. return false,nil
  1564. end
  1565. return true,tab
  1566. end
  1567. --分享文件列表
  1568. s.resp.share_file_list = function(msg_body,fd)
  1569. function insert_target(file,folder_id,target_user_id)
  1570. local is_have,_ = s.resp.get_folder_have_file_name(target_user_id,folder_id,file.file_name)
  1571. if is_have==true then
  1572. else
  1573. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",
  1574. target_user_id,
  1575. folder_id,
  1576. file.file_type,
  1577. file.file_name,
  1578. file.file_info,
  1579. file.duration,
  1580. file.classification_id,
  1581. file.id)
  1582. skynet.error("sql",sql)
  1583. db:query(sql)
  1584. end
  1585. -- skynet.error(sql)
  1586. end
  1587. for j = 1, #msg_body.target_user_id_list, 1 do
  1588. local target_user_id = msg_body.target_user_id_list[j]
  1589. local my_user_id = msg_body.user_id
  1590. skynet.fork(function()
  1591. for i = 1, #msg_body.file_id_list, 1 do
  1592. local file_id = msg_body.file_id_list[i].file_id
  1593. local folder_name = msg_body.file_id_list[i].folder_name
  1594. local sql = string.format("SELECT * FROM res_list_tab WHERE id = %d limit 1", file_id)
  1595. local res = db:query(sql)
  1596. local tab = tools.getDbResData(res)
  1597. if #res>0 then
  1598. --检测被分享的是否拥有此文件夹
  1599. local isOk,folder_data = s.resp.get_folder_by_name({user_id=target_user_id,folder_name=folder_name,classification_id=tab.classification_id,folder_type=tab.file_type})
  1600. if isOk then --拥有此文件夹,直接插入
  1601. skynet.error("拥有此文件夹,直接插入",folder_name)
  1602. insert_target(tab,folder_data.id,target_user_id)
  1603. else -- 没有此文件夹,创建一个再插入
  1604. local new_sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,is_public,classification_id) VALUES (%d, '%s',%d,%d,%d)",
  1605. target_user_id,
  1606. folder_name,
  1607. tab.file_type
  1608. ,0
  1609. ,tab.classification_id)
  1610. -- skynet.error(sql)
  1611. local new_res = db:query(new_sql)
  1612. new_sql = string.format("select *from folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND classification_id = %d limit 1",
  1613. target_user_id,
  1614. folder_name,
  1615. tab.file_type,
  1616. tab.classification_id)
  1617. new_res = db:query(new_sql)
  1618. if new_res and #new_res > 0 then
  1619. local folder_info = {}
  1620. folder_info = tools.getDbResData(new_res)
  1621. insert_target(tab,folder_info.id,target_user_id)
  1622. end
  1623. end
  1624. end
  1625. end
  1626. end)
  1627. end
  1628. return true
  1629. end
  1630. --分享文件夹列表
  1631. s.resp.share_folder_list = function(msg_body,fd)
  1632. function getInfoByName(name,list)
  1633. for i = 1, #list, 1 do
  1634. if list[i].file_name == name then
  1635. return list[i]
  1636. end
  1637. end
  1638. return nil
  1639. end
  1640. function insert_target_folder(target_folder_id,my_folder_id,my_user_id,target_user_id)
  1641. local isok = s.resp.folder_res_list({
  1642. folder_id = my_folder_id,
  1643. user_id =my_user_id,
  1644. is_public=0},nil,function(list)
  1645. skynet.error("insert_target_folder",#list)
  1646. if #list>0 then
  1647. local file_name_list = {}
  1648. for i = 1, #list, 1 do
  1649. local _f = list[i]
  1650. table.insert(file_name_list,i,_f.file_name)
  1651. end
  1652. s.resp.check_file_name_list_is_repeat_by_folder_id({file_name_list=file_name_list,user_id=target_user_id,folder_id=target_folder_id},fd,function(data)
  1653. for i = 1, #data.normal_file_name_list, 1 do
  1654. local file = getInfoByName(data.normal_file_name_list[i],list)
  1655. if file~=nil then
  1656. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",
  1657. target_user_id,
  1658. target_folder_id,
  1659. file.file_type,
  1660. file.file_name,
  1661. file.file_info,
  1662. file.duration,
  1663. file.classification_id,
  1664. file.id)
  1665. db:query(sql)
  1666. end
  1667. end
  1668. end)
  1669. end
  1670. end)
  1671. end
  1672. for j = 1, #msg_body.target_user_id_list, 1 do
  1673. local target_user_id = msg_body.target_user_id_list[j]
  1674. local my_user_id = msg_body.user_id
  1675. skynet.fork(function()
  1676. for i = 1, #msg_body.folder_id_list, 1 do
  1677. local folder_id = msg_body.folder_id_list[i].folder_id
  1678. local folder_name = msg_body.folder_id_list[i].folder_name
  1679. local sql = string.format("SELECT * FROM folder_list_tab WHERE id = %d limit 1", folder_id)
  1680. local res = db:query(sql)
  1681. local tab = tools.getDbResData(res)
  1682. if #res>0 then
  1683. --检测被分享的是否拥有此文件夹
  1684. local isOk,folder_data = s.resp.get_folder_by_name({user_id=target_user_id,folder_name=folder_name,classification_id=tab.classification_id,folder_type=tab.folder_type})
  1685. if isOk then --拥有此文件夹,直接插入
  1686. skynet.error("拥有此文件夹,直接插入 ")
  1687. insert_target_folder(folder_data.id,folder_id,my_user_id,target_user_id)
  1688. else -- 没有此文件夹,创建一个再插入
  1689. local new_sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,is_public,classification_id) VALUES (%d, '%s',%d,%d,%d)",
  1690. target_user_id,
  1691. folder_name,
  1692. tab.folder_type
  1693. ,0
  1694. ,tab.classification_id)
  1695. skynet.error("没有此文件夹,创建一个再插入 ")
  1696. skynet.error(sql)
  1697. db:query(new_sql)
  1698. new_sql = string.format("select *from folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND classification_id = %d limit 1",
  1699. target_user_id,
  1700. folder_name,
  1701. tab.folder_type,
  1702. tab.classification_id)
  1703. local new_res = db:query(new_sql)
  1704. if new_res and #new_res > 0 then
  1705. local folder_info = {}
  1706. folder_info = tools.getDbResData(new_res)
  1707. insert_target_folder(folder_info.id,folder_id,my_user_id,target_user_id)
  1708. end
  1709. end
  1710. end
  1711. end
  1712. end)
  1713. end
  1714. return true
  1715. end
  1716. s.resp.get_folder_info = function(msg_body,fd)
  1717. local listToFind= {}
  1718. for i = 1, #msg_body.folder_id_list, 1 do
  1719. table.insert(listToFind,i,msg_body.folder_id_list[i])
  1720. end
  1721. local findString = table.concat(listToFind, ',')
  1722. skynet.fork(function()
  1723. local sql = string.format("SELECT * FROM folder_list_tab WHERE id IN (%s) AND user_id = %d ", findString,msg_body.user_id)
  1724. local res = db:query(sql)
  1725. if res and #res > 0 then
  1726. local tab = {}
  1727. if #res == 1 then
  1728. tab[1] = tools.getDbResData(res)
  1729. else
  1730. tab = tools.getDbResData(res)
  1731. end
  1732. tools.response_db_get_folder_info(fd,msg_body,true,tab)
  1733. else
  1734. tools.response_db_get_folder_info(fd,msg_body,false,{})
  1735. end
  1736. end)
  1737. end
  1738. s.init = function()
  1739. db=mysql.connect({
  1740. host=runconfig.db_tost,
  1741. port=runconfig.db_port,
  1742. database=runconfig.db_name,
  1743. user="root",
  1744. password=runconfig.db_pw,
  1745. max_packet_size = 1024 * 1024,
  1746. on_connect = nil
  1747. })
  1748. if not db then
  1749. skynet.error("failed to connect")
  1750. skynet.exit()
  1751. else
  1752. skynet.error("success to connect to mysql server")
  1753. end
  1754. --设置utf8字符集
  1755. local res = db:query("set charset utf8");
  1756. end
  1757. s.start(...)