import _ from 'lodash'; import cfg from '../cfg/cfg'; import {Response} from '../shared/lib/response'; import {RecDB} from '../lib/mongo.js'; import {timed} from '../utils/metrics'; let config = cfg.recommend.mongo; // 获取用户的观看电影的卡片 let getreadCards = async(opts) => { opts = opts || {}; let id = opts.id; if (!id) { return { total: 0, data: [] }; } let readCards = timed('personRec.readCards', async() => { let doRead = await RecDB.findOne({ query: { id: opts.id }, collection: 'label' }); return doRead; }); let res = await readCards(); let results = res ? res.result : null; results = results || []; return results; // let videos = _.orderBy(results, ['weight'], ['desc']); // let videos = _.map(_.sortByOrder(results, ['weight'], ['desc']), _.values); // let videos = _.map(results, 'cards'); }; // 获取每个卡片对应的电影 let getRealtimeCardVideos = async(opts) => { opts = opts || {}; let cards = opts.cards; if (!cards) { return { total: 0, data: [] }; } let read = timed('personRec.realtimeCardVideos', async() => { let doRead = await RecDB.findOne({ query: { cards: opts.cards }, collection: 'label1' }); return doRead; }); let res = await read(); res = res ? res.result : null; // console.log(JSON.stringify(res)); res = res || []; let videos = _.chain(res) .forEach((val) => { val.type = 'realtimeCard'; }) .value(); return videos; }; // 获得所有的热度卡片 let getHotCards = async (opts) => { opts = opts || {}; let page = opts.page; let pageSize = opts.pageSize; let read = timed('personalRec.readCards', async () => { let doRead = await RecDB.read({ options: { skip: (page-1) * pageSize; limit: page; } collection: 'movielabel' }); return doRead; }); let res = await read(); let hotCards = res ? res.result : null; hotCards = hotCards || []; return hotCards; return { total: hotCards.length, data: hotCards }; }; let getUserRecords = async(opts) => { opts = opts || {}; let macId = opts.macId; if (!macId) { return { total: 0, data: [] }; } // userrecord 用户的实时记录 let readRecords = timed('personRec.readRecords', async() => { let doRead = await RecDB.findOne({ query: { mac: opts.macId }, collection: 'userrecord' }); return doRead; }); let res = await readRecords(); let result = res ? res.vv : null; result = result || []; let videos = _.map(result, 'id'); return videos; }; let getPersonalCards = async (opts) => { opts = opts || {}; let page = opts.page; let pageSize = opts.pageSize; let realtimeCard = []; // console.log(group+'11111'); // 获取分组对应的 collection console.log(collectionName); let macId = opts.macId; if (!macId || !collectionName) { return { total: 0, data: [] }; } let readVideos = timed('personalRec.readPersonalCards', async () => { let doRead = await RecDB.read({ query: { mac: macId }, collection: 'personalCards' }); return doRead; }); let [res, record, oldrecord] = await Promise.all([ readVideos(), getUserRecords({macId}), getoldRecords({macId}) ]); // console.log('----------------'); // console.log(oldrecord); // console.log('----------------'); // console.log(macId, collectionName, res) // 将获取的卡片排序 // 将获取的卡片转换成电影 let cardVideos = []; for (let labe of realtimeCard) { let weight = labe.weight; cardVideos = _.uniq(_.concat(cardVideos, card), 'id'); } console.log(cardVideos); userPrefers = _.concat(userPrefers, record); let real = _.chain(realtime).map((val) => { let temp = { id: val.id, group: val.group, type: val.type }; // console.log(JSON.stringify(temp)); return temp; }).filter(val => !_.includes(userPrefers, val.id)) .value(); // res = res ? res.result : null; res = res ? res[0].result : null; // console.log(res); let realtimeCards = []; if (res.length === 0) { let hotCards = await getHotCards({page: page, pageSize: pageSize}); for (let video of record) { // console.log(video) let appendLabel = await getreadCards({id: video}); let weigh = appendLabel.weight; let movies = await getRealtimeCardVideos({cards: appendLabel.id}); let movie = _.find(movies, (item) => item.id === video); weigh = weigh + (movie.weight ? movie.weight: 0); let card = _.find(hotCards, (item) => item.labelid === appendLabel.labelid); card.weight = weigh; // realtimeCard = _.uniq(_.concat(realtimeCard, appendLabel)); // console.log(JSON.stringify(append)); }; realtimeCards = _.orderBy(results, ['weight'], ['desc']); }; else { for (let video of record) { // console.log(video) let appendLabel = await getreadCards({id: video}); let weigh = appendLabel.weight; let movies = await getRealtimeCardVideos({cards: appendLabel.id}); let movie = _.find(movies, (item) => item.id === video); weigh = weigh + (movie.weight ? movie.weight: 0); let card = _.find(res, (item) => item.labelid === appendLabel.labelid); card.weight = weigh; let start = (page - 1) * pageSize; let end = page * pageSize; realtimeCards = _.orderBy(res, ['weight'], ['desc']); return { total: realtimeCards.length, data: realtimeCards }; }; }; }; let realtimeRecHandler = async (ctx) => { let query = ctx.query; // let page = query.page; let page = parseInt(query.page, 10) || 1; let pageSize = parseInt(query.pageSize, 10) || 10; let macId = query.macId; let res = await getPersonalVideos({ page, pageSize, macId }); let pageCount = Math.ceil(res.total / pageSize); return new Response({ data: { page, pageSize, pageCount, macId, total: res.total, videos: res.data } }); }; export { realtimeRecHandler, };