init.lua 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  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. s.resp.get_file_info_by_id = function(msg_body,fd)
  454. skynet.fork(function()
  455. local isOk,file_info = s.resp.get_res_file_by_id(msg_body.file_id,msg_body.user_id)
  456. if isOk then
  457. tools.response_db_get_file_info_by_id(fd,msg_body,true,file_info)
  458. else
  459. tools.response_db_get_file_info_by_id(fd,msg_body,false,nil)
  460. end
  461. end)
  462. return true
  463. end
  464. s.resp.get_not_have_file_list_by_list = function(msg_body,fd)
  465. skynet.fork(function()
  466. local temp = {}
  467. local count = 1
  468. for i = 1, #msg_body.file_id_list, 1 do
  469. local file_id = msg_body.file_id_list[i]
  470. local isOk,file_info = s.resp.get_res_file_by_id(file_id,msg_body.user_id)
  471. if not isOk then
  472. table.insert(temp,count,file_id)
  473. count = count +1
  474. end
  475. end
  476. tools.response_db_get_not_have_file_list_by_list(fd,msg_body,true,temp)
  477. end)
  478. return true
  479. end
  480. --根据id获取资源
  481. s.resp.get_res_file_by_id = function(file_id,user_id)
  482. local select_user_id_sql = ""
  483. if user_id~=nil then
  484. select_user_id_sql = " AND user_id = " ..user_id
  485. end
  486. local sql = string.format("select * from res_list_tab where id = %d AND is_delete = 0"..select_user_id_sql,file_id)
  487. -- skynet.error("sql",sql)
  488. local res = db:query(sql)
  489. -- 判断是否找到数据
  490. if res and #res > 0 then
  491. local tab = tools.getDbResData(res)
  492. skynet.error("Found data:",tab)
  493. return true,tab
  494. else
  495. skynet.error("No data found.")
  496. return false,nil
  497. end
  498. end
  499. --根据id获取文件夹
  500. s.resp.get_res_folder_by_id = function(folder_id)
  501. local sql = string.format("select * from folder_list_tab where id = %d ",folder_id)
  502. -- skynet.error("sql",sql)
  503. local res = db:query(sql)
  504. -- 判断是否找到数据
  505. if res and #res > 0 then
  506. local tab = tools.getDbResData(res)
  507. skynet.error("Found data:",tab)
  508. return true,tab
  509. else
  510. skynet.error("No data found.")
  511. return false,nil
  512. end
  513. end
  514. --搜索资源
  515. s.resp.search_res = function(msg_body,fd)
  516. skynet.fork(function(_fd)
  517. local sql_duration = ""
  518. if msg_body.duration~=nil then
  519. sql_duration = string.format(" AND duration >= %f",msg_body.duration)
  520. end
  521. 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)
  522. -- skynet.error("sql",sql)
  523. local res = db:query(sql)
  524. if res and #res > 0 then
  525. local tab = {}
  526. if #res == 1 then
  527. tab[1] = tools.getDbResData(res)
  528. else
  529. tab = tools.getDbResData(res)
  530. end
  531. skynet.error("Found data:")
  532. -- tools.dump(tab)
  533. -- return true,tab
  534. tools.response_db_search_res(_fd,msg_body,true,tab)
  535. else
  536. skynet.error("No data found.")
  537. -- return false,nil
  538. tools.response_db_search_res(_fd,msg_body,false,nil)
  539. end
  540. end,fd)
  541. return true
  542. end
  543. --根据所有文件id查到所有文件的url并删除
  544. local fork_get_file_list_url_and_delete = function(deleteString,call_back)
  545. local sql = string.format("SELECT * FROM res_list_tab WHERE id IN (%s)", deleteString)
  546. local res = db:query(sql)
  547. if res and #res > 0 then
  548. local tab = {}
  549. if #res == 1 then
  550. tab[1] = tools.getDbResData(res)
  551. else
  552. tab = tools.getDbResData(res)
  553. end
  554. skynet.error("Found data:fork_get_file_list_url_and_delete")
  555. local url_list = {}
  556. local count = 1
  557. for i = 1, #tab, 1 do
  558. if tab[i].ref_id==0 then
  559. local file_info = cjson.decode(tab[i].file_info)
  560. table.insert(url_list,count,file_info.path)
  561. count = count + 1
  562. end
  563. end
  564. skynet.send("tools_work","lua","delete_file",url_list)
  565. -- tools.dump(url_list)
  566. -- return true,tab
  567. else
  568. skynet.error("No data found.")
  569. -- return false,nil
  570. end
  571. if call_back~=nil then
  572. call_back()
  573. end
  574. end
  575. --执行删除文件操作
  576. local fork_delete_file = function(msg_body)
  577. -- tools.dump(msg_body)
  578. local listToDelete = {}
  579. for i = 1, #msg_body.file_id_list, 1 do
  580. table.insert(listToDelete,i,msg_body.file_id_list[i])
  581. end
  582. local deleteString = table.concat(listToDelete, ',')
  583. local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  584. db:query(sql)
  585. skynet.fork(fork_get_file_list_url_and_delete,deleteString,function()
  586. 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)
  587. db:query(delete_sql)
  588. end)
  589. end
  590. --执行删除文件夹操作
  591. local fork_delete_folder = function(msg)
  592. for i = 1, #msg.folder_id_list, 1 do
  593. local isok = s.resp.folder_res_list({
  594. folder_id = msg.folder_id_list[i],
  595. user_id = msg.user_id,
  596. is_public=msg.is_public},nil,function(list)
  597. -- skynet.error("执行删除文件夹操作",#list)
  598. if #list>0 then
  599. local temp = {}
  600. for i = 1, #list, 1 do
  601. table.insert(temp,i,list[i].id)
  602. end
  603. skynet.fork(fork_delete_file,{file_id_list=temp,user_id=msg.user_id,is_public=msg.is_public})
  604. end
  605. end)
  606. end
  607. end
  608. --删除资源文件
  609. s.resp.delete_res = function(msg_body)
  610. skynet.fork(fork_delete_file,msg_body)
  611. return true
  612. end
  613. --删除文件夹
  614. s.resp.delete_folder =function(msg_body)
  615. local listToDelete = {}
  616. for i = 1, #msg_body.folder_id_list, 1 do
  617. table.insert(listToDelete,i,msg_body.folder_id_list[i])
  618. end
  619. local deleteString = table.concat(listToDelete, ',')
  620. 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)
  621. db:query(sql)
  622. skynet.fork(fork_delete_folder,msg_body)
  623. return true
  624. end
  625. --新建模板
  626. s.resp.create_template = function(msg_body,fd)
  627. local template_info = "{}"
  628. if msg_body.template_info~=nil and msg_body.template_info~="" then
  629. template_info = msg_body.template_info
  630. end
  631. 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)
  632. local res = db:query(sql)
  633. -- skynet.error("sql",sql)
  634. return true
  635. end
  636. --获取模板列表
  637. s.resp.get_template_list = function(msg_body,fd)
  638. skynet.fork(function(_fd)
  639. 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)
  640. skynet.error("sql",sql)
  641. local res = db:query(sql)
  642. -- 判断是否找到数据
  643. if res and #res > 0 then
  644. local tab = {}
  645. if #res == 1 then
  646. tab[1] = tools.getDbResData(res)
  647. else
  648. tab = tools.getDbResData(res)
  649. end
  650. skynet.error("Found data:",tab)
  651. -- return true,tab
  652. tools.response_db_get_template_list(_fd,msg_body,true,tab)
  653. else
  654. skynet.error("No data found.")
  655. -- return false,nil
  656. tools.response_db_get_template_list(_fd,msg_body,false,nil)
  657. end
  658. end,fd)
  659. return true
  660. end
  661. --删除模板
  662. s.resp.delete_template = function(msg_body)
  663. local listToDelete = {}
  664. for i = 1, #msg_body.template_id_list, 1 do
  665. table.insert(listToDelete,i,msg_body.template_id_list[i])
  666. end
  667. local deleteString = table.concat(listToDelete, ',')
  668. local sql = string.format("DELETE FROM template_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  669. db:query(sql)
  670. skynet.error(sql)
  671. return true
  672. end
  673. --保存/修改模板
  674. s.resp.save_template = function(msg_body,fd)
  675. local template_info = "{}"
  676. -- tools.dump(msg_body)
  677. if msg_body.template_info~="" and msg_body.template_info~=nil then
  678. template_info = msg_body.template_info
  679. end
  680. 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)
  681. -- skynet.error("sql",sql)
  682. db:query(sql)
  683. return true
  684. end
  685. --搜索模板
  686. s.resp.search_template = function(msg_body,fd)
  687. skynet.fork(function(_fd)
  688. 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)
  689. -- 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)
  690. skynet.error("sql",sql)
  691. local res = db:query(sql)
  692. -- 判断是否找到数据
  693. if res and #res > 0 then
  694. local tab = {}
  695. if #res == 1 then
  696. tab[1] = tools.getDbResData(res)
  697. else
  698. tab = tools.getDbResData(res)
  699. end
  700. skynet.error("Found data:",tab)
  701. -- return true,tab
  702. tools.response_db_search_template(_fd,msg_body,true,tab)
  703. else
  704. skynet.error("No data found.")
  705. -- return false,nil
  706. tools.response_db_search_template(_fd,msg_body,false,nil)
  707. end
  708. end,fd)
  709. return true
  710. end
  711. --根据模板ID获取模板信息
  712. s.resp.get_template_info_by_id = function(msg_body,fd,call_back)
  713. skynet.fork(function(_fd)
  714. local sql = string.format("SELECT * FROM template_list_tab WHERE user_id = %d AND id = %d",msg_body.user_id ,msg_body.template_id)
  715. -- 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)
  716. skynet.error("sql",sql)
  717. local res = db:query(sql)
  718. -- 判断是否找到数据
  719. if res and #res > 0 then
  720. local tab = tools.getDbResData(res)
  721. skynet.error("Found data:",tab)
  722. -- return true,tab
  723. if call_back~=nil and fd ==nil then
  724. call_back(tab)
  725. else
  726. tools.response_db_get_template_info_by_id(_fd,msg_body,true,tab)
  727. end
  728. else
  729. skynet.error("No data found.")
  730. -- return false,nil
  731. if call_back~=nil and fd ==nil then
  732. call_back(nil)
  733. else
  734. tools.response_db_search_template(_fd,msg_body,false,nil)
  735. end
  736. end
  737. end,fd)
  738. return true
  739. end
  740. --获取指定类型所有文件
  741. s.resp.get_file_list_by_type = function(msg_body,fd)
  742. skynet.fork(function(_fd,_msg_body)
  743. local sql_duration = ""
  744. if msg_body.duration~=nil then
  745. sql_duration = string.format(" AND duration >= %f",msg_body.duration)
  746. end
  747. 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)
  748. local res = db:query(sql)
  749. -- skynet.error(sql)
  750. if res and #res > 0 then
  751. local tab = {}
  752. if #res == 1 then
  753. tab[1] = tools.getDbResData(res)
  754. else
  755. tab = tools.getDbResData(res)
  756. end
  757. tools.response_db_get_file_list_by_type(_fd,_msg_body,true,tab)
  758. -- return true,tab
  759. else
  760. skynet.error("No data found.")
  761. -- return false,nil
  762. tools.response_db_get_file_list_by_type(_fd,_msg_body,false,nil)
  763. end
  764. end,fd,msg_body)
  765. end
  766. --随机一个类型,获取相应数量的资源列表
  767. s.resp.get_random_res_by_type = function(user_id,file_type,classification_id,duration)
  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", file_type,classification_id,duration,user_id)
  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. -- local test = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  778. -- tools.shuffle(tab)
  779. -- tools.dump(test)
  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.get_random_res_by_folder_list = function(file_type,classification_id,folder_list,duration,user_id)
  789. local listToSelect = {}
  790. for i = 1, #folder_list, 1 do
  791. table.insert(listToSelect,i,folder_list[i].folder_id)
  792. end
  793. local selectString = table.concat(listToSelect, ',')
  794. skynet.error("file_type",file_type)
  795. skynet.error("classification_id",classification_id)
  796. skynet.error("folder_list")
  797. tools.dump(folder_list)
  798. skynet.error("user_id",user_id)
  799. 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)
  800. local res = db:query(sql)
  801. if res and #res > 0 then
  802. local tab = {}
  803. if #res == 1 then
  804. tab[1] = tools.getDbResData(res)
  805. else
  806. tab = tools.getDbResData(res)
  807. end
  808. -- tools.dump(tab)
  809. return true,tab
  810. else
  811. skynet.error("No data found.")
  812. return false,nil
  813. end
  814. end
  815. --生成滚动视频
  816. s.resp.generate_scroll_video = function(template_tab,fd)
  817. local isOk,error_info = s.resp.check_template_scroll_isRight(template_tab)
  818. return isOk,error_info
  819. end
  820. --生成解压视频数据库
  821. s.resp.generate_video = function(_msg_body,_fd)
  822. skynet.fork(function(msg_body,fd)
  823. s.resp.get_template_info_by_id(msg_body,nil,function(tab)
  824. -- tools.dump(tab)
  825. if tab ~=nil then
  826. local isOk,error_info = s.resp.check_template_isRight(tab)
  827. if tab.video_stype == 1 then --解压
  828. if not isOk then
  829. return tools.response(fd,200,error_info)
  830. end
  831. else
  832. isOk,error_info = s.resp.check_template_scroll_isRight(tab)
  833. if not isOk then
  834. return tools.response(fd,200,error_info)
  835. end
  836. end
  837. -- tools.dump(tab)
  838. local user_id = msg_body.user_id
  839. local template_id = msg_body.template_id
  840. local file_name = tab.book_name
  841. local video_stype = tab.video_stype
  842. --先查找是否有这个名的文件夹
  843. 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)
  844. local res = db:query(sql)
  845. local info = tools.getDbResData(res)
  846. local write_video_db = function(folder_id)
  847. skynet.error("开始生成",folder_id)
  848. local template_info = cjson.decode(tab.template_info)
  849. if template_info.data~=nil and template_info.data~="" then
  850. template_info = tools.base64decode(template_info.data)
  851. template_info = cjson.decode(template_info)
  852. end
  853. -- tools.dump(template_info)
  854. local gen_num = 0
  855. if template_info.creat_video_list~=nil then
  856. gen_num = #template_info.creat_video_list
  857. end
  858. local custom = {}
  859. if template_info.custom~=nil and template_info.custom~="" then
  860. custom = template_info.custom
  861. end
  862. skynet.error("gen_num",gen_num)
  863. -- tools.dump(custom)
  864. for i = 1, gen_num, 1 do -- 模拟数据每次生成5条
  865. local item_info = template_info.creat_video_list[i]
  866. local head_video,content_video,end_video,bgm_audio,bottom,corner,background_image = s.resp.parse_template_info(tab,item_info)
  867. -- tools.dump(head_video)
  868. -- tools.dump(content_video)
  869. -- tools.dump(end_video)
  870. -- tools.dump(bgm_audio)
  871. local is_corner_mark = item_info.is_corner_mark
  872. local is_video_bottom_text = item_info.is_video_bottom_text
  873. local hot_tag_type = 0
  874. local item_custom = {}
  875. if item_info.custom~=nil and item_info.custom~="" then
  876. item_custom = item_info.custom
  877. end
  878. if is_corner_mark then
  879. hot_tag_type = 1
  880. end
  881. local fictitious_tag_type = 0
  882. if is_video_bottom_text then
  883. fictitious_tag_type = 1
  884. end
  885. local gen_info = {user_id=0,id=0,project_name=tab.project_name,
  886. book_name=tab.book_name,
  887. ratio=tab.ratio,
  888. video_stype=tab.video_stype,
  889. crop_time=tab.video_crop_time,
  890. marketing_title_info={marketing_title_top_h = 0,stroke_h=0,marketing_title=item_info.title_html},
  891. video_list = content_video,
  892. subtitles = {stype=0,subtitle_font="",subtitles_path=template_info.subtitles_path},
  893. subtitles_audio={path = template_info.subtitles_audio_path,duration=template_info.subtitles_audio_duration },
  894. video_last_frame =end_video[1] or {},
  895. video_first_frame = head_video[1] or {},
  896. bgm_list = bgm_audio[1],
  897. corner_mark_image = corner[1] or {},
  898. bottom_text_image = bottom[1] or {},
  899. background_image = background_image[1] or {},
  900. hot_tag = {type=hot_tag_type,path=""}, --上边
  901. fictitious_tag = {type=fictitious_tag_type,path=""}, --下边
  902. custom = custom,
  903. video_custom = item_custom,
  904. }
  905. local video_name = gen_info.book_name.."__视频"..i
  906. local book_name = gen_info.book_name
  907. local ratio = gen_info.ratio
  908. local video_stype = gen_info.video_stype
  909. local video_url = ""
  910. local generate_video_info = {}
  911. generate_video_info =item_info
  912. generate_video_info.book_name = tab.book_name
  913. generate_video_info.project_name = tab.project_name
  914. generate_video_info.ratio = tab.ratio
  915. generate_video_info.video_stype = tab.video_stype
  916. generate_video_info = cjson.encode(generate_video_info)
  917. local video_state = 0
  918. 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,
  919. template_id,
  920. folder_id,
  921. video_name,
  922. book_name,
  923. ratio,
  924. video_stype,
  925. video_url,video_state,cjson.encode({data=tools.base64encode(generate_video_info)}))
  926. -- skynet.error("123sss")
  927. -- tools.dump(gen_info)
  928. db:query(sql)
  929. -- skynet.error(sql)
  930. 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)
  931. res = db:query(sql)
  932. if res and #res > 0 then
  933. local tab = {}
  934. if #res > 0 then
  935. tab = tools.getDbResData(res)
  936. gen_info.id = tab.id
  937. gen_info.user_id = user_id
  938. sql = string.format("UPDATE generate_video_list_tab SET is_use = %d WHERE id =%d",1,gen_info.id)
  939. db:query(sql)
  940. skynet.send("tools_work","lua","generate_video",gen_info)
  941. end
  942. -- return true,tab
  943. else
  944. skynet.error("No data found.")
  945. end
  946. end
  947. --获取5个未使用的生成视频文件
  948. tools.response_db_generate_video(fd,true)
  949. end
  950. if res and #res > 0 then --存在这个文件夹,直接写入到这个文件夹
  951. local folder_id = info.id
  952. write_video_db(folder_id)
  953. else -- 没有这个文件夹则创建一个
  954. 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)
  955. db:query(sql)
  956. --查找新建的文件夹id
  957. 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)
  958. res = db:query(sql)
  959. info = tools.getDbResData(res)
  960. local folder_id = info.id
  961. write_video_db(folder_id)
  962. end
  963. end
  964. end)
  965. end,_msg_body,_fd)
  966. return true
  967. end
  968. --获取生成视频文件夹列表
  969. s.resp.get_generate_video_folder_list = function(msg_body,fd)
  970. skynet.fork(function(_fd,_msg_body)
  971. -- local count = msg_body.count
  972. -- local page = (msg_body.page - 1) * count
  973. 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)
  974. -- 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)
  975. local res = db:query(sql)
  976. -- skynet.error('folder_list',sql)
  977. -- 判断是否找到数据
  978. if res and #res > 0 then
  979. local tab = {}
  980. if #res == 1 then
  981. tab[1] = tools.getDbResData(res)
  982. else
  983. tab = tools.getDbResData(res)
  984. end
  985. skynet.error("Found data:")
  986. -- tools.dump(tab)
  987. -- return true,tab
  988. tools.response_db_generate_video_folder_list(_fd,msg_body,true,tab)
  989. else
  990. skynet.error("No data found.")
  991. tools.response_db_folder_list(_fd,msg_body,false,nil)
  992. -- return false,nil
  993. end
  994. end,fd,msg_body)
  995. return true
  996. end
  997. --获取文件夹内的生成视频
  998. s.resp.get_video_list_by_folder_id = function(msg_body,fd,call_back)
  999. skynet.fork(function(_fd)
  1000. 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)
  1001. local res = db:query(sql)
  1002. -- skynet.error('folder_res_list',sql)
  1003. -- 判断是否找到数据
  1004. if res and #res > 0 then
  1005. local tab = {}
  1006. if #res == 1 then
  1007. tab[1] = tools.getDbResData(res)
  1008. else
  1009. tab = tools.getDbResData(res)
  1010. end
  1011. skynet.error("Found data:")
  1012. if not call_back and _fd~=nil then
  1013. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,true,tab)
  1014. else
  1015. if call_back ~=nil then
  1016. call_back(tab)
  1017. end
  1018. end
  1019. -- tools.dump(tab)
  1020. -- return true,tab
  1021. else
  1022. skynet.error("No data found.",call_back)
  1023. if call_back == nil and _fd~=nil then
  1024. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,false,nil)
  1025. else
  1026. if call_back ~=nil then
  1027. call_back({})
  1028. end
  1029. end
  1030. -- return false,nil
  1031. end
  1032. end,fd)
  1033. return true
  1034. end
  1035. --根据所有生成视频文件id查到所有文件的url并删除
  1036. local fork_get_gen_video_file_list_url_and_delete = function(delete_list,call_back)
  1037. local url_list = {}
  1038. for i = 1, #delete_list, 1 do
  1039. table.insert(url_list,i, delete_list[i].path)
  1040. end
  1041. skynet.send("tools_work","lua","delete_file",url_list)
  1042. if call_back~=nil then
  1043. call_back()
  1044. end
  1045. end
  1046. --执行删除生成视频文件操作
  1047. local fork_delete_gen_video_file = function(msg_body)
  1048. -- tools.dump(msg_body)
  1049. local listToDelete = {}
  1050. for i = 1, #msg_body.file_list, 1 do
  1051. table.insert(listToDelete,i,msg_body.file_list[i].id)
  1052. end
  1053. local deleteString = table.concat(listToDelete, ',')
  1054. local sql = string.format("UPDATE generate_video_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  1055. db:query(sql)
  1056. skynet.fork(fork_get_gen_video_file_list_url_and_delete,msg_body.file_list,function()
  1057. local delete_sql = string.format("DELETE FROM generate_video_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  1058. db:query(delete_sql)
  1059. skynet.error(delete_sql)
  1060. end)
  1061. end
  1062. --执行删除生成视频文件夹操作
  1063. local fork_delete_gen_video_folder = function(msg)
  1064. for i = 1, #msg.folder_id_list, 1 do
  1065. local isok = s.resp.get_video_list_by_folder_id({
  1066. folder_id = msg.folder_id_list[i],
  1067. user_id = msg.user_id},nil,function(list)
  1068. skynet.error("执行删除生成视频文件夹操作",#list)
  1069. if #list>0 then
  1070. local temp = {}
  1071. for i = 1, #list, 1 do
  1072. table.insert(temp,i,list[i])
  1073. end
  1074. skynet.fork(fork_delete_gen_video_file,{file_list=temp,user_id=msg.user_id})
  1075. end
  1076. end)
  1077. local delete_sql = string.format("DELETE FROM generate_video_folder_list_tab WHERE id = %d", msg.folder_id_list[i])
  1078. db:query(delete_sql)
  1079. end
  1080. end
  1081. --删除生成视频文件夹
  1082. s.resp.delete_generate_video_folder = function(msg_body,fd)
  1083. skynet.fork(fork_delete_gen_video_folder,msg_body,fd)
  1084. return true
  1085. end
  1086. --删除生成视频
  1087. s.resp.delete_generate_video = function(msg_body,fd)
  1088. local temp = {}
  1089. msg_body.file_list = {}
  1090. local listToDelete = {}
  1091. for i = 1, #msg_body.file_id_list, 1 do
  1092. table.insert(listToDelete,i,msg_body.file_id_list[i])
  1093. end
  1094. local deleteString = table.concat(listToDelete, ',')
  1095. local sql = string.format("select * from generate_video_list_tab WHERE id IN (%s) AND is_delete = 0",deleteString)
  1096. local res = db:query(sql)
  1097. skynet.error(sql)
  1098. if res and #res > 0 then
  1099. local tab = {}
  1100. if #res == 1 then
  1101. tab[1] = tools.getDbResData(res)
  1102. else
  1103. tab = tools.getDbResData(res)
  1104. end
  1105. for i = 1, #tab, 1 do
  1106. table.insert(msg_body.file_list,i,tab[i])
  1107. end
  1108. skynet.fork(fork_delete_gen_video_file,msg_body,fd)
  1109. skynet.error("Found data:")
  1110. else
  1111. skynet.error("No data found.")
  1112. end
  1113. return true
  1114. end
  1115. --重命名生成视频文件夹的名字
  1116. s.resp.reset_generate_video_folder_name = function(msg_body,fd)
  1117. local sql = string.format("UPDATE generate_video_list_tab SET video_name ='%s' WHERE id = %d ",msg_body.name,msg_body.folder_id)
  1118. db:query(sql)
  1119. return true
  1120. end
  1121. --生成视频搜索
  1122. s.resp.search_generate_video_file = function(msg_body,fd)
  1123. skynet.fork(function(_fd)
  1124. -- tools.dump(msg_body)
  1125. 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)
  1126. -- skynet.error("sql",sql)
  1127. local res = db:query(sql)
  1128. if res and #res > 0 then
  1129. local tab = {}
  1130. if #res == 1 then
  1131. tab[1] = tools.getDbResData(res)
  1132. else
  1133. tab = tools.getDbResData(res)
  1134. end
  1135. skynet.error("Found data:")
  1136. -- tools.dump(tab)
  1137. -- return true,tab
  1138. tools.response_db_search_generate_video_file(_fd,msg_body,true,tab)
  1139. else
  1140. skynet.error("No data found.")
  1141. -- return false,nil
  1142. tools.response_db_search_generate_video_file(_fd,msg_body,false,nil)
  1143. end
  1144. end,fd)
  1145. return true
  1146. end
  1147. --获取生成视频文件夹列表每个文件夹的文件数量
  1148. s.resp.folder_gen_video_list_nums = function(folder_list,fd,total_count)
  1149. skynet.fork(function(_fd)
  1150. local temp ={}
  1151. for i = 1, #folder_list, 1 do
  1152. local msg_body = folder_list[i]
  1153. -- tools.dump(msg_body)
  1154. 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)
  1155. local res = db:query(sql)
  1156. local count = 0
  1157. if res and #res > 0 then
  1158. count = #res
  1159. skynet.error("Found data:")
  1160. -- tools.dump(tab)
  1161. -- return true,tab
  1162. else
  1163. skynet.error("No data found.",msg_body.id)
  1164. end
  1165. temp[msg_body.id] = count
  1166. -- table.insert(temp,msg_body.id,count)
  1167. end
  1168. tools.response_db_folder_generate_video_list_nums(_fd,folder_list,temp,total_count)
  1169. end,fd)
  1170. end
  1171. --更新生成视频的状态
  1172. s.resp.update_gen_video_file_status = function(msg_body)
  1173. local custom = "{}"
  1174. if msg_body.custom~=nil and msg_body.custom~="" then
  1175. custom = msg_body.custom
  1176. end
  1177. 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)
  1178. db:query(sql)
  1179. skynet.error(sql)
  1180. return true
  1181. end
  1182. --检测指定类型文件是否够数量
  1183. s.resp.checkNumsByType = function(msg_body)
  1184. local duration = 0
  1185. if msg_body.duration~= nil then
  1186. duration = msg_body.duration
  1187. end
  1188. local limit = msg_body.limit
  1189. local type = msg_body.type
  1190. 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)
  1191. -- skynet.error("sql",sql)
  1192. local res = db:query(sql)
  1193. if res and #res > 0 then
  1194. if #res >= tonumber(limit) then
  1195. return true
  1196. end
  1197. return false
  1198. else
  1199. return false
  1200. end
  1201. end
  1202. --修改密码
  1203. s.resp.reset_password = function(msg_body)
  1204. local sql = string.format("UPDATE users SET password = '%s' WHERE id = %d ",msg_body.new_pw,msg_body.user_id)
  1205. db:query(sql)
  1206. return true
  1207. end
  1208. --检测是否有此文件
  1209. s.resp.check_have_file = function(msg_body)
  1210. 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)
  1211. -- skynet.error("sql",sql)
  1212. local res = db:query(sql)
  1213. -- 判断是否找到数据
  1214. if res and #res > 0 then
  1215. local tab = tools.getDbResData(res)
  1216. skynet.error("Found data:",tab)
  1217. return true,tab
  1218. else
  1219. skynet.error("No data found.")
  1220. return false,nil
  1221. end
  1222. end
  1223. --检测是否有此文件夹
  1224. s.resp.check_have_folder = function(msg_body)
  1225. 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)
  1226. -- skynet.error("sql",sql)
  1227. local res = db:query(sql)
  1228. -- 判断是否找到数据
  1229. if res and #res > 0 then
  1230. local tab = tools.getDbResData(res)
  1231. skynet.error("Found data:",tab)
  1232. return true,tab
  1233. else
  1234. skynet.error("No data found.")
  1235. return false,nil
  1236. end
  1237. end
  1238. --检测滚屏模板是否正常
  1239. s.resp.check_template_scroll_isRight = function(info)
  1240. local isok = s.resp.checkNumsByType({
  1241. duration = 0,
  1242. type = 6,
  1243. limit = 1,
  1244. user_id = info.user_id})
  1245. if not isok then
  1246. return false,cjson.encode({code=10001,msg = "新建模板失败,图片资源不足!"})
  1247. end
  1248. isok = s.resp.checkNumsByType({
  1249. duration = 0,
  1250. type = 4,
  1251. limit = 1,
  1252. user_id = info.user_id}
  1253. )
  1254. if not isok then
  1255. return false,cjson.encode({code=10001,msg = "新建模板失败,BGM音频不足!"})
  1256. end
  1257. isok = s.resp.checkNumsByType({
  1258. duration = 0,
  1259. type = 3,
  1260. limit = 1,
  1261. user_id = info.user_id}
  1262. )
  1263. if not isok then
  1264. return false,cjson.encode({code=10001,msg = "新建模板失败,片尾资源不足!"})
  1265. end
  1266. return true,nil
  1267. end
  1268. --检查模板是否正常
  1269. s.resp.check_template_isRight = function(info)
  1270. local isok = s.resp.checkNumsByType({
  1271. duration = 0,
  1272. type = 1,
  1273. limit = 1,
  1274. user_id = info.user_id})
  1275. if not isok then
  1276. return false,cjson.encode({code=10001,msg = "新建模板失败,片头资源不足!"})
  1277. end
  1278. isok = s.resp.checkNumsByType({
  1279. duration = 0,
  1280. type = 3,
  1281. limit = 1,
  1282. user_id = info.user_id}
  1283. )
  1284. if not isok then
  1285. return false,cjson.encode({code=10001,msg = "新建模板失败,片尾资源不足"})
  1286. end
  1287. local subtitles_audio_duration = 0
  1288. if info.template_info ~=nil and info.template_info~="" then
  1289. local template_info = cjson.decode(info.template_info)
  1290. template_info = tools.base64decode(template_info.data)
  1291. template_info = cjson.decode(template_info)
  1292. if template_info.subtitles_audio_duration~=nil then
  1293. subtitles_audio_duration = tonumber(template_info.subtitles_audio_duration)
  1294. end
  1295. end
  1296. local duration = info.video_crop_time + 5
  1297. local limit = math.floor(subtitles_audio_duration/duration)
  1298. skynet.error("limit",limit)
  1299. skynet.error("subtitles_audio_duration",subtitles_audio_duration)
  1300. skynet.error("duration",duration)
  1301. isok = s.resp.checkNumsByType({
  1302. duration = duration,
  1303. type = 2,
  1304. limit =limit+1 ,
  1305. user_id = info.user_id}
  1306. )
  1307. if not isok then
  1308. return false,cjson.encode({code=10001,msg = "正片资源不足"})
  1309. end
  1310. isok = s.resp.checkNumsByType({
  1311. duration = 0,
  1312. type = 4,
  1313. limit = 1,
  1314. user_id = info.user_id}
  1315. )
  1316. if not isok then
  1317. return false,cjson.encode({code=10001,msg = "BGM音效资源不足!"})
  1318. end
  1319. return true,nil
  1320. end
  1321. --接收到删除引用的文件列表信息
  1322. s.resp.recv_ref_file_list_del_info = function(msg_body)
  1323. skynet.error("接收到删除引用的文件列表信息")
  1324. local listToDelete = {}
  1325. for i = 1, #msg_body.file_id_list, 1 do
  1326. table.insert(listToDelete,i,msg_body.file_id_list[i])
  1327. end
  1328. local deleteString = table.concat(listToDelete, ',')
  1329. -- local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE ref_id IN (%s)",1,deleteString)
  1330. -- db:query(sql)
  1331. local delete_sql = string.format("DELETE FROM res_list_tab WHERE ref_id IN (%s)", deleteString)
  1332. db:query(delete_sql)
  1333. skynet.error(delete_sql)
  1334. return true
  1335. end
  1336. --检测引用的文件夹是否有自己上传的文件
  1337. s.resp.check_ref_folder_have_self_file = function(msg_body)
  1338. local check_user_folder = function(info) --检测用户的文件夹
  1339. local user_id = info.user_id
  1340. local isHave = false
  1341. local folder_id = info.id
  1342. 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)
  1343. local res = db:query(sql)
  1344. local delete_tab = {}
  1345. -- 判断是否找到数据
  1346. if res and #res > 0 then
  1347. local tab = {}
  1348. if #res == 1 then
  1349. tab[1] = tools.getDbResData(res)
  1350. else
  1351. tab = tools.getDbResData(res)
  1352. end
  1353. local count = 1
  1354. for i = 1, #tab, 1 do
  1355. if tab[i].ref_id==0 then
  1356. isHave = true
  1357. else
  1358. table.insert(delete_tab,count,tab[i])
  1359. count = count+1
  1360. end
  1361. end
  1362. return isHave,delete_tab,info
  1363. else
  1364. return isHave,delete_tab,info
  1365. end
  1366. end
  1367. local sql = ""
  1368. local folder_id = msg_body.folder_id
  1369. --获取当前引用的文件夹
  1370. sql = string.format("select * from folder_list_tab where ref_id = %d ",folder_id)
  1371. local res = db:query(sql)
  1372. local tab_list = {}
  1373. if res and #res > 0 then
  1374. local tab = {}
  1375. if #res == 1 then
  1376. tab[1] = tools.getDbResData(res)
  1377. else
  1378. tab = tools.getDbResData(res)
  1379. end
  1380. for i = 1, #tab, 1 do
  1381. local isHave,delete_tab,info = check_user_folder(tab[i])
  1382. table.insert(tab_list,i,{isHave = isHave,delete_tab=delete_tab,info=info})
  1383. end
  1384. return tab_list
  1385. else
  1386. return tab_list
  1387. end
  1388. end
  1389. --接收到删除引用的文件夹列表信息
  1390. s.resp.recv_ref_folder_list_del_info = function(msg_body)
  1391. local delete_file_list = function(delete_tab)
  1392. local listToDelete = {}
  1393. for i = 1, #delete_tab, 1 do
  1394. table.insert(listToDelete,i,delete_tab[i].ref_id)
  1395. end
  1396. s.resp.recv_ref_file_list_del_info({file_id_list=listToDelete})
  1397. end
  1398. -- skynet.error("接收到删除引用的文件夹列表信息")
  1399. -- tools.dump(msg_body)
  1400. for i = 1, #msg_body.folder_id_list, 1 do
  1401. local folder_id = msg_body.folder_id_list[i]
  1402. local list = s.resp.check_ref_folder_have_self_file({folder_id=folder_id})
  1403. for i = 1, #list, 1 do
  1404. tools.dump(list[i])
  1405. delete_file_list(list[i].delete_tab)
  1406. local isHave = list[i].isHave
  1407. local folder_info = list[i].info
  1408. if isHave then --有自定义的文件不删除这个文件夹,但是要将其引用设置为0
  1409. skynet.error("有自定义的文件不删除这个文件夹,但是要将其引用设置为0")
  1410. 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)
  1411. db:query(sql)
  1412. skynet.error(sql)
  1413. else --没有自己上传的文件夹,将其文件夹删除
  1414. skynet.error("没有自己上传的文件夹,将其文件夹删除")
  1415. local sql = string.format("DELETE FROM folder_list_tab WHERE id = %d AND user_id = %d ",folder_info.id,folder_info.user_id)
  1416. db:query(sql)
  1417. skynet.error(sql)
  1418. end
  1419. end
  1420. end
  1421. return true
  1422. end
  1423. --接收到修改引用的文件信息
  1424. s.resp.recv_ref_file_modify_info = function(msg_body)
  1425. local file_id = msg_body.file_id
  1426. local name = msg_body.name
  1427. local sql = string.format("UPDATE res_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,file_id)
  1428. db:query(sql)
  1429. return true
  1430. end
  1431. s.resp.update_generate_video_custom = function(msg_body)
  1432. local list = msg_body.data
  1433. skynet.fork(function()
  1434. for i = 1, #list, 1 do
  1435. local video_id = list[i].video_id
  1436. local custom = "{}"
  1437. if list[i].custom~=nil and list[i].custom~="" then
  1438. custom = list[i].custom
  1439. end
  1440. 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)
  1441. db:query(sql)
  1442. -- skynet.error(sql)
  1443. end
  1444. end)
  1445. -- skynet.error(sql)
  1446. return true
  1447. end
  1448. --接收到修改引用的文件夹信息
  1449. s.resp.recv_ref_folder_modify_info = function(msg_body)
  1450. local folder_id = msg_body.folder_id
  1451. local name = msg_body.name
  1452. local sql = string.format("UPDATE folder_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,folder_id)
  1453. db:query(sql)
  1454. return true
  1455. end
  1456. --获取用户所有的文件数
  1457. s.resp.get_user_folder_list_nums = function(msg_body)
  1458. local sql = string.format("select COUNT(user_id) AS nums from folder_list_tab where user_id = %d ",msg_body.user_id)
  1459. local res = db:query(sql)
  1460. local count = 0
  1461. if res and #res > 0 then
  1462. count = res.nums
  1463. end
  1464. return count
  1465. end
  1466. --获取用户指定类型的数量
  1467. s.resp.get_user_file_count_by_type = function(msg_body)
  1468. 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)
  1469. local res = db:query(sql)
  1470. local count = 0
  1471. if res and #res > 0 then
  1472. count = res.nums
  1473. end
  1474. return count
  1475. end
  1476. s.resp.check_file_name_list_is_repeat_by_folder_id = function(msg_body,fd,call_back)
  1477. -- tools.dump(msg_body)
  1478. skynet.fork(function()
  1479. local nameString = ""
  1480. for i = 1, #msg_body.file_name_list, 1 do
  1481. nameString = nameString.."'"..msg_body.file_name_list[i].."'"
  1482. if i<#msg_body.file_name_list then
  1483. nameString = nameString..","
  1484. end
  1485. end
  1486. local sql = string.format("SELECT file_name FROM res_list_tab WHERE file_name IN (%s) AND folder_id = %d AND user_id = %d ",
  1487. nameString,
  1488. msg_body.folder_id,
  1489. msg_body.user_id)
  1490. local res = db:query(sql)
  1491. local tab = {}
  1492. -- skynet.error("sql",sql)
  1493. if #res == 1 then
  1494. tab[1] = tools.getDbResData(res)
  1495. else
  1496. if #res>0 then
  1497. tab = tools.getDbResData(res)
  1498. end
  1499. end
  1500. -- tools.dump(tab)
  1501. local normal_file_name_list ={}
  1502. local repeat_file_name_list ={}
  1503. for i = 1, #tab, 1 do
  1504. table.insert(repeat_file_name_list,i,tab[i].file_name)
  1505. end
  1506. function check(file_name,list)
  1507. for i = 1, #list, 1 do
  1508. if list[i]==file_name then
  1509. return false
  1510. end
  1511. end
  1512. return true
  1513. end
  1514. local count = 1
  1515. for i = 1, #msg_body.file_name_list, 1 do
  1516. if check(msg_body.file_name_list[i],repeat_file_name_list) then
  1517. table.insert(normal_file_name_list,count,msg_body.file_name_list[i])
  1518. count = count+1
  1519. end
  1520. end
  1521. if call_back~=nil then
  1522. call_back({normal_file_name_list=normal_file_name_list,repeat_file_name_list=repeat_file_name_list})
  1523. else
  1524. 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})
  1525. end
  1526. end)
  1527. return true
  1528. end
  1529. --获取用户的分享用户列表
  1530. s.resp.get_share_user_list = function(msg_body,fd)
  1531. local isOk,user_data = s.resp.getUserById(msg_body.user_id)
  1532. local temp = {}
  1533. if isOk then
  1534. local select_share = nil
  1535. if user_data.user_info~=nil and user_data.user_info~="" then
  1536. local info = cjson.decode(user_data.user_info)
  1537. select_share = info.select_share
  1538. end
  1539. local count = 1
  1540. skynet.fork(function()
  1541. function action()
  1542. tools.response_db_get_share_user_list(fd,msg_body,true,temp)
  1543. end
  1544. if select_share~=nil and select_share~="" then
  1545. for i = 1, #select_share, 1 do
  1546. local user_id = select_share[i]
  1547. local _,ud = s.resp.getUserById(user_id)
  1548. if _ then
  1549. if ud.id~=msg_body.user_id then
  1550. table.insert(temp,count,ud)
  1551. count = count +1
  1552. end
  1553. end
  1554. end
  1555. end
  1556. if user_data.permit_id == 2 then --说明是管理
  1557. s.resp.get_group_list(user_data.group_id,function(list)
  1558. -- skynet.error("user_data.group_id",user_data.group_id)
  1559. -- tools.dump(list)
  1560. for i = 1, #list, 1 do
  1561. if list[i].permit_id~=2 and list[i].id~=user_data.id then
  1562. table.insert(temp,count,list[i])
  1563. count = count +1
  1564. end
  1565. end
  1566. action()
  1567. end)
  1568. else
  1569. action()
  1570. end
  1571. end)
  1572. return true
  1573. end
  1574. return false
  1575. end
  1576. --根据名字获取文件夹
  1577. s.resp.get_folder_by_name = function(msg_body,fd)
  1578. 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)
  1579. local res = db:query(sql)
  1580. local tab = {}
  1581. tab = tools.getDbResData(res)
  1582. if #res <=0 then
  1583. return false,nil
  1584. end
  1585. return true,tab
  1586. end
  1587. s.resp.get_folder_have_file_name = function(user_id,folder_id,file_name)
  1588. 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)
  1589. local res = db:query(sql)
  1590. local tab = {}
  1591. tab = tools.getDbResData(res)
  1592. skynet.error("#res",#res)
  1593. if #res <=0 then
  1594. return false,nil
  1595. end
  1596. return true,tab
  1597. end
  1598. --分享文件列表
  1599. s.resp.share_file_list = function(msg_body,fd)
  1600. function insert_target(file,folder_id,target_user_id)
  1601. local is_have,_ = s.resp.get_folder_have_file_name(target_user_id,folder_id,file.file_name)
  1602. if is_have==true then
  1603. else
  1604. 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)",
  1605. target_user_id,
  1606. folder_id,
  1607. file.file_type,
  1608. file.file_name,
  1609. file.file_info,
  1610. file.duration,
  1611. file.classification_id,
  1612. file.id)
  1613. skynet.error("sql",sql)
  1614. db:query(sql)
  1615. end
  1616. -- skynet.error(sql)
  1617. end
  1618. for j = 1, #msg_body.target_user_id_list, 1 do
  1619. local target_user_id = msg_body.target_user_id_list[j]
  1620. local my_user_id = msg_body.user_id
  1621. skynet.fork(function()
  1622. for i = 1, #msg_body.file_id_list, 1 do
  1623. local file_id = msg_body.file_id_list[i].file_id
  1624. local folder_name = msg_body.file_id_list[i].folder_name
  1625. local sql = string.format("SELECT * FROM res_list_tab WHERE id = %d limit 1", file_id)
  1626. local res = db:query(sql)
  1627. local tab = tools.getDbResData(res)
  1628. if #res>0 then
  1629. --检测被分享的是否拥有此文件夹
  1630. 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})
  1631. if isOk then --拥有此文件夹,直接插入
  1632. skynet.error("拥有此文件夹,直接插入",folder_name)
  1633. insert_target(tab,folder_data.id,target_user_id)
  1634. else -- 没有此文件夹,创建一个再插入
  1635. 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)",
  1636. target_user_id,
  1637. folder_name,
  1638. tab.file_type
  1639. ,0
  1640. ,tab.classification_id)
  1641. -- skynet.error(sql)
  1642. local new_res = db:query(new_sql)
  1643. 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",
  1644. target_user_id,
  1645. folder_name,
  1646. tab.file_type,
  1647. tab.classification_id)
  1648. new_res = db:query(new_sql)
  1649. if new_res and #new_res > 0 then
  1650. local folder_info = {}
  1651. folder_info = tools.getDbResData(new_res)
  1652. insert_target(tab,folder_info.id,target_user_id)
  1653. end
  1654. end
  1655. end
  1656. end
  1657. end)
  1658. end
  1659. return true
  1660. end
  1661. --分享文件夹列表
  1662. s.resp.share_folder_list = function(msg_body,fd)
  1663. function getInfoByName(name,list)
  1664. for i = 1, #list, 1 do
  1665. if list[i].file_name == name then
  1666. return list[i]
  1667. end
  1668. end
  1669. return nil
  1670. end
  1671. function insert_target_folder(target_folder_id,my_folder_id,my_user_id,target_user_id)
  1672. local isok = s.resp.folder_res_list({
  1673. folder_id = my_folder_id,
  1674. user_id =my_user_id,
  1675. is_public=0},nil,function(list)
  1676. skynet.error("insert_target_folder",#list)
  1677. if #list>0 then
  1678. local file_name_list = {}
  1679. for i = 1, #list, 1 do
  1680. local _f = list[i]
  1681. table.insert(file_name_list,i,_f.file_name)
  1682. end
  1683. 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)
  1684. for i = 1, #data.normal_file_name_list, 1 do
  1685. local file = getInfoByName(data.normal_file_name_list[i],list)
  1686. if file~=nil then
  1687. 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)",
  1688. target_user_id,
  1689. target_folder_id,
  1690. file.file_type,
  1691. file.file_name,
  1692. file.file_info,
  1693. file.duration,
  1694. file.classification_id,
  1695. file.id)
  1696. db:query(sql)
  1697. end
  1698. end
  1699. end)
  1700. end
  1701. end)
  1702. end
  1703. for j = 1, #msg_body.target_user_id_list, 1 do
  1704. local target_user_id = msg_body.target_user_id_list[j]
  1705. local my_user_id = msg_body.user_id
  1706. skynet.fork(function()
  1707. for i = 1, #msg_body.folder_id_list, 1 do
  1708. local folder_id = msg_body.folder_id_list[i].folder_id
  1709. local folder_name = msg_body.folder_id_list[i].folder_name
  1710. local sql = string.format("SELECT * FROM folder_list_tab WHERE id = %d limit 1", folder_id)
  1711. local res = db:query(sql)
  1712. local tab = tools.getDbResData(res)
  1713. if #res>0 then
  1714. --检测被分享的是否拥有此文件夹
  1715. 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})
  1716. if isOk then --拥有此文件夹,直接插入
  1717. skynet.error("拥有此文件夹,直接插入 ")
  1718. insert_target_folder(folder_data.id,folder_id,my_user_id,target_user_id)
  1719. else -- 没有此文件夹,创建一个再插入
  1720. 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)",
  1721. target_user_id,
  1722. folder_name,
  1723. tab.folder_type
  1724. ,0
  1725. ,tab.classification_id)
  1726. skynet.error("没有此文件夹,创建一个再插入 ")
  1727. skynet.error(sql)
  1728. db:query(new_sql)
  1729. 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",
  1730. target_user_id,
  1731. folder_name,
  1732. tab.folder_type,
  1733. tab.classification_id)
  1734. local new_res = db:query(new_sql)
  1735. if new_res and #new_res > 0 then
  1736. local folder_info = {}
  1737. folder_info = tools.getDbResData(new_res)
  1738. insert_target_folder(folder_info.id,folder_id,my_user_id,target_user_id)
  1739. end
  1740. end
  1741. end
  1742. end
  1743. end)
  1744. end
  1745. return true
  1746. end
  1747. s.resp.get_folder_info = function(msg_body,fd)
  1748. local listToFind= {}
  1749. for i = 1, #msg_body.folder_id_list, 1 do
  1750. table.insert(listToFind,i,msg_body.folder_id_list[i])
  1751. end
  1752. local findString = table.concat(listToFind, ',')
  1753. skynet.fork(function()
  1754. local sql = string.format("SELECT * FROM folder_list_tab WHERE id IN (%s) AND user_id = %d ", findString,msg_body.user_id)
  1755. local res = db:query(sql)
  1756. if res and #res > 0 then
  1757. local tab = {}
  1758. if #res == 1 then
  1759. tab[1] = tools.getDbResData(res)
  1760. else
  1761. tab = tools.getDbResData(res)
  1762. end
  1763. tools.response_db_get_folder_info(fd,msg_body,true,tab)
  1764. else
  1765. tools.response_db_get_folder_info(fd,msg_body,false,{})
  1766. end
  1767. end)
  1768. end
  1769. s.init = function()
  1770. db=mysql.connect({
  1771. host=runconfig.db_tost,
  1772. port=runconfig.db_port,
  1773. database=runconfig.db_name,
  1774. user="root",
  1775. password=runconfig.db_pw,
  1776. max_packet_size = 1024 * 1024,
  1777. on_connect = nil
  1778. })
  1779. if not db then
  1780. skynet.error("failed to connect")
  1781. skynet.exit()
  1782. else
  1783. skynet.error("success to connect to mysql server")
  1784. end
  1785. --设置utf8字符集
  1786. local res = db:query("set charset utf8");
  1787. end
  1788. s.start(...)