<?php
/** 店铺登录
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace App\Model;
use Think\Model;
class GoodsModel extends Model{
	public function goods_list($post){
        $return = array('status'=>3,'msg'=>'');
		$list=$goods_huopin = $map = $category = array();
        $map['g.status'] = 1;
        $map['g.goods_type'] = 5;
		$map['g.is_delete'] = 0;
		$map['g.publish_wait'] = 1;
		$map['sg.shop_id'] = $post['shop_id'];
		$field = 'g.id,g.cat_id,g.goods_name,g.img,g.shop_price,sg.goods_stock,g.promote_id,g.promote_price';
//        $pagesize=20;
        // $pagesize=empty($pagesize)?C('WEB_ARTICLE_PAGE'):$pagesize;
//        $info=page_list('Goods',$map,'id desc',$field,$pagesize,'',1);
		$count =  M('Goods')->alias('g')->JOIN('JOIN __SHOPS_GOODS_STOCK__ sg ON g.id=sg.goods_id')->where($map)->field($field)->count();
		if($count){
			$info = M('Goods')->alias('g')->JOIN('JOIN __SHOPS_GOODS_STOCK__ sg ON g.id=sg.goods_id')->where($map)->field($field)->select();
			foreach($info as $k=>$v){
				$category[] = $v['cat_id'];
				if($v['goods_stock']){
					$goods_huopin = $this->getattr_price($v['id'],$v['goods_stock']);
					$info[$k]['attr']=attr_key2name($v['goods_stock'],$v['id']);
					$info[$k]['attr_key']= $v['goods_stock'];
					$list[$v['id'].'-'.$v['goods_stock']]=$info[$k];
					$list[$v['id'].'-'.$v['goods_stock']]['market_price'] = number_format($goods_huopin['data']['attr_price'],2);
					
				}else{
					$info[$k]['market_price']=$info[$k]['shop_price'];
					$list[$v['id'].'-']=$info[$k];
				}
			}
		}else{
			return $return=array('status'=>2,'msg'=>'暂无数据');
		}
		$category_ids = array_unique($category);
		if(empty($category_ids)){$return['status']=0;return $return;}
		$tc_map['id']=array('in',$category_ids);
		$tc_map['status']=1;
		$category_list=M('PurchaseCategory')->where($tc_map)->field(true)->order('sort asc')->select();	
		if(empty($category_list)){$return['status']=2;$return['msg']='暂无分类数据';return $return;}
		$category_info = array();
		foreach($category_list as $k=>$v){
			$category_info[$v['id']]=$v;
		}
		$goods_list = $category_list = array();
		$goods_list = $list;
		$category_list = $category_info;
		$cate_goods_list=array();
		
		if($goods_list){
			foreach($goods_list as $k=>$v){
				if(empty($cate_goods_list[$v['cat_id']])) {
					if ($category_list[$v['cat_id']]) {
						$cate_goods_list[$v['cat_id']] = $category_list[$v['cat_id']];
					}
				}
				$cate_goods_list[$v['cat_id']]['goods_list'][$k]=$v;
			}
		}
		
        if($cate_goods_list){
			$return['status']=1; $return['data'] = $cate_goods_list;
		}else{
			$return=array('status'=>2,'msg'=>'暂无数据');
		}
        return $return;
    }
	public function category_goods_list($post){
        $return = array('status'=>3,'msg'=>'');
        $map = $tc_map = array();
        $map['status'] = 1;
        $map['goods_type'] = 5;
		$map['is_delete'] = 0;
		$map['publish_wait'] = 1;
        if(!isset($post['keyword'])){
            $post['keyword']='';
        }
        $snlike= array('like', '%' . $post['keyword'] . '%');
        if (is_numeric($post['keyword'])) {
            $map['id|goods_sn|goods_name|goods_code'] = array(intval($post['keyword']),$snlike,$snlike,$snlike, '_multi' => true);
        } else if (!empty($post['keyword'])) {
            $map['goods_sn|goods_name|goods_code|goods_zhuji'] =$snlike;
        }
        if (!empty($post['catid'])) {
            $cat_sons = get_children($post['catid'], 1,5);
            $catstr = implode(',', $cat_sons);
            $extension_goods_id = get_extension_goods($post['catid'], 1);
            $extensionstr = implode(',', $extension_goods_id);
            $where = array();
            if (!empty($cat_sons) && !empty($catstr)) {
                $map['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;
            }
        }
		$field = 'id,cat_id,goods_sn,goods_name,img,shop_price,market_price';
		$goods_list = M('Goods')->where($map)->field($field)->order('id desc')->select();
		if(empty($goods_list)){$return['status']=3;$return['msg']='暂无商品数据';return $return;}
		
		if($goods_list){
			foreach($goods_list as $k=>$v){
				$category[] = $v['cat_id'];
				$goods_list[$k] = $v;
				$goods_list[$k]['format_spec'] = D("Common/GoodsAttr")->getattr($v['id']);
				$goods_list[$k]['goods_name']=str_cut($v['goods_name'],70);
				$goods_list[$k]['img']=thumb($v['img'],200,200);
				$goods_list[$k]['shop_price']=format_price($v['shop_price']);
				$goods_list[$k]['market_price']=format_price($v['market_price']);
				$goods_list[$k]['price']=$v['shop_price'];
				$goods_list[$k]['vipprice']=$this->getmemberprice($v['id'],0,$v['shop_price']);
			}
		}
		// $tc_map['pid']=0;
		$category_ids = array_unique($category);
		// if(empty($category_ids)){$return['status']=3;$return['msg']='暂无分类数据';return $return;}
        if($category_ids){
            $tc_map['id']=array('in',$category_ids);
    		$tc_map['status']=1;
    		$category_list=M('PurchaseCategory')->where($tc_map)->cache(true)->field(true)->order('sort asc')->select();
        }
			
		// if(empty($category_list)){$return['status']=3;$return['msg']='暂无分类数据';return $return;}
		
		$cate_goods_list=array();
		foreach($category_list as $key=>$val){
			$cate_goods_list[$val['id']] = $val;
			$cate_goods_list[$val['id']]['goods_list'] = '';
			foreach($goods_list as $k=>$v){
				$cate_goods_list[$v['cat_id']]['goods_list'][$k]=$v;
			}
		}
        if($cate_goods_list){
			$return['status']=1; $return['data'] = $cate_goods_list;
			$return['goods_list'] = $goods_list;
		}else{
			$return=array('status'=>2,'msg'=>'暂无数据');
		}
        return $return;
    }
    /**
     * 根据会员组获取商品会员价
     */
    private function getmemberprice($goods_id,$rankid,$shop_price=0){
        if(empty($goods_id))return $shop_price;$rankinfo=array();
        if(empty($rankid)){
            $map = array();
            $map['status'] = 1;
            $rankinfo = M("MemberRank")->where($map)->cache(true)->order('sort desc,id desc')->field("id,goods_price")->find();
            $rankid=$rankinfo['id'];
        }
        if(empty($rankid))return $shop_price;               
        $map = array();
        $map['goods_id'] = $goods_id;
        $map['user_rank'] = $rank_id;
        $user_price = M('MemberPrice')->where($map)->getField('user_price');
        if($user_price){
            return $user_price < $shop_price?$user_price:$shop_price;
        }else{
            if(empty($rankinfo['goods_price'])){
                $map = array();
                $map['id'] = $rank_id;
                $rank = M("MemberRank")->where($map)->cache(true)->getField("goods_price");
            }else{
                $rank = $rankinfo['goods_price'];
            }
            if(empty($rank))return $shop_price;
            return format_decimal($shop_price*$rank/100);
        }
    }
	public function category_lists(){
        $return = array('status'=>3,'msg'=>'');
		$tc_map = $category_list = array();
		$tc_map['pid']=0;
		$tc_map['status']=1;
		$category_list = M('PurchaseCategory')->where($tc_map)->cache(true)->field('id,name')->order('sort asc')->select();
		if(empty($category_list)){
			$category_list[]=array('id'=>0,'name'=>'默认分类');
		}else{
			$return['status'] = 1; $return['data'] = $category_list;
		}
        return $return;
	}
	public function category_two_lists($postdata){
        $return = array('status'=>3,'msg'=>'');
		$tc_map = $category_list = array();
		$tc_map['pid']=$postdata['pid'];
		$tc_map['status']=1;
		$category_list = M('PurchaseCategory')->where($tc_map)->cache(true)->field('id,name')->order('sort asc')->select();
		if(empty($category_list)){
			$return['status'] = 2;$return['msg']='暂无分类数据';
		}else{
			$return['status'] = 1; $return['data'] = $category_list;
		}
        return $return;
	}
	public function goods_lists($post){
        $return = array('status'=>3,'msg'=>'');
		$catid = $post['cate_id'];
		if(empty($catid)){
			$return['msg']='参数错误!';return $return;
		}else{
			$cat_sons = get_children($catid, 1,5);
			$catstr = implode(',', $cat_sons);
			
			$map = array();
			$map['status'] = 1;
			$map['goods_type'] = 5;
			$map['is_delete'] = 0;
			$map['publish_wait'] = 1;
			if (!empty($cat_sons) && !empty($catstr)) {
				$map['cat_id'] = array('in', $cat_sons);
			}
			$field = 'id,cat_id,goods_sn,goods_name,img,shop_price,market_price';
			// $goods_list = M('Goods')->where($map)->field($field)->order('id desc')->select();
			$goods_list_arr = page_list('Goods',$map,'id desc',$field,15,'',1);
			if(empty($goods_list_arr['list'])){
				$return['msg']='没有更多的数据了!';return $return;
			}else{
				$goods_list = $goods_list_arr['list'];
				foreach($goods_list as $k=>$v){
					$goods_list[$k] = $v;
					$goods_list[$k]['goods_name']=str_cut($v['goods_name'],70);
					$goods_list[$k]['img']=thumb($v['img'],200,200);
					$goods_list[$k]['shop_price']=format_price($v['shop_price']);
					$goods_list[$k]['market_price']=format_price($v['market_price']);
				}
				
				if($goods_list){
					$goods_list_arr['list'] = $goods_list;
					$return['status']=1;
					$return['data'] = $goods_list_arr;
				}else{
					$return['msg']='没有更多的数据了!';
				}
				return $return;
			}
		}
    }
	public function unit_lists(){
        $return = array('status'=>3,'msg'=>'');
		$tc_map = $unit_list = array();
		$tc_map['status']=1;
		$unit_list = M('GoodsUnit')->where($tc_map)->field('id,name')->order('id asc')->select();
		if(empty($unit_list)){
			$return['status'] = 2;$return['msg']='暂无单位';
		}else{
			$return['status'] = 1; $return['data'] = $unit_list;
		}
        return $return;
	}
	public function goods_info($goods_id,$shop_id){
		$result = $data = array();
		$result['status'] = 1;
		if(empty($goods_id)){
			$result['status'] = 2;
			$result['msg']='商品ID不能为空';
		}
		$map = array();
		$map['id'] = $goods_id;
		$map['status'] = 1;
		$field = 'id,cat_id,goods_name,img,goods_sn,goods_code,unit_name,unit_id,supply_price,shop_price,market_price';
		$data = M('Goods')->where($map)->field($field)->find();
		if(empty($data)){
			$result['status'] = 2;
			$result['msg']='该商品已下架或不存在';
		}else{
			$data['current_shop_id'] = $shop_id;
			$data['is_custom'] = $data['custom_goods_price'] = 0;
			
			$map = array();$map['id'] = $shop_id;
			$shop_center_rule = M('Shops')->where($map)->getField('shop_center_rule');
			if($shop_center_rule){
				$shop_center_rule_array = json_decode($shop_center_rule,true);
				$data['is_custom'] = $shop_center_rule_array['custom_goods_price'];
				if($data['is_custom'] == 1){
					$map = array();$map['shop_id'] = $shop_id;$map['goods_id'] = $goods_id;
					$data['custom_goods_price'] = M('ShopsGoods')->where($map)->getField('goods_price');
				}
			}
			
			$map = array();
			$map['shop_id'] = $shop_id;
			$map['goods_id'] = $goods_id;
			$map['goods_stock'] = 0;
			$data['goods_number'] = M('ShopsGoodsStock')->where($map)->getField('goods_number');
			
			$map = array();
			$map['id'] = $data['unit_id'];
			$data['unit_name'] = M('GoodsUnit')->where($map)->getField('name');
			
			$map = $cate_map = array();
			$map['goods_id'] = $goods_id;
			$cate_map['id'] = $data['cat_id'];
			$data['cate_name'] = M('PurchaseCategory')->where($cate_map)->getField('name');
			$data['gallery_list']=M('GoodsGallery')->where($map)->getField('goods_img',true);
			$result['data'] = $data;
		}
		return $result;
	}
	// 添加商品
    public function goods_add($postdata){
        $return = $sa_data = array();
        $return['status'] = 1;
		if(empty($postdata)){
            $return['status'] = 3;
            $return['msg'] = '参数不能为空';
            return $return;
        }
		$postdata['shop_id'] = 0;
		if($postdata['current_shop_id']){
			$map = array();$map['id'] = $postdata['current_shop_id'];
			$shop_type = M('Shops')->where($map)->getField('shop_type');
			if($shop_type != 1){
				$postdata['shop_id'] = $postdata['current_shop_id'];
			}
		}
        $model = M("Goods");
        $check_data = $this->check_post($postdata);
        if($check_data['status'] == 0){
			$return['status'] = 3;
            $return['msg'] = $check_data['msg'];
            return $return;
        }
		$check_ret = $this->check_goods_info($postdata,5);
		if($check_ret['status'] == 3){
            return $check_ret;
        }
		
		$map_sn = array();
		if($postdata['id']){
			$map_sn['id'] = array('neq',$postdata['id']);
		}
		if($postdata['goods_sn']){
			$map_sn['goods_sn'] = $postdata['goods_sn'];
			$map_sn['goods_type'] = 5;
			$g_count = $model->where($map_sn)->count();
			if($g_count){
				$return['status'] = 3;
				$return['msg']='商品编码不能重复';
				return $return;
			}
		}else{
			$return['status'] = 3;
			$return['msg']='商品编码不能为空';
			return $return;
		}
		if($postdata['img'] && str_exists($postdata['img'],'image/upload_up.png')){
			$postdata['img'] = '';
		}
		
		$postdata['goods_type'] = 5;
		$postdata['publish_wait'] = 1;
        $postdata = $this->format_post($postdata);
		
		// $return['status'] = 1;
		// $return['data'] = $postdata;
		// return $return;
		
        $goods_id = $model->add($postdata);
		
		$postdata['id'] = $goods_id;
		$postdata['goods_id'] = $goods_id;
		$this->update_goods_code($postdata);//更新货码
		// $this->update_goods_sn($postdata,1);//更新货号
		if($postdata['gallery_list']){
			$this->goods_gallery($postdata,1);//商品相册
		}
        if ($goods_id) {
			$return['status'] = 1;
			$return['goods_id'] = $goods_id;
			
			if($postdata['goods_number']){
				//添加门店库存
				$this->add_shops_goods($goods_id,$postdata);
			}
			if($goods_id && $postdata['custom_goods_price']){
				//添加门店商品价格
				$this->add_goods_price($goods_id,$postdata);
			}
			$return['msg'] = '添加成功';
        } else {
			$return['status'] = 3;
			$return['msg'] = '添加失败';
        }
        return $return;
    }
	public function add_goods_price($goods_id,$postdata){
		$shop_id = $postdata['current_shop_id'];
		if($shop_id){
			$map = array();$map['id'] = $shop_id;
			$shop_center_rule = M('Shops')->where($map)->getField('shop_center_rule');
			if($shop_center_rule && $postdata['custom_goods_price']){
				$shop_center_rule_array = json_decode($shop_center_rule,true);
				$is_custom = $shop_center_rule_array['custom_goods_price'];
				if($is_custom == 1){
					$map = array();$map['shop_id'] = $shop_id;$map['goods_id'] = $goods_id;
					$count = M('ShopsGoods')->where($map)->count();
					if($count){
						$data = array();$data['goods_price'] = $postdata['custom_goods_price'];
						M('ShopsGoods')->where($map)->save($data);
					}else{
						$data = array();
						$data['goods_id'] = $goods_id;
						$data['goods_sn'] = idtoname($goods_id,'Goods','goods_sn','id',false);
						$data['suppliers_id'] = idtoname($goods_id,'Goods','suppliers_id','id',false);
						$data['shop_id'] = $postdata['current_shop_id'];
						$data['addtime'] = NOW_TIME;
						$data['status'] = 1;
						$data['qh_count'] = idtoname($goods_id,'Goods','qh_count','id',false);
						$data['is_delete'] = 0;
						$data['goods_type'] = 1;
						$data['stock_count'] = 0;
						$data['goods_price'] = $postdata['custom_goods_price'];
						M('ShopsGoods')->add($data);
					}
				}
			}
		}
	}
	/**
     * 更新货码
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    public function update_goods_code($postdata, $type = 0){
        $goods_code = array();
        if(empty($postdata['goods_zhuji'])){
           $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'];
			}
		}
		if(!empty($goods_code))M('Goods')->where('id=' . $postdata['id'])->save($goods_code);
		unset($goods_code, $postdata);
    }
	public function add_shops_goods($goods_id,$postdata){
		$is_attr = 0;
		if($postdata && $goods_id && $postdata['goods_number'] && $postdata['current_shop_id']){
			$where = array();
			$where['shop_id'] = $postdata['current_shop_id'];
			$where['goods_id'] = $goods_id;
			$count_g = M('ShopsGoods')->where($where)->count();
			if(!$count_g){
				$data = array();
				$data['goods_id'] = $goods_id;
				$data['goods_sn'] = idtoname($goods_id,'Goods','goods_sn','id',false);
				$data['suppliers_id'] = idtoname($goods_id,'Goods','suppliers_id','id',false);
				$data['shop_id'] = $postdata['current_shop_id'];
				$data['addtime'] = NOW_TIME;
				$data['status'] = 1;
				$data['qh_count'] = idtoname($goods_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'] = $goods_id;
				if($is_attr == 1){
					$attr_key = $postdata['attr_key'];
					$data['goods_stock'] = $attr_key;
				}else{
					$data['goods_stock'] = 0;
				}
				$data['suppliers_id'] = idtoname($goods_id,'Goods','suppliers_id','id',false);
				$data['shop_id'] = $postdata['current_shop_id'];
				// $data['goods_number'] = intval($goods_num[$k]);
				$data['goods_number'] = intval($postdata['goods_number']);
				$data['sell_count'] = 0;
				$data['qh_count'] = idtoname($goods_id,'Goods','qh_count','id',false);
				$data['addtime'] = $data['updatetime'] = NOW_TIME;
				$data['status'] = 1;
				$gs_add = M('ShopsGoodsStock')->add($data);
				if($gs_add){
					$is_type = 0;
					$goods_attr_key=empty($postdata['attr_key'])?'':$postdata['attr_key'];
					change_stock_log($postdata['current_shop_id'],0,0,$goods_id,$data['goods_number'],$is_type,'初始库存',9,$goods_attr_key);
				}
			}else{
				$goods_number_old = M('ShopsGoodsStock')->where($where)->getField('goods_number');
				if($goods_number_old == 0){
					$is_type = 0;
					$goods_number_new = intval($postdata['goods_number']);
				}else{
					$cha = $postdata['goods_number'] - $goods_number_old;
					$goods_number_new = $postdata['goods_number'];
					$goods_num = abs($cha);
					if($cha > 0){ $is_type = 0;}
					if($cha < 0){ $is_type = 1;}
					if($cha == 0){ return true;}
				}
				$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($postdata['current_shop_id'],0,$goods_number_old,$postdata['id'],$goods_num,$is_type,'初始库存',9,$goods_attr_key);
				}
			}
		}
	}
	public function add_attr($goods_id,$post){
		$attr_option = $attr_sn = array();
		$attr_option = json_decode(htmlspecialchars_decode($post['attr_options']),true);
		$attr_sn = json_decode(htmlspecialchars_decode($post['attr_sn']),true);
		$money = json_decode(htmlspecialchars_decode($post['money']),true);
		
		if($attr_sn){
			foreach($attr_sn as $k => $v){
				if($v){
					$map_sn = array();
					$map_sn['goods_sn'] = $v;
					$map_sn['goods_type'] = 5;
					$count = M('Goods')->where($map_sn)->count();
					$map = array();
					$map['goods_attr_sn'] = $v;
					$g_count = M('GoodsAttrStock')->where($map)->count();
					if($count || $g_count){
						$return['status'] = 3;
						$return['msg']='商品条码重复';
						return $return;
					}
				}
			}
		}
		
		if($attr_option){
			foreach($attr_option as $k => $v){
				$arr_value = explode(',',$v);
				if($arr_value){
					$data = array();
					foreach($arr_value as $key=>$val){
						$data['goods_id'] = $goods_id;
						$data['attr_id'] = $k;
						$data['attr_value'] = $val;
						M('GoodsAttr')->add($data);
					}
				}else{
					return false;
				}
				
			}
		}
		
		$map = array();
		$map['goods_id'] = $goods_id;
		$attr_stock = M('GoodsAttr')->where($map)->field('id,attr_id,attr_value')->select();
		
		$attr_stock_data = array();
		if($attr_stock){
			foreach($attr_stock as $key => $val){
				$attr_stock_data[$val['attr_id'].'-'.$val['attr_value']] = $val['id'];
			}
		}
        
		if($attr_sn){
			foreach($attr_sn as $k => $v){
				$attr_keys = explode('_',$k);
				$attr_key = '';
				foreach($attr_keys as $key => $val){
					if($key == 0){
						$attr_key = $attr_stock_data[$val];
					}else{
						$attr_key = $attr_key.'_'. $attr_stock_data[$val];
					}
				}
				$attr_price = $money[$k];
				$add_data = array();
				$add_data['goods_id'] = $goods_id;
				$add_data['attr_key'] = $attr_key;
				$add_data['goods_attr_sn'] = $v;
				$add_data['attr_price'] = $attr_price;
				M('GoodsAttrStock')->add($add_data);
			}
		}
    }
	// 编辑商品
    public function goods_edit($postdata){
        $return = $sa_data = array();
        $return['status'] = 1;
		if(empty($postdata)){
            $return['status'] = 3;
            $return['msg'] = '参数不能为空';
            return $return;
        }
		if(empty($postdata['goods_id'])){
            $return['status'] = 3;
            $return['msg'] = '参数不能为空';
            return $return;
        }
		if($postdata['is_root'] != 1){
			if($postdata['current_shop_id']){
				$shop_map = array();$shop_map['id'] = $postdata['current_shop_id'];
				$shop_type = M('Shops')->where($shop_map)->getField('shop_type');
				if($shop_type != 1){
					$count = 0;
					$goods_map = array();$goods_map['id'] = $postdata['goods_id'];
					$goods_map['shop_id'] = $postdata['current_shop_id'];
					$count = M('Goods')->where($goods_map)->count();
					if($count <= 0){
						$return['status'] = 3;
						$return['msg']='加盟店没有权限编辑商品';
						return $return;exit;
					}
				}
			}
		}
		if($postdata['img'] && str_exists($postdata['img'],'image/upload_up.png')){
			$postdata['img'] = '';
		}
		unset($postdata['shop_id']);
        $model = M("Goods");
        $check_data = $this->check_post($postdata);
		
        if($check_data['status'] == 0){
			$return['status'] = 3;
            $return['msg'] = $check_data['msg'];
            return $return;
        }
		$postdata['goods_type'] = 5;
        $postdata = $this->format_post($postdata,$postdata['goods_id']);
		
		$check_ret = $this->check_goods_info($postdata,5,$postdata['goods_id']);
		if($check_ret['status'] == 3){
            return $check_ret;
        }
		$map_sn = array();
		if($postdata['goods_id']){
			$map_sn['id'] = array('neq',$postdata['goods_id']);
		}
		if($postdata['goods_sn']){
			$map_sn['goods_sn'] = $postdata['goods_sn'];
			$map_sn['goods_type'] = 5;
			$g_count = $model->where($map_sn)->count();
			if($g_count){
				$return['status'] = 3;
				$return['msg']='商品编码不能重复';
				return $return;
			}
		}
		$map_id = array();
		$map_id['id'] = $postdata['goods_id'];
        $ret = $model->where($map_id)->save($postdata);
		$this->update_goods_code($postdata);//更新货码
		// $this->update_goods_sn($postdata,1);//更新货码
		if($postdata['gallery_list']){
			$this->goods_gallery($postdata,0);//商品相册
		}
		if($postdata['goods_id'] && $postdata['custom_goods_price']){
			//编辑门店商品价格
			$this->add_goods_price($postdata['goods_id'],$postdata);
		}
        if ($ret) {
			$return['status'] = 1;
			$return['msg'] = '编辑成功';		
        } else {
			$return['status'] = 3;
			$return['msg'] = '编辑失败';
        }
        return $return;
    }
    /**
     * 检查提交的数据是否合法
     * @param array $postdata
     * @param $sid account表的id
     */
    private function check_post($postdata = array()){
        $return = array();
        $return['status'] = 0;
        $return['msg'] = '';
        if (empty($postdata))$return['msg'] = "非法数据";
        if ($postdata['goods_name'] == '')$return['msg'] = "商品名称不得为空";
        if ($postdata['supply_price'] == '')$return['msg'] = "成本价不得为空";
        if ($postdata['shop_price'] == '')$return['msg'] = "本店价不得为空";
        if ($postdata['market_price'] == '')$return['msg'] = "市场价不得为空";
		if(empty($return['msg'])){
			$return['status'] = 1;
		}
        return $return;
    }
	/**
     * 格式化提交的数据
     * @param array $postdata
     * @return array
     */
    private function format_post($postdata = array(),$goods_id = 0){
        $postdata['qh_count'] = intval($postdata['qh_count']);
		if($goods_id == 0){
			$postdata['addtime'] = NOW_TIME;
		}
        $postdata['updatetime'] = NOW_TIME;
        return $postdata;
    }
	private function check_goods_info($postdata,$goods_type,$goods_id = 0){
		$return=array('status'=>3,'msg'=>'');
		$map = array();
		$map['goods_name'] = $postdata['goods_name'];
		$map['goods_type'] = $goods_type;
		if($goods_id > 0){
			$map['id'] = array('neq',$goods_id);
		}
		$count = M('Goods')->where($map)->count();
		if($count){
			$return['msg']='商品名称不能重复';
		}else{
			$return['status'] = 1;
		}
		if(isset($postdata['goods_sn'])){
			$map = array();
			$map['goods_sn'] = $postdata['goods_sn'];
			if($goods_id > 0){
				$map['id'] = array('neq',$goods_id);
			}
			$g_count = M('Goods')->where($map)->count();
			if($g_count){
				$return['msg']='商品编号不能重复';
			}else{
				$return['status'] = 1;
			}
		}else{
			$return['msg']='编号不能为空';
		}
        return $return;
	}
	/**
     * 更新货号
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
    private function update_goods_sn($postdata, $type = 0){
        $goods_sn = array();
        if(empty($postdata['goods_zhuji'])){
           $goods_sn['goods_zhuji']= pinyin_long($postdata['goods_name']);
        }
        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;
        }
        if(!empty($goods_sn))M('Goods')->where('id=' . $postdata['id'])->save($goods_sn);
        unset($goods_sn, $postdata);
    }
	/**
     * 商品相册
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     */
	private function goods_gallery($postdata, $type = 0){
		$gallery_list = $map_gallery = $add_data = array();
		if($type == 0 && $postdata['goods_id']){
			$map_gallery['goods_id'] = $postdata['goods_id'];
			M('GoodsGallery')->where($map_gallery)->delete();
		}
		$gallery_list = json_decode(htmlspecialchars_decode($postdata['gallery_list']));
		if($gallery_list){
			foreach($gallery_list as $v){
				if(!empty($v)){
					$add_data['goods_id'] = $postdata['goods_id'];
					$add_data['goods_img'] = $v;
					M('GoodsGallery')->add($add_data);
				}
			}
		}
		unset($gallery_list, $postdata, $add_data);
	}
	public function getcateTree($pid = '', $assign = '', $goods_type){
	    if($goods_type==5){
            $GoodsCategory = getcache('PurchaseCategory');
        }else{
            $GoodsCategory = getcache('GoodsCategory');
        }
        $Tree = new \Common\Vendor\Tree();
        $select_categorys = '<option value="0">请选择商品分类</option>';
        $treemini = $Tree->treemini($GoodsCategory);
        $select_categorys .= $Tree->treeminioption($treemini, $pid);
        if ($assign) return $select_categorys; else $this->assign('select_categorys', $select_categorys);
        unset($GoodsCategory, $select_categorys);
    }
	public function package_add($postdata){
		$return=array('status'=>3,'msg'=>'');
        $model = M('Goods');
		if($postdata['is_integral']!=1){
			$postdata['is_integral']=0;
		}
		if($postdata['goods_name'] == ''){
            $return['msg']='套餐名称不能为空';
            return $return;
        }
		$map = array();
		if($postdata['id'])$map['id'] = array('neq',$postdata['id']);
		$map['goods_name'] = $postdata['goods_name'];
		$map['goods_type'] = 6;
		$count = $model->where($map)->count();
		if($count){
			$return['msg']='套餐名称重复';
            return $return;
		}
		$map = array();if($postdata['id'])$map['id'] = array('neq',$postdata['id']);
		if($postdata['goods_sn']){
			$map['goods_sn'] = $postdata['goods_sn'];
			$map['goods_type'] = 6;
			$g_count = $model->where($map)->count();
			if($g_count){
				$return['msg']='编号重复';
				return $return;
			}
		}
		$postdata['services_name'] = json_decode(htmlspecialchars_decode($postdata['services_name']),true);
		$postdata['services_num'] = json_decode(htmlspecialchars_decode($postdata['services_num']),true);
		if($postdata['services_name']){
			foreach($postdata['services_name'] as $k=>$v){
				$arr[$v]=$postdata['services_num'][$k];
			}
		}
		$postdata['contain_service'] = json_encode(array_filter($arr));
		unset($postdata['services_name'],$postdata['services_num']);
		if($postdata['shop_ids']){
			$postdata['shop_ids'] = ','.$postdata['shop_ids'].',';
		}else{
			$postdata['shop_ids'] = 0;
		}
		$postdata['continued'] = intval($postdata['continued']);
		$postdata['shop_price'] = $postdata['market_price'];
		$postdata['addtime'] = NOW_TIME;
		$postdata['publish_wait'] = 1;
		$postdata['status'] = 1;
		$postdata['goods_type'] = 6;
		if($postdata['id'])$postdata['updatetime'] = NOW_TIME;
		if($postdata['id']){
			$id = $model->save($postdata);
			$content['goods_id'] = $postdata['id'];
			$content['content'] = $postdata['content']?$postdata['content']:'计次卡详情';
			M('GoodsContent')->save($content);
			$this->update_goods_sn($postdata);//更新货号
			if($id){
				$return['status'] = 1;
				$return['msg'] = '编辑成功';
			}else{
				$return['msg']='编辑失败';
			}
		}else{
			$id = $model->add($postdata);
			$content['goods_id'] = $id;
			$content['content'] = $postdata['content']?$postdata['content']:'计次卡详情';
			M('GoodsContent')->add($content);
			$postdata['id'] = $id;
			$this->update_goods_sn($postdata);//更新货号
			if($id){
				$return['status'] = 1;
				$return['msg'] = '添加成功';
			}else{
				$return['msg']='添加失败';
			}
		}
        return $return;
	}
	public function package_list(){
        $return=array('status'=>3,'msg'=>'');
        $map=array();
        $map['status']=1;
        $map['goods_type']=6;
		$field = 'id,goods_name,shop_price,contain_service,img,continued,shop_ids,promote_price';
        // $info=M("Member")->where($map)->field('id,goods_name,shop_price,contain_service,continued,shop_ids')->order('reg_time desc')->select();
        // $pagesize=5;
        $pagesize=empty($pagesize)?C('WEB_ARTICLE_PAGE'):$pagesize;
        $info=page_list('Goods',$map,'id desc',$field,$pagesize,'',1);
        if($info['list']){
			foreach($info['list'] as $k=>$v){
				$info['list'][$k]['contain_service'] = json_decode($v['contain_service'],true);
			}
			$return['status']=1; 
			$return['data'] = $info['list'];
		}else{$return=array('status'=>2,'msg'=>'暂无数据');}
        return $return;
    }
	public function get_package($id){
        $return=array('status'=>3,'msg'=>'');
        $info=M("Goods")->field(true)->find($id);
		$info['content'] = idtoname($id,'GoodsContent','content','goods_id',false);
		$info['contain_service'] = json_decode($info['contain_service'],true);
		$ids_array = $shop_ids = array();
		$shop_ids = array_filter(explode(',', $info['shop_ids']));
		$info['shop_ids'] = $shop_ids;
        if($info){
            $return['status'] = 1;
            $return['data'] = $info;
        }else{
            $return=array('status'=>2,'msg'=>'暂无数据');
        }
        return $return;
    }
	public function delete_goods($ids){
		$return=array('status'=>3,'msg'=>'');
		if($ids == ''){
			$return['status'] = 2;
            $return['msg'] = '参数不能为空';
		}else{
			$goods_ids = json_decode(htmlspecialchars_decode($ids),true);
			$map=array();
			$map['id'] = array('in',$goods_ids);
			$map['goods_type'] = 5;
			$map['status'] = 1;
			$save_data['is_delete'] = 1;
			$save_data['status'] = 0;
			
			$ret = M('Goods')->where($map)->save($save_data);
			if($ret > 0){
				$return['status'] = 1;
				$return['msg'] = '删除成功！';
			}else{
				$return['status'] = 3;
				$return['msg'] = '删除失败！';
			}
		}
		return $return;
	}
	public function delete_category($cat_id){
		$return = $map =  $save_data = array();
		if(empty($cat_id)){
			$return['status'] = 2;
            $return['msg'] = '参数不能为空';
			return $return;
		}
		$ret = $this->check_delete($cat_id);
		if($ret['status'] == 1){
			//删除
			$map['id'] = $cat_id;
			$map['status'] = 1;
			$save_data['status'] = 0;
			
			$result = M('PurchaseCategory')->where($map)->save($save_data);
			if($result){
				$return['status'] = 1;$return['msg']='删除成功！';
			}else{
				$return['status'] = 3;$return['msg']='删除失败！';
			}
		}else{
			$return = $ret;
		}
		return $return;
	}
	private function check_delete($cat_id){
		$return=array();
		$map = array();
		$map['pid'] = $cat_id;
		$count_cate = M('PurchaseCategory')->where($map)->count();
		if($count_cate){
			$return['status'] = 3;$return['msg']='请先清除子分类';return $return;
		}
		
		$map = array();
		$map['cat_id'] = $cat_id;
		$map['goods_type'] = 5;
		$count = M('Goods')->where($map)->count();
		if($count){
			$return['status'] = 3;$return['msg']='分类下绑定了商品,无法删除';return $return;
		}else{
			$return['status'] = 1;
		}
		return $return;
	}
	public function category_info($cat_id){
		$return=array('status'=>3,'msg'=>'');
		$map = array();
		$map['id'] = $cat_id;
		$map['status'] = 1;
		$cate_info = M('PurchaseCategory')->where($map)->field('id,name')->find();
		if($cate_info){
			$return['status'] = 1;$return['data'] = $cate_info;
		}else{
			$return['status'] = 3;$return['msg'] = '没找到该分类信息';
		}
		return $return;
	}
	//添加或编辑分类
	public function save_category($postdata){
		$return=array('status'=>3,'msg'=>'操作失败');
		if(empty($postdata)){
			$return['status'] = 2;
            $return['msg'] = '参数不能为空';
			return $return;
		}
		$cat_id = $postdata['id'];
		$save_data = $map = array();
		$save_data['name'] = $save_data['seotitle'] = $save_data['seokeywords'] = $save_data['seodescription'] = $postdata['name'];
		if($cat_id){
			$map['id'] = $cat_id;
			$map['status'] = 1;
			$ret = M('PurchaseCategory')->where($map)->save($save_data);
			savecache('PurchaseCategory');
			$return['msg'] = '编辑成功';
		}else{
			$save_data['status'] = 1;
			$ret = M('PurchaseCategory')->add($save_data);
			savecache('PurchaseCategory');
			$return['msg'] = '添加成功';
		}
		if($ret){
			$return['status'] = 1;
		}
		return $return;
	}
	//获取属性信息
	public function attr_info($attr_id){
		$return=array('status'=>3,'msg'=>'');
		$map = array();
		$map['id'] = $attr_id;
		$map['status'] = 1;
		$attr_info = M('PurchaseCategory')->where($map)->field('id,name,options')->find();
		if($attr_info){
			$attr_info['options'] = strtoarray($attr_info['options']);
			$return['status'] = 1;$return['data'] = $attr_info;
		}else{
			$return['msg'] = '没找到该属性信息';
		}
		return $return;
	}
	//添加或编辑分类checkbox
	public function save_attr($postdata){
		$return=array('status'=>3,'msg'=>'操作失败');
		if(empty($postdata)){
			$return['status'] = 2;
            $return['msg'] = '参数不能为空';
			return $return;
		}
		
		$attr_id = $postdata['id'];
		if($attr_id){
			$save_data = $map = array();
			$map['id'] = $attr_id;
			$map['status'] = 1;
			$save_data['name'] = $postdata['name'];
			$ret = M('GoodsAttribute')->where($map)->save($save_data);
			$return['msg'] = '编辑成功';
		}else{
			$add_data = array();
			$add_data['name'] = $postdata['name'];
			$add_data['options'] = '';
			$add_data['type_id'] = 0;
			$add_data['status'] = 1;
			$add_data['type'] = 'checkbox';
			$add_data['is_img'] = 0;
			$ret = M('GoodsAttribute')->add($add_data);
			$return['msg'] = '添加成功';
		}
		if($ret){
			$return['status'] = 1;
		}
		return $return;
	}
	public function save_attr_options($postdata){
		$return=array('status'=>3,'msg'=>'操作失败');
		if(empty($postdata['attr_name'])){
			$return['status'] = 2;
            $return['msg'] = '参数不能为空';
			return $return;
		}
		
		$attr_id = $postdata['attr_id'];
		if($attr_id){
			$save_data = $map = array();
			$map['id'] = $attr_id;
			$map['status'] = 1;
			$options = M('GoodsAttribute')->where($map)->getField('options');
			
			$save_data['options'] = $options.','.$postdata['attr_name'];
			$ret = M('GoodsAttribute')->where($map)->save($save_data);
			if($ret){
				$return['status'] = 1;
			}
		}
		return $return;
	}
	public function package_send($uid,$package_id){
		$return=array('status'=>3,'msg'=>'');
		$goods_info = M('Goods')->field('shop_price,supply_price,contain_service,continued')->find($package_id);
		if($goods_info){
			$service_arr = json_decode($goods_info['contain_service'],true);
			foreach($service_arr as $k=>$v){
				$m_package = array();
				$m_package['uid'] = $uid;
				$m_package['order_id'] = 0;
				$m_package['service_sn'] = createOrderNo();
				$m_package['package_id'] = $package_id;
				$m_package['goods_id'] = 0;
				$m_package['goods_names'] = $k;
				$m_package['service_num'] = $v;
				$m_package['service_type'] = 0;
				$m_package['use_shop'] = 0;
				$m_package['use_num'] = 0;
				$m_package['continued'] = $goods_info['continued'];
				$m_package['addtime'] = NOW_TIME;
				$m_package['endtime'] = NOW_TIME + $goods_info['continued']*86400;
				$m_package['package_price'] = $goods_info['shop_price'];//余额
				$m_package['supply_price'] = $goods_info['supply_price'];
				$m_package['use_time'] = 0;
				M('MemberPackage')->add($m_package);
			}
			$return['status'] = 1;
		}else{
			$return['msg'] = '套餐出错';
			return $return;
		}
		return $return;
	}
	public function get_member_package($postdata){
		$return=array('status'=>3,'msg'=>'');
		$where = array();
		if(!empty($postdata['card_sn'])){
			$whereor['card_sn'] = $postdata['card_sn'];
			$whereor['mobile']= $postdata['card_sn'];
			$whereor['_logic'] = 'or';
			$where['_complex'] = $whereor;
		}else{
			$return['msg'] = '请填写手机号';
			return $return;
		}
		$user_info = M("Member")->where($where)->find();
		if(!$user_info['id']){
			$return['msg'] = '没有此会员';
			return $return;
		}else{
			$map = array();
			$map['uid'] = $user_info['id']; 
			$map['endtime'] = array("gt",NOW_TIME);
			$map['package_id'] = array("neq",0);
			if($postdata['order_id'])$map['order_id'] = $postdata['order_id'];
			$member_package = M("MemberPackage")->where($map)->field('id,package_id,order_id,uid')->group('order_id')->select();
			if($member_package){
				foreach($member_package as $k=>$v){
					$member_package[$k]['package_name'] = idtoname($v['package_id'],'Goods','goods_name','id',false);
				}unset($k,$v);
				$return['status'] = 1;
				$return['data'] = $member_package;
			}else{
				$return['msg'] = '该会员暂无计次卡';
			}
		}
		return $return;
	}
	public function get_package_service($postdata){
		$return=array('status'=>3,'msg'=>'');
		$map = array();
		$map['uid'] = $postdata['uid']; 
		$map['endtime'] = array("gt",NOW_TIME);
		$map['package_id'] = $postdata['package_id'];
		$map['order_id'] = $postdata['order_id'];
		$member_package = M("MemberPackage")->where($map)->field(true)->select();
		if($member_package){
			foreach($member_package as $k=>$v){
				$member_package[$k]['wei_num'] = $v['service_num'] - $v['use_num'];
			}
			$return['status'] = 1;
			$return['data'] = $member_package;
		}else{
			$return['msg'] = '计次卡出错，请重试';
		}
		return $return;
	}
	public function sub_hx_package($postdata){
		$return=array('status'=>3,'msg'=>'');
		if($postdata){
			foreach($postdata as $k=>$v){
				$map = array();
				$map['id'] = $k;
				$m_package = M('MemberPackage')->where($map)->find();
				$save = array();
				$save['id'] = $k;
				$save['use_num'] = $m_package['use_num'] + $v;
				$save['use_time'] = NOW_TIME;
				$res = M('MemberPackage')->save($save);
			}
			$return['status'] = 1;
			$return['msg'] = '核销成功';
		}else{
			$return['msg'] = '数据错误，请重试';
		}
		return $return;
	}
	public function getattr_price($goodsid,$attr_key){
		$goodsid=intval($goodsid);if(empty($goodsid))return false;
		$map=array();
		$map['goods_id']=$goodsid;
		if($attr_key){
			$map['attr_key']=$attr_key;
		}
		$info=M('GoodsAttrStock')->where($map)->field('attr_price')->find();
		$return['data']=$info;
		return $return;
	}
	
	/*服务开始*/
	public function service_list(){
        $return=array('status'=>3,'msg'=>'');
        $map=array();
        $map['status']=1;
        $map['goods_type']=4;
		$field = 'id,goods_name,shop_price,img,shop_ids,service_spec,simple_description';
        $pagesize=empty($pagesize)?C('WEB_ARTICLE_PAGE'):$pagesize;
        $info=page_list('Goods',$map,'id desc',$field,$pagesize,'',1);
        if($info['list']){
			// foreach($info['list'] as $k=>$v){
				// $info['list'][$k]['contain_service'] = json_decode($v['contain_service'],true);
			// }
			$return['status']=1; 
			$return['data'] = $info['list'];
		}else{$return=array('status'=>2,'msg'=>'暂无数据');}
        return $return;
    }
	public function service_info($goods_id){
		$result = $data = array();
		$result['status'] = 1;
		if(empty($goods_id)){
			$result['status'] = 3;
			$result['msg']='服务ID不能为空';
			return $result;
		}
		$map = array();
		$map['id'] = $goods_id;
		$map['goods_type'] = 4;
		$map['status'] = 1;
		$field = 'id,cat_id,goods_name,img,goods_sn,supply_price,shop_price,market_price,shop_ids,service_tostore,service_tohome,simple_description';
		$data = M('Goods')->where($map)->field($field)->find();
		if(empty($data)){
			$result['status'] = 3;
			$result['msg']='该服务已下架或不存在';
		}else{
			$map = $cate_map = array();
			$map['goods_id'] = $goods_id;
			$cate_map['id'] = $data['cat_id'];
			$data['cate_name'] = M('ServiceCategory')->where($cate_map)->getField('name');
			$data['content'] = M('GoodsContent')->where($map)->getField('content',true);
			$result['data'] = $data;
		}
		return $result;
	}
	public function service_cate_list(){
		$result = $data = array();
		$result['status'] = 1;
		$map = array();
		$map['status'] = 1;
		$data = M('ServiceCategory')->where($map)->field('id,name')->select();
		if(empty($data)){
			$result['status'] = 2;
			$result['msg']='没有该服务分类';
		}else{
			$result['data'] = $data;
		}
		return $result;
	}
	//添加或编辑分类
	public function save_service_category($postdata){
		$return=array('status'=>3,'msg'=>'操作失败');
		if(empty($postdata)){
			$return['status'] = 3;
            $return['msg'] = '参数不能为空';
			return $return;
		}
		$cat_id = $postdata['id'];
		$save_data = $map = array();
		$save_data['name'] = $save_data['seotitle'] = $save_data['seokeywords'] = $save_data['seodescription'] = $postdata['name'];
		if($cat_id){
			$map['id'] = $cat_id;
			$map['status'] = 1;
			$ret = M('ServiceCategory')->where($map)->save($save_data);
			savecache('ServiceCategory');
			$return['msg'] = '编辑成功';
		}else{
			$save_data['status'] = 1;
			$ret = M('ServiceCategory')->add($save_data);
			savecache('ServiceCategory');
			$return['msg'] = '添加成功';
		}
		if($ret){
			$return['status'] = 1;
		}
		return $return;
	}
	// 编辑服务
    public function service_edit($postdata){
        $return = $sa_data = array();
        $return['status'] = 1;
		if(empty($postdata)){
            $return['status'] = 3;
            $return['msg'] = '参数不能为空';
            return $return;
        }
        $model = M("Goods");
        $check_data = $this->check_post($postdata);
		
        if($check_data['status'] == 0){
			$return['status'] = 3;
            $return['msg'] = $check_data['msg'];
            return $return;
        }
		$postdata['goods_type'] = 4;
		$postdata['publish_wait'] = 1;
		$postdata['status'] = 1;
		$postdata['updatetime'] = NOW_TIME;
		$postdata['addtime'] = NOW_TIME;
		
		$map_sn = array();
		if($postdata['goods_id']){
			$map_sn['id'] = array('neq',$postdata['goods_id']);
		}
		if($postdata['goods_sn']){
			$map_sn['goods_sn'] = $postdata['goods_sn'];
			$g_count = $model->where($map_sn)->count();
			if($g_count){
				$return['status'] = 3;
				$return['msg']='服务编号重复,请重新填写';
				return $return;
			}
		}
		$map_id = array();
		$map_id['id'] = $postdata['goods_id'];
        $ret = $model->where($map_id)->save($postdata);
		$this->update_goods_sn($postdata,1);//更新货号
        if ($ret) {
			$return['status'] = 1;
			$return['msg'] = '编辑成功';
			
			if($postdata['content']){
				$map = array();$map['goods_id'] = $postdata['goods_id'];
				$content = array();$content['content'] = $postdata['content'];
				M('GoodsContent')->where($map)->save($content);
			}
        } else {
			$return['status'] = 3;
			$return['msg'] = '编辑失败';
        }
        return $return;
    }
	// 添加服务
    public function service_add($postdata){
        $return = $sa_data = array();
        $return['status'] = 1;
		if(empty($postdata)){
            $return['status'] = 3;
            $return['msg'] = '参数不能为空';
            return $return;
        }
        $model = M("Goods");
		
		$map_sn = array();
		if($postdata['goods_sn']){
			$map_sn['goods_sn'] = $postdata['goods_sn'];
			$g_count = $model->where($map_sn)->count();
			if($g_count){
				$return['status'] = 3;
				$return['msg']='编号不能重复,请重新填写';
				return $return;
			}
		}
		
		$postdata['status'] = 1;
		$postdata['goods_type'] = 4;
		$postdata['publish_wait'] = 1;
		$postdata['updatetime'] = NOW_TIME;
        $postdata = $this->format_post($postdata);
		
		$goods_id = $model->add($postdata);
		
		$postdata['id'] = $goods_id;
		$postdata['goods_id'] = $goods_id;
		$this->update_goods_sn($postdata,1);//更新货号
		
        if ($goods_id) {
			$return['status'] = 1;
			$return['goods_id'] = $goods_id;
			$return['msg'] = '添加成功';
			
			if($postdata['content']){
				$content = array();$content['goods_id'] = $goods_id;
				$content['content'] = $postdata['content'];
				M('GoodsContent')->add($content);
			}
        } else {
			$return['status'] = 3;
			$return['msg'] = '添加失败';
        }
        return $return;
    }
}
?>