init.lua 73 KB

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