CartModel.php 1.0 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/9/28
 * Time: 15:36
 */

namespace app\cart\model;
use think\Model;
use think\Db;

class CartModel extends Model
{

    public function addToCart($uid, $gid, $num)
    {
        if ($this->checkIsExist($uid, $gid)) {
            $cart = [
                'uid' => $uid,
                'gid' => $gid,
                'num' => $num
            ];
            if (Db::name('zj_cart')->insert($cart)) {
                return Db::name('zj_cart')->getLastInsID();
            } else {
                return false;
            }
        } else {
            $map = [
                'uid' => $uid,
                'gid' => $gid
            ];
            if (Db::name('zj_cart')->where($map)->setInc('num', $num)) {
                return true;
            } else {
                return false;
            }

        }
    }

    private function checkIsExist($uid, $gid)
    {
        return (Db::name('zj_cart')->where(['uid' => $uid, 'gid' => $gid])->count() == 0) ? true : false;
    }

}