<?php
/**
 * 营销活动控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */
namespace Uapp\Controller;
use Think\Controller;
class PromotActController extends BaseController
{
    public function _initialize(){
        parent::_initialize ();
    }
    //活动列表
    public function index()
    {
        $result=array('status'=>1,'msg'=>'');
        $shop_id=get_wxsmall_shop_id();
        $map = array();
        $map['start_time'] = array('lt', NOW_TIME);
        $map['end_time'] = array('gt', NOW_TIME);
        $map['act_type'] = array('in', array(1,2,3,4));
        $map['is_pt'] = 0;//排除拼团
        $map['is_limit_time'] = 0;//限时促销
        $map['is_check'] = array("elt",1);//审核过
        $map['_string'] = 'shop_id = '.$shop_id.' or shop_id = 0';
        $pagesize=empty($pagesize)?C('WEB_ACTIVE_PAGE'):$pagesize;
        $list = page_list('PromotAct', $map, 'sort desc', '', $pagesize, '', 1);
        $activity_list=array();
        if($list) {
            foreach ($list['list'] as $k => $v) {
                if ($v['act_type'] == 1) {
                    $activity_list[$k]['title_name'] = '满' . $v['min_amount'] . '减' . $v['act_type_ext'];
                } elseif ($v['act_type'] == 2) {
                    $activity_list[$k]['title_name'] = '满' . $v['min_amount'] . '享' . ($v['act_type_ext'] / 10) . '折';
                } else {
                    $activity_list[$k]['title_name'] = $v['act_name'];
                }
                $img=format_url($v['img']);
                $img=str_replace('https','http',$img);
                $activity_list[$k]['img']=$img;
                $activity_list[$k]['id']=$v['id'];
                $activity_list[$k]['end_time']=$v['end_time'];
                $activity_list[$k]['is_online']=$v['is_online'];
                $activity_list[$k]['is_offline']=$v['is_offline'];
            }
            if($activity_list) {
                $result['data'] = $activity_list;
            }else{
                $result['status']=2;
                $result['msg']='未获取到数据';
            }
        }else{
            $result['status']=2;
            $result['msg']='未获取到数据';
        }
        $this->ajaxReturn($result);
    }

    //活动内容
    public function cont()
    {
        $result=array('status'=>1,'msg'=>'');
        $getdata = I('get.');
        $id = I('id');
        $cart_id=I('cart_id');
        $actinfo = M('PromotAct')->where('id=' . $id)->field('id,end_time,act_type,min_amount,act_type_ext,big_img,act_name,is_online,is_offline')->find();
        if($actinfo['end_time']<NOW_TIME){//活动过期
             $result['data']['info']['overdue']=1;
             $this->ajaxReturn($result);
        }
        if ($actinfo['act_type'] == 1) {
            $actinfo['title_name'] = '满' . $actinfo['min_amount'] . '减' . $actinfo['act_type_ext'];
        } elseif ($actinfo['act_type'] == 2) {
            $actinfo['title_name'] = '满' . $actinfo['min_amount'] . '享' . ($actinfo['act_type_ext'] / 10) . '折';
        } else {
            $actinfo['title_name'] = $actinfo['act_name'];
        }
        $img=format_url($actinfo['big_img']);
        $img=str_replace('https','http',$img);
        $actinfo['big_img']=$img;
        //活动下的商品
        $data = D('Common/Promot')->get_active_goods($getdata);
        $result['data']['info']=$actinfo;
        $result['data']['order']=$data['order'];

        if($data['goods_info']['list']){
            foreach($data['goods_info']['list'] as $key=>$val){
                $img=format_url($val['img']);
                $img=str_replace('https','http',$img);
                $data['goods_info']['list'][$key]['img']=$img;
                $data['goods_info']['list'][$key]['shop_price']=format_price($val['shop_price']);
                $data['goods_info']['list'][$key]['market_price']=format_price($val['market_price']);
                $data['goods_info']['list'][$key]['id']=$val['id'];
            }
            //获取赠品
            $gift_list=array();
            if($actinfo['act_type']==3 || $actinfo['act_type']==4) {
                $list = D("Common/Promot")->get_gift_goods($id);
                if ($list['status'] == 1 && $list['gift_list']) {
                    $gift_list = $list['gift_list'];
                }
            }

            //获取购物车的赠品
            $gift_id=0;
            if($cart_id){
                $map=array();
                $map['id']=$cart_id;
                $cart_info=M("Cart")->where($map)->field('sum(shop_price*goods_num) as total_price,actgoodsid')->find();
                if($cart_info['actgoodsid']){
                    if(stripos($cart_info['actgoodsid'],'-')){
                        $act_id=explode('-',$cart_info['actgoodsid']);
                        $gift_id=$act_id[0];
                    }else{
                        $gift_id=$cart_info['actgoodsid'];
                    }
                }
            }
            $result['data']['gift_id']=$gift_id;
            $result['data']['gift_list']=$gift_list;
            $result['data']['list']=$data['goods_info']['list'];
        }else{
            $result['status']=2;
            $result['msg']='未获取到数据';
        }
        $this->ajaxReturn($result);
    }

    public function seckill(){
        $result=array('status'=>1,'msg'=>'');
        $getdata = I('get.');
        $map['is_limit_time']=1;
        $map['pid']=0;
        $map['start_time']=array('lt',NOW_TIME);
        $map['end_time']=array('gt',NOW_TIME);
        $act_info=M('PromotAct')->where($map)->field(true)->find();
        if($act_info){
            $end_time=$act_info['end_time'];
            $getdata['act_id'] = $act_info['id'];
            $data = D('Common/Promot')->get_promot_goods($getdata);
            if($data['goods_info']['list']){
                $goods_list=array();
                foreach($data['goods_info']['list'] as $key=>$val){
                    $goods_list[$key]['img']=thumb($val['img'],500,500);
                    $goods_list[$key]['id']=$val['id'];
                    $goods_list[$key]['goods_name']=$val['goods_name'];
                    $goods_list[$key]['promote_price']=format_price($val['promote_price']);
                    $goods_list[$key]['shop_price']=format_price($val['shop_price']);
                }
                if($goods_list){
                    $result['data']['list']=$goods_list;
                }else{
                    $result['status']=2;
                    $result['msg']='未获取到数据';
                }

                $result['data']['end_time']=$end_time;
            }else{
                $result['status']=2;
                $result['msg']='未获取到数据';
                $result['data']['end_time']=$end_time;
            }
        }else{
            $result['status']=2;
            $result['msg']='未获取到数据';
        }
        $this->ajaxReturn($result);
    }
}
?>