//
//  BaseViewController+ClassAction.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/9/27.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension BaseViewController {
 
    ///获取缓存中的信息，填写AccountInfo。跳转web页面
    func setCacheAccountInfo(companyInfo:SelecCompanyDepartmentInfo,selecClassModel:ApplicationHttpListModel){
        AccountInfo.sharedInstance().selecCompanyName = companyInfo.selecCompanyName
        AccountInfo.sharedInstance().selecCompanyId = companyInfo.selecCompanyId
        AccountInfo.sharedInstance().sysOrgService = companyInfo.org_service
        AccountInfo.sharedInstance().sysType = companyInfo.sysType
        AccountInfo.sharedInstance().selecDepartmentName = companyInfo.selecDepartmentName
        AccountInfo.sharedInstance().docId = companyInfo.docId
        AccountInfo.sharedInstance().db_source = companyInfo.db_source
        AccountInfo.sharedInstance().sysOrg = companyInfo.sysOrg
        AccountInfo.sharedInstance().sysUserId = companyInfo.sysUserId
        AccountInfo.sharedInstance().partyOpcode = companyInfo.partyOpcode
        AccountInfo.sharedInstance().cmsRealDb = companyInfo.cmsRealDb
        AccountInfo.sharedInstance().cmsTestDb = companyInfo.cmsTestDb
        AccountInfo.sharedInstance().omsRealDb = companyInfo.omsRealDb
        AccountInfo.sharedInstance().omsTestDb = companyInfo.omsTestDb
        AccountInfo.sharedInstance().tmsRealDb = companyInfo.tmsRealDb
        AccountInfo.sharedInstance().tmsTestDb = companyInfo.tmsTestDb
        AccountInfo.sharedInstance().appRealDb = companyInfo.appRealDb
        AccountInfo.sharedInstance().appTestDb = companyInfo.appTestDb
        AccountInfo.sharedInstance().tms_version = companyInfo.tms_version
        self.setWebUrlAction(selecClassModel: selecClassModel)
    }
    
    ///设置URl中所需的AccountInfo
    func setClassWebUrlAccountInfo(selecClassModel:ApplicationHttpListModel,companyModel:SelectCompanyHttpCompanyDataModel,departmentModel:SelectCompanyHttpOrgListDataModel){
        AccountInfo.sharedInstance().selecCompanyName = judgmentStrNil(str: companyModel.companyname)
        AccountInfo.sharedInstance().selecCompanyId = judgmentStrNil(str: companyModel.id)
        AccountInfo.sharedInstance().sysOrgService = judgmentStrNil(str: companyModel.org_service)
        AccountInfo.sharedInstance().sysType = judgmentStrNil(str: companyModel.sysname)
        AccountInfo.sharedInstance().selecDepartmentName = judgmentStrNil(str: departmentModel.partyName)
        AccountInfo.sharedInstance().docId = judgmentStrNil(str: selecClassModel.applicationid)
        AccountInfo.sharedInstance().db_source = judgmentStrNil(str: companyModel.db_source)
        AccountInfo.sharedInstance().sysOrg = judgmentStrNil(str: departmentModel.org)
        AccountInfo.sharedInstance().sysUserId = judgmentStrNil(str: companyModel.userid)
        AccountInfo.sharedInstance().partyOpcode = judgmentStrNil(str: departmentModel.partyOpcode)
        AccountInfo.sharedInstance().cmsRealDb = judgmentStrNil(str: companyModel.cmsRealDb)
        AccountInfo.sharedInstance().cmsTestDb = judgmentStrNil(str: companyModel.cmsTestDb)
        AccountInfo.sharedInstance().omsRealDb = judgmentStrNil(str: companyModel.omsRealDb)
        AccountInfo.sharedInstance().omsTestDb = judgmentStrNil(str: companyModel.omsTestDb)
        AccountInfo.sharedInstance().tmsRealDb = judgmentStrNil(str: companyModel.tmsRealDb)
        AccountInfo.sharedInstance().tmsTestDb = judgmentStrNil(str: companyModel.tmsTestDb)
        AccountInfo.sharedInstance().appRealDb = judgmentStrNil(str: companyModel.appRealDb)
        AccountInfo.sharedInstance().appTestDb = judgmentStrNil(str: companyModel.appTestDb)
        AccountInfo.sharedInstance().sysUserId = judgmentStrNil(str: companyModel.userid)
        AccountInfo.sharedInstance().tms_version = judgmentStrNil(str: companyModel.tms_version)
        let companyInfo = SelecCompanyDepartmentInfo()
        companyInfo.tags = judgmentStrNil(str: selecClassModel.sysid)
        companyInfo.selecCompanyName = judgmentStrNil(str: companyModel.companyname)
        companyInfo.selecCompanyId = judgmentStrNil(str: companyModel.id)
        companyInfo.selecDepartmentName = judgmentStrNil(str: departmentModel.partyName)
        companyInfo.docId = judgmentStrNil(str: selecClassModel.applicationid)
        companyInfo.db_source = judgmentStrNil(str: companyModel.db_source)
        companyInfo.sysOrg = judgmentStrNil(str: departmentModel.org)
        companyInfo.sysUserId = judgmentStrNil(str: companyModel.userid)
        companyInfo.sysType = judgmentStrNil(str: companyModel.sysname)
        companyInfo.org_service = judgmentStrNil(str: companyModel.org_service)
        companyInfo.partyOpcode = judgmentStrNil(str: departmentModel.partyOpcode)
        companyInfo.cmsRealDb = judgmentStrNil(str: companyModel.cmsRealDb)
        companyInfo.cmsTestDb = judgmentStrNil(str: companyModel.cmsTestDb)
        companyInfo.omsRealDb = judgmentStrNil(str: companyModel.omsRealDb)
        companyInfo.omsTestDb = judgmentStrNil(str: companyModel.omsTestDb)
        companyInfo.tmsRealDb = judgmentStrNil(str: companyModel.tmsRealDb)
        companyInfo.tmsTestDb = judgmentStrNil(str: companyModel.tmsTestDb)
        companyInfo.appRealDb = judgmentStrNil(str: companyModel.appRealDb)
        companyInfo.appTestDb = judgmentStrNil(str: companyModel.appTestDb)
        companyInfo.sysUserId = judgmentStrNil(str: companyModel.userid)
        companyInfo.tms_version = judgmentStrNil(str: companyModel.tms_version)
        companyInfo.userId = AccountInfo.sharedInstance().UserId
        self.setSelecCompanyDepartment(info: companyInfo, block: {[weak self] in
            self?.setWebUrlAction(selecClassModel: selecClassModel)
        })
    }
    ///设置URl，并开始跳转
    func setWebUrlAction(selecClassModel:ApplicationHttpListModel){
        var url:String = ""
        self.appViewsSstatisticsAction(httpModel: selecClassModel, type: "1")
        if AccountInfo.sharedInstance().oldType == "1" && selecClassModel.pointstate == "0" {
            actionGotoWeb(webViewUrl: selecClassModel.hurl!, isHomePushType: .YesIsHomePushType, isShowTitleType: self.getHtitleType(htitle: selecClassModel.htitle), appid: selecClassModel.appid!, isEncryptionType: .YesEncryptionType)
            return
        }
        if (selecClassModel.belong == "IT_ROBOT") {
            self.selectGotoAutonomous(model: selecClassModel)
            return
        }
        url = self.getSignInAction(model: selecClassModel)+"&tmsVer="+judgmentStrNil(str: AccountInfo.sharedInstance().tms_version)+"&loginType="+judgmentStrNil(str: AccountInfo.sharedInstance().loginType)+"&docId="+judgmentStrNil(str: AccountInfo.sharedInstance().docId)+"&loginName="+judgmentStrNil(str: AccountInfo.sharedInstance().userName!)+"&loginCompany="+judgmentStrNil(str: AccountInfo.sharedInstance().infoCompanyName!)+"&sysDbSource="+judgmentStrNil(str: AccountInfo.sharedInstance().db_source)+"&sysUserId="+judgmentStrNil(str: AccountInfo.sharedInstance().sysUserId)+"&userName="+judgmentStrNil(str: AccountInfo.sharedInstance().partyOpcode)+"&sysCompanyName="+judgmentStrNil(str: AccountInfo.sharedInstance().selecCompanyName)+"&sysCompanyId="+judgmentStrNil(str: AccountInfo.sharedInstance().selecCompanyId)+"&sysOrgService="+judgmentStrNil(str: AccountInfo.sharedInstance().sysOrgService)+"&sysType="+judgmentStrNil(str: AccountInfo.sharedInstance().sysType)+"&sysOrgId="+judgmentStrNil(str: AccountInfo.sharedInstance().sysOrg)+"&sysOrgName="+judgmentStrNil(str: AccountInfo.sharedInstance().selecDepartmentName)+"&cmsRealDb="+judgmentStrNil(str: AccountInfo.sharedInstance().cmsRealDb)+"&cmsTestDb="+judgmentStrNil(str: AccountInfo.sharedInstance().cmsTestDb)+"&omsTestDb="+judgmentStrNil(str: AccountInfo.sharedInstance().omsTestDb)+"&omsRealDb="+judgmentStrNil(str: AccountInfo.sharedInstance().omsRealDb)+"&tmsRealDb="+judgmentStrNil(str: AccountInfo.sharedInstance().tmsRealDb)+"&tmsTestDb="+judgmentStrNil(str: AccountInfo.sharedInstance().tmsTestDb)+"&appRealDb="+judgmentStrNil(str: AccountInfo.sharedInstance().appRealDb)+"&appTestDb="+judgmentStrNil(str: AccountInfo.sharedInstance().appTestDb)+"&title="+judgmentStrNil(str: selecClassModel.name!)
        actionGotoWeb(webViewUrl: url, isHomePushType: .YesIsHomePushType, isShowTitleType: self.getHtitleType(htitle: judgmentStrNil(str: selecClassModel.htitle!)), appid: judgmentStrNil(str: selecClassModel.appid!), isEncryptionType: .YesEncryptionType)
        
    }
    /// 应用访问量
    func appViewsSstatisticsAction(httpModel:ApplicationHttpListModel, type: String){
        let model = ClickRateModel()
        model.name = httpModel.name
        model.belong = httpModel.belong
        model.scope = httpModel.scope
        model.type = type
        self.getClickRateHttpCmd(infoModel: model)
    }
    func selectGotoAutonomous(model:ApplicationHttpListModel) {
        if model.belong == "IT_ROBOT" {
            var strUrl = model.hurl!
            if (strUrl.contains("?")) {
                strUrl = strUrl + "&userId="
            } else {
                strUrl = strUrl + "?userId="
            }
            let dic:NSMutableDictionary = NSMutableDictionary()
            let type:NSMutableDictionary = NSMutableDictionary()
            type["type"] = judgmentStrNil(str: AccountInfo.sharedInstance().CustomerServicePower)
            dic["user_labels"] = type

            let url = strUrl + judgmentStrNil(str: AccountInfo.sharedInstance().UserId) + "&userCompany=" + judgmentStrNil(str: AccountInfo.sharedInstance().infoCompanyName) + "&userName=" + judgmentStrNil(str: AccountInfo.sharedInstance().userName) + "&IDMToken="+judgmentStrNil(str: AccountInfo.sharedInstance( ).singlePoint_access_token) + "&loginType="+judgmentStrNil(str: AccountInfo.sharedInstance().loginType) + "&clientId=" + md5(strs: judgmentStrNil(str: AccountInfo.sharedInstance().UserId)) + "&customField="+judgmentStrNil(str: dicValueString(dic as! [String : Any]))
            
            
//            self.actionGotoWebGotoWeb(webViewUrl: url, isHomePushType: .NOIsHomePushType, previousPageShowTitleType: .YesShowTitleType)
            actionGotoWeb(webViewUrl: url, isHomePushType: .NOIsHomePushType, isShowTitleType: self.getHtitleType(htitle: judgmentStrNil(str: model.htitle!)), appid: judgmentStrNil(str: model.appid!), isEncryptionType: .YesEncryptionType)
        }
    }
}
