//
//  UpcomingNewCountHttp.swift
//  MobilePortal
//
//  Created by caiyang on 2020/11/16.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

class UpcomingNewCountHttpAction: NSObject {
    
    class func sharedInstance()->UpcomingNewCountHttpAction {
        struct SharedStatic {
            static var instance: UpcomingNewCountHttpAction = UpcomingNewCountHttpAction()
        }
        return SharedStatic.instance
    }
    var applicationCountBlock:selectBlock!
    
    
    /// 获取所有的应用
    func getAllApplicationHttpListCmd(resultBlock:@escaping selectBlock){
        self.applicationCountBlock = resultBlock
        let cmd:HttpCommand = AllApplicationHttpListCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpMoreApplicationsTwoHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpAllApplicationHttpListCmd_userid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        attrDic[kHttpAllApplicationHttpListCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        //        if self.businessType == .UpcomingType {
        attrDic[kHttpAllApplicationHttpListCmd_value] = "7"
        //        } else {
        //            attrDic[kHttpAllApplicationHttpListCmd_value] = "8"
        //        }
        
        dic[kHttpAllApplicationHttpListCmd_attr] = attrDic
        dic[kHttpAllApplicationHttpListCmd_orderby] = "createon"
        dic[kHttpAllApplicationHttpListCmd_pageNum] = "1"
        dic[kHttpAllApplicationHttpListCmd_pageSize] = ""
        dic[kHttpAllApplicationHttpListCmd_sort] = "desc"
        let filtersDic:NSMutableDictionary = NSMutableDictionary()
        let filtersArray = NSMutableArray()
        filtersArray.add(filtersDic)
        dic[kHttpAllApplicationHttpListCmd_filters] = filtersArray
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpMoreApplicationsTwoHttpResultResponse(result:RequestResult){
        let r:AllApplicationHttpListResult = result as! AllApplicationHttpListResult
        if r.isOk() {
            let array:NSMutableArray = r.getAllApplicationHttpListResult()
            self.settableViewCellModel(modelArray: array)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    func settableViewCellModel(modelArray:NSMutableArray) {
        var upcomingKey = ""
        if modelArray.count == 0 {
            AccountInfo.sharedInstance().upcomingCount = "0"
            self.applicationCountBlock()
            return
        }
        for index in 0..<modelArray.count {
            let httpModel:ApplicationHttpListModel = modelArray[index] as! ApplicationHttpListModel
            if (index == 0) {
                upcomingKey = httpModel.todokey!
            } else {
                upcomingKey = upcomingKey + "," + httpModel.todokey!
            }
        }
        self.getUpcomingCountCmdAction(type: "1", key: upcomingKey, resultBlock: { [weak self] (value:Any) in
            self?.setUpcomingCountDataAction(array: value as! NSMutableArray)
        }) { (value:Any) in
            
        }
    }
    // 处理待办数目
    func setUpcomingCountDataAction(array:NSMutableArray) {
        var numBer = 0
        for index in 0..<array.count {
            let model:UpcomingNewCountModel = array[index] as! UpcomingNewCountModel
//            model.count = "10"
            numBer = numBer + Int(model.count!)!
        }
        AccountInfo.sharedInstance().upcomingCount = String(numBer)
        self.applicationCountBlock()
    }
    
    // MARK: 获取所有的待办数目
    func getUpcomingCountCmdAction(type:String,key:String,resultBlock:@escaping passParameterBlock,errorBlock:@escaping passParameterBlock){
        let cmd:HttpCommand = UpcomingNewCountHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpUpcomingNewCountHttpResultResponse(result: result, resultBlock: resultBlock, errorBlock: errorBlock)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpUpcomingNewCountHttpPostCmd_key] = key
        dic[kHttpUpcomingNewCountHttpPostCmd_userCode] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpUpcomingNewCountHttpPostCmd_type] = type
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate = completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpUpcomingNewCountHttpResultResponse(result:RequestResult,resultBlock:@escaping passParameterBlock,errorBlock:@escaping passParameterBlock){
        let r:UpcomingNewCountHttpResult = result as! UpcomingNewCountHttpResult
        if r.isOk() {
            let httpArray = r.getUpcomingNewCountHttpResult() as NSMutableArray
            resultBlock(httpArray as NSMutableArray)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
            errorBlock("")
        }
    }
    
}
