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

namespace App\Controller;
use Think\Controller;
class GoodsController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
	public function goods_list(){
		$post = I('post.');
        $return = D('Goods')->category_goods_list($post);
        $this->ajaxReturn($return);
	}
	public function get_cate(){
		$result=array();
		$return=D('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'=>'');
		$auth_code='goodslist_info';
        $result=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$post);
        if($result['status']==1) {$result=D('Goods')->category_lists();}
		$this->ajaxReturn($result);
	}
	public function goods_lists(){
		$result=array('status'=>3,'msg'=>'');
		$post = I('post.');
		$cate_id = I('cate_id');
		if(empty($cate_id)){
			$result['msg'] = '参数错误！';
		}else{
			$return=D('Goods')->goods_lists($post);
			if($return['status'] == 1){$result['status']=1; $result = $return;}
			else{$result=array('status'=>3,'msg'=>$return['msg']);}
		}
		$this->ajaxReturn($result);
	}
	public function unit_lists(){
		$result=array('status'=>3,'msg'=>'');
		$result=D('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'=>'');
		$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('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'] = 2;
			$result['msg']='参数不能为空';
			$this->ajaxReturn($result);
		}
		if(isset($postdata['goods_id'])){
			$result=D('Goods')->goods_edit($postdata);
		}else{
			$result=D('Goods')->goods_add($postdata);
		}
		$this->ajaxReturn($result);
    }
	// 删除商品
	public function delete_goods(){
		$result=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		$auth_code='goodslist_del';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$post);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}

		if($postdata['ids'] == ''){
			$result['status'] = 2;
            $result['data'] = '参数不能为空';
		}
        $result = D('Goods')->delete_goods($postdata['ids']);
        $this->ajaxReturn($result);
	}
	//删除分类
	public function delete_category(){
		$result=array('status'=>3,'msg'=>'');
		$postdata = I('post.');

		if(empty($postdata['cat_id'])){
			$result['status'] = 2;
            $result['data'] = '参数不能为空';
		}
        $result = D('Goods')->delete_category($postdata['cat_id']);
        $this->ajaxReturn($result);
	}
	//获取分类信息
	public function category_info(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');

		if(empty($postdata['cat_id'])){
			$return['msg']='参数不能为空';
			$this->ajaxReturn($return);
		}
		$return = D('Goods')->category_info($postdata['cat_id']);
		$this->ajaxReturn($return);
	}
	//添加或编辑分类
	public function save_category(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		$return = D('Goods')->save_category($postdata);
		$this->ajaxReturn($return);
	}
	//获取属性信息
	public function attr_info(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if(empty($postdata['attr_id'])){
			$return['msg']='参数不能为空';
			$this->ajaxReturn($return);
		}
		$return = D('Goods')->attr_info($postdata['attr_id']);
		$this->ajaxReturn($return);
	}
	//添加或编辑属性
	public function save_attr(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');

		$return = D('Goods')->save_attr($postdata);
		$this->ajaxReturn($return);
	}
	//添加属性值
	public function save_attr_options(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');

		$return = D('Goods')->save_attr_options($postdata);
		$this->ajaxReturn($return);
	}
	//选中的属性值
	public function set_all_options(){
		$return = D('Goods')->add_attr();
		$this->ajaxReturn($return);
	}
	public function package_add(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');

		// $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('Goods')->package_add($postdata);
		$this->ajaxReturn($return);
	}
	public function package_list(){
        $return = D('Goods')->package_list();
        $this->ajaxReturn($return);
	}
	public function get_package(){
		$return=array('status'=>3,'msg'=>'');
        $id=I('id');
		$return = D('Goods')->get_package($id);
        $this->ajaxReturn($return);
	}
	public function package_send(){
		$return=array('status'=>3,'msg'=>'');
        $uid=I('uid');
        $package_id=I('package_id');
		if(!$uid||!$package_id){
			$return['msg']='参数错误';
			$this->ajaxReturn($return);
		}
		// $return['data'] = $uid.'++'.$package_id;
		// $this->ajaxReturn($return);
		$return = D('Goods')->package_send($uid,$package_id);
        $this->ajaxReturn($return);
	}
	public function get_member_package(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I("post.");
		$return = D('Goods')->get_member_package($postdata);
		$this->ajaxReturn($return);
	}
	public function get_package_service(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I("post.");
		$return = D('Goods')->get_package_service($postdata);
		$this->ajaxReturn($return);
	}
	public function sub_hx_package(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I("post.");
		$return = D('Goods')->sub_hx_package($postdata);
		$this->ajaxReturn($return);
	}
	
	/*服务开始*/
	public function service_list(){
		$postdata = I("param.");
		$auth_code = 'service_list';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $return = D('App/Goods')->service_list();
        $this->ajaxReturn($return);
	}
	public function service_info(){
		$result = array('status'=>3,'msg'=>'');
		$goods_id = I('service_id');
		if(empty($goods_id)){
			$result['status'] = 3;
			$result['msg']='服务ID不能为空';
			$this->ajaxReturn($result);
		}
		$result = D('App/Goods')->service_info($goods_id);
		$this->ajaxReturn($result);
	}
	public function service_cate_list(){
        $return = D('App/Goods')->service_cate_list();
        $this->ajaxReturn($return);
	}
	//添加或编辑分类
	public function save_service_category(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		$return = D('App/Goods')->save_service_category($postdata);
		$this->ajaxReturn($return);
	}
	//添加或编辑服务
	public function save_service(){
		$result=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if(empty($postdata)){
			$result['status'] = 3;
			$result['msg']='参数不能为空';
			$this->ajaxReturn($result);
		}
		
		if(isset($postdata['goods_id'])){
			$auth_code = 'service_edit';
		}else{
			$auth_code = 'service_add';
		}
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		if(isset($postdata['goods_id'])){
			$result=D('Goods')->service_edit($postdata);
		}else{
			$result=D('Goods')->service_add($postdata);
		}
		$this->ajaxReturn($result);
    }
	// 搜索服务核销码
	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 = 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 = 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 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 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);
	}
}
?>