const system=require("../../system");
const ServiceBase=require("../sve.base");

class TmkRemarkService extends ServiceBase{
    constructor(){
        super(ServiceBase.getDaoName(TmkRemarkService));
        this.tmkremarkDao = system.getObject("db.tmkremarkDao");
    }

    //增加备注
    async insertRemark(obj) {
        var title = obj.title;
        var remark = obj.remark;
        var username = obj.username;
        var sql = "INSERT INTO tmk_remark (title,remark,username) values(\'" + title + "\',\'" + remark + "\',\'" + username + "\')";
        var result = await this.tmkremarkDao.customExecAddOrPutSql(sql);
        return result;
    }

    //删除备注
    async delRemark(obj) {
        // var title = obj.title;
        var username = obj.username;
        var userid = obj.id;
        var sql = "DELETE FROM tmk_remark WHERE id = \'"+userid+"\' AND username = \'"+username+"\'";
        var result = await this.tmkremarkDao.customExecAddOrPutSql(sql);
        return result;
    }

    // //查询备注列表
    // async selectRemark(obj) {
    //     var title = obj.title;
    //     var sql = "SELECT * FROM tmk_remark WHERE title = \'" + title+"\'";
    //     var result = await this.tmkremarkDao.customQuery(sql);
    //     return result;
    // }

    //查询备注列表（分页）
    async selectRemark(obj) {
        try {
            var currentPage;
            var title = obj.title == null ? "" : obj.title;
            //每页显示条数
            var pageSize = obj.pageSize == null ? 15 : obj.pageSize;
            //当前显示第几页
            if (obj.currentPage == null) {
                currentPage = 0;
            } else {
                currentPage =obj.currentPage;
            }
            var currentPagenum = Number((currentPage - 1) * pageSize);
            var sql = "SELECT * FROM tmk_remark WHERE title = \'" + title + "\' order by id desc  limit " + currentPagenum + "," + pageSize;
            //获取总条数
            var allremark = await this.selectAlltRemark(obj);
            //总条数
            var totalSize = allremark == null ? 0 : allremark;
            //分页查询
            var result = await this.tmkremarkDao.customQuery(sql);
            var data = {
                "code": 1,
                "totalSize": totalSize.length,
                "pageSize": pageSize,
                "currentPage":currentPage, "data": result
            };
            return data;
        }catch (e) {
            return {code:-1,msg:"操作失败",data:[]};
        }
    }

    //查询当前备注列表
    async selectAlltRemark(obj) {
        var title = obj.title == null ? "" : obj.title;
        var selectsql = "SELECT * FROM tmk_remark WHERE title = \'"+ title+"\'";
        var selectres = await this.tmkremarkDao.customQuery(selectsql);
        return selectres;
    }
}

module.exports=TmkRemarkService;