修改商城模型调用

This commit is contained in:
邹景立 2021-09-13 14:44:49 +08:00
parent 265d8a5838
commit a0f2609c61
20 changed files with 205 additions and 70 deletions

View File

@ -48,8 +48,8 @@ class Address extends Auth
}
} else {
$map = ['uuid' => $this->uuid, 'code' => $data['code']];
$address = DataUserAddress::mk()->where($map)->find();
if (empty($address)) $this->error('修改地址不存在!');
$addr = DataUserAddress::mk()->where($map)->find();
if (empty($addr)) $this->error('修改地址不存在!');
DataUserAddress::mk()->where($map)->update($data);
}
// 去除其它默认选项
@ -68,7 +68,8 @@ class Address extends Auth
*/
public function get()
{
$query = $this->_query('DataUserAddress')->withoutField('deleted');
$model = DataUserAddress::mk();
$query = $this->_query($model)->withoutField('deleted');
$query->equal('code')->where(['uuid' => $this->uuid, 'deleted' => 0]);
$result = $query->order('type desc,id desc')->page(false, false, false, 15);
$this->success('获取地址数据!', $result);
@ -113,11 +114,12 @@ class Address extends Auth
public function remove()
{
$map = $this->_vali([
'uuid.value' => $this->uuid, 'code.require' => '地址编号不能为空!',
'uuid.value' => $this->uuid,
'code.require' => '地址不能为空!',
]);
$address = DataUserAddress::mk()->where($map)->find();
if (empty($address)) $this->error('需要删除的地址不存在!');
if (DataUserAddress::mk()->where($map)->update(['deleted' => 1]) !== false) {
$data = DataUserAddress::mk()->where($map)->find();
if (empty($data)) $this->error('需要删除的地址不存在!');
if ($data->save(['deleted' => 1]) !== false) {
$this->success('删除地址成功!');
} else {
$this->error('删除地址失败!');
@ -132,10 +134,10 @@ class Address extends Auth
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function _getAddress(string $code): ?array
private function _getAddress(string $code): array
{
$map = ['code' => $code, 'uuid' => $this->uuid, 'deleted' => 0];
return DataUserAddress::mk()->withoutField('deleted')->where($map)->find();
$data = DataUserAddress::mk()->withoutField('deleted')->where($map)->find();
return empty($data) ? [] : $data->toArray();
}
}

View File

@ -3,6 +3,7 @@
namespace app\data\controller\api\auth;
use app\data\controller\api\Auth;
use app\data\model\DataUserBalance;
/**
* 用户余额转账
@ -11,12 +12,6 @@ use app\data\controller\api\Auth;
*/
class Balance extends Auth
{
/**
* 绑定数据表
* @var string
*/
private $table = 'DataUserBalance';
/**
* 获取用户余额记录
* @throws \think\db\exception\DataNotFoundException
@ -25,7 +20,7 @@ class Balance extends Auth
*/
public function get()
{
$query = $this->_query($this->table);
$query = $this->_query(DataUserBalance::class);
$query->withoutField('deleted,create_by')->where(['uuid' => $this->uuid, 'deleted' => 0]);
$result = $query->like('create_at#date')->order('id desc')->page(true, false, false, 10);
$this->success('获取数据成功', $result);

View File

@ -3,6 +3,7 @@
namespace app\data\controller\api\auth;
use app\data\controller\api\Auth;
use app\data\model\DataUser;
use app\data\service\RebateService;
use app\data\service\UserAdminService;
use app\data\service\UserUpgradeService;
@ -16,15 +17,8 @@ use think\exception\HttpResponseException;
*/
class Center extends Auth
{
/**
* 绑定数据表
* @var string
*/
private $table = 'DataUser';
/**
* 更新用户资料
* @throws \think\db\exception\DbException
*/
public function set()
{
@ -39,7 +33,7 @@ class Center extends Auth
]);
foreach ($data as $key => $vo) if ($vo === '') unset($data[$key]);
if (empty($data)) $this->error('没有修改的数据!');
if ($this->app->db->name($this->table)->where(['id' => $this->uuid])->update($data) !== false) {
if (DataUser::mk()->where(['id' => $this->uuid])->update($data) !== false) {
$this->success('更新资料成功!', $this->getUser());
} else {
$this->error('更新资料失败!');
@ -125,15 +119,15 @@ class Center extends Auth
$map[] = ['deleted', '=', 0];
$map[] = ['path', 'like', "%-{$this->uuid}-%"];
// 查询邀请的朋友
$query = $this->_query($this->table);
$query = $this->_query(DataUser::class);
$query->like('nickname|username#nickname')->equal('vip_code,pids,pid1,id#uuid');
$query->field('id,pid0,pid1,pid2,pids,username,nickname,headimg,order_amount_total,teams_amount_total,teams_amount_direct,teams_amount_indirect,teams_users_total,teams_users_direct,teams_users_indirect,rebate_total,rebate_used,rebate_lock,create_at');
$result = $query->where($map)->order('id desc')->page(true, false, false, 15);
// 统计当前用户所有下属数
$total = $this->app->db->name($this->table)->where($map)->count();
$total = DataUser::mk()->where($map)->count();
// 统计当前用户本月下属数
$map[] = ['create_at', 'like', date('Y-m-%')];
$month = $this->app->db->name($this->table)->where($map)->count();
$month = DataUser::mk()->where($map)->count();
// 返回结果列表数据及统计
$result['total'] = ['user_total' => $total, 'user_month' => $month];
$this->success('获取我邀请的朋友', $result);

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 用户优惠方案模型
* Class BaseUserDiscount
* @package app\data\model
*/
class BaseUserDiscount extends Model
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 用户支付参数模型
* Class BaseUserPayment
* @package app\data\model
*/
class BaseUserPayment extends Model
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 用户等级配置模型
* Class BaseUserUpgrade
* @package app\data\model
*/
class BaseUserUpgrade extends Model
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 商城商品主模型
* Class ShopGoods
* @package app\data\model
*/
class ShopGoods extends Model
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 商城商品分类模型
* Class ShopGoodsCate
* @package app\data\model
*/
class ShopGoodsCate extends Model
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 商城商品规格模型
* Class ShopGoodsItem
* @package app\data\model
*/
class ShopGoodsItem extends Model
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 商城商品标题模型
* Class ShopGoodsMark
* @package app\data\model
*/
class ShopGoodsMark extends Model
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 商城商品库存模型
* Class ShopGoodsStock
* @package app\data\model
*/
class ShopGoodsStock extends Model
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace app\data\model;
use think\admin\Model;
/**
* 商城订单发货模型
* Class ShopOrderSend
* @package app\data\model
*/
class ShopOrderSend extends Model
{
}

View File

@ -2,6 +2,12 @@
namespace app\data\service;
use app\data\model\ShopGoods;
use app\data\model\ShopGoodsCate;
use app\data\model\ShopGoodsItem;
use app\data\model\ShopGoodsMark;
use app\data\model\ShopGoodsStock;
use app\data\model\ShopOrder;
use think\admin\extend\DataExtend;
use think\admin\Service;
@ -20,8 +26,7 @@ class GoodsService extends Service
public function getMarkData(): array
{
$map = ['status' => 1];
$query = $this->app->db->name('ShopGoodsMark');
return $query->where($map)->order('sort desc,id desc')->column('name');
return ShopGoodsMark::mk()->where($map)->order('sort desc,id desc')->column('name');
}
/**
@ -34,8 +39,7 @@ class GoodsService extends Service
*/
public function getCateTree(string $type = 'arr2tree'): array
{
$map = ['deleted' => 0, 'status' => 1];
$query = $this->app->db->name('ShopGoodsCate')->where($map)->order('sort desc,id desc');
$query = ShopGoodsCate::mk()->where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc');
return DataExtend::$type($query->withoutField('sort,status,deleted,create_at')->select()->toArray());
}
@ -46,8 +50,7 @@ class GoodsService extends Service
*/
public function getCateData(bool $simple = true): array
{
$map = ['status' => 1, 'deleted' => 0];
$cates = $this->app->db->name('ShopGoodsCate')->where($map)->column('id,pid,name', 'id');
$cates = ShopGoodsCate::mk()->where(['status' => 1, 'deleted' => 0])->column('id,pid,name', 'id');
foreach ($cates as $cate) if (isset($cates[$cate['pid']])) $cates[$cate['id']]['parent'] =& $cates[$cate['pid']];
foreach ($cates as $key => $cate) {
$id = $cate['id'];
@ -78,11 +81,10 @@ class GoodsService extends Service
public function stock(string $code): bool
{
// 商品入库统计
$query = $this->app->db->name('ShopGoodsStock');
$query->field('goods_code,goods_spec,ifnull(sum(goods_stock),0) stock_total');
$query = ShopGoodsStock::mk()->field('goods_code,goods_spec,ifnull(sum(goods_stock),0) stock_total');
$stockList = $query->where(['goods_code' => $code])->group('goods_code,goods_spec')->select()->toArray();
// 商品销量统计
$query = $this->app->db->table('shop_order a')->field('b.goods_code,b.goods_spec,ifnull(sum(b.stock_sales),0) stock_sales');
$query = ShopOrder::mk()->alias('a')->field('b.goods_code,b.goods_spec,ifnull(sum(b.stock_sales),0) stock_sales');
$query->leftJoin('shop_order_item b', 'a.order_no=b.order_no')->where("b.goods_code='{$code}' and a.status>0 and a.deleted_status<1");
$salesList = $query->group('b.goods_code,b.goods_spec')->select()->toArray();
// 组装更新数据
@ -98,13 +100,13 @@ class GoodsService extends Service
foreach ($dataList as $vo) {
$map = ['goods_code' => $code, 'goods_spec' => $vo['goods_spec']];
$set = ['stock_total' => $vo['stock_total'], 'stock_sales' => $vo['stock_sales']];
$this->app->db->name('ShopGoodsItem')->where($map)->update($set);
ShopGoodsItem::mk()->where($map)->update($set);
}
// 更新商品主体销量及库存
$this->app->db->name('ShopGoods')->where(['code' => $code])->update([
ShopGoods::mk()->where(['code' => $code])->update([
'stock_total' => intval(array_sum(array_column($dataList, 'stock_total'))),
'stock_sales' => intval(array_sum(array_column($dataList, 'stock_sales'))),
'stock_virtual' => $this->app->db->name('ShopGoodsItem')->where(['goods_code' => $code])->sum('number_virtual'),
'stock_virtual' => ShopGoodsItem::mk()->where(['goods_code' => $code])->sum('number_virtual'),
]);
return true;
}
@ -121,8 +123,8 @@ class GoodsService extends Service
public function bindData(array &$data = [], bool $simple = true): array
{
[$cates, $codes] = [$this->getCateData(), array_unique(array_column($data, 'code'))];
$marks = $this->app->db->name('ShopGoodsMark')->where(['status' => 1])->column('name');
$items = $this->app->db->name('ShopGoodsItem')->whereIn('goods_code', $codes)->where(['status' => 1])->select()->toArray();
$marks = ShopGoodsMark::mk()->where(['status' => 1])->column('name');
$items = ShopGoodsItem::mk()->whereIn('goods_code', $codes)->where(['status' => 1])->select()->toArray();
foreach ($data as &$vo) {
[$vo['marks'], $vo['cateids'], $vo['cateinfo']] = [str2arr($vo['marks'], ',', $marks), str2arr($vo['cateids']), []];
[$vo['slider'], $vo['specs'], $vo['items']] = [str2arr($vo['slider'], '|'), json_decode($vo['data_specs'], true), []];

View File

@ -2,6 +2,11 @@
namespace app\data\service;
use app\data\model\BaseUserDiscount;
use app\data\model\DataUser;
use app\data\model\ShopOrder;
use app\data\model\ShopOrderItem;
use app\data\model\ShopOrderSend;
use think\admin\Service;
/**
@ -31,7 +36,7 @@ class OrderService extends Service
public function stock(string $orderNo): bool
{
$map = ['order_no' => $orderNo];
$codes = $this->app->db->name('ShopOrderItem')->where($map)->column('goods_code');
$codes = ShopOrderItem::mk()->where($map)->column('goods_code');
foreach (array_unique($codes) as $code) GoodsService::instance()->stock($code);
return true;
}
@ -48,10 +53,10 @@ class OrderService extends Service
{
// 目标订单数据
$map = [['order_no', '=', $orderNo], ['status', '>=', 4]];
$order = $this->app->db->name('ShopOrder')->where($map)->find();
$order = ShopOrder::mk()->where($map)->find();
if (empty($order)) return null;
// 订单用户数据
$user = $this->app->db->name('DataUser')->where(['id' => $order['uuid']])->find();
$user = DataUser::mk()->where(['id' => $order['uuid']])->find();
if (empty($user)) return null;
// 更新用户购买资格
$entry = $this->vipEntry($order['uuid']);
@ -61,9 +66,9 @@ class OrderService extends Service
UserUpgradeService::instance()->bindAgent($user['id'], $puid1);
}
// 重置用户信息并绑定订单
$user = $this->app->db->name('DataUser')->where(['id' => $order['uuid']])->find();
$user = DataUser::mk()->where(['id' => $order['uuid']])->find();
if ($user['pid1'] > 0) {
$this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update([
ShopOrder::mk()->where(['order_no' => $orderNo])->update([
'puid1' => $user['pid1'], 'puid2' => $user['pid2'],
]);
}
@ -76,19 +81,17 @@ class OrderService extends Service
* 刷新用户入会礼包
* @param integer $uuid 用户UID
* @return integer
* @throws \think\db\exception\DbException
*/
private function vipEntry(int $uuid): int
{
// 检查是否购买入会礼包
$query = $this->app->db->table('shop_order a')->join('shop_order_item b', 'a.order_no=b.order_no');
$query = ShopOrder::mk()->alias('a')->join('shop_order_item b', 'a.order_no=b.order_no');
$entry = $query->where("a.uuid={$uuid} and a.status>=4 and a.payment_status=1 and b.vip_entry>0")->count() ? 1 : 0;
// 用户最后支付时间
$query = $this->app->db->name('ShopOrder');
$lastMap = [['uuid', '=', $uuid], ['status', '>=', 4], ['payment_status', '=', 1]];
$lastDate = $query->where($lastMap)->order('payment_datetime desc')->value('payment_datetime');
$lastDate = ShopOrder::mk()->where($lastMap)->order('payment_datetime desc')->value('payment_datetime');
// 更新用户支付信息
$this->app->db->name('DataUser')->where(['id' => $uuid])->update(['buy_vip_entry' => $entry, 'buy_last_date' => $lastDate]);
DataUser::mk()->where(['id' => $uuid])->update(['buy_vip_entry' => $entry, 'buy_last_date' => $lastDate]);
return $entry;
}
@ -103,7 +106,7 @@ class OrderService extends Service
{
if ($disId > 0) {
$map = ['id' => $disId, 'status' => 1, 'deleted' => 0];
$discount = $this->app->db->name('BaseUserDiscount')->where($map)->value('items');
$discount = BaseUserDiscount::mk()->where($map)->value('items');
$disitems = json_decode($discount ?: '[]', true) ?: [];
if (is_array($disitems) && count($disitems) > 0) foreach ($disitems as $vo) {
if ($vo['level'] == $vipCode) $disRate = floatval($vo['discount']);
@ -126,10 +129,10 @@ class OrderService extends Service
if (empty($data)) return $data;
// 关联发货信息
$nobs = array_unique(array_column($data, 'order_no'));
$trucks = $this->app->db->name('ShopOrderSend')->whereIn('order_no', $nobs)->column('*', 'order_no');
$trucks = ShopOrderSend::mk()->whereIn('order_no', $nobs)->column('*', 'order_no');
foreach ($trucks as &$item) unset($item['id'], $item['uuid'], $item['status'], $item['deleted'], $item['create_at']);
// 关联订单商品
$query = $this->app->db->name('ShopOrderItem')->where(['status' => 1, 'deleted' => 0]);
$query = ShopOrderItem::mk()->where(['status' => 1, 'deleted' => 0]);
$items = $query->withoutField('id,uuid,status,deleted,create_at')->whereIn('order_no', $nobs)->select()->toArray();
// 关联用户数据
$fields = 'phone,username,nickname,headimg,status,vip_code,vip_name';

View File

@ -2,6 +2,7 @@
namespace app\data\service;
use app\data\model\BaseUserPayment;
use app\data\model\DataUserPayment;
use app\data\model\ShopOrder;
use app\data\service\payment\AlipayPaymentService;
@ -203,7 +204,7 @@ abstract class PaymentService
try {
if (empty($payment)) {
$map = ['code' => $code, 'status' => 1, 'deleted' => 0];
$payment = app()->db->name('BaseUserPayment')->where($map)->find();
$payment = BaseUserPayment::mk()->where($map)->find();
}
if (empty($payment)) {
throw new Exception("支付参数[#{$code}]禁用关闭");

View File

@ -2,9 +2,12 @@
namespace app\data\service;
use app\data\model\BaseUserDiscount;
use app\data\model\BaseUserUpgrade;
use app\data\model\DataUser;
use app\data\model\DataUserRebate;
use app\data\model\ShopOrder;
use app\data\model\ShopOrderItem;
use think\admin\Exception;
use think\admin\extend\CodeExtend;
use think\admin\Service;
@ -173,8 +176,8 @@ class RebateService extends Service
*/
private function checkPrizeStatus(string $prize, int $level): bool
{
$map = [['number', '=', $level], ['rebate_rule', 'like', "%,{$prize},%"]];
return $this->app->db->name('BaseUserUpgrade')->where($map)->count() > 0;
$model = BaseUserUpgrade::mk()->where(['number' => $level]);
return $model->whereLike('rebate_rule', "%,{$prize},%")->count() > 0;
}
/**
@ -314,11 +317,11 @@ class RebateService extends Service
$puids = array_reverse(str2arr($this->user['path'], '-'));
if (empty($puids) || $this->order['amount_total'] <= 0) return false;
// 获取可以参与奖励的代理
$vips = $this->app->db->name('BaseUserUpgrade')->whereLike('rebate_rule', '%,' . self::PRIZE_05 . ',%')->column('number');
$vips = BaseUserUpgrade::mk()->whereLike('rebate_rule', '%,' . self::PRIZE_05 . ',%')->column('number');
$users = DataUser::mk()->whereIn('vip_code', $vips)->whereIn('id', $puids)->orderField('id', $puids)->select()->toArray();
// 查询需要计算奖励的商品
foreach ($this->app->db->name('ShopOrderItem')->where(['order_no' => $this->order['order_no']])->cursor() as $item) {
$itemJson = $this->app->db->name('BaseUserDiscount')->where(['status' => 1, 'deleted' => 0])->value('items');
foreach (ShopOrderItem::mk()->where(['order_no' => $this->order['order_no']])->cursor() as $item) {
$itemJson = BaseUserDiscount::mk()->where(['status' => 1, 'deleted' => 0])->value('items');
if (!empty($itemJson) && is_array($rules = json_decode($itemJson, true))) {
[$tVip, $tRate] = [$item['vip_code'], $item['discount_rate']];
foreach ($users as $user) if (isset($rules[$user['vip_code']]) && $user['vip_code'] > $tVip) {
@ -355,7 +358,7 @@ class RebateService extends Service
// 记录原始等级
$prevLevel = $this->user['vip_code'];
// 获取可以参与奖励的代理
$vips = $this->app->db->name('BaseUserUpgrade')->whereLike('rebate_rule', '%,' . self::PRIZE_06 . ',%')->column('number');
$vips = BaseUserUpgrade::mk()->whereLike('rebate_rule', '%,' . self::PRIZE_06 . ',%')->column('number');
foreach (DataUser::mk()->whereIn('vip_code', $vips)->whereIn('id', $puids)->orderField('id', $puids)->cursor() as $user) {
if ($user['vip_code'] > $prevLevel) {
if (($amount = $this->_prize06amount($prevLevel + 1, $user['vip_code'])) > 0.00) {

View File

@ -2,7 +2,9 @@
namespace app\data\service;
use app\data\model\DataUser;
use app\data\model\DataUserBalance;
use app\data\model\ShopOrder;
use think\admin\Exception;
use think\admin\Service;
@ -25,11 +27,10 @@ class UserBalanceService extends Service
*/
public function confirm(string $orderNo): array
{
$map = [['status', '>=', 4], ['order_no', '=', $orderNo]];
$order = $this->app->db->name('ShopOrder')->where($map)->find();
$order = ShopOrder::mk()->where([['status', '>=', 4], ['order_no', '=', $orderNo]])->find();
if (empty($order)) throw new Exception('需处理的订单状态异常');
if ($order['reward_balance'] > 0) data_save('DataUserBalance', [
if ($order['reward_balance'] > 0) data_save(DataUserBalance::class, [
'uuid' => $order['uuid'],
'code' => "CZ{$order['order_no']}",
'name' => "订单余额充值",
@ -53,7 +54,7 @@ class UserBalanceService extends Service
$total = abs(DataUserBalance::mk()->whereRaw("uuid='{$uuid}' and amount>0 and deleted=0")->sum('amount'));
$count = abs(DataUserBalance::mk()->whereRaw("uuid='{$uuid}' and amount<0 and deleted=0")->sum('amount'));
if (empty($nots)) {
$this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]);
DataUser::mk()->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]);
} else {
$count -= DataUserBalance::mk()->whereRaw("uuid={$uuid}")->whereIn('code', $nots)->sum('amount');
}

View File

@ -48,7 +48,6 @@ class UserRebateService extends Service
$map = [['status', '>=', 4], ['order_no', '=', $orderNo]];
$order = ShopOrder::mk()->where($map)->find();
if (empty($order)) return [0, '需处理的订单状态异常!'];
$map = [['status', '=', 0], ['order_no', 'like', "{$orderNo}%"]];
DataUserRebate::mk()->where($map)->update(['status' => 1]);
if (UserUpgradeService::instance()->upgrade($order['uuid'])) {

View File

@ -153,5 +153,4 @@ class UserTransferService extends Service
if (empty($data)) $data = sysdata('TransferWxpay');
return is_null($name) ? $data : ($data[$name] ?? '');
}
}

View File

@ -2,6 +2,7 @@
namespace app\data\service;
use app\data\model\BaseUserUpgrade;
use app\data\model\DataUser;
use app\data\model\DataUserBalance;
use app\data\model\ShopOrder;
@ -22,8 +23,8 @@ class UserUpgradeService extends Service
*/
public function levels(): array
{
$query = $this->app->db->name('BaseUserUpgrade');
return $query->where(['status' => 1])->order('number asc')->column('*', 'number');
$model = BaseUserUpgrade::mk()->where(['status' => 1]);
return $model->order('number asc')->column('*', 'number');
}
/**