init.lua 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  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. local generate_status = 1; --生成状态 0:不生成 1:生成
  879. if item_custom~=nil then
  880. if item_custom.generate_status~=nil then
  881. generate_status = tonumber(item_custom.generate_status)
  882. end
  883. end
  884. if generate_status == 1 then
  885. if is_corner_mark then
  886. hot_tag_type = 1
  887. end
  888. local fictitious_tag_type = 0
  889. if is_video_bottom_text then
  890. fictitious_tag_type = 1
  891. end
  892. local gen_info = {user_id=0,id=0,project_name=tab.project_name,
  893. book_name=tab.book_name,
  894. ratio=tab.ratio,
  895. video_stype=tab.video_stype,
  896. crop_time=tab.video_crop_time,
  897. marketing_title_info={marketing_title_top_h = 0,stroke_h=0,marketing_title=item_info.title_html},
  898. video_list = content_video,
  899. subtitles = {stype=0,subtitle_font="",subtitles_path=template_info.subtitles_path},
  900. subtitles_audio={path = template_info.subtitles_audio_path,duration=template_info.subtitles_audio_duration },
  901. video_last_frame =end_video[1] or {},
  902. video_first_frame = head_video[1] or {},
  903. bgm_list = bgm_audio[1],
  904. corner_mark_image = corner[1] or {},
  905. bottom_text_image = bottom[1] or {},
  906. background_image = background_image[1] or {},
  907. hot_tag = {type=hot_tag_type,path=""}, --上边
  908. fictitious_tag = {type=fictitious_tag_type,path=""}, --下边
  909. custom = custom,
  910. video_custom = item_custom,
  911. }
  912. local video_name = gen_info.book_name.."__视频"..i
  913. local book_name = gen_info.book_name
  914. local ratio = gen_info.ratio
  915. local video_stype = gen_info.video_stype
  916. local video_url = ""
  917. local generate_video_info = {}
  918. generate_video_info =item_info
  919. generate_video_info.book_name = tab.book_name
  920. generate_video_info.project_name = tab.project_name
  921. generate_video_info.ratio = tab.ratio
  922. generate_video_info.video_stype = tab.video_stype
  923. generate_video_info = cjson.encode(generate_video_info)
  924. local video_state = 0
  925. 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,
  926. template_id,
  927. folder_id,
  928. video_name,
  929. book_name,
  930. ratio,
  931. video_stype,
  932. video_url,video_state,cjson.encode({data=tools.base64encode(generate_video_info)}))
  933. -- skynet.error("123sss")
  934. -- tools.dump(gen_info)
  935. db:query(sql)
  936. -- skynet.error(sql)
  937. 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)
  938. res = db:query(sql)
  939. if res and #res > 0 then
  940. local tab = {}
  941. if #res > 0 then
  942. tab = tools.getDbResData(res)
  943. gen_info.id = tab.id
  944. gen_info.user_id = user_id
  945. sql = string.format("UPDATE generate_video_list_tab SET is_use = %d WHERE id =%d",1,gen_info.id)
  946. db:query(sql)
  947. skynet.send("tools_work","lua","generate_video",gen_info)
  948. end
  949. -- return true,tab
  950. else
  951. skynet.error("No data found.")
  952. end
  953. end
  954. end
  955. --获取5个未使用的生成视频文件
  956. tools.response_db_generate_video(fd,true)
  957. end
  958. if res and #res > 0 then --存在这个文件夹,直接写入到这个文件夹
  959. local folder_id = info.id
  960. write_video_db(folder_id)
  961. else -- 没有这个文件夹则创建一个
  962. 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)
  963. db:query(sql)
  964. --查找新建的文件夹id
  965. 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)
  966. res = db:query(sql)
  967. info = tools.getDbResData(res)
  968. local folder_id = info.id
  969. write_video_db(folder_id)
  970. end
  971. end
  972. end)
  973. end,_msg_body,_fd)
  974. return true
  975. end
  976. --获取生成视频文件夹列表
  977. s.resp.get_generate_video_folder_list = function(msg_body,fd)
  978. skynet.fork(function(_fd,_msg_body)
  979. -- local count = msg_body.count
  980. -- local page = (msg_body.page - 1) * count
  981. 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)
  982. -- 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)
  983. local res = db:query(sql)
  984. -- skynet.error('folder_list',sql)
  985. -- 判断是否找到数据
  986. if res and #res > 0 then
  987. local tab = {}
  988. if #res == 1 then
  989. tab[1] = tools.getDbResData(res)
  990. else
  991. tab = tools.getDbResData(res)
  992. end
  993. skynet.error("Found data:")
  994. -- tools.dump(tab)
  995. -- return true,tab
  996. tools.response_db_generate_video_folder_list(_fd,msg_body,true,tab)
  997. else
  998. skynet.error("No data found.")
  999. tools.response_db_folder_list(_fd,msg_body,false,nil)
  1000. -- return false,nil
  1001. end
  1002. end,fd,msg_body)
  1003. return true
  1004. end
  1005. --获取文件夹内的生成视频
  1006. s.resp.get_video_list_by_folder_id = function(msg_body,fd,call_back)
  1007. skynet.fork(function(_fd)
  1008. 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)
  1009. local res = db:query(sql)
  1010. -- skynet.error('folder_res_list',sql)
  1011. -- 判断是否找到数据
  1012. if res and #res > 0 then
  1013. local tab = {}
  1014. if #res == 1 then
  1015. tab[1] = tools.getDbResData(res)
  1016. else
  1017. tab = tools.getDbResData(res)
  1018. end
  1019. skynet.error("Found data:")
  1020. if not call_back and _fd~=nil then
  1021. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,true,tab)
  1022. else
  1023. if call_back ~=nil then
  1024. call_back(tab)
  1025. end
  1026. end
  1027. -- tools.dump(tab)
  1028. -- return true,tab
  1029. else
  1030. skynet.error("No data found.",call_back)
  1031. if call_back == nil and _fd~=nil then
  1032. tools.response_db_get_video_list_by_folder_id(_fd,msg_body,false,nil)
  1033. else
  1034. if call_back ~=nil then
  1035. call_back({})
  1036. end
  1037. end
  1038. -- return false,nil
  1039. end
  1040. end,fd)
  1041. return true
  1042. end
  1043. --根据所有生成视频文件id查到所有文件的url并删除
  1044. local fork_get_gen_video_file_list_url_and_delete = function(delete_list,call_back)
  1045. local url_list = {}
  1046. for i = 1, #delete_list, 1 do
  1047. table.insert(url_list,i, delete_list[i].path)
  1048. end
  1049. skynet.send("tools_work","lua","delete_file",url_list)
  1050. if call_back~=nil then
  1051. call_back()
  1052. end
  1053. end
  1054. --执行删除生成视频文件操作
  1055. local fork_delete_gen_video_file = function(msg_body)
  1056. -- tools.dump(msg_body)
  1057. local listToDelete = {}
  1058. for i = 1, #msg_body.file_list, 1 do
  1059. table.insert(listToDelete,i,msg_body.file_list[i].id)
  1060. end
  1061. local deleteString = table.concat(listToDelete, ',')
  1062. local sql = string.format("UPDATE generate_video_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  1063. db:query(sql)
  1064. skynet.fork(fork_get_gen_video_file_list_url_and_delete,msg_body.file_list,function()
  1065. -- local delete_sql = string.format("DELETE FROM generate_video_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,msg_body.user_id)
  1066. -- db:query(delete_sql)
  1067. -- skynet.error(delete_sql)
  1068. end)
  1069. end
  1070. --执行删除生成视频文件夹操作
  1071. local fork_delete_gen_video_folder = function(msg)
  1072. for i = 1, #msg.folder_id_list, 1 do
  1073. local isok = s.resp.get_video_list_by_folder_id({
  1074. folder_id = msg.folder_id_list[i],
  1075. user_id = msg.user_id},nil,function(list)
  1076. skynet.error("执行删除生成视频文件夹操作",#list)
  1077. if #list>0 then
  1078. local temp = {}
  1079. for i = 1, #list, 1 do
  1080. table.insert(temp,i,list[i])
  1081. end
  1082. skynet.fork(fork_delete_gen_video_file,{file_list=temp,user_id=msg.user_id})
  1083. end
  1084. end)
  1085. local delete_sql = string.format("DELETE FROM generate_video_folder_list_tab WHERE id = %d", msg.folder_id_list[i])
  1086. db:query(delete_sql)
  1087. end
  1088. end
  1089. --删除生成视频文件夹
  1090. s.resp.delete_generate_video_folder = function(msg_body,fd)
  1091. skynet.fork(fork_delete_gen_video_folder,msg_body,fd)
  1092. return true
  1093. end
  1094. --删除生成视频
  1095. s.resp.delete_generate_video = function(msg_body,fd)
  1096. local temp = {}
  1097. msg_body.file_list = {}
  1098. local listToDelete = {}
  1099. for i = 1, #msg_body.file_id_list, 1 do
  1100. table.insert(listToDelete,i,msg_body.file_id_list[i])
  1101. end
  1102. local deleteString = table.concat(listToDelete, ',')
  1103. local sql = string.format("select * from generate_video_list_tab WHERE id IN (%s) AND is_delete = 0",deleteString)
  1104. local res = db:query(sql)
  1105. skynet.error(sql)
  1106. if res and #res > 0 then
  1107. local tab = {}
  1108. if #res == 1 then
  1109. tab[1] = tools.getDbResData(res)
  1110. else
  1111. tab = tools.getDbResData(res)
  1112. end
  1113. for i = 1, #tab, 1 do
  1114. table.insert(msg_body.file_list,i,tab[i])
  1115. end
  1116. skynet.fork(fork_delete_gen_video_file,msg_body,fd)
  1117. skynet.error("Found data:")
  1118. else
  1119. skynet.error("No data found.")
  1120. end
  1121. return true
  1122. end
  1123. --重命名生成视频文件夹的名字
  1124. s.resp.reset_generate_video_folder_name = function(msg_body,fd)
  1125. local sql = string.format("UPDATE generate_video_list_tab SET video_name ='%s' WHERE id = %d ",msg_body.name,msg_body.folder_id)
  1126. db:query(sql)
  1127. return true
  1128. end
  1129. --生成视频搜索
  1130. s.resp.search_generate_video_file = function(msg_body,fd)
  1131. skynet.fork(function(_fd)
  1132. -- tools.dump(msg_body)
  1133. 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)
  1134. -- skynet.error("sql",sql)
  1135. local res = db:query(sql)
  1136. if res and #res > 0 then
  1137. local tab = {}
  1138. if #res == 1 then
  1139. tab[1] = tools.getDbResData(res)
  1140. else
  1141. tab = tools.getDbResData(res)
  1142. end
  1143. skynet.error("Found data:")
  1144. -- tools.dump(tab)
  1145. -- return true,tab
  1146. tools.response_db_search_generate_video_file(_fd,msg_body,true,tab)
  1147. else
  1148. skynet.error("No data found.")
  1149. -- return false,nil
  1150. tools.response_db_search_generate_video_file(_fd,msg_body,false,nil)
  1151. end
  1152. end,fd)
  1153. return true
  1154. end
  1155. --获取生成视频文件夹列表每个文件夹的文件数量
  1156. s.resp.folder_gen_video_list_nums = function(folder_list,fd,total_count)
  1157. skynet.fork(function(_fd)
  1158. local temp ={}
  1159. for i = 1, #folder_list, 1 do
  1160. local msg_body = folder_list[i]
  1161. -- tools.dump(msg_body)
  1162. 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)
  1163. local res = db:query(sql)
  1164. local count = 0
  1165. if res and #res > 0 then
  1166. count = #res
  1167. skynet.error("Found data:")
  1168. -- tools.dump(tab)
  1169. -- return true,tab
  1170. else
  1171. skynet.error("No data found.",msg_body.id)
  1172. end
  1173. temp[msg_body.id] = count
  1174. -- table.insert(temp,msg_body.id,count)
  1175. end
  1176. tools.response_db_folder_generate_video_list_nums(_fd,folder_list,temp,total_count)
  1177. end,fd)
  1178. end
  1179. --更新生成视频的状态
  1180. s.resp.update_gen_video_file_status = function(msg_body)
  1181. local custom = "{}"
  1182. if msg_body.custom~=nil and msg_body.custom~="" then
  1183. custom = msg_body.custom
  1184. end
  1185. 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)
  1186. db:query(sql)
  1187. skynet.error(sql)
  1188. return true
  1189. end
  1190. --检测指定类型文件是否够数量
  1191. s.resp.checkNumsByType = function(msg_body)
  1192. local duration = 0
  1193. if msg_body.duration~= nil then
  1194. duration = msg_body.duration
  1195. end
  1196. local limit = msg_body.limit
  1197. local type = msg_body.type
  1198. 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)
  1199. -- skynet.error("sql",sql)
  1200. local res = db:query(sql)
  1201. if res and #res > 0 then
  1202. if #res >= tonumber(limit) then
  1203. return true
  1204. end
  1205. return false
  1206. else
  1207. return false
  1208. end
  1209. end
  1210. --修改密码
  1211. s.resp.reset_password = function(msg_body)
  1212. local sql = string.format("UPDATE users SET password = '%s' WHERE id = %d ",msg_body.new_pw,msg_body.user_id)
  1213. db:query(sql)
  1214. return true
  1215. end
  1216. --检测是否有此文件
  1217. s.resp.check_have_file = function(msg_body)
  1218. 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)
  1219. -- skynet.error("sql",sql)
  1220. local res = db:query(sql)
  1221. -- 判断是否找到数据
  1222. if res and #res > 0 then
  1223. local tab = tools.getDbResData(res)
  1224. skynet.error("Found data:",tab)
  1225. return true,tab
  1226. else
  1227. skynet.error("No data found.")
  1228. return false,nil
  1229. end
  1230. end
  1231. --检测是否有此文件夹
  1232. s.resp.check_have_folder = function(msg_body)
  1233. 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)
  1234. -- skynet.error("sql",sql)
  1235. local res = db:query(sql)
  1236. -- 判断是否找到数据
  1237. if res and #res > 0 then
  1238. local tab = tools.getDbResData(res)
  1239. skynet.error("Found data:",tab)
  1240. return true,tab
  1241. else
  1242. skynet.error("No data found.")
  1243. return false,nil
  1244. end
  1245. end
  1246. --检测滚屏模板是否正常
  1247. s.resp.check_template_scroll_isRight = function(info)
  1248. local isok = s.resp.checkNumsByType({
  1249. duration = 0,
  1250. type = 6,
  1251. limit = 1,
  1252. user_id = info.user_id})
  1253. if not isok then
  1254. return false,cjson.encode({code=10001,msg = "新建模板失败,图片资源不足!"})
  1255. end
  1256. isok = s.resp.checkNumsByType({
  1257. duration = 0,
  1258. type = 4,
  1259. limit = 1,
  1260. user_id = info.user_id}
  1261. )
  1262. if not isok then
  1263. return false,cjson.encode({code=10001,msg = "新建模板失败,BGM音频不足!"})
  1264. end
  1265. isok = s.resp.checkNumsByType({
  1266. duration = 0,
  1267. type = 3,
  1268. limit = 1,
  1269. user_id = info.user_id}
  1270. )
  1271. if not isok then
  1272. return false,cjson.encode({code=10001,msg = "新建模板失败,片尾资源不足!"})
  1273. end
  1274. return true,nil
  1275. end
  1276. --检查模板是否正常
  1277. s.resp.check_template_isRight = function(info)
  1278. local isok = s.resp.checkNumsByType({
  1279. duration = 0,
  1280. type = 1,
  1281. limit = 1,
  1282. user_id = info.user_id})
  1283. if not isok then
  1284. return false,cjson.encode({code=10001,msg = "新建模板失败,片头资源不足!"})
  1285. end
  1286. isok = s.resp.checkNumsByType({
  1287. duration = 0,
  1288. type = 3,
  1289. limit = 1,
  1290. user_id = info.user_id}
  1291. )
  1292. if not isok then
  1293. return false,cjson.encode({code=10001,msg = "新建模板失败,片尾资源不足"})
  1294. end
  1295. local subtitles_audio_duration = 0
  1296. if info.template_info ~=nil and info.template_info~="" then
  1297. local template_info = cjson.decode(info.template_info)
  1298. template_info = tools.base64decode(template_info.data)
  1299. template_info = cjson.decode(template_info)
  1300. if template_info.subtitles_audio_duration~=nil then
  1301. subtitles_audio_duration = tonumber(template_info.subtitles_audio_duration)
  1302. end
  1303. end
  1304. local duration = info.video_crop_time + 5
  1305. local limit = math.floor(subtitles_audio_duration/duration)
  1306. skynet.error("limit",limit)
  1307. skynet.error("subtitles_audio_duration",subtitles_audio_duration)
  1308. skynet.error("duration",duration)
  1309. isok = s.resp.checkNumsByType({
  1310. duration = duration,
  1311. type = 2,
  1312. limit =limit+1 ,
  1313. user_id = info.user_id}
  1314. )
  1315. if not isok then
  1316. return false,cjson.encode({code=10001,msg = "正片资源不足"})
  1317. end
  1318. isok = s.resp.checkNumsByType({
  1319. duration = 0,
  1320. type = 4,
  1321. limit = 1,
  1322. user_id = info.user_id}
  1323. )
  1324. if not isok then
  1325. return false,cjson.encode({code=10001,msg = "BGM音效资源不足!"})
  1326. end
  1327. return true,nil
  1328. end
  1329. --接收到删除引用的文件列表信息
  1330. s.resp.recv_ref_file_list_del_info = function(msg_body)
  1331. skynet.error("接收到删除引用的文件列表信息")
  1332. local listToDelete = {}
  1333. for i = 1, #msg_body.file_id_list, 1 do
  1334. table.insert(listToDelete,i,msg_body.file_id_list[i])
  1335. end
  1336. local deleteString = table.concat(listToDelete, ',')
  1337. -- local sql = string.format("UPDATE res_list_tab SET is_delete = %d WHERE ref_id IN (%s)",1,deleteString)
  1338. -- db:query(sql)
  1339. local delete_sql = string.format("DELETE FROM res_list_tab WHERE ref_id IN (%s)", deleteString)
  1340. db:query(delete_sql)
  1341. skynet.error(delete_sql)
  1342. return true
  1343. end
  1344. --检测引用的文件夹是否有自己上传的文件
  1345. s.resp.check_ref_folder_have_self_file = function(msg_body)
  1346. local check_user_folder = function(info) --检测用户的文件夹
  1347. local user_id = info.user_id
  1348. local isHave = false
  1349. local folder_id = info.id
  1350. 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)
  1351. local res = db:query(sql)
  1352. local delete_tab = {}
  1353. -- 判断是否找到数据
  1354. if res and #res > 0 then
  1355. local tab = {}
  1356. if #res == 1 then
  1357. tab[1] = tools.getDbResData(res)
  1358. else
  1359. tab = tools.getDbResData(res)
  1360. end
  1361. local count = 1
  1362. for i = 1, #tab, 1 do
  1363. if tab[i].ref_id==0 then
  1364. isHave = true
  1365. else
  1366. table.insert(delete_tab,count,tab[i])
  1367. count = count+1
  1368. end
  1369. end
  1370. return isHave,delete_tab,info
  1371. else
  1372. return isHave,delete_tab,info
  1373. end
  1374. end
  1375. local sql = ""
  1376. local folder_id = msg_body.folder_id
  1377. --获取当前引用的文件夹
  1378. sql = string.format("select * from folder_list_tab where ref_id = %d ",folder_id)
  1379. local res = db:query(sql)
  1380. local tab_list = {}
  1381. if res and #res > 0 then
  1382. local tab = {}
  1383. if #res == 1 then
  1384. tab[1] = tools.getDbResData(res)
  1385. else
  1386. tab = tools.getDbResData(res)
  1387. end
  1388. for i = 1, #tab, 1 do
  1389. local isHave,delete_tab,info = check_user_folder(tab[i])
  1390. table.insert(tab_list,i,{isHave = isHave,delete_tab=delete_tab,info=info})
  1391. end
  1392. return tab_list
  1393. else
  1394. return tab_list
  1395. end
  1396. end
  1397. --接收到删除引用的文件夹列表信息
  1398. s.resp.recv_ref_folder_list_del_info = function(msg_body)
  1399. local delete_file_list = function(delete_tab)
  1400. local listToDelete = {}
  1401. for i = 1, #delete_tab, 1 do
  1402. table.insert(listToDelete,i,delete_tab[i].ref_id)
  1403. end
  1404. s.resp.recv_ref_file_list_del_info({file_id_list=listToDelete})
  1405. end
  1406. -- skynet.error("接收到删除引用的文件夹列表信息")
  1407. -- tools.dump(msg_body)
  1408. for i = 1, #msg_body.folder_id_list, 1 do
  1409. local folder_id = msg_body.folder_id_list[i]
  1410. local list = s.resp.check_ref_folder_have_self_file({folder_id=folder_id})
  1411. for i = 1, #list, 1 do
  1412. tools.dump(list[i])
  1413. delete_file_list(list[i].delete_tab)
  1414. local isHave = list[i].isHave
  1415. local folder_info = list[i].info
  1416. if isHave then --有自定义的文件不删除这个文件夹,但是要将其引用设置为0
  1417. skynet.error("有自定义的文件不删除这个文件夹,但是要将其引用设置为0")
  1418. 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)
  1419. db:query(sql)
  1420. skynet.error(sql)
  1421. else --没有自己上传的文件夹,将其文件夹删除
  1422. skynet.error("没有自己上传的文件夹,将其文件夹删除")
  1423. local sql = string.format("DELETE FROM folder_list_tab WHERE id = %d AND user_id = %d ",folder_info.id,folder_info.user_id)
  1424. db:query(sql)
  1425. skynet.error(sql)
  1426. end
  1427. end
  1428. end
  1429. return true
  1430. end
  1431. --接收到修改引用的文件信息
  1432. s.resp.recv_ref_file_modify_info = function(msg_body)
  1433. local file_id = msg_body.file_id
  1434. local name = msg_body.name
  1435. local sql = string.format("UPDATE res_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,file_id)
  1436. db:query(sql)
  1437. return true
  1438. end
  1439. s.resp.update_generate_video_custom = function(msg_body)
  1440. local list = msg_body.data
  1441. skynet.fork(function()
  1442. for i = 1, #list, 1 do
  1443. local video_id = list[i].video_id
  1444. local custom = "{}"
  1445. if list[i].custom~=nil and list[i].custom~="" then
  1446. custom = list[i].custom
  1447. end
  1448. 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)
  1449. db:query(sql)
  1450. -- skynet.error(sql)
  1451. end
  1452. end)
  1453. -- skynet.error(sql)
  1454. return true
  1455. end
  1456. --接收到修改引用的文件夹信息
  1457. s.resp.recv_ref_folder_modify_info = function(msg_body)
  1458. local folder_id = msg_body.folder_id
  1459. local name = msg_body.name
  1460. local sql = string.format("UPDATE folder_list_tab SET folder_name ='%s' WHERE ref_id = %d",name,folder_id)
  1461. db:query(sql)
  1462. return true
  1463. end
  1464. --获取用户所有的文件数
  1465. s.resp.get_user_folder_list_nums = function(msg_body)
  1466. local sql = string.format("select COUNT(user_id) AS nums from folder_list_tab where user_id = %d ",msg_body.user_id)
  1467. local res = db:query(sql)
  1468. local count = 0
  1469. if res and #res > 0 then
  1470. count = res.nums
  1471. end
  1472. return count
  1473. end
  1474. --获取用户指定类型的数量
  1475. s.resp.get_user_file_count_by_type = function(msg_body)
  1476. 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)
  1477. local res = db:query(sql)
  1478. local count = 0
  1479. if res and #res > 0 then
  1480. count = res.nums
  1481. end
  1482. return count
  1483. end
  1484. s.resp.check_file_name_list_is_repeat_by_folder_id = function(msg_body,fd,call_back)
  1485. -- tools.dump(msg_body)
  1486. skynet.fork(function()
  1487. local nameString = ""
  1488. for i = 1, #msg_body.file_name_list, 1 do
  1489. nameString = nameString.."'"..msg_body.file_name_list[i].."'"
  1490. if i<#msg_body.file_name_list then
  1491. nameString = nameString..","
  1492. end
  1493. end
  1494. local sql = string.format("SELECT file_name FROM res_list_tab WHERE file_name IN (%s) AND folder_id = %d AND user_id = %d ",
  1495. nameString,
  1496. msg_body.folder_id,
  1497. msg_body.user_id)
  1498. local res = db:query(sql)
  1499. local tab = {}
  1500. -- skynet.error("sql",sql)
  1501. if #res == 1 then
  1502. tab[1] = tools.getDbResData(res)
  1503. else
  1504. if #res>0 then
  1505. tab = tools.getDbResData(res)
  1506. end
  1507. end
  1508. -- tools.dump(tab)
  1509. local normal_file_name_list ={}
  1510. local repeat_file_name_list ={}
  1511. for i = 1, #tab, 1 do
  1512. table.insert(repeat_file_name_list,i,tab[i].file_name)
  1513. end
  1514. function check(file_name,list)
  1515. for i = 1, #list, 1 do
  1516. if list[i]==file_name then
  1517. return false
  1518. end
  1519. end
  1520. return true
  1521. end
  1522. local count = 1
  1523. for i = 1, #msg_body.file_name_list, 1 do
  1524. if check(msg_body.file_name_list[i],repeat_file_name_list) then
  1525. table.insert(normal_file_name_list,count,msg_body.file_name_list[i])
  1526. count = count+1
  1527. end
  1528. end
  1529. if call_back~=nil then
  1530. call_back({normal_file_name_list=normal_file_name_list,repeat_file_name_list=repeat_file_name_list})
  1531. else
  1532. 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})
  1533. end
  1534. end)
  1535. return true
  1536. end
  1537. --获取用户的分享用户列表
  1538. s.resp.get_share_user_list = function(msg_body,fd)
  1539. local isOk,user_data = s.resp.getUserById(msg_body.user_id)
  1540. local temp = {}
  1541. if isOk then
  1542. local select_share = nil
  1543. if user_data.user_info~=nil and user_data.user_info~="" then
  1544. local info = cjson.decode(user_data.user_info)
  1545. select_share = info.select_share
  1546. end
  1547. local count = 1
  1548. skynet.fork(function()
  1549. function action()
  1550. tools.response_db_get_share_user_list(fd,msg_body,true,temp)
  1551. end
  1552. if select_share~=nil and select_share~="" then
  1553. for i = 1, #select_share, 1 do
  1554. local user_id = select_share[i]
  1555. local _,ud = s.resp.getUserById(user_id)
  1556. if _ then
  1557. if ud.id~=msg_body.user_id then
  1558. table.insert(temp,count,ud)
  1559. count = count +1
  1560. end
  1561. end
  1562. end
  1563. end
  1564. if user_data.permit_id == 2 then --说明是管理
  1565. s.resp.get_group_list(user_data.group_id,function(list)
  1566. -- skynet.error("user_data.group_id",user_data.group_id)
  1567. -- tools.dump(list)
  1568. for i = 1, #list, 1 do
  1569. if list[i].permit_id~=2 and list[i].id~=user_data.id then
  1570. table.insert(temp,count,list[i])
  1571. count = count +1
  1572. end
  1573. end
  1574. action()
  1575. end)
  1576. else
  1577. action()
  1578. end
  1579. end)
  1580. return true
  1581. end
  1582. return false
  1583. end
  1584. --根据名字获取文件夹
  1585. s.resp.get_folder_by_name = function(msg_body,fd)
  1586. 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)
  1587. local res = db:query(sql)
  1588. local tab = {}
  1589. tab = tools.getDbResData(res)
  1590. if #res <=0 then
  1591. return false,nil
  1592. end
  1593. return true,tab
  1594. end
  1595. s.resp.get_folder_have_file_name = function(user_id,folder_id,file_name)
  1596. 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)
  1597. local res = db:query(sql)
  1598. local tab = {}
  1599. tab = tools.getDbResData(res)
  1600. skynet.error("#res",#res)
  1601. if #res <=0 then
  1602. return false,nil
  1603. end
  1604. return true,tab
  1605. end
  1606. --分享文件列表
  1607. s.resp.share_file_list = function(msg_body,fd)
  1608. function insert_target(file,folder_id,target_user_id)
  1609. local is_have,_ = s.resp.get_folder_have_file_name(target_user_id,folder_id,file.file_name)
  1610. if is_have==true then
  1611. else
  1612. 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)",
  1613. target_user_id,
  1614. folder_id,
  1615. file.file_type,
  1616. file.file_name,
  1617. file.file_info,
  1618. file.duration,
  1619. file.classification_id,
  1620. file.id)
  1621. skynet.error("sql",sql)
  1622. db:query(sql)
  1623. end
  1624. -- skynet.error(sql)
  1625. end
  1626. for j = 1, #msg_body.target_user_id_list, 1 do
  1627. local target_user_id = msg_body.target_user_id_list[j]
  1628. local my_user_id = msg_body.user_id
  1629. skynet.fork(function()
  1630. for i = 1, #msg_body.file_id_list, 1 do
  1631. local file_id = msg_body.file_id_list[i].file_id
  1632. local folder_name = msg_body.file_id_list[i].folder_name
  1633. local sql = string.format("SELECT * FROM res_list_tab WHERE id = %d limit 1", file_id)
  1634. local res = db:query(sql)
  1635. local tab = tools.getDbResData(res)
  1636. if #res>0 then
  1637. --检测被分享的是否拥有此文件夹
  1638. 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})
  1639. if isOk then --拥有此文件夹,直接插入
  1640. skynet.error("拥有此文件夹,直接插入",folder_name)
  1641. insert_target(tab,folder_data.id,target_user_id)
  1642. else -- 没有此文件夹,创建一个再插入
  1643. 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)",
  1644. target_user_id,
  1645. folder_name,
  1646. tab.file_type
  1647. ,0
  1648. ,tab.classification_id)
  1649. -- skynet.error(sql)
  1650. local new_res = db:query(new_sql)
  1651. 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",
  1652. target_user_id,
  1653. folder_name,
  1654. tab.file_type,
  1655. tab.classification_id)
  1656. new_res = db:query(new_sql)
  1657. if new_res and #new_res > 0 then
  1658. local folder_info = {}
  1659. folder_info = tools.getDbResData(new_res)
  1660. insert_target(tab,folder_info.id,target_user_id)
  1661. end
  1662. end
  1663. end
  1664. end
  1665. end)
  1666. end
  1667. return true
  1668. end
  1669. --分享文件夹列表
  1670. s.resp.share_folder_list = function(msg_body,fd)
  1671. function getInfoByName(name,list)
  1672. for i = 1, #list, 1 do
  1673. if list[i].file_name == name then
  1674. return list[i]
  1675. end
  1676. end
  1677. return nil
  1678. end
  1679. function insert_target_folder(target_folder_id,my_folder_id,my_user_id,target_user_id)
  1680. local isok = s.resp.folder_res_list({
  1681. folder_id = my_folder_id,
  1682. user_id =my_user_id,
  1683. is_public=0},nil,function(list)
  1684. skynet.error("insert_target_folder",#list)
  1685. if #list>0 then
  1686. local file_name_list = {}
  1687. for i = 1, #list, 1 do
  1688. local _f = list[i]
  1689. table.insert(file_name_list,i,_f.file_name)
  1690. end
  1691. 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)
  1692. for i = 1, #data.normal_file_name_list, 1 do
  1693. local file = getInfoByName(data.normal_file_name_list[i],list)
  1694. if file~=nil then
  1695. 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)",
  1696. target_user_id,
  1697. target_folder_id,
  1698. file.file_type,
  1699. file.file_name,
  1700. file.file_info,
  1701. file.duration,
  1702. file.classification_id,
  1703. file.id)
  1704. db:query(sql)
  1705. end
  1706. end
  1707. end)
  1708. end
  1709. end)
  1710. end
  1711. for j = 1, #msg_body.target_user_id_list, 1 do
  1712. local target_user_id = msg_body.target_user_id_list[j]
  1713. local my_user_id = msg_body.user_id
  1714. skynet.fork(function()
  1715. for i = 1, #msg_body.folder_id_list, 1 do
  1716. local folder_id = msg_body.folder_id_list[i].folder_id
  1717. local folder_name = msg_body.folder_id_list[i].folder_name
  1718. local sql = string.format("SELECT * FROM folder_list_tab WHERE id = %d limit 1", folder_id)
  1719. local res = db:query(sql)
  1720. local tab = tools.getDbResData(res)
  1721. if #res>0 then
  1722. --检测被分享的是否拥有此文件夹
  1723. 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})
  1724. if isOk then --拥有此文件夹,直接插入
  1725. skynet.error("拥有此文件夹,直接插入 ")
  1726. insert_target_folder(folder_data.id,folder_id,my_user_id,target_user_id)
  1727. else -- 没有此文件夹,创建一个再插入
  1728. 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)",
  1729. target_user_id,
  1730. folder_name,
  1731. tab.folder_type
  1732. ,0
  1733. ,tab.classification_id)
  1734. skynet.error("没有此文件夹,创建一个再插入 ")
  1735. skynet.error(sql)
  1736. db:query(new_sql)
  1737. 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",
  1738. target_user_id,
  1739. folder_name,
  1740. tab.folder_type,
  1741. tab.classification_id)
  1742. local new_res = db:query(new_sql)
  1743. if new_res and #new_res > 0 then
  1744. local folder_info = {}
  1745. folder_info = tools.getDbResData(new_res)
  1746. insert_target_folder(folder_info.id,folder_id,my_user_id,target_user_id)
  1747. end
  1748. end
  1749. end
  1750. end
  1751. end)
  1752. end
  1753. return true
  1754. end
  1755. s.resp.get_folder_info = function(msg_body,fd)
  1756. local listToFind= {}
  1757. for i = 1, #msg_body.folder_id_list, 1 do
  1758. table.insert(listToFind,i,msg_body.folder_id_list[i])
  1759. end
  1760. local findString = table.concat(listToFind, ',')
  1761. skynet.fork(function()
  1762. local sql = string.format("SELECT * FROM folder_list_tab WHERE id IN (%s) AND user_id = %d ", findString,msg_body.user_id)
  1763. local res = db:query(sql)
  1764. if res and #res > 0 then
  1765. local tab = {}
  1766. if #res == 1 then
  1767. tab[1] = tools.getDbResData(res)
  1768. else
  1769. tab = tools.getDbResData(res)
  1770. end
  1771. tools.response_db_get_folder_info(fd,msg_body,true,tab)
  1772. else
  1773. tools.response_db_get_folder_info(fd,msg_body,false,{})
  1774. end
  1775. end)
  1776. end
  1777. s.resp.setItem = function(_msg_body,_fd)
  1778. skynet.fork(function(msg_body,fd)
  1779. local sql = string.format("SELECT user_data FROM user_data_tab WHERE user_id = %d ",msg_body.user_id)
  1780. local res = db:query(sql)
  1781. if res and #res > 0 then
  1782. local tab = tools.getDbResData(res)
  1783. if tab.user_data~=nil then
  1784. local temp_tab = cjson.decode(tab.user_data)
  1785. temp_tab[msg_body.key] = msg_body.base64value
  1786. sql = string.format("UPDATE user_data_tab SET user_data ='%s' WHERE user_id = %d ",cjson.encode(temp_tab),msg_body.user_id)
  1787. db:query(sql)
  1788. tools.response_setItem(fd,true)
  1789. else
  1790. tools.response_setItem(fd,false)
  1791. end
  1792. else
  1793. local temp_tab = {}
  1794. temp_tab[msg_body.key] = msg_body.base64value
  1795. sql = string.format("INSERT INTO user_data_tab (user_id,user_data) VALUES (%d, '%s')",msg_body.user_id,cjson.encode(temp_tab))
  1796. db:query(sql)
  1797. tools.response_setItem(fd,true)
  1798. end
  1799. end,_msg_body,_fd)
  1800. return true
  1801. end
  1802. s.resp.getItem = function(_msg_body,_fd)
  1803. skynet.fork(function(msg_body,fd)
  1804. local sql = string.format("SELECT user_data FROM user_data_tab WHERE user_id = %d ",msg_body.user_id)
  1805. local res = db:query(sql)
  1806. if res and #res > 0 then
  1807. local tab = tools.getDbResData(res)
  1808. if tab.user_data~=nil then
  1809. local temp_tab = cjson.decode(tab.user_data)
  1810. tools.response_getItem(fd,true,temp_tab[msg_body.key])
  1811. else
  1812. tools.response_getItem(fd,false,nil)
  1813. end
  1814. else
  1815. tools.response_getItem(fd,false,nil)
  1816. end
  1817. end,_msg_body,_fd)
  1818. return true
  1819. end
  1820. s.init = function()
  1821. if runconfig.is_debug then
  1822. db=mysql.connect({
  1823. host=runconfig.db_debug_config.db_tost,
  1824. port=runconfig.db_debug_config.db_port,
  1825. database=runconfig.db_debug_config.db_name,
  1826. user=runconfig.db_debug_config.db_user,
  1827. password=runconfig.db_debug_config.db_pw,
  1828. max_packet_size = 1024 * 1024,
  1829. on_connect = nil
  1830. })
  1831. else
  1832. db=mysql.connect({
  1833. host=runconfig.db_release_config.db_tost,
  1834. port=runconfig.db_release_config.db_port,
  1835. database=runconfig.db_release_config.db_name,
  1836. user=runconfig.db_release_config.db_user,
  1837. password=runconfig.db_release_config.db_pw,
  1838. max_packet_size = 1024 * 1024,
  1839. on_connect = nil
  1840. })
  1841. end
  1842. if not db then
  1843. skynet.error("failed to connect")
  1844. skynet.exit()
  1845. else
  1846. skynet.error("success to connect to mysql server")
  1847. end
  1848. --设置utf8字符集
  1849. local res = db:query("set charset utf8");
  1850. end
  1851. s.start(...)