//
//  ServiceViewController+http.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/20.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension ServiceViewController {
    func getServiceHttpCmd(){
      
        let cmd:HttpCommand = ServiceHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpServiceHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpServiceHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpServiceHttpCmd_attr] = attrDic
        dic[kHttpServiceHttpCmd_orderby] = "publishtime"
        dic[kHttpServiceHttpCmd_pageNum] = "0"
        dic[kHttpServiceHttpCmd_pageSize] = "20"
        dic[kHttpServiceHttpCmd_sort] = "desc"
        let filtersDic:NSMutableDictionary = NSMutableDictionary()
        filtersDic[kHttpServiceHttpCmd_value] = "4"
        let filtersArray = NSMutableArray()
        filtersArray.add(filtersDic)
        dic[kHttpServiceHttpCmd_filters] = filtersArray
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpServiceHttpResultResponse(result:RequestResult){
        let r:ServiceHttpResult = result as! ServiceHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getServiceHttpResult() as NSMutableArray
            httpListData = array[0] as! ServiceHttpListModelData
            self.dataSource = NSMutableArray()
            self.initSetData(httpArray: httpListData.httpModelArray)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    func initSetData(httpArray:NSMutableArray){
        for index in 0..<httpArray.count {
            let httpModel:ServiceHttpListModel = httpArray[index] as! ServiceHttpListModel
            self.dataSource .add(self.getServiceCollectionViewCellModel(httpModel:httpModel))
        }
        delegate.dataSource = self.dataSource
        collectionView.delegate = self.delegate
        collectionView.dataSource = self.delegate
        self.collectionView.reloadData()
    }
}
