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

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

    //查询收藏列表
    async selectCollect(obj) {
        var title = obj.title;
        var username = obj.username;
        var selectsql = "SELECT * FROM tmk_collect WHERE title = \'" + title + "\'AND username = \'" + username+"\'";
        var selectres = await this.tmkDao.customQuery(selectsql);
        return selectres;
    }

    //增加收藏
    async insertCollect(obj) {
        var title = obj.title;
        var detail = obj.detail;
        detail = JSON.stringify(detail);
        // var ObjectbusinessAddress = obj.detail.ObjectbusinessAddress;
        // var businessTerm = obj.detail.businessTerm;
        // var companyType= obj.detail.companyType;
        // var creditCode= obj.detail.creditCode;
        // var dateIssue= obj.detail.dateIssue;
        // var foundedTime= obj.detail.foundedTime;
        // var legalRepresentative= obj.detail.legalRepresentative;
        // var managementState= obj.detail.managementState;
        // var name= obj.detail.name;
        // var organizationCode= obj.detail.organizationCode;
        // var registerOffice= obj.detail.registerOffice;
        // var registeredCapital= obj.detail.registeredCapital;
        // var registrID= obj.detail.registrID;
        // var scope= obj.detail.scope;
        var username = obj.username;
        var sql = "INSERT INTO tmk_collect (title,detail,username) values(\'" + title + "\',\'" + detail + "\',\'" + username + "\')";
        var result = await this.tmkDao.customExecAddOrPutSql(sql);
        return result;
    }

    //删除收藏
    async delCollect(obj) {
        var title = obj.title;
        var username = obj.username;
        var sql = "DELETE FROM tmk_collect WHERE title = \'"+title+"\' AND username = \'"+username+"\'";
        var result = await this.tmkDao.customExecAddOrPutSql(sql);
        return result;
    }

    //查询当前用户收藏列表（分页查询）
    async selectAllCollect(obj) {
        try {
            var currentPage;
            var username = obj.username == null ? "" : obj.username;
            //每页显示条数
            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 pageNo = obj.pageNo;
            // var pageSize = obj.pageSize;
            // var sql = "SELECT * FROM tmk_collect WHERE username = \'" + username+"\'";
            //-------------------------------

            var sql = "SELECT * FROM tmk_collect WHERE username = \'" + username + "\' limit " + currentPagenum + "," + pageSize;
            //获取总条数
            var uercollect = await this.selectuserCollect(obj);
            //总条数
            var totalSize = uercollect == null ? 0 : uercollect;

            var result = await this.tmkDao.customQuery(sql);
            var data={ "code":1,
                "totalSize":totalSize.length,
                "pageSize":pageSize,
                "currentPage":currentPage,"data":result };
            return data;
        }catch (e) {
            return {code:-1,msg:"操作失败",data:null};
        }
    }

    //查询当前用户收藏列表
    async selectuserCollect(obj) {
        var username = obj.username;
        var selectsql = "SELECT * FROM tmk_collect WHERE username = \'"+ username+"\'";
        var selectres = await this.tmkDao.customQuery(selectsql);
        return selectres;
    }
}

module.exports=TmkService;