<?php
namespace Smi\Controller;

trait BigWheelController
{	
	/**
	 * 商品设置
	 */
	public function bw_index()
	{	
		$data = I('post.');

		$list = M('big_wheel_goods')->where(array('admin_id' => $this->business_id))->select();

		if (!empty($data)) {
			if (count($list) != 8) {
				alertMsg('奖品数量必须等于8');die;
			}

			if (count($data['probability']) != count($list)) {
				alertMsg('设置概率的商品不匹配');die;
			}

			$probability = 0;
			foreach ($list as $k => $v) {
				$probability += $data['probability'][$k];
			}

			if ($probability > 100) {
				alertMsg('所有商品权重总和不能大于100');die;
			}
			// M('big_wheel_goods')->startTrans();
			foreach ($list as $k => $v) {
				$result = M('big_wheel_goods')->where(array('id' => $v['id']))->save(array('probability' => $data['probability'][$k]));
				// if (!$result) {
				// 	M('big_wheel_goods')->rollback();
				// 	alertMsg('设置失败');die;
				// }
			}

			// M('big_wheel_goods')->commit();
			$this->success('设置成功');die;
		}

		$this->assign('list', $list);
		$this->display('BigWheel/index');
	}

	/**
	 * 删除商品
	 */
	public function bw_delete($id)
	{
		$info = M('big_wheel_goods')->where(array('id' => $id))->find();

		if ($info['admin_id'] != $this->business_id) {
			alertMsg('非法操作');die;
		}

		$result = M('big_wheel_goods')->where(array('id' => $id))->delete();

		if ($result) {
			unlink($this->root.'/Uploads/'.$info['img']);
			$this->success('删除成功');die;
		} else {
			alertMsg('删除失败');die;
		}
	}

	/**
	 * 添加商品
	 */
	public function bw_add_goods()
	{	
		$data = I('post.');
		if (!empty($data)) {
			$count = M('big_wheel_goods')->where(array('admin_id' => $this->business_id))->count();
			if ($count >= 8) {
				alertMsg('奖品数量不能大于8个');die;
			}

			$add = array(
					'admin_id' => $this->business_id,
					'goods_name' => $data['goods_name'],
					'status' => empty($data['status']) ? 2 : $data['status'],
					'create_time' => time()
				);
			if (empty($_FILES['img']['name'])) {
				alertMsg('请上传奖品图片');die;
			}
			$upload = ToolsController::upload('big_wheel/');
			if ($upload['status'] == -1) {
				alertMsg($upload['msg']);die;
			} else {
				$add['img'] = $upload['file_path'];
			}

			$result = M('big_wheel_goods')->add($add);

			if ($result) {
				$this->success('添加成功');die;
			} else {
				unlink($this->root.'/Uploads/'.$add['img']);
				alertMsg('添加失败');die;
			}
		}

		$this->assign('op', '添加');
		$this->display('BigWheel/add_goods');
	}

	/**
	 * 编辑商品
	 */
	public function bw_edit_goods($id)
	{	
		$data = I('post.');

		if (!empty($data)) {
			$info = M('big_wheel_goods')->where(array('id' => $data['id']))->find();

			$save = array(
					'goods_name' => $data['goods_name'],
					'status' => $data['status'],
					'update_time' => time(),
				);

			if (!empty($_FILES['img']['name'])) {
				$upload = ToolsController::upload('big_wheel/');
				if ($upload['status'] == -1) {
					alertMsg($upload['msg']);die;
				} else {
					$save['img'] = $upload['file_path'];
				}
			}

			$result = M('big_wheel_goods')->where(array('id' => $data['id']))->save($save);

			if ($result) {
				if (!empty($_FILES['img']['name'])) {
					unlink($this->root.'/Uploads/'.$info['img']);
				}
				$this->success('更新成功');die;
			} else {
				if (!empty($_FILES['img']['name'])) {
					unlink($this->root.'/Uploads/'.$save['img']);
				}
				alertMsg('更新失败');die;
			}
		}

		$info = M('big_wheel_goods')->where(array('id' => $id))->find();

		$this->assign('op', '编辑');
		$this->assign('info', $info);
		$this->display('BigWheel/add_goods');
	}

	/**
	 * 大转盘配置
	 */
	public function bw_config()
	{	
		$data = I('post.');
		if (!empty($data)) {
			$save = array(
					'int' => $data['int'],
					'status' => $data['status'],
					'update_time' => time()
				);
			$result = M('big_wheel_config')->where(array('id' => $data['id']))->save($save);

			if ($result) {
				$this->success('更新成功');die;
			} else {
				alertMsg('更新失败');die;
			}
		}

		$config = M('big_wheel_config')->where(array('admin_id' => $this->business_id))->find();
		if (empty($config)) {
			M('big_wheel_config')->add(array('admin_id' => $this->business_id, 'create_time' => time()));
			$config = M('big_wheel_config')->find();
		}

		$this->assign('config', $config);
		$this->display('BigWheel/config');
	}

	/**
	 * 中奖记录
	 */
	public function bw_winning_record($status = '')
	{	
		$where = 'am_big_wheel_winning_record.admin_id = '.$this->business_id;
		if (!empty($status)) {
			$where .= ' AND am_big_wheel_winning_record.status = '.$status;
		}

		$field = 'am_big_wheel_winning_record.*, am_big_wheel_goods.goods_name, am_big_wheel_goods.img';
		$count = M('big_wheel_winning_record')
				->join(array('LEFT JOIN am_big_wheel_goods ON am_big_wheel_goods.id = am_big_wheel_winning_record.bw_goods_id', 'LEFT JOIN am_users ON am_users.id = am_big_wheel_winning_record.user_id'))
				->field($field)
				->where($where)
				->count();
		$Page = new \Think\Page($count, 10);
		$show = $Page->show();
		$limit = $Page->firstRow.', '.$Page->listRows;

		$list = M('big_wheel_winning_record')
				->join(array('LEFT JOIN am_big_wheel_goods ON am_big_wheel_goods.id = am_big_wheel_winning_record.bw_goods_id', 'LEFT JOIN am_users ON am_users.id = am_big_wheel_winning_record.user_id'))
				->field($field)
				->where($where)
				->limit($limit)
				->select();

		$this->assign('page', $show);
		$this->assign('list', $list);
		$this->display('BigWheel/winning_record');
	}

	/**
     * 获奖记录入录物流单号
     */
    public function bw_logistics_no()
    {
        $data = I('post.');

        if (empty($data['id'])) $this->ajaxReturn(['status' => -1, 'msg' => 'id参数不合法']);

        if (empty($data['logistics_no'])) $this->ajaxReturn(['status' => -1, 'msg' => 'logistics_no参数不合法']);

        $result = M('big_wheel_winning_record')->where(array('id' => $data['id']))->save(array('logistics_no' => $data['logistics_no'], 'status' => 1));

        if ($result) {
            $this->ajaxReturn(['status' => 1, 'msg' => '入录成功']);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '入录失败']);
        }
    }

    /**
     * 删除获奖记录
     */
    public function bw_delete_record($id)
    {	
    	$info = M('big_wheel_winning_record')->where(array('id' => $id))->find();

		if ($info['admin_id'] != $this->business_id) {
			alertMsg('非法操作');die;
		}

    	$result = M('big_wheel_winning_record')->where(array('id' => $id))->delete();

    	if ($result) {
    		$this->success('删除成功');
    	} else {
    		alertMsg('删除失败');
    	}
    }
}