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

namespace Wxsmall\Model;
use Think\Model;
class CouponsModel extends Model{
    /* 用户模型自动完成 */
    protected $_auto = array(
        array('reg_ip', 'get_client_ip', self::MODEL_INSERT, 'function', 1),
        array('reg_time', NOW_TIME, self::MODEL_INSERT),
        array('last_ip', 0, self::MODEL_INSERT),
        array('last_login', 0, self::MODEL_INSERT),
        array('status', 1, self::MODEL_INSERT),
        array('rank_id', 1, self::MODEL_INSERT),
    );
    public function get_cpupon_color(){
        $color_array=array(
            'Color010'=>'c-1',
            'Color020'=>'c-2',
            'Color030'=>'c-3',
            'Color040'=>'c-4',
            'Color050'=>'c-5',
            'Color060'=>'c-6',
            'Color070'=>'c-7',
            'Color080'=>'c-8',
            'Color090'=>'c-9',
            'Color100'=>'c-10',
            'Color101'=>'c-11',
        );
        return $color_array;
    }
    /**
     * 获取优惠券内容
     */
    public function get_coupon_cont($id){
        $return=array('status'=>3,'msg'=>'');
        if(empty($id)){$return['msg']='参数错误';return $return;}
        $map=array('id'=>$id);
        $info=M('Coupons')->where($map)->field(true)->find();
        if(empty($info)){$return['msg']='没有优惠券';return $return;}
        if($info['type']==1){
            $info['start_time']=time_format($info['begin_timestamp']);
            $info['end_time']='至'.time_format($info['end_timestamp']);
        }else{
            if(empty($val['fixed_begin_term'])){
                $info['start_time']='领取后立即生效';
            }else {
                $info['start_time']='领取后'.$info['fixed_term'].'天生效';
            }
            $info['end_time']=',有效期'.$info['fixed_begin_term'].'天';
        }
        if($info['rank_id']=='CASH'){
            $info['money']=intval($info['reduce_cost']);
        }elseif($info['rank_id']=='DISCOUNT'){
            $money=(100-$info['discount'])/10;
            $info['money']=$money.'折';
        }
        if($info['is_show']==0){
            $info['shop_name']='全部门店可用';
        }else{
            $map=array();
            $ids=array_filter(explode(',',$info['is_show']));
            $map['id']=array('in',$ids);
            $shop_name=M("Shops")->where($map)->getField('name',true);
            $info['shop_name']=implode(',',$shop_name);
        }
        $info['least_cost']=intval($info['least_cost']);
        $color=D("Wxsmall/Coupons")->get_cpupon_color();
        $info['color']=$color[$info['color']];
        $info['class']='flexbox voucher_huang '.$info['color'];// app 需要
        // $info['status_cont']='免费领取';
        // $info['status_type']=11;
        if($info['buy_type']==0){
            $info['status_cont']="免费领取";
            $info['status_type']=11;
        }elseif($info['buy_type']==1){
            $info['status_cont']="积分兑换";
            $info['status_type']=12;
        }
        $memberinfo = checkuserauthtype();
        $where['uid'] = $memberinfo['id'];
        $where['coupons_id'] = $info['id'];
        $where['status'] = 0;
        $coupons_get = M('CouponsGet')->where($where)->count('id');
        $info['coupons_get'] = 1;//未领取
        if(!$coupons_get)$info['coupons_get'] = 0;//已领取
        // --2-27 -- zjn
        $share_url=geturl('Mobile/Index/kaquan_cont',array('id'=>$id,'eid'=>$memberinfo['id']),true,true);
        $info['share_url']=$share_url;
        // --2-27 -- zjn  end
        $return['status']=1;
        $return['data']=$info;
        return $return;
    }
    /**
     * 领取优惠券
     */
    public function get_coupon_wx($param){
        $return = array('status'=>2,'msg'=>'缺少必要参数');
        if(empty($param['id'])){
            return $return;
        }
        $time = NOW_TIME;
        $coupons_id = $param['id'];
        $uid = $param['uid'];
        $map = array();
        $map['id'] = $coupons_id;
        $coupons = M('Coupons')->where($map)->field(true)->find();
        $map = array();
        $map['coupons_id'] = $coupons_id;
        $map['uid'] = $uid;
        $get_coupons = M('CouponsGet')->where($map)->count();
        if($coupons['quantity']<=0){
            $return['status'] = 2;
            $return['msg'] = '库存不足';
            return $return;
        }else if($get_coupons >= $coupons['get_limit']){
            $return['status'] = 2;
            $return['msg'] = '领取次数到达上限';
            return $return;
        }elseif ($time > $coupons['end_timestamp'] && $coupons['end_timestamp']) {
            $return['status'] = 2;
            $return['msg'] = '优惠券已过期';
            return $return;
        }
        if(!$param['from'])$from=1;
        // 3-25 zjn
        $map=array();
        $map['id']=$uid;
        $member_info=M("Member")->where($map)->field(true)->find();
        if($coupons['buy_type']==1) {
            if ($coupons['buy_money'] > $member_info['usepoints']) {
                $return['msg'] = '积分不足，不能购买';
                $return['status'] = 2;
                return $return;
            }
            $from=5;
            if(empty($from_temp))$from_temp='积分兑换';
        }
        if($from==1 && empty($from_temp))$from_temp='免费领取';
        // ---------end---------
        $add_data['coupons_id'] = $coupons['id'];
        $add_data['uid'] = $uid;
        $add_data['card_id'] = $coupons['card_id'];
        $add_data['rank_id'] = $coupons['rank_id'];
        $add_data['discount'] = $coupons['discount'];
        $add_data['reduce_cost'] = $coupons['reduce_cost'];
        $add_data['least_cost'] = $coupons['least_cost'];
        if($coupons['type'] == '1'){//固定日期
            $add_data['begin_timestamp'] = $coupons['begin_timestamp'];
            $add_data['end_timestamp'] = $coupons['end_timestamp'];
        }else{//固定时长
            $add_data['begin_timestamp'] = $time+$coupons['fixed_begin_term']*24*60*60;
            $tian = $coupons['fixed_term']+$coupons['fixed_begin_term'];
            $add_data['end_timestamp'] = $time+$tian*24*60*60;
        }
        $add_data['add_time'] = $time;
        $add_data['from'] = $from;
        $add_data['from_temp'] = $from_temp;
        $add_data['buy_money'] = $coupons['buy_money'];
        $add_data['buy_type'] = $coupons['buy_type'];
        $add = M('CouponsGet')->add($add_data);
        if($add){
            if($from!=2) {//转赠的券不扣积分和库存
                switch ($coupons['buy_type']) {
                    case 1:
                        $msg = '使用' . floatval($coupons['buy_money']) . '积分兑换卡券';
                        account_change($uid, -$coupons['buy_money'], 4, 4, $msg);
                        break;
                }
            }
        }
        //微信卡券更新
        $Weixin = new \Common\Vendor\WeixinCoupon();
        $map = array();
        $map['id'] = $coupons_id;
        $card_id = M('Coupons')->where($map)->getfield('card_id');
        $AddCartParam = $Weixin->AddCartParam($card_id);
        if($AddCartParam){
            $return['data']['cardId'] = $AddCartParam['cardId'];
            $return['data']['cardExt'] = json_encode($AddCartParam['cardExt']);
//            if(empty($AddCartParam['cardId'])){
//                $add = M('CouponsGet')->add($add_data);
//            }
        }
        //微信卡券更新end
        $return['status'] = 1;
        $return['data']['add_id'] = $add;
        return $return;
    }
    /**
     * 格式化优惠券
     */
    public function format_coupon($list){
        $return = array('status'=>2,'msg'=>'缺少必要参数');
        if(!$list){
            return $return;
        }
        foreach($list as $key=>$val){
            $list[$key]['rank_type']=$this->get_coupon_type($val['rank_id']);
            if($val['type']==1){
                $list[$key]['start_time']=time_format($val['begin_timestamp']);
                $list[$key]['end_time']='至'.time_format($val['end_timestamp']);
            }else{
                if(empty($val['fixed_begin_term'])){
                    $list[$key]['start_time']='领取后立即生效';
                }else {
                    $list[$key]['start_time'] = '领取后' . $val['fixed_begin_term'] . '天生效';
                }
                $list[$key]['end_time']=',有效期'.$val['fixed_term'].'天';
            }
            if($val['rank_id']=='CASH'){
                $list[$key]['money']=intval($val['reduce_cost']);
            }elseif($val['rank_id']=='DISCOUNT'){
                $money=(100-$val['discount'])/10;
                $list[$key]['money']=$money.'折';
            }
            if($val['is_show']==0){
                $list[$key]['shop_name']='全部门店可用';
            }else{
                $map=array();
                $ids=array_filter(explode(',',$val['is_show']));
                $map['id']=array('in',$ids);
                $shop_name=M("Shops")->where($map)->getField('name',true);
                $list[$key]['shop_name']=implode(',',$shop_name);
            }
            $list[$key]['least_cost']=intval($val['least_cost']);
            $color=$this->get_cpupon_color();
            $list[$key]['color']=$color[$val['color']];
            $list[$key]['class']='flexbox voucher_huang '.$color[$val['color']];// app 需要
            if($val['buy_type']==0){
                $list[$key]['status_cont']="免费领取";
                $list[$key]['status_type']=11;
            }elseif($val['buy_type']==1){
                $list[$key]['status_cont']="积分兑换";
                $list[$key]['status_type']=12;
            }
            // elseif($val['buy_type']==2){
            //     $list[$key]['status_cont']="现金购买";
            // }
            // $list[$key]['status_cont']="免费领取";
            // $list[$key]['status_type']=11;
            // if($val['buy_type']>0){
            //     unset($list[$key]);
            // }
        }
        $return['data']=$list;
        return $return;
    }
    /**获取卡券类型
     * @param $type rank_id
     * @return bool|string
     */
    private function get_coupon_type($type){
        if(empty($type))return false;
        switch($type){
            case 'GENERAL_COUPON':
                return '优惠券';
            break;
            case 'DISCOUNT':
                return '折扣券';
            break;
            case 'GIFT':
                return '礼品券';
            break;
            case 'CASH':
                return '代金券';
            break;
            case 'GROUPON':
                return '团购券';
            break;
        }
    }
    
}