ProductDetailsViewController.m 8.37 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9
//
//  ProductDetailsViewController.m
//  Lighting
//
//  Created by 曹云霄 on 16/5/4.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ProductDetailsViewController.h"
曹云霄's avatar
曹云霄 committed
10 11
#import "ProductDetailsTableViewCell.h"
#import "ProductDetailsHeaderView.h"
曹云霄's avatar
曹云霄 committed
12
#import "goodsDetailsSectionview.h"
曹云霄's avatar
曹云霄 committed
13

曹云霄's avatar
曹云霄 committed
14
@interface ProductDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
曹云霄's avatar
曹云霄 committed
15 16

@property (nonatomic,strong) ProductDetailsHeaderView *headerView;
曹云霄's avatar
曹云霄 committed
17

曹云霄's avatar
曹云霄 committed
18 19 20 21 22
/**
 *  商品详情数据
 */
@property (nonatomic,strong )TOGoodsEntity *entity;

曹云霄's avatar
曹云霄 committed
23 24 25 26 27 28
@end

@implementation ProductDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
曹云霄's avatar
曹云霄 committed
29 30
    
    [self uiConfigAction];
曹云霄's avatar
曹云霄 committed
31 32
    [self getGoodsListDetails];
    
曹云霄's avatar
曹云霄 committed
33 34 35 36 37 38 39 40 41 42
}

#pragma mark -UI
- (void)uiConfigAction
{
    self.productDetilsTableview.dataSource = self;
    self.productDetilsTableview.delegate = self;
    [self CreateHeaderView];
}

曹云霄's avatar
曹云霄 committed
43

曹云霄's avatar
曹云霄 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#pragma mark -获取商品详情
- (void)getGoodsListDetails

{
    [self CreateMBProgressHUDLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@%@",ServerAddress,@"/goods/getGoods/",_goodsID];
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:url WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
           self.entity = [[TOGoodsEntity alloc]initWithDictionary:returnValue[@"data"] error:nil];
            [self HeaderViewAssignment];
            [self.productDetilsTableview reloadData];
        }
        else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
        
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
        NSLog(@"%@",error);
    }];
}


曹云霄's avatar
曹云霄 committed
77 78 79 80
#pragma mark -头部视图
- (void)CreateHeaderView
{
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"ProductDetailsHeaderView" owner:self options:nil] lastObject];
曹云霄's avatar
曹云霄 committed
81 82 83 84
    self.headerView.goodsNumber.delegate = self;
    //增加,减少商品
    [self.headerView.reduceButton addTarget:self action:@selector(reduceGoodsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView.addButton addTarget:self action:@selector(reduceGoodsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
曹云霄's avatar
曹云霄 committed
85
    self.productDetilsTableview.tableHeaderView = self.headerView;
曹云霄's avatar
曹云霄 committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    
    //添加至购物车
    [self.headerView.addGoodsShoppingbagsButton addTarget:self action:@selector(addGoodsShoppingbags) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark -header赋值
- (void)HeaderViewAssignment
{
    self.headerView.serialNumber.text = self.entity.series;
    self.headerView.nameLabe.text = self.entity.name;
    self.headerView.brandName.text = self.entity.brandId;
    self.headerView.dorpPriceLabe.text = [self.entity.tagPrice stringValue];
    [self.headerView.goodsImageview sd_setImageWithURL:[NSURL URLWithString:self.entity.pictures] placeholderImage:ReplaceImage];
    
曹云霄's avatar
曹云霄 committed
101 102 103 104 105
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
106
    
曹云霄's avatar
曹云霄 committed
107 108
    ProductDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productDetailscell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
曹云霄's avatar
曹云霄 committed
109
    cell.datas = self.entity;
曹云霄's avatar
曹云霄 committed
110
    return cell;
曹云霄's avatar
曹云霄 committed
111
    
曹云霄's avatar
曹云霄 committed
112 113
}

曹云霄's avatar
曹云霄 committed
114

曹云霄's avatar
曹云霄 committed
115 116
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
曹云霄's avatar
曹云霄 committed
117
    return 1;
曹云霄's avatar
曹云霄 committed
118 119
}

曹云霄's avatar
曹云霄 committed
120

曹云霄's avatar
曹云霄 committed
121 122 123
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
曹云霄's avatar
曹云霄 committed
124 125
}

曹云霄's avatar
曹云霄 committed
126

曹云霄's avatar
曹云霄 committed
127 128
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
曹云霄's avatar
曹云霄 committed
129
    return 1;
曹云霄's avatar
曹云霄 committed
130 131 132 133 134 135 136 137 138
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
曹云霄's avatar
曹云霄 committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    
    goodsDetailsSectionview *sectionView = [[[NSBundle mainBundle] loadNibNamed:@"goodsDetailsSectionview" owner:self options:nil] firstObject];
    sectionView.goodSectionLabe.text = section?@"图文详情":@"商品参数";
    
    return sectionView;
}


#pragma mark -减少商品----增加商品
- (void)reduceGoodsButtonClick:(UIButton *)sender {
    
    NSInteger goodsNumber = [self.headerView.goodsNumber.text integerValue];
    switch (sender.tag) {
        case 100://减少
        {
            if (goodsNumber <= 1) {
                
                //不能小于1
                [self ErrorMBProgressView:@"不能小于1"];
                return;
            }
            goodsNumber --;
zhu's avatar
zhu committed
161
            self.headerView.goodsNumber.text = [NSString stringWithFormat:@"%d",goodsNumber];
曹云霄's avatar
曹云霄 committed
162 163 164 165 166 167 168 169 170 171 172 173
            
        }
            break;
        case 101://增加
        {
            if (goodsNumber >= [self.entity.number integerValue]) {
                
                //不能大于库存
                [self ErrorMBProgressView:@"超过库存"];
                return;
            }
            goodsNumber ++;
zhu's avatar
zhu committed
174
            self.headerView.goodsNumber.text = [NSString stringWithFormat:@"%d",goodsNumber];
曹云霄's avatar
曹云霄 committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

        }
            break;
            
        default:
            break;
    }
}





#pragma mark -UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"%@",textField.text);
    NSString *inputString = textField.text;
    if (![self isPureInt:inputString]) {
        
        [self ErrorMBProgressView:@"输入格式错误"];
        textField.text = @"1";
        return;
    }
    
    if ([inputString integerValue] > [self.entity.number integerValue]) {
        
        [self ErrorMBProgressView:@"超过库存数量"];
zhu's avatar
zhu committed
203
        textField.text = [NSString stringWithFormat:@"%d",[self.entity.number integerValue]];
曹云霄's avatar
曹云霄 committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        return;
    }
}

