Files
2024-09-27 01:32:49 +08:00

290 lines
9.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\tools;
use app\ydyd_service\model\UserCartModel;
use think\Db;
use think\facade\Cache;
class UserExtendCart
{
/*************************************************************/
// 华源商品数据
protected $ex_goods = [];
/*************************************************************/
// 当前用户
protected $user_id = 0;
public function __construct($user_id=null)
{
$this->user_id = session('user_id')?session('user_id'):$user_id;
//增加扩展的华源商品 不加入缓存 实时读取数据库
$extend_where[] = ['user_id',"=",$this->user_id];
$extend_where[] = ['type',"=",'extend'];
//$extend_cart_data = Db::name("shopping_cart")->where($extend_where)->column("batch_id as id,goods_number as num, unit_price as price, type, extend_data",'batch_id');
//扩展商品无price_id字段暂用batch_id代替
$extend_cart_data = Db::name("user_cart")->where($extend_where)->column("id,batch_id,goods_number as num,unit_price as price,type,extend_data",'batch_id');
if ($extend_cart_data){
foreach ($extend_cart_data as $item){
$this->initExCart($item, true);
}
//$this->setExtendGoods($extend_cart_data);//不使用缓存
$this->setExtendGoods($this->ex_goods);//不使用缓存
$this->extend_store();
}
}
public function getExtendGoods()
{
return $this->ex_goods;
}
public function setExtendGoods($goods)
{
$this->ex_goods = $goods;
}
/**
* 初始化 加入购物车-》华源商品扩展
* @param $data
* @param bool $init_flag
* @return mixed
* @throws \Exception
*/
public function initExCart($data,$init_flag = false)
{
if ($data && $init_flag){
$pid= $data['batch_id'];
$this->ex_goods[$pid]['id'] = $data['id'];
$this->ex_goods[$pid]['num'] = $data['num'];
$this->ex_goods[$pid]['batch_id'] = $data['batch_id'];
$this->ex_goods[$pid]['price'] = $data['price'];
$this->ex_goods[$pid]['total'] = $this->ex_goods[$pid]['num'] * $this->ex_goods[$pid]['price'];
$this->ex_goods[$pid]['type'] = 'extend';
$this->ex_goods[$pid]['extend_data'] = $data['extend_data'];
}
}
/***************************************************/
/**
* 加入购物车-》华源商品扩展
* @param $data
* @param bool $init_flag
* @return mixed
* @throws \Exception
*/
public function add($data,$init_flag = false)
{
if (!is_array($data) || !isset($data['id']) || !isset($data['num']) || !isset($data['price']) || ($data['price'] <0)) {
throw new \Exception('购物车ADD方法参数设置错误');
}
//添加商品支持多商品添加
//$options = isset($data['options']) ? $data['options'] : '';
//$sid = md5($data['id']);
$sid = $data['id'];
$_where[] = ['user_id',"=",$this->user_id];
$_where[] = ["batch_id","=",$sid];
//生成维一ID用于处理相同商品有不同属性时
if (isset($this->ex_goods[$sid])) {
//如果数量为0删除商品
if ($data['num'] <= 0) {
unset($this->ex_goods[$sid]);
UserCartModel::where($_where)->delete();
} else {
//已经存在相同商品时增加商品数量
$this->ex_goods[$sid]['num'] = $this->ex_goods[$sid]['num'] + $data['num'];
$this->ex_goods[$sid]['total'] = $this->ex_goods[$sid]['num'] * $this->ex_goods[$sid]['price'];
$this->ex_goods[$sid]['type'] = 'extend';
$this->ex_goods[$sid]['extend_data'] = $data['extend_data'];
if (!$init_flag){
$update_data['goods_number'] = $this->ex_goods[$sid]['num'];
$update_data['unit_price'] = $this->ex_goods[$sid]['price'];
$update_data['type'] = 'extend';
$update_data['extend_data'] = $this->ex_goods[$sid]['extend_data'];
UserCartModel::where($_where)->update($update_data);
}
}
} else {
if ($data['num'] > 0) {
$this->ex_goods[$sid] = $data;
$this->ex_goods[$sid]['total'] = $data['num'] * $data['price'];
if (!$init_flag) {
$add_data['user_id'] = $this->user_id;
$add_data['batch_id'] = $sid;
$add_data['unit_price'] = $data['price'];
$add_data['goods_number'] = $data['num'];
$add_data['type'] = 'extend';
$add_data['extend_data'] = $data['extend_data'];
UserCartModel::create($add_data);
}
}
}
return true;
}
/******************************************************/
/****************************************************/
// 扩展商品 不加入缓存
private function extend_store(){
$cart = [
'goods_list' => $this->ex_goods,
'total_num' => $this->getTotalNums(),
'total_price' => $this->getTotalPrice(),
'price_id' => $this->getAllPriceID(),
];
return $cart;
}
/*****************************************************/
/**
* 统计购物车中商品分类数量
*/
public function getTotalNums()
{
$rows = 0;
foreach ($this->ex_goods as $v) {
//$rows += $v['num'];
$rows ++;
}
return $rows;
}
/**
* 获得商品汇总价格
*/
public function getTotalPrice()
{
$total = 0;
foreach ($this->ex_goods as $v) {
$total += $v['price'] * $v['num'];
}
return $total;
}
/**
* batch_id 充当price_id
* */
public function getAllPriceId(){
$data = [];
foreach ($this->ex_goods as $v) {
$data[]= $v['batch_id'];
}
return $data;
}
/**
* 更新购物车数量
* @param $data
* $data=[
* "sid"=>'', //商品的唯一SID
* "num"=>2, //商品数量
* ]
* @return bool
* @throws \Exception
*/
public function update($data)
{
if ( ! isset($data['sid']) || ! isset($data['num'])) {
throw new \Exception('购物车update方法参数错误缺少sid或num值');
}
$sid = $data['sid'];
$_where[] = ['user_id',"=",$this->user_id];
$_where[] = ["batch_id","=",$sid];
if (isset($this->ex_goods[$sid])) {
if ($data['num'] == 0) {
unset($this->ex_goods[$sid]);
UserCartModel::where($_where)->delete();
} else {
$this->ex_goods[$sid]['num'] = $data['num'];
if (isset($data['price']) && is_numeric($data['price']) && $data['price'] > 0){
$this->ex_goods[$sid]['price'] = $data['price'];
}
$this->ex_goods[$sid]['total'] = $data['num'] * $this->ex_goods[$sid]['price'];
$update_data['goods_number'] = $this->ex_goods[$sid]['num'];
$update_data['unit_price'] = $this->ex_goods[$sid]['price'];
UserCartModel::where($_where)->update($update_data);
}
return true;
}
return false;
}
/**
* 获得购物车中的所有数据
* @return mixed
*/
public function getAllData()
{
return $this->extend_store();
}
/**
* 获取购物车内所有商品的ID信息
* @return array
*/
public function getAllId()
{
$id_arr = [];
foreach ($this->ex_goods as $k => $item) {
$id_arr[] = $k;
}
return $id_arr;
}
/**
* 获取购物车内所有商品的ID信息(user_cart.id)
* @return array
*/
public function getAllUcId()
{
$id_arr = [];
if (is_array($this->ex_goods)&&$this->ex_goods){
foreach ($this->ex_goods as $k => $item) {
$id_arr[] = $item['id'];
}
}
return $id_arr;
}
/**
* 删除购物车
* @param $sid 商品SID编号
* @return bool
* @throws \Exception
*/
public function del($sid)
{
if (isset($this->ex_goods[$sid])) {
unset($this->ex_goods[$sid]);
$_where[] = ['user_id',"=",$this->user_id];
$_where[] = ["batch_id","=",$sid];
ShoppingCartModel::where($_where)->delete();
return true;
}
return false;
}
/**
* 删除所有商品
* @return bool
* @throws \Exception
*/
public function flush()
{
//ShoppingCartModel::where("user_id",$this->user_id)->delete();
UserCartModel::where("user_id",$this->user_id)->delete();
$this->setExtendGoods([]);
$this->extend_store();
return true;
}
}