CNLiveTradingRecordController.m 9.33 KB
//
//  CNLiveTradingRecordController.m
//  CNLiveBalanceModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright (c) 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveTradingRecordController.h"
#import "MJExtension.h"
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"
#import "CNLiveNetworking.h"
#import "CNLiveEnvironment.h"

#import "CNUserInfoManager.h"
#import "CNLiveBaseKit.h"
#import "CNLiveCategory.h"
#import "CNLiveRefreshHeader.h"

#import "CNLiveServices.h"
#import "CNLiveBalanceRequest.h"

#import "CNLiveBalanceNavigationBar.h"
#import "CNLiveTradingRecordView.h"
#import "CNLiveMonthBillController.h" //月账单
#import "CNLiveTradingRecordModel.h"
#import "CNLiveTradingRecordCell.h"
#import "CNLiveTradingRecordDetailController.h"

@interface CNLiveTradingRecordController ()<UITableViewDelegate, UITableViewDataSource,CNLiveBalanceNavigationBarDelegate,CNLiveTradingRecordViewDelegate>
@property (nonatomic, strong) CNLiveBalanceNavigationBar *navigationBar;
@property (nonatomic, strong) CNLiveTradingRecordView *tradingRecordView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, assign) NSInteger pageNo;

//type: 0 -> 个人中心交易记录  1 -> 查看与此商户的交易记录
@property (nonatomic, assign) TradingRecordType type;

//ID: 0 -> 用户id  1 -> 查看与此商户的交易记录 toId
@property (nonatomic, copy) NSString *ID;

@property (nonatomic, copy) NSString *date;
@property (nonatomic, copy) NSString *startTime;
@property (nonatomic, copy) NSString *endTime;

@end

@implementation CNLiveTradingRecordController
static const CGFloat height = 80;
#pragma mark - 初始化
- (instancetype)initWithType:(TradingRecordType)type ID:(NSString *)ID date:(NSString *)date startTime:(NSString *)startTime endTime:(NSString *)endTime{
    if(self = [super init]){
        self.type = type;
        self.ID = ID;
         
        self.date = date;
        self.startTime = startTime;
        self.endTime = endTime;
     }
     return self;
}

#pragma mark - 加载数据
- (void)loadData {
    [CNLiveBalanceRequest getTradingRecordListWithType:self.type == TradingRecordTypeDefault?@"0":@"Special" ID:self.ID startTime:self.startTime endTime:self.endTime block:^(BOOL isSuccess, NSDictionary * _Nonnull data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (error) {
            [self.data removeAllObjects];
            [self.tableView reloadData];
            [self showEmptyViewWithImage:[UIImage imageNamed:@"wufalianjie"] text:error.localizedDescription detailText:@"别紧张,试试看刷新页面~" buttonTitle:@"     重试     " buttonAction:@selector(loadData)];
            [self setCNAPIErrorEmptyView];
            self.emptyView.verticalOffset = -(NavigationContentTop+height);
            return;
        }
        
        if (isSuccess) {
            CNLiveTradingRecordModel *model = [CNLiveTradingRecordModel mj_objectWithKeyValues:data];
            if(self.pageNo == 1&&self.data.count > 0){
                [self.data removeAllObjects];
            }
            [self.data addObjectsFromArray:self.type == TradingRecordTypeDefault?model.list:model.transferRecordList];
            
            self.tradingRecordView.model = model;
            [self.tableView reloadData];
        }
    }];
    
}

#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self createSubViews];
    
    NSArray *times = [CNLiveBalanceRequest getMonthFirstAndLastDayWithDate:[NSDate date]];
        
    self.startTime = self.startTime?self.startTime:times.firstObject;
    self.endTime = self.endTime?self.endTime:times.lastObject;
    self.pageNo = 1;
    
    [self.tableView.mj_header beginRefreshing];

}

- (void)createSubViews {
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.tradingRecordView];
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.0;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNLiveTradingRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveTradingRecordCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.data[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.type == TradingRecordTypeSpecial){
        CNLiveTradingRecordListModel *model = self.data[indexPath.row];
        CNLiveTradingRecordDetailController *vc = [[CNLiveTradingRecordDetailController alloc]initWithType:TradingRecordDetailTypeSpecial model:model date:self.date startTime:self.date endTime:self.date];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        CNLiveTradingRecordListModel *model = self.data[indexPath.row];
        CNLiveTradingRecordDetailController *vc = [[CNLiveTradingRecordDetailController alloc]initWithType:TradingRecordDetailTypeDefault model:model date:self.date startTime:self.date endTime:self.date];
        [self.navigationController pushViewController:vc animated:YES];
    }

}

#pragma mark - CNLiveTradingRecordViewDelegate
- (void)tradingRecordView:(CNLiveTradingRecordView *)view date:(NSString *)date startTime:(NSString *)startTime endTime:(NSString *)endTime{
    self.date = date;
    self.startTime = startTime;
    self.endTime = endTime;
    [self loadData];
}

- (void)pushToMonthBillController{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年M月"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    CNLiveMonthBillController *vc = [[CNLiveMonthBillController alloc]initWithDate:self.date?self.date:dateTime startTime:self.startTime endTime:self.endTime];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
- (NSMutableArray *)data {
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
    }
    return _data;
}
- (CNLiveBalanceNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [[CNLiveBalanceNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationContentTop)];
        _navigationBar.backgroundColor = [UIColor whiteColor];
        _navigationBar.delegate = self;
        _navigationBar.title.text = @"交易记录";
        _navigationBar.title.textColor = [UIColor blackColor];
        UIImage *image = [self getImageWithImageName:@"mine_back_black_b" bundleName:@"CNLiveBalanceModule" targetClass:[self class]];
        [_navigationBar.left setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _navigationBar.right = nil;

    }
    return _navigationBar;
}
- (CNLiveTradingRecordView *)tradingRecordView{
    if(!_tradingRecordView){
        _tradingRecordView = [[CNLiveTradingRecordView alloc]initWithFrame:CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, height)];
        _tradingRecordView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
        _tradingRecordView.delegate = self;
        _tradingRecordView.isShow = self.type == TradingRecordTypeDefault;
        _tradingRecordView.date = self.date;
    }
    return _tradingRecordView;
}
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavigationContentTop+height, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop-height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[CNLiveTradingRecordCell class] forCellReuseIdentifier:kCNLiveTradingRecordCell];
        _tableView.mj_header = [CNLiveRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];

    }
    return _tableView;
}
#pragma mark - 响应方法
- (void)goBack {
    if (self.presentingViewController != nil) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - CNLiveBalanceNavigationBarDelegate
- (void)navigationBar:(CNLiveBalanceNavigationBar *)navigationBar actionEvents:(NSString *)actionEvents{
    if ([actionEvents isEqualToString:@"1"]) {
        [self goBack];
    }else if ([actionEvents isEqualToString:@"2"]){
        
    }
}

@end