<?php
/** App商品控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author ziyi <ziyi@206207038.com>
 */

namespace Pad\Controller;
use Common\Controller\HanchaoController;
class GoodsController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
	public function goods_list(){
		$post = I('post.');
		$return = D('App/Goods')->category_goods_list($post);
        $this->ajaxReturn($return);
	}
	public function get_cate(){
		$result=array();
		$return=D('App/Goods')->getcateTree('',1,5);
		if($return){
			$result['status'] = 1;
			$result['data'] = $return;
		}else{
			$result['status'] = 2;
			$result['msg'] = '暂无分类！';
		}
		$this->ajaxReturn($result);
	}
	public function category_lists(){
		$result=array('status'=>3,'msg'=>'');
		$result=D('App/Goods')->category_lists();
		$this->ajaxReturn($result);
	}
	public function unit_lists(){
		$result=array('status'=>3,'msg'=>'');
		$result=D('App/Goods')->unit_lists();
		$this->ajaxReturn($result);
	}
	/**
     * 自定义属性列表
     */
	public function attr_lists(){
		$result = array('status'=>3,'msg'=>'');
		$map = array();
		$map['status'] = 1;
		$list = M('GoodsAttribute')->where($map)->field('id,name,options')->select();
		if(empty($list)){
			$result['status'] = 2;
			$result['msg']='没有属性信息';
		}else{
			foreach($list as $k => $v){
				// $options = substr($v['options'], 0, -1);
				$list[$k]['options'] = explode(',',$v['options']);
			}
			$result['status']=1;
			$result['data'] = $list;
		}
		$this->ajaxReturn($result);
    }
	public function goods_info(){
		$result = array('status'=>3,'msg'=>'');
		
		$postdata=I('param.');
		$auth_code = 'goodslist_edit';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		$goods_id = I('goods_id');
		$shop_id = I('shop_id');
		if(empty($goods_id)){
			$result['status'] = 2;
			$result['msg']='商品ID不能为空';
			$this->ajaxReturn($result);
		}
		$result = D('App/Goods')->goods_info($goods_id,$shop_id);
		$this->ajaxReturn($result);
	}
	//添加或编辑商品
	public function save_goods(){
		$result=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		
		if(isset($postdata['goods_id'])){
			$auth_code = 'goodslist_edit';
		}else{
			$auth_code = 'goodslist_add';
		}
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		if(empty($postdata)){
			$result['status'] = 3;
			$result['msg']='参数不能为空';
			$this->ajaxReturn($result);
		}
		// if($postdata['is_root'] != 1){
			// $result['msg']='没有该权限';
			// $this->ajaxReturn($result);
		// }
		if(isset($postdata['goods_id'])){
			$result=D('App/Goods')->goods_edit($postdata);
		}else{
			// $result['status'] = 1;
			// $result['data'] = $postdata;
			// $this->ajaxReturn($result);
			
			$result=D('App/Goods')->goods_add($postdata);
			
		}
		$this->ajaxReturn($result);
    }
	// 删除商品
	public function delete_goods(){
		$result=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if($postdata['is_root'] != 1){
			$result['msg']='没有该权限';
			$this->ajaxReturn($result);
		}
		if($postdata['ids'] == ''){
			$result['status'] = 2;
            $result['data'] = '参数不能为空';
		}
        $result = D('App/Goods')->delete_goods($postdata['ids']);
        $this->ajaxReturn($result);
	}
	//删除分类
	public function delete_category(){
		$result=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if($postdata['is_root'] != 1){
			$result['msg']='没有该权限';
			$this->ajaxReturn($result);
		}
		if(empty($postdata['cat_id'])){
			$result['status'] = 2;
            $result['data'] = '参数不能为空';
		}
        $result = D('App/Goods')->delete_category($postdata['cat_id']);
        $this->ajaxReturn($result);
	}
	//获取分类信息
	public function category_info(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if($postdata['is_root'] != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		if(empty($postdata['cat_id'])){
			$return['msg']='参数不能为空';
			$this->ajaxReturn($return);
		}
		$return = D('App/Goods')->category_info($postdata['cat_id']);
		$this->ajaxReturn($return);
	}
	//添加或编辑分类
	public function save_category(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		
		$auth_code = 'goodslist_add';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		if($postdata['is_root'] != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('App/Goods')->save_category($postdata);
		$this->ajaxReturn($return);
	}
	//获取属性信息
	public function attr_info(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if($postdata['is_root'] != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		if(empty($postdata['attr_id'])){
			$return['msg']='参数不能为空';
			$this->ajaxReturn($return);
		}
		$return = D('App/Goods')->attr_info($postdata['attr_id']);
		$this->ajaxReturn($return);
	}
	//添加或编辑属性
	public function save_attr(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if($postdata['is_root'] != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('App/Goods')->save_attr($postdata);
		$this->ajaxReturn($return);
	}
	//添加属性值
	public function save_attr_options(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		// if($postdata['is_root'] != 1){
			// $return['msg']='您没有操作权限';
			// $this->ajaxReturn($return);
		// }
		
		$return = D('App/Goods')->save_attr_options($postdata);
		$this->ajaxReturn($return);
	}
	//选中的属性值
	public function set_all_options(){
		$return = D('App/Goods')->add_attr();
		$this->ajaxReturn($return);
	}
	public function package_add(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if($postdata['is_root'] != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		unset($postdata['is_root'],$postdata['root_id'],$postdata['root_token'],$postdata['appid']);
		// $return['data'] = htmlspecialchars_decode($postdata['services_name']);
		// $return['data'] = $postdata['services_name'];
		// $return['data'] = json_decode(htmlspecialchars_decode($postdata['services_name']),true);
		// $this->ajaxReturn($return);
		$return = D('App/Goods')->package_add($postdata);
		$this->ajaxReturn($return);
	}
	public function package_list(){
        $return = D('App/Goods')->package_list();
        $this->ajaxReturn($return);
	}
	public function get_package(){
		$return=array('status'=>3,'msg'=>'');
        $id=I('id');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('App/Goods')->get_package($id);
        $this->ajaxReturn($return);
	}
	public function package_send(){
		$return=array('status'=>3,'msg'=>'');
        $uid=I('uid');
        $package_id=I('package_id');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		if(!$uid||!$package_id){
			$return['msg']='参数错误';
			$this->ajaxReturn($return);
		}
		// $return['data'] = $uid.'++'.$package_id;
		// $this->ajaxReturn($return);
		$return = D('App/Goods')->package_send($uid,$package_id);
        $this->ajaxReturn($return);
	}
	public function get_member_package(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I("post.");
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('App/Goods')->get_member_package($postdata);
		$this->ajaxReturn($return);
	}
	public function get_package_service(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I("post.");
		$return = D('App/Goods')->get_package_service($postdata);
		$this->ajaxReturn($return);
	}
	public function sub_hx_package(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I("post.");
		$return = D('App/Goods')->sub_hx_package($postdata);
		$this->ajaxReturn($return);
	}
	// 核销开始
	// 搜索服务核销码
	public function select_hx(){
		$result = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_service';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		if(empty($postdata)){
			$result['status'] = 3;
			$result['msg']='参数不能为空';
			$this->ajaxReturn($result);
		}
		$return = D('Common/Shops')->select_hx($postdata);
		if($return['status'] == 1){
			$result['status'] = 1;
			$result['data'] = $return['data'];
		}else{
			$result['msg'] = $return['info'];
		}
		$this->ajaxReturn($result);
	}
	// 实现服务核销码核销
	public function service_hx(){
		$result = $return = array();
		
		$postdata = I("param.");
		$auth_code = 'hexiao_service';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		if(empty($postdata)){
			$result['status'] = 3;
			$result['msg']='参数不能为空';
			$this->ajaxReturn($result);
		}
		$return = D('Common/Shops')->service_hx($postdata);
		if($return['status'] == 1){
			$result['status'] = 1;
		}else{
			$result['status'] = 3;
		}
		$result['msg'] = $return['info'];
		$this->ajaxReturn($result);
	}
	/**
     * 提货码查询
     */
	public function select_thm(){
		$result = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_thm';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		$return = D('Common/Shops')->select_thm($postdata);
		if($return['status'] == 1){
			$result = $return;
		}else{
			$result['msg'] = $return['info'];
		}
		$this->ajaxReturn($result);
	}
    /**
     * 实现提货码核销
     */
    public function check_goods(){
		$result = $data = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_thm';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $data['location_id']=I('shop_id');
        $data['sn_number']=I('sn_number');
        $return=D("Common/Shops")->check_goods($data);
		if($return['status'] == 1){
			$result['status'] = 1;
		}else{
			$result['status'] = 3;
		}
		$result['msg'] = $return['msg'];
        $this->ajaxReturn($result);
    }
	/**
     * 礼品码查询
     */
	public function select_gift(){
		$result = array();$result['status'] = 3;
		$postdata = I("param.");
		
		$auth_code = 'hexiao_gift';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		$return = D('Common/Shops')->select_win_gift($postdata);
		if($return['status'] == 1){
			$result = $return;
		}else{
			$result['msg'] = $return['info'];
		}
		$this->ajaxReturn($result);
	}
    /**
     * 实现礼品码核销
     */
    public function check_gift(){
		$result = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_gift';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $data['sn_number']=I('sn_number');
        $data['user_name']=I('hx_name');
        $data['location_id']=I('shop_id');
        $return=D("Common/Shops")->check_win_gift($data);
		if($return['status'] == 1){
			$result['status'] = 1;
		}else{
			$result['status'] = 3;
		}
		$result['msg'] = $return['msg'];
        $this->ajaxReturn($result);
    }
	// 核销结束
	
	// 核销记录开始
	// 服务核销记录
	public function service_jilu(){
		$result = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_service';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $shop_id=I('shop_id');
        $return=D("Common/Shops")->check_service_jilu($shop_id);
		if($return['status'] == 1){
			$result['status'] = 1;
			$result['data'] = $return['data'];
		}else{
			$result['status'] = 3;
			$result['msg'] = $return['msg'];
		}
        $this->ajaxReturn($result);
	}
	// 服务核销记录内容页
	public function service_jilu_cont(){
		$result = $data = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_service';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		if($postdata['hx_id']){
			$map = array();$map['id'] = $postdata['hx_id'];
			$data['info'] = M('MemberPackage')->where($map)->field('order_id,hx_name')->find();
			$map = array();$map['id'] = $data['info']['order_id'];$map['type'] = 11;
			$data['order_info'] = M('OrderInfo')->where($map)->field(true)->find();
			$data['order_info']['user_name'] = show_user_name($data['order_info']['uid']);
			$data['order_info']['pay_time'] = time_format($data['order_info']['pay_time']);
			$map = array();$map['order_id'] = $data['order_info']['id'];
			$data['order_goods'] = M('OrderGoods')->where($map)->field(true)->select();
			
			if($data['info']){
				$result['status'] = 1;
				$result['data'] = $data;
			}else{
				$result['status'] = 3;
				$result['msg'] = '查询失败';
			}
		}else{
			$result['status'] = 3;
			$result['msg'] = '参数错误';
		}
        $this->ajaxReturn($result);
	}
	// 提货码核销记录
	public function thm_jilu(){
		$result = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_thm';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $shop_id=I('shop_id');
        $return=D("Common/Shops")->check_thm_jilu($shop_id);
		if($return['status'] == 1){
			$result['status'] = 1;
			$result['data'] = $return['data'];
		}else{
			$result['status'] = 3;
			$result['msg'] = $return['msg'];
		}
        $this->ajaxReturn($result);
	}
	// 提货码核销记录内容页
	public function thm_jilu_cont(){
		$result = $data = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_thm';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		if($postdata['hx_id']){
			$map = array();$map['id'] = $postdata['hx_id'];$map['send_type'] = 1;$map['check_status'] = 1;
			$data['order_info'] = M('OrderInfo')->where($map)->field(true)->find();
			$data['order_info']['user_name'] = show_user_name($data['order_info']['uid']);
			$data['order_info']['pay_time'] = time_format($data['order_info']['pay_time']);
			$map = array();$map['order_id'] = $data['order_info']['id'];
			$data['order_goods'] = M('OrderGoods')->where($map)->field(true)->select();
			
			if($data){
				$result['status'] = 1;
				$result['data'] = $data;
			}else{
				$result['status'] = 3;
				$result['msg'] = '查询失败';
			}
		}else{
			$result['status'] = 3;
			$result['msg'] = '参数错误';
		}
        $this->ajaxReturn($result);
	}
	// 礼品码核销记录
	public function gift_jilu(){
		$result = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_gift';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $shop_id=I('shop_id');
        $return=D("Common/Shops")->check_gift_jilu($shop_id);
		if($return['status'] == 1){
			$result['status'] = 1;
			$result['data'] = $return['data'];
		}else{
			$result['status'] = 3;
			$result['msg'] = $return['msg'];
		}
        $this->ajaxReturn($result);
	}
	// 礼品码核销记录内容页
	public function gift_jilu_cont(){
		$result = $data = array();$result['status'] = 3;
		
		$postdata = I("param.");
		$auth_code = 'hexiao_gift';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		if($postdata['hx_id']){
			$map = array();$map['id'] = $postdata['hx_id'];
			$data['order_info'] = M('WinGift')->where($map)->field(true)->find();
			$data['order_info']['user_name'] = show_user_name($data['order_info']['user_id']);
			$data['order_info']['hx_time'] = time_format($data['order_info']['check_time']);
			$map = array();$map['id'] = $data['order_info']['gift_id'];
			$data['prize_goods'] = M('Prize')->where($map)->field(true)->find();
			
			if($data){
				$result['status'] = 1;
				$result['data'] = $data;
			}else{
				$result['status'] = 3;
				$result['msg'] = '查询失败';
			}
		}else{
			$result['status'] = 3;
			$result['msg'] = '参数错误';
		}
        $this->ajaxReturn($result);
	}
	// 核销记录结束
}
?>