//#import $owl_create_site_order:services/modelService.jsx
//#import $owl_website:services/modelService.jsx
//#import $owl_warehouse:services/modelService.jsx
//#import $owl_orgs:services/modelService.jsx

function getUnprocessedOrder(){
    //获取所有已经支付但是没有处理的订单
    var searchArgs = {
        paystate:'paid',
        processstate:'unprocessed'
    }
    var searchResult = owl_create_site_orderService.search("0", searchArgs,null,0, 1000, null);
    if(searchResult.state==='ok'){
        return searchResult.list;
    }
    $.log("create_site_jobs error," + JSON.stringify(searchResult));
    return [];
}

function getParentSiteOrgId(){
    var id = "owl_mall_setting_0_current_site_org_id";
    var setting = owl_mall_settingService.get(id);
    if(setting){
        $.log("parentSiteOrgId=" + setting.args_value)
        return setting.args_value;
    }
    $.log("error!! not parent org of website configed. please set owl_mall_setting_0_current_site_org_id");
    return null;
}

function getOrCreateOrg(uid){
    $.log("getOrCreateOrg for uid=" + uid);
    var searchArgs = {
        ownerId:uid
    }
    var sr = owl_orgsService.search("0",searchArgs,null,0,1000,null);
    if(sr.state==='ok'){
      if(sr.list.length > 0){
          $.log("got org for uid=" + uid + ",orgId=" + sr.list[0].id);
          return sr.list[0];
      }
    }
    //否则
    $.log("create org for uid=" + uid + "")
    var u = owl_mall_uService.get(uid);
    var parentId = getParentSiteOrgId();


    if(!parentId){
        $.log("org parentId==null)");
        return null;
    }
    $.log("org parentId=" + parentId);
    var parentOrg = owl_orgsService.get(parentId);
    if(!parentOrg){
        $.log("org parentId=null")
        return null;
    }

    var org = {
        name: u.name || u.mobile,
        ownerId:u.id,
        status:'active',
        parent_id:parentId,
        parent_name:parentOrg.name
    }
    $.log("before add org")
    owl_orgsService.add(org);
    $.log("after add org, org.id=" + org.id)

    return org;
}

function addWebSite(order,org){
    var searchArgs = {
        orderId:order.id
    }
    var sr = owl_websiteService.search("0",searchArgs,null,0,1000,null);
    if(sr.state!='ok'){
        return null;
    }
    if(sr.list.length>0){
        return null;
    }
    var website = {
        siteName:order.siteName,
        siteTitle:order.siteTitle,
        siteDomain:"",
        appLogo:"",
        sitePics:[],
        status:"up",
        orderId:order.id,
        ownerId:order.uid,
        _orgId:org.id,
        _orgIds:org.parentPathIds
    }
    owl_websiteService.add(website);
    return website;
}

function addWarehouse(order,org){
    var searchArgs = {
        orderId:order.id
    }
    var sr = owl_warehouseService.search("0",searchArgs,null,0,1000,null);
    if(sr.state!='ok'){
        return null;
    }
    if(sr.list.length>0){
        return null;
    }
    var warehouse = {
        name:"默认仓库",
        _orgId:org.id,
        _orgIds:org.parentPathIds,
        ownerId:order.uid,
        orderId:order.id,
        mobile:order.phone
    }
    owl_warehouseService.add(warehouse);
    return warehouse;
}

function createSite(order){
    //找到订单用户对应的orgId
    var org = getOrCreateOrg(order.uid);
    if(!org){
        return;
    }
    //对某个订单处理
    var website = addWebSite(order,org);
    if(!website){
        return;
    }
    order.siteId = website.id;
    owl_create_site_orderService.update(order);
    if(order.warehouse == 'Yes'){
        var warehouse = addWarehouse(order,org);
        order.warehouseId = warehouse.id;
    }
    order.processstate = 'processed';
    owl_create_site_orderService.update(order);
}

function doMainThings(){
    var orders = getUnprocessedOrder();
    if(orders && orders.length>0){
        orders.forEach(function(order){
            createSite(order);
        });
    }
    $.sleep(3000);
}

function main(){

    //锁
    var lockkey = "create_site_job";
    try{
        owl_websiteService.lock("lockkey");
        doMainThings();
    }
    catch(e){
        $.log(e.toString());
    }
    finally{
        owl_websiteService.unlock("lockkey");
    }
}

main();

