<?php
/**
 * 权限规则模型
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Model;
use Think\Model;
class GoodsModel extends Model
{
    protected $_auto = array(
        array('promote_start_date', 'strtotime', self::MODEL_BOTH, 'function'),
        array('promote_end_date', 'strtotime', self::MODEL_BOTH, 'function'),
    );//更新商品属性

    public function update_goodstype($postdata, $type = 0)
    {
        $goodstypearr = $newgoodsattr = array();
        if ($postdata['config']) {
            $a = 0;
            foreach ($postdata['config'] as $k => $v) {
                if (str_exists($k, '_') || !$v) continue;
                if (!empty($v[$k . '_text'])) {
                    if (!in_array($postdata['id'] . $k . $v[$k . '_text'], $check_unique_array)) {
                        $newgoodsattr[$a]['goods_id'] = !empty($postdata['id']) ? $postdata['id'] : 0;
                        $newgoodsattr[$a]['attr_id'] = !empty($k) ? $k : 0;
                        $newgoodsattr[$a]['attr_value'] = $v[$k . '_text'];
                        $newgoodsattr[$a]['attr_price'] = 0;
                        $newgoodsattr[$a]['supplier_price'] = 0;
                        $newgoodsattr[$a]['attr_img'] = '';
                        $check_unique_array[] = $postdata['id'] . $k . $v[$k . '_text'];
                        $goodstypearr[] = $v[$k . '_text'] . "0";
                        ++$a;
                    }

                } elseif (!empty($v[$k . '_select'])) {
                    foreach ($v[$k . '_select'] as $x => $y) {
                        if (!in_array($postdata['id'] . $k . $v[$k . '_select'][$x], $check_unique_array) && is_numeric($v[$k . '_select'][$x])) {
                            $newgoodsattr[$a]['goods_id'] = !empty($postdata['id']) ? $postdata['id'] : 0;
                            $newgoodsattr[$a]['attr_id'] = !empty($k) ? $k : 0;
                            $newgoodsattr[$a]['attr_value'] = $v[$k . '_select'][$x];
                            $newgoodsattr[$a]['attr_price'] = floatval($v[$k . '_price'][$x]);
                            $newgoodsattr[$a]['supplier_price'] = floatval($v[$k . '_balance_price'][$x]);
                            $newgoodsattr[$a]['attr_img'] = !empty($v[$k . '_img'][$x]) ? $v[$k . '_img'][$x] : '';
                            $check_unique_array[] = $postdata['id'] . $k . $v[$k . '_select'][$x];
                            $goodstypearr[] = $newgoodsattr[$a]['attr_value'] . $newgoodsattr[$a]['attr_price'] . $newgoodsattr[$a]['attr_img'];
                            ++$a;
                        }
                    }
                }
            }
            if ($postdata['configstr'] != md5(implode('', $goodstypearr)) || $type) {
                $goodsattrmod = M('GoodsAttr');
                $oldids=array();
                foreach($newgoodsattr as $v){
                    $map=array();
                    $map['goods_id']=$v['goods_id'];
                    $map['attr_id']=$v['attr_id'];
                    $map['attr_value']=$v['attr_value'];
                    $attrid=$goodsattrmod->where($map)->getField('id');
                    if($attrid){
                        $oldids[]=$attrid;
                        $goodsattrmod->where($map)->save($v);
                    }else{
                        $oldids[]=$goodsattrmod->add($v);
                    }
                }                
                if (empty($type)) {
                    //M('GoodsAttrStock')->where('goods_id='.$postdata['id'])->delete();//属性发生变化，删除所有对应货品属性
                    $map=array();
                    $map['goods_id']=$postdata['id'];
                    $map['id']=array('not in',$oldids);
                    $goodsattrmod->where($map)->delete();//print_r($newgoodsattr);exit;
                }
            }
		}else{
			M('GoodsAttrStock')->where('goods_id=' . $postdata['id'])->delete();//属性全部删除，删除所有对应货品属性
			if(!empty($postdata['id']))M('GoodsAttr')->where('goods_id=' . $postdata['id'])->delete();
		}
        unset($goodstypearr, $newgoodsattr, $postdata);
    }

    /**
     * 更新扩增分类
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    public function update_goodsattr($postdata, $type = 0)
    {
        $newgoodscat = array();
        if (($postdata['more_cat'] && $postdata['morecatid'] != implode(',', $postdata['more_cat'])) || ($type && $postdata['more_cat'])) {
            $goodscatmod = M('GoodsCat');
            if (empty($type)) $goodscatmod->where('goods_id=' . $postdata['id'])->delete();
            foreach ($postdata['more_cat'] as $v) {
                if (!$v) continue;
                $newgoodscat[] = array('goods_id' => $postdata['id'], 'catid' => $v);
            }
            if ($newgoodscat) $goodscatmod->addAll($newgoodscat);
        }
        unset($newgoodscat, $postdata);
    }
    

    /**
     * 更新商品相册
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    public function update_goodsimg($postdata, $type = 0)
    {
        $newgoodsimg = array();
        $moreimgstr = md5(implode('', $postdata['goods_img']) . implode('', $postdata['img_desc']) . implode('', $postdata['img_sort']));
        if (($postdata['goods_img'] && $postdata['moreimgstr'] != $moreimgstr) || ($type && $postdata['goods_img'])) {
            $goodsimgmod = M('GoodsGallery');
            if (empty($type)) $goodsimgmod->where('goods_id=' . $postdata['id'])->delete();
            foreach ($postdata['goods_img'] as $k => $v) {
                if (!$v) continue;
                $newgoodsimg[] = array('goods_id' => $postdata['id'], 'goods_img' => $v, 'img_desc' => $postdata['img_desc'][$k], 'img_sort' => $postdata['img_sort'][$k]);
            }
            if ($newgoodsimg) $goodsimgmod->addAll($newgoodsimg);
        }elseif(!$postdata['goods_img'] && empty($type)){
            M('GoodsGallery')->where('goods_id=' . $postdata['id'])->delete();
        }
        unset($newgoodsimg, $postdata);
    }

    /**
     * 更新会员价
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    public function update_user_price($postdata, $type = 0)
    {
        $user_price = $postdata['user_price'];
        $arr = array();
        if (!empty($user_price)) {
            $member_price = M('member_price');
            $member_price->where('goods_id=' . $postdata['id'])->delete();
            foreach ($user_price as $key => $value) {
                if ($value != -1) {
                    $arr['goods_id'] = $postdata['id'];
                    $arr['user_rank'] = $key;
                    $arr['user_price'] = $value;
                    $member_price->add($arr);
                }
            }
        }
        unset($arr, $postdata);
    }

    /**
     * 更新货码 同时更新商品助记码
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    public function update_goods_code($postdata, $type = 0){
        $goods_code = array();
        if(empty($postdata['goods_zhuji']) && !empty($postdata['goods_name'])){
           $goods_code['goods_zhuji']= pinyin_long($postdata['goods_name']);
        }
		if (empty($postdata['goods_code'])) {
			$sn_prefix=C("GOODS_NUM_PREFIX");
			$num_sn = str_pad($postdata['id'], 7, 000, STR_PAD_LEFT);
			$goods_code['goods_code'] = $sn_prefix . $num_sn;
		}else{
			$map_sn = array();
			$map_sn['id'] = array('neq',$postdata['id']);
			$map_sn['goods_code'] = $postdata['goods_code'];
			$map_sn['goods_type'] = 5;
			$g_count = M('goods')->where($map_sn)->count();
			if($g_count){
				$sn_prefix=C("GOODS_NUM_PREFIX");
				$num_sn = str_pad($postdata['id'], 7, 000, STR_PAD_LEFT);
				$goods_code['goods_code'] = $sn_prefix . $num_sn;
			}else{
				$goods_code['goods_code'] = $postdata['goods_code'];
			}
		}
		M('Goods')->where('id='. $postdata['id'])->save($goods_code);
		unset($goods_code, $postdata);
    }
    /**
     * 更新包装商品编号
     * @param array $postdata 表单提交数据
     */
    public function update_packgoods_sn($postdata)
    {
        $goods_sn = array();
        $sn_prefix=C("GOODS_NUM_PREFIX");
        if (empty($postdata['goods_sn'])) {
            $num_sn = str_pad($postdata['packid'], 9, '000'.mt_rand(0,9), STR_PAD_LEFT);
            $goods_sn['goods_sn'] = $sn_prefix . $num_sn;
            M('GoodsPackage')->where('id=' . $postdata['packid'])->save($goods_sn);
        }
        unset($goods_sn, $postdata);
    }
    /**
     * 更新商品详情和seo
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    public function update_goods_content($postdata, $type = 0)
    {
        $save = $where = array();
        $save['goods_id'] = $postdata['id'];
        $save['content'] = $postdata['content'];
        $save['seotitle'] = $postdata['seotitle'];
        $save['seokeywords'] = $postdata['seokeywords'];
        $save['seodescription'] = $postdata['seodescription'];
        if (empty($type)) {
            $where['goods_id'] = $postdata['id'];
            $goodscontent = M('GoodsContent')->where('goods_id = ' . $postdata['id'])->find();
            if (empty($goodscontent)) {
                M('GoodsContent')->add($save);
            } else {
                M('GoodsContent')->where($where)->save($save);
            }
        } else {
            $save['goods_id'] = $postdata['id'];
            M('GoodsContent')->add($save);
        }
        unset($where, $save, $postdata);
    }

    //更新商品属性库存,货品编号
    public function update_goodsattr_stock($postdata){
		// echo "<pre>";	 print_r($postdata);
        $newgoodsattr = $check_unique_array = array();$ten=1;
        if ($postdata['config']) {
            $goodssn=idtoname($postdata['id'],'Goods','goods_sn');
            foreach ($postdata['config'] as $k => $v) {
                if (!in_array("select", array_keys($postdata['config']))) continue;//必须有属性组合
                foreach ($v as $selectkey => $selectval) {
                    if($k == "select"){
                        $attr_key_array = array();
                        foreach ($selectval as $key => $val) {
                            $where['goods_id'] = $postdata['id'];
                            $where['attr_id'] = $key;
                            $where['attr_value'] = $val;
                            if (!is_numeric($val)) continue;
                            $attr_key_array[] = M('GoodsAttr')->where($where)->getField('id');
                        }
                        if (count($attr_key_array) != count($selectval)) continue;//属性选择不全，不存入数据库
                        sort($attr_key_array);
                        $select_attr_key = implode('_', $attr_key_array);
                        if (in_array($select_attr_key, $check_unique_array)) continue;
                        $newgoodsattr[$selectkey]['goods_id'] = $postdata['id'];
                        $newgoodsattr[$selectkey]['attr_key'] = $select_attr_key;
                        $check_unique_array[] = $select_attr_key;
                    }else if($k == "goods_attr_sn"){
                        if(empty($selectval))$selectval=$goodssn.'-'.$ten;
                        if(!empty($selectval) && !in_array($selectval, $check_goods_attr_sn)){
							$newgoodsattr[$selectkey][$k] = !empty($selectval) ? $selectval : '';
							$check_goods_attr_sn[] = !empty($selectval) ? $selectval : '';
						}$ten++;
                    }else{
                        $newgoodsattr[$selectkey][$k] = !empty($selectval) ? $selectval : 0;
                    }
                }
            }
			
            foreach($newgoodsattr as $key=>$vv){
                if(empty($vv['attr_key'])){
                    unset($newgoodsattr[$key]);
                }
            }
            $goodsattrmod = M('GoodsAttrStock');
            //var_dump($check_goods_attr_sn);print_r($newgoodsattr);exit;
			if($check_goods_attr_sn){
    			$map = array();
    			$map['goods_attr_sn'] = array('in', $check_goods_attr_sn);
                $map['goods_id'] = array('neq', $postdata['id']);
                $check_goods_attr_sn_count = $goodsattrmod->where($map)->count();
                if ($check_goods_attr_sn_count > 0) {
                    return false;
                }
            }
            $old=$goodsattrmod->where('goods_id=' . $postdata['id'])->getField('attr_key',true);
            $nodel=array();
            if($old){
                foreach ($newgoodsattr as $v) {
                    if(in_array($v['attr_key'],$old)){$nodel[]=$v['attr_key'];
                        $map=array();
                        $map['goods_id']=$postdata['id'];
                        $map['attr_key']=$v['attr_key'];
                        $goodsattrmod->where($map)->save($v);
                    }
                }
            }
            
            $map=array();
            $map['goods_id']=$postdata['id'];
            if($nodel)$map['attr_key']=array('notin',$nodel);
            
            $goodsattrmod->where($map)->delete();
            foreach ($newgoodsattr as $v) {
                if($nodel && !in_array($v['attr_key'],$nodel) || empty($nodel))$goodsattrmod->add($v);
            }
        }
        unset($goodstypearr, $newgoodsattr, $postdata);
        return true;
    }
	/**
     * 保存仓库的初始库存
     * @param array $shop_ids 仓库id的数组
     * @param array $is_type 增加或者减少的数组
     * @param array $goods_num 分别对应的数量
     */
	public function save_shops_stock($postdata){
		$return = array();$return['status'] = 1;
		$shop_ids = $postdata['shop_ids'];
		$is_type = $postdata['type_num'];
		$goods_num = $postdata['goods_num'];
		$is_attr = $postdata['is_attr'];
		// echo "<pre>";print_r($shop_ids);print_r($is_type);print_r($goods_num);exit;
		if(!$shop_ids || !$is_type || !$goods_num){
			$return['status'] = 0;$return['msg'] = '参数错误';return $return;
		}
        $change_desc = empty($postdata['remarks'])?'管理员变更':$postdata['remarks'];
		foreach($shop_ids as $k => $v){
			if(floatval($goods_num[$k])){
				if(floatval($goods_num[$k]) < 0){
					$return['status'] = 0;$return['msg'] = '变更数量必须是正数！';return $return;
				}else{
					$where = array();
					$where['shop_id'] = $v;
					$where['goods_id'] = $postdata['id'];
					$count_g = M('ShopsGoods')->where($where)->count();
					if(!$count_g){
						$data = array();
						$data['goods_id'] = $postdata['id'];
						$data['goods_sn'] = idtoname($postdata['id'],'Goods','goods_sn','id',false);
						$data['suppliers_id'] = idtoname($postdata['id'],'Goods','suppliers_id','id',false);
						$data['shop_id'] = $v;
						$data['addtime'] = NOW_TIME;
						$data['status'] = 1;
						$data['qh_count'] = idtoname($postdata['id'],'Goods','qh_count','id',false);
						$data['is_delete'] = 0;
						$data['goods_type'] = 1;
						$data['stock_count'] = 0;
						M('ShopsGoods')->add($data);
					}
					if($is_attr == 1){
						$attr_key = $postdata['attr_key'];
						$where['goods_stock'] = $attr_key;
					}else{
						$where['goods_stock'] = 0;
					}
					$count_gs = M('ShopsGoodsStock')->where($where)->count();
					if(!$count_gs){
						$data = array();
						$data['goods_id'] = $postdata['id'];
						if($is_attr == 1){
							$attr_key = $postdata['attr_key'];
							$data['goods_stock'] = $attr_key;
						}else{
							$data['goods_stock'] = 0;
						}
						$data['suppliers_id'] = idtoname($postdata['id'],'Goods','suppliers_id','id',false);
						$data['shop_id'] = $v;
						$data['goods_number'] = floatval($goods_num[$k]);
						$data['sell_count'] = 0;
						$data['qh_count'] = idtoname($postdata['id'],'Goods','qh_count','id',false);
						$data['addtime'] = $data['updatetime'] = NOW_TIME;
						$data['status'] = 1;
						$gs_add = M('ShopsGoodsStock')->add($data);
						if($gs_add){
                            $goods_attr_key=empty($postdata['attr_key'])?'':$postdata['attr_key'];
							change_stock_log($v,0,0,$postdata['id'],$data['goods_number'],$is_type[$k],$change_desc,9,$goods_attr_key);
						}
					}else{
						$goods_number_old = M('ShopsGoodsStock')->where($where)->getField('goods_number');
						if($goods_number_old == 0){
							if($is_type[$k] == 0){
								$goods_number_new = floatval($goods_num[$k]);
							}
						}else{
							if($is_type[$k] == 0){
								$goods_number_new = floatval($goods_number_old) + floatval($goods_num[$k]);
							}else{
								$goods_number_new = floatval($goods_number_old) - floatval($goods_num[$k]);
							}
						}
						$data = array();
						$data['goods_number'] = $goods_number_new;
						$data['updatetime'] = NOW_TIME;
						$data['status'] = 1;
						$gs_save = M('ShopsGoodsStock')->where($where)->save($data);
						if($gs_save){
                            $goods_stock=M('ShopsGoodsStock')->where($where)->getField('goods_stock');
                            $goods_attr_key=empty($goods_stock)?'':$goods_stock;
							change_stock_log($v,0,$goods_number_old,$postdata['id'],$goods_num[$k],$is_type[$k],$change_desc,9,$goods_attr_key);
						}
					}
				}
			}
		}
		return $return;
	}
    /**
     * 商品关于列表页的筛选条件
     * @param array $postdata
     * @param $goods_type 商品类型 1普通 5订货
     * @return array
     */
    public function index_condition($postdata = array(),$goods_type=1)
    {
        $map = array();
        if(!isset($postdata['keyword'])){
            $postdata['keyword']='';
        }
        $snlike= array('like', '%' . $postdata['keyword'] . '%');
        if (is_numeric($postdata['keyword'])) {
            $map['id|goods_sn|goods_code|goods_name'] = array(intval($postdata['keyword']), $snlike, $snlike, $snlike, '_multi' => true);
        } else if (!empty($postdata['keyword'])) {
            $map['goods_sn|goods_code|goods_name|goods_zhuji'] =$snlike;
        }
        if (!empty($postdata['catid'])) {
            $cat_sons = get_children($postdata['catid'], 1,$goods_type);
            $catstr = implode(',', $cat_sons);
            $extension_goods_id = get_extension_goods($postdata['catid'], 1);
            $extensionstr = implode(',', $extension_goods_id);
            $where = array();
            if (!empty($cat_sons) && !empty($catstr)) {
                $where['cat_id'] = array('in', $cat_sons);
            }
            if (!empty($extension_goods_id) && !empty($extensionstr)) {
                $where['id'] = array('in', $extension_goods_id);
            }
            if (!empty($where)) {
                $where['_logic'] = 'or';
                $map['_complex'] = $where;
            }
        }
        if (!empty($postdata['suppliersid'])) {
            $map['suppliers_id'] = $postdata['suppliersid'];
        }
        if (!empty($postdata['brandid'])) {
            $map['brand_id'] = $postdata['brandid'];
        }
        if (isset($postdata['shops_keyword'])) {
            if(!empty($postdata['shops_keyword'])) {
                $shop_ids = array();
                if (is_numeric($postdata['shops_keyword'])) {
                    $shop_map['id|name'] = array(intval($postdata['shops_keyword']), array('like', '%' . $postdata['shops_keyword'] . '%'), '_multi' => true);
                } else if (!empty($postdata['shops_keyword'])) {
                    $shop_map['name'] = array('like', '%' . (string)$postdata['shops_keyword'] . '%');
                }
                $shops = M('Shops')->field('id')->where($shop_map)->select();
                foreach ($shops as $k => $v) {
                    $shop_ids[$k] = $v['id'];
                }
                if (empty($shop_ids)) {
                    $map['shop_id'] = array('lt', 0);
                } else {
                    $map['shop_id'] = array('in', $shop_ids);
                }
            }else{
                $map['shop_id'] = array('gt', 0);
            }
        } else if (!empty($postdata['shop_id'])) {
            $map['shop_id'] = $postdata['shop_id'];
        } else {
            $map['shop_id'] = 0;
        }
        $map['is_delete'] = !empty($postdata['is_delete'])?$postdata['is_delete']:0;
        if(!isset($postdata['publish_wait'])){
            $postdata['publish_wait']='';
        }
        $map['publish_wait'] = $postdata['publish_wait'];
        return $map;
    }

    /**
     * 更新商品标签
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    public function update_goods_label($postdata, $type = 0)
    {
        $id = $postdata['id'];
        $goods_label = $postdata['goods_label'];
        $selectstr = implode(',', $postdata['goods_label']);
        if ($type == 0 && !empty($goods_label) && $selectstr != $postdata['selectstr']) {
            $map['goods_id'] = $id;
            M('ProjectLabelLink')->where($map)->delete();
            foreach ($goods_label as $k => $v) {
                $map['label_id'] = $v;
                M('ProjectLabelLink')->add($map);
            }
        } elseif ($type == 1) {
            $data['goods_id'] = $id;
            foreach ($goods_label as $k => $v) {
                $data['label_id'] = $v;
                M('ProjectLabelLink')->add($data);
            }
        }
        unset($goods_label, $map, $data);
    }
    /**
    * 更新商品包装
    * @param array $postdata 表单提交数据
    */
    public function update_goods_package($postdata)
    {
        $return=array('status'=>0,'msg'=>'请填写大于0的数值');
        $id = $postdata['id'];
        $linkids = $postdata['linkid'];
        $packname = array_filter($postdata['linkid']);
        $ratio = $postdata['ratio'];
        if (!empty($packname)) {
            $map['goodsid'] = intval($id);
            foreach ($packname as $k => $v) {
                $map['linkid'] = $linkids[$k];
                $map['attr_key'] = $postdata['attr_key'][$k];
                $res = M('GroupGoods')->where($map)->count();
                if($res){
                    $data['ratio']=floatval($ratio[$k]);
                    M('GroupGoods')->where($map)->save($data);
                }else{
                    $num = floatval($ratio[$k]);
                    if($num<1){
                        return $return;
                    }
                    $map['ratio'] = floatval($ratio[$k]);
                    $packid=M('GroupGoods')->add($map);
                }
            }
        }
        $return['status']=1;
        return $return;
        unset($packname,$map, $data);
    }
	/**
     * 更新商品一品多码
     */
    public function update_goods_moresn($postdata){
		$return=array('status'=>0,'msg'=>'参数错误');
		if(!$postdata['goods_sns'] || !$postdata['goods_id']){return $return;}
        $save_data = $goods_sn_arr = array();
		$goods_sns = str_replace(array("\r\n", "\r", "\n", "，"), ",", $postdata['goods_sns']); 
		$goods_sn_arr = array_unique(array_filter(explode(',',$goods_sns)));
		if($goods_sn_arr){
			foreach($goods_sn_arr as $v){
				if (!$v) continue;
				$map = array();
				$map['goods_id'] = array('neq',$postdata['goods_id']);
				$map['goods_sn'] = $v;
				$g_count = M('GoodsMorecode')->where($map)->count();
				if(!$g_count){
					$save_data[] = array('goods_id' => $postdata['goods_id'], 'goods_sn' => $v);
				}
			}
			
			$map_sn = array();$map_sn['goods_id'] = $postdata['goods_id'];
			M('GoodsMorecode')->where($map_sn)->delete();
			if ($save_data) {
				$ret = M('GoodsMorecode')->addAll($save_data);
				if($ret){
					$return['status'] = 1;
				}else{
					$return['msg'] = '编辑失败，没有任何修改！';
				}
			}else{
				$return['msg'] = '编辑失败，商品编码为空！';
			}
		}else{
			$return['msg'] = '编辑失败，商品编码为空！';
		}
		
        unset($save_data, $goods_sn_arr, $postdata);
		return $return;
    }

    /**
     * 只更新服务货号
     * @param array $postdata 表单提交数据
     */
    public function update_service_sn($postdata)
    {
        if(!$postdata['id']) return false;
        $goods_sn = array();
        if (empty($postdata['goods_sn'])) {
            $sn_prefix=C("GOODS_NUM_PREFIX");
            $num_sn = str_pad($postdata['id'], 7, 000, STR_PAD_LEFT);
            $goods_sn['goods_sn'] = $sn_prefix . $num_sn;
        }else{
            $map_sn = array();
            $map_sn['id'] = array('neq',$postdata['id']);
            $map_sn['goods_sn'] = $postdata['goods_sn'];
            $g_count = M('goods')->where($map_sn)->count();
            if($g_count){
                $sn_prefix=C("GOODS_NUM_PREFIX");
                $num_sn = str_pad($postdata['id'], 7, 000, STR_PAD_LEFT);
                $goods_sn['goods_sn'] = $sn_prefix . $num_sn;
            }else{
                $goods_sn['goods_sn'] = $postdata['goods_sn'];
            }
        }
        M('Goods')->where('id=' . $postdata['id'])->save($goods_sn);
        unset($goods_sn, $postdata);
    }

    /**
     * 更新商品素材库 jxr
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    public function update_goodsmatter($postdata,$type = 0){
        $matter_img_arr = array();
        if ($type || $postdata['matter_img']) {
            $mattermod = M('GoodsMatter');
            if (empty($type)) $mattermod->where('goods_id=' . $postdata['id'])->delete();
            foreach ($postdata['matter_img'] as $k => $v) {
                if (!$v) continue;
                $matter_img_arr[] = array('goods_id' => $postdata['id'], 'matter_img' => $v, 'sort' => $postdata['sort'][$k], 'status' => 1);
            }
            if ($matter_img_arr) $mattermod->addAll($matter_img_arr);
        }elseif(!$postdata['matter_img'] && empty($type)){
            M('GoodsMatter')->where('goods_id=' . $postdata['id'])->delete();
        }
        unset($matter_img_arr, $postdata);
    }


}