init.lua 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  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.resp.setItem = function(_msg_body,_fd)
  1770. skynet.fork(function(msg_body,fd)
  1771. local sql = string.format("SELECT user_data FROM user_data_tab WHERE user_id = %d ",msg_body.user_id)
  1772. local res = db:query(sql)
  1773. if res and #res > 0 then
  1774. local tab = tools.getDbResData(res)
  1775. if tab.user_data~=nil then
  1776. local temp_tab = cjson.decode(tab.user_data)
  1777. temp_tab[msg_body.key] = msg_body.base64value
  1778. sql = string.format("UPDATE user_data_tab SET user_data ='%s' WHERE user_id = %d ",cjson.encode(temp_tab),msg_body.user_id)
  1779. db:query(sql)
  1780. tools.response_setItem(fd,true)
  1781. else
  1782. tools.response_setItem(fd,false)
  1783. end
  1784. else
  1785. local temp_tab = {}
  1786. temp_tab[msg_body.key] = msg_body.base64value
  1787. sql = string.format("INSERT INTO user_data_tab (user_id,user_data) VALUES (%d, '%s')",msg_body.user_id,cjson.encode(temp_tab))
  1788. db:query(sql)
  1789. tools.response_setItem(fd,true)
  1790. end
  1791. end,_msg_body,_fd)
  1792. return true
  1793. end
  1794. s.resp.getItem = function(_msg_body,_fd)
  1795. skynet.fork(function(msg_body,fd)
  1796. local sql = string.format("SELECT user_data FROM user_data_tab WHERE user_id = %d ",msg_body.user_id)
  1797. local res = db:query(sql)
  1798. if res and #res > 0 then
  1799. local tab = tools.getDbResData(res)
  1800. if tab.user_data~=nil then
  1801. local temp_tab = cjson.decode(tab.user_data)
  1802. tools.response_getItem(fd,true,temp_tab[msg_body.key])
  1803. else
  1804. tools.response_getItem(fd,false,nil)
  1805. end
  1806. else
  1807. tools.response_getItem(fd,false,nil)
  1808. end
  1809. end,_msg_body,_fd)
  1810. return true
  1811. end
  1812. s.init = function()
  1813. db=mysql.connect({
  1814. host=runconfig.db_tost,
  1815. port=runconfig.db_port,
  1816. database=runconfig.db_name,
  1817. user="root",
  1818. password=runconfig.db_pw,
  1819. max_packet_size = 1024 * 1024,
  1820. on_connect = nil
  1821. })
  1822. if not db then
  1823. skynet.error("failed to connect")
  1824. skynet.exit()
  1825. else
  1826. skynet.error("success to connect to mysql server")
  1827. end
  1828. --设置utf8字符集
  1829. local res = db:query("set charset utf8");
  1830. end
  1831. s.start(...)