<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class CarController extends Controller
{	
	/**
	 * 添加购物车信息
	 */
	public function add_car()
	{
		$data = I('post.');
		$this->check_param($data);
        if (empty($data['car_content'])) apiReturn(['status' => -1, 'msg' => '购物车信息不能为空']);

        $data['car_content'] = html_entity_decode($data['car_content']);
        $data['car_content'] = stripslashes($data['car_content']);
        $data['car_content'] = json_decode($data['car_content'], true);
        $temp = array();
        foreach ($data['car_content'] as $key => $vo) {
            $vo['price'] = $vo['sum_price'] / $vo['goods_num'];
            $temp[] = $vo;
        }
        $data['car_content'] = $temp;
        $data['car_content'] = serialize($data['car_content']);
        $data['admin_id'] = $data['business_id'];
        unset($data['business_id']);
        $data['create_time'] = time();

        if ($insert_id = M('car')->add($data)) {
            apiReturn(['status' => 1, 'msg' => '添加成功', 'car_id' => $insert_id]);
        } else {
            apiReturn(['status' => -1, 'msg' => '添加失败']);
        }
	}

	/**
	 * 检查参数
	 */
	private function check_param($data = '')
	{
		if (empty($data['business_id'])) apiReturn(['status' => -1, 'msg' => '商户id不能为空']);
		if (empty($data['users_id'])) apiReturn(['status' => -1, 'msg' => '用户id不能为空']);
	}
}