<?php
namespace Api\Controller;

class PreController extends BaseController
{		
	/**
	 * [pre 领券中心]
	 * @AuthorHTL
	 * @DateTime  2018-02-26T18:24:51+0800
	 * @return    [type]                   [description]
	 */
	public function pre()
	{
		if (empty($this->data['openid'])) $this->ajaxReturn(['status' => -1, 'msg' => 'openid为空']);

		$user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');

		$list = M('pre')->where(array('admin_id' => $this->data['business_id']))->field('id, pre_money, full_money, nums, re_nums, num, validity_day')->select();

		if (empty($list)) $this->ajaxReturn(['status' => -1, 'msg' => '没有优惠券数据']);

		// 获取用户已领取数量
		foreach ($list as $key => &$value) {
			$count = M('user_pre')->where(array('user_id' => $user_id, 'pre_id' => $value['id']))->count();

			$value['num'] = $count;
		}

		$this->ajaxReturn(['status' => 1, 'msg' => '已获取优惠券数据', 'data' => $list]);
	}

	/**
	 * [get_pre 领取优惠券]
	 * @AuthorHTL
	 * @DateTime  2018-02-24T14:11:38+0800
	 * @return    [type]                   [description]
	 */
	public function get_pre()
	{
		if (empty($this->data['pre_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '优惠券id为空']);

		if (empty($this->data['openid'])) $this->ajaxReturn(['status' => -1, 'msg' => 'openid为空']);

		$user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');
		
		$pre_info = M('pre')->where(array('id' => $this->data['pre_id']))->field('pre_money, full_money, nums, re_nums, num, validity_day')->find();
		// 检测优惠券剩余数量
		if (($pre_info['nums'] - $pre_info['re_nums']) <= 0) $this->ajaxReturn(['status' => -1, 'msg' => '优惠券已全部被领完，无法领取！']);

		// 检测用户领取优惠券数量
		$count = M('user_pre')->where(array('user_id' => $user_id, 'pre_id' => $this->data['pre_id']))->count();
		if ($count >= $pre_info['num']) $this->ajaxReturn(['status' => -1, 'msg' => '每个用户只能领取'.$pre_info.'张优惠券，您已超过限额，无法领取！']);


		// 添加用户已领取的优惠券
		$today_start_time = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$result = M('user_pre')->add(
				array(
						'user_id' => $user_id, 
						'pre_id' => $this->data['pre_id'],
						'pre_money' => $pre_info['pre_money'], 
						'full_money' => $pre_info['full_money'], 
						'status' => 1,
						'start_time' => $today_start_time,
						'end_time' => $today_start_time + $pre_info['validity_day'] * 24 * 60 * 60,
						'create_time' => time()
					)
				);

		if ($result) {
			M('pre')->where(array('id' => $this->data['pre_id']))->setInc('re_nums', 1);
			$this->ajaxReturn(['status' => 1, 'msg' => '领取成功']);
		} else {
			$this->ajaxReturn(['status' => -1, 'msg' => '领取优惠券失败']);
		}
	}

	/**
	 * [pre_list 获取用户优惠券]
	 * @AuthorHTL
	 * @DateTime  2018-02-24T15:27:09+0800
	 * @return    [type]                   [description]
	 */
	public function pre_list()
	{
		if (empty($this->data['openid'])) $this->ajaxReturn(['status' => -1, 'msg' => 'openid为空']);

		// if (empty($this->data['status'])) $this->ajaxReturn(['status' => -1, 'msg' => '优惠券状态为空']); # 1未使用 2已使用 3已过期

		if (empty($this->data['page'])) $this->ajaxReturn(['status' => -1, 'msg' => '分页为空']);

		$user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');

		$where = 'user_id = '.$user_id;

		// switch ($this->data['status']) {
		// 	case 1: #未使用
		// 		$where .= 'status = 1 AND end_time > '.time();
		// 		break;

		// 	case 2: # 已使用
		// 		$where .= 'status = 2';
		// 		break;

		// 	case 3: # 已过期
		// 		$where .= 'status = 1 AND end_time <= '.time();
		// 		break;
			
		// 	default:
		// 		$this->ajaxReturn(['status' => -1, 'msg' => '优惠券状态参数非法']);
		// 		break;
		// }
		$where .= 'status = 1 AND end_time > '.time();

		$list = M('user_pre')->where($where)->field('id, pre_money, full_money, start_time, end_time')->page($this->data['page'])->select();

		if (empty($list)) {
			$this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
		} else {
			$this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
		}
	}
}