const system = require("../../../system");
const Dao = require("../../dao.base");

class UserDao extends Dao {
    constructor() {
        super(Dao.getModelName(UserDao));
    }

    async register(user) {
        console.log(user)

        var sql = "SELECT id FROM tbl_users WHERE loginname LIKE :loginname ";
        var list = await this.customQuery(sql, {loginname: "%" + user.loginname + "%"});
        console.log(list)

    }

    async getAuths(userid) {
        var self = this;
        return this.model.findOne({
            where: {id: userid},
            include: [{model: self.db.models.account, attributes: ["id", "isSuper", "referrerOnlyCode"]},
                {
                    model: self.db.models.role, as: "Roles", attributes: ["id", "code"], include: [
                        {model: self.db.models.product, as: "Products", attributes: ["id", "code"]}
                    ]
                },
            ],
        });
    }

    extraModelFilter() {
        //return {"key":"include","value":[{model:this.db.models.app,},{model:this.db.models.role,as:"Roles",attributes:["id","name"],joinTableAttributes:['created_at']}]};
        return {
            "key": "include",
            "value": [{model: this.db.models.app,}, {
                model: this.db.models.role,
                as: "Roles",
                attributes: ["id", "name"]
            }]
        };
    }

    extraWhere(obj, w, qc, linkAttrs) {
        if (obj.codepath && obj.codepath != "") {
            //  if(obj.codepath.indexOf("userarch")>0){//说明是应用管理员的查询
            //    console.log(obj);
            //     w["app_id"]=obj.appid;
            //  }
        }
        if (linkAttrs.length > 0) {
            var search = obj.search;
            var lnkKey = linkAttrs[0];
            var strq = "$" + lnkKey.replace("~", ".") + "$";
            w[strq] = {[this.db.Op.like]: "%" + search[lnkKey] + "%"};

        }
        return w;
    }

    //修改用户(user表)公司的唯一码
    async putUserCompanyOnlyCode(userId, company_only_code, result) {
        var customerObj = {companyOnlyCode: company_only_code};
        var putSqlWhere = {where: {id: userId}};
        this.updateByWhere(customerObj, putSqlWhere);
        return result;
    }

}

module.exports = UserDao;