'use strict'; const Service = require('egg').Service; class PostService extends Service { async add(uid, data) { let mysql = this.app.mysql; data.user_id = uid; let ret = await mysql.insert("posts", data); return ret; } async list(query) { let mysql = this.app.mysql; let limit = ` limit ${query.page*query.size},${query.size}`; let where = ""; let orderBy = ""; if( query.cmplyIds ) { //保险公司 } if( query.insDur ) { //保险期间 } if( query.insCategory) //保险分类 { } if( query.orderBy == 0 ) { //智能排序 } else if( query.orderBy == 1 ) { //保额高到低 } else if( query.orderBy == 2 ) { //价格高到低 } else if( query.orderBy == 3 ) { //价格低到高 } let ret = await mysql.query(`select id, title, subtitle, tags, coverage, price from posts ${where} ${orderBy} ${limit}`); return {page:query.page,size:query.size, list:ret}; } async delete(uid, id) { let mysql = this.app.mysql; let ret = await mysql.query("delete from posts where id=? and user_id=?",[id, uid]); return ret.affectedRows == 1; } async update( data ) { let mysql = this.app.mysql; let ret = await mysql.update("posts", data, {where: {id: data.id}}); return ret.affectedRows == 1; } async postDetail(uid, id ) { let mysql = this.app.mysql; console.log( id ); let ret = await mysql.query("select id,coverage, price, tags, title, html, agree_num, like_num, comment_num from posts where id =?",[id]); ret = ret[0]; //获取返对,收藏,喜欢状态 let unagree = await mysql.query("select id from post_unagree where post_id=? and user_id=?",[id, uid]); unagree = unagree[0]; let collected = await mysql.query("select id from post_collect where post_id=? and user_id=?",[id, uid]); collected = collected[0]; let liked = await mysql.query("select id from post_like where post_id=? and user_id=?",[id, uid]); liked = liked[0]; ret.unagreed = !!unagree; ret.collected = !!collected; ret.liked = !!liked; return ret; } async agree(id) { let mysql = this.app.mysql; let ret = await mysql.query("update posts set agree_num=agree_num+1 where id=?", [id]); return ret.affectedRows == 1; } async unagree(uid, id, unagree) { let mysql = this.app.mysql; let ret = await mysql.query("select id from post_unagree where post_id=? and user_id=?", [id, uid]); if( unagree == 1 ) { if( ret[0] ) { return true; } ret = await mysql.insert("post_unagree",{post_id: id, user_id: uid}); return ret.affectedRows == 1; } ret = await mysql.query("delete from post_unagree where post_id=? and user_id=? ",[id, uid]); return ret.affectedRows == 1; } async like(uid, id, like) { let mysql = this.app.mysql; let ret; if( like == 1 ) { ret = await mysql.query("select * from post_like where post_id=? and user_id=?",[id, uid]); ret = ret[0]; if( !ret ) { await mysql.query("update posts set like_num=like_num+1 where id=?", [id]); ret = await mysql.insert("post_like", {post_id:id, user_id: uid}); } } else { ret = await mysql.query("delete from post_like where post_id =? and user_id = ?",[id, uid]); if( ret.affectedRows == 1 ) { await mysql.query("update posts set like_num=like_num-1 where id=?", [id]); } } console.log( ret ); return true; } async collect(uid, id, collect) { let mysql = this.app.mysql; let ret = await mysql.query("select id from post_collect where post_id=? and user_id=?", [id, uid]); if( collect == 1 ) { if( ret[0] ) { return true; } ret = await mysql.insert("post_collect",{post_id: id, user_id: uid}); return ret.affectedRows == 1; } ret = await mysql.query("delete from post_collect where post_id=? and user_id=? ",[id, uid]); return ret.affectedRows == 1; } } module.exports = PostService;