<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class IntController extends BaseController
{
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * 获取积分商品列表
	 */
	public function get_int_list()
	{
		if (empty($this->data['page'])) apiReturn(['status' => -1, 'msg' => 'page参数不能为空']);
		if (empty($this->data['status'])) apiReturn(['status' => -1, 'msg' => 'status参数不能为空']);
		if (!($this->data['status'] == 1 || $this->data['status'] == 2 || $this->data['status'] == 3))  apiReturn(['status' => -1, 'msg' => 'status参数不合法']);// status 1积分 2销量 3最新
		if (empty($this->data['sort'])) apiReturn(['status' => -1, 'msg' => 'sort参数不能为空']);
		if (!($this->data['sort'] == 'DESC' || $this->data['sort'] == 'ASC'))  apiReturn(['status' => -1, 'msg' => 'sort参数不合法']);// sort DESC降序 ASC升序

		$where = 'am_int_market.admin_id = '.$this->data['business_id'].' AND status = 1';
		$order = '';

		if ($this->data['status']  == 1) {
			$order = '`int` '.$this->data['sort'];
		} elseif ($this->data['status'] == 2) {
			$order = '`sales_volume` '.$this->data['sort'];
		} elseif ($this->data['status'] == 3) {
			$order = '`create_time` '.$this->data['sort'];
		}

		$list = M('int_market')->where($where)->field('id, img, status, name, int, sales_volume, create_time')->order($order)->page($this->data['page'])->select();
		
		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

		foreach ($list as $key => &$value) {
			$value['img'] = $this->url.'/Uploads/'.$value['img'];
			$value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 获取我的积分
	 */
	public function get_my_int()
	{	
		if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid参数不能为空']);

		$int = M('user_int')->join('am_users ON am_users.id = am_user_int.user_id')->where('am_users.openid = "'.$this->data['openid'].'" AND am_users.admin_id = '.$this->data['business_id'])->limit(1)->getField('int');

		if (empty($int)) $int = 0; 

		apiReturn(['status' => 1, 'msg' => '已获取结果', 'data' => $int]);
	}

	/**
	 * 积分兑换商品
	 */
	public function exchange()
	{
		if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid参数不能为空']);

		if (empty($this->data['int_goods_id'])) apiReturn(['status' => -1, 'msg' => 'int_goods_id参数不能为空']);

		if (empty($this->data['name'])) apiReturn(['status' => -1, 'msg' => 'name参数不能为空']);

		if (empty($this->data['mobile'])) apiReturn(['status' => -1, 'msg' => 'mobile参数不能为空']);

		if (empty($this->data['address'])) apiReturn(['status' => -1, 'msg' => 'address参数不能为空']);

		$user_int = M('user_int')->join('am_users ON am_users.id = am_user_int.user_id')->where('am_users.openid = "'.$this->data['openid'].'" AND am_users.admin_id = '.$this->data['business_id'])->field('am_user_int.*')->limit(1)->select();

		if (empty($user_int)) {
			$int = 0;
		} else {
			$int = $user_int[0]['int'];
		}

		$int_goods_info = M('int_market')->where(array('id' => $this->data['int_goods_id']))->find();

		// 判断用户积分余额
		if ($int < $int_goods_info['int']) apiReturn(['status' => -1, 'msg' => '用户积分不足，无法兑换']);

		$user_info = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->find();
		$exchange_no = substr(str_replace(':', '', str_replace(' ', '', str_replace('-', '', date('Y-m-d H:i:s').$this->data['int_goods_id'].$user_info['id'].mt_rand(10000,99999)))), 0, 18);

		$add = array(
				'user_id' => $user_info['id'],
				'int_market_id' => $this->data['int_goods_id'],
				'exchange_no' => $exchange_no,
				'status' => 2,
				'name' => $this->data['name'],
				'mobile' => $this->data['mobile'],
				'address' => $this->data['address'],
				'int' => $int_goods_info['int'],
				'create_time' => time()
			);
		

		$result = M('exchange')->add($add);
		$after_int = $user_int[0]['int'] - $int_goods_info['int'];

		if ($result) {
			// 扣积分
			M('user_int')->where(array('id' => $user_int[0]['id']))->save(array('int' => $after_int));

			// 增兑换量
			M('int_market')->where(array('id' => $int_goods_info['id']))->save(array('sales_volume' => $int_goods_info['sales_volume'] + 1));
			apiReturn(['status' => 1, 'msg' => '兑换成功']);
		} else {
			apiReturn(['status' => -1, 'msg' => '兑换失败']);
		}
	}

	/**
	 * 积分兑换记录
	 */
	public function exchange_log()
	{
		if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid参数不能为空']);

		if (empty($this->data['page'])) apiReturn(['status' => -1, 'msg' => 'page参数不能为空']);

		$field = 'am_exchange.id, am_int_market.name int_name, am_int_market.img, am_exchange.exchange_no, am_exchange.status, am_exchange.name username, am_exchange.address, logistics_no, am_exchange.int, am_exchange.create_time';

		$where = array('am_users.openid' => $this->data['openid']);
		if (!empty($this->data['status'])) {
			$where['am_exchange.status'] = $this->data['status'];
		}

		$list = M('exchange')
				->join('am_users ON am_exchange.user_id = am_users.id')
				->join('am_int_market ON am_exchange.int_market_id = am_int_market.id')
				->where($where)
				->field($field)
				->page($this->data['page'])
				->select();
		// echo M()->getLastSql();die;
		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

		foreach ($list as $k => &$vo) {
			$vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
			$vo['img'] = $this->url.'/Uploads/'.$vo['img'];
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 获取轮播图
	 */
	public function chart_list()
	{
		$list = M('int_chart')->where(array('admin_id' => $this->data['business_id'], 'status' => 1))->field('id, title, img')->select();

		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);
		
		foreach ($list as $k => &$v) {
			$v['img'] = $this->url.'/Uploads/'.$v['img'];
		}
		
		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}
}