const system=require("../system");
const settings=require("../../config/settings.js");
const reclient=system.getObject("util.redisClient");
const md5 = require("MD5");
//获取平台配置兑换率
//初次登录的赠送数量
//创建一笔交易
//同时增加账户数量,增加系统平台账户
var dbf=system.getObject("db.common.connection");
var db=dbf.getCon();
db.sync({force:true}).then(async ()=>{
  console.log("sync complete...");
  //创建平台配置
  const PConfig=db.models.pconfig;
  var pconfigObjs=await PConfig.bulkCreate([
    {configType:"price",configValue:"100",app_id:settings.platformid,company_id:settings.platformcompanyid,appkey:settings.appKey},
    {configType:"initGift",configValue:"10000",app_id:settings.platformid,company_id:settings.platformcompanyid,appkey:settings.appKey},
    {configType:"apiInitGift",configValue:"500",app_id:settings.platformid,company_id:settings.platformcompanyid,appkey:settings.appKey},
    {configType:"apiCallPrice",configValue:"10",app_id:settings.platformid,company_id:settings.platformcompanyid,appkey:settings.appKey},
    {configType:"logOpWrite",configValue:"-1",app_id:settings.platformid,company_id:settings.platformcompanyid,appkey:settings.appKey},//操作日志步骤是否写入，0否，1是
  ]);
  //创建平台账户
  const PAccount=db.models.paccount;
  const pa=await PAccount.create({});

  //创建APP
  const App=db.models.app;
  const Account=db.models.account;
  const Company=db.models.company;
  const Auth=db.models.auth;
      //创建role
  const Role=db.models.role;
  var hp=settings.homePage();
  var authUrl=settings.authUrl();
  //建立平台应用和上帝超级管理员
  App.create({appkey:"wx76a324c5d201d1a4",
                 name:"蜂擎开放平台",
                 homePage:hp,
                 docUrl:settings.docUrl(),
                 authUrl:authUrl,isEnabled:true,isSaas:true,secret:"f99d413b767f09b5dff0b3610366cc46",logoUrl:"",
                 isCommon:true,
                 isPublic:false,
                 appType:"web",
                }).then(app=>{
    const User=db.models.user;
    User.create({owner_id:settings.platformid,userName:"sm",password:md5("123"+ "_" + settings.salt),isSuper:true,isAdmin:true,isEnabled:true,nickName:"superman"}).then(function(u){
      app.setCreator(u);
      u.setApp(app);
      Account.create({nickName:"superman",userName:"sm",password:md5("123")}).then(function(ac){
        u.setAccount(ac);
      });
      //设置当前平台初始的租户,并默认设置当前租户所其用的APP
      Company.create({name:"蜂擎云服科技有限公司"}).then(async function(cmp){
        await u.addCompany(cmp,{through:{isCurrent:false}});
        await cmp.addApp(app);
        Role.bulkCreate([
          {  name:"租户",code:"common",isSystem:1,app_id:app.id,company_id:cmp.id},
          {  name:"路过",code:"pass",isSystem:1,app_id:app.id,company_id:cmp.id},
        ]);
      });
      //设置当前初始租户所开启的应用
      //初始化平台的租户权限
      Auth.create({
        rolecode:"common",
        bizcode:"companyinfo",
        codepath:"paasroot/tanentCenter/appMag/companyinfo",
        authstrs:"add,edit,delete",
        app_id:1,
        company_id:1
      });
    });
  });

  //创建role
  // if(settings.env=="prod"){
  //   reclient.flushall(()=>{
  //     console.log("clear caches ok.....");
  //   });
  // }
  // reclient.flushall(()=>{
  //   console.log("clear caches ok.....");
  // });
});