//
//@synthesize fid;
//@synthesize createName;
//@synthesize createBy;
//@synthesize createDate;
//@synthesize updateName;
//@synthesize updateBy;
//@synthesize updateDate;
//@synthesize sysOrgCode;
////@synthesize goodsId;
//@synthesize goodsCode;
//@synthesize goodsName;
//@synthesize goodsCover;
//@synthesize goodsSpec;
//@synthesize goodsBrand;
//@synthesize goodsSellerPrice;
//@synthesize goodsNum;
//@synthesize goodsPrice;
//@synthesize goodsTotalPrice;
//@synthesize consumerId;

#pragma mark -添加至购物车
- (void)addGoodsShoppingbags
{
勾芒's avatar
勾芒 committed
232 233 234 235 236 237 238 239 240 241 242
    //判断是否有当前客户
    if (![Shoppersmanager manager].currentCustomer) {
        
        [self ErrorMBProgressView:@"必须设置当前客户"];
        return;
    }
    [self CreateMBProgressHUDLoding];
    SaveShoppingCartRequest *shopCar = [[SaveShoppingCartRequest alloc]init];
    shopCar.consumerId = [[Customermanager manager] customerID];
    shopCar.goodsId = _goodsID;
    shopCar.count = [self.headerView.goodsNumber.text intValue];
曹云霄's avatar
曹云霄 committed
243 244
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/shopcart/save"] WithRequestType:0 WithParameter:shopCar WithReturnValueBlock:^(id returnValue) {
    
勾芒's avatar
勾芒 committed
245 246 247 248
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            [self SuccessMBProgressView:@"加入购物车成功"];
勾芒's avatar
勾芒 committed
249 250 251
            //刷新购物车
            [[NSNotificationCenter defaultCenter]postNotificationName:@"GOODSNUMBER" object:returnValue[@"data"]];
            
勾芒's avatar
勾芒 committed
252 253 254 255
        }else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
曹云霄's avatar
曹云霄 committed
256 257 258 259 260
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        
    } WithFailureBlock:^(id error) {
勾芒's avatar
勾芒 committed
261 262
       
        [self RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
263
    }];
曹云霄's avatar
曹云霄 committed
264 265
}

曹云霄's avatar
曹云霄 committed
266 267


曹云霄's avatar
曹云霄 committed
268 269 270 271 272 273 274
#pragma mark -判断是否为纯数字
- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

曹云霄's avatar
曹云霄 committed
275 276


勾芒's avatar
勾芒 committed
277 278 279 280 281 282
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


曹云霄's avatar
曹云霄 committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end