admin/app/model/newModel/goods/Goods.php

220 lines
8.8 KiB
PHP
Raw 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
/** ZJMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2022-2032 四川正今科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.zjphp.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道官方颁发授权证书,无纸质授权凭证书视为侵权行为。
* =========================================================
*/
namespace app\model\newModel\goods;
use addon\fenxiao\newModel\Fenxiao;
use addon\supermember\model\MemberLevelOrder;
use app\model\member\MemberAccount as MemberAccountModel;
use app\model\NewBaseModel;
use think\facade\Db;
use think\facade\Log;
class Goods extends NewBaseModel{
protected $pk = 'id'; // 默认主键id
protected $name = 'goods';
protected $autoWriteTimestamp = false; // 开启自动时间戳
protected $deleteTime = false; // 软删除字段
/**
* Common: 获取器 —— 处理商品图片
* Author: wu-hui
* Time: 2022/12/06 13:50
* @param $value
* @return string
*/
public function getGoodsImageAttr($value){
$image = (string)explode(',',$value)[0];
return img($image);
}
/**
* Common: 商品选择器 —— 商品列表获取
* Author: wu-hui
* Time: 2022/12/06 16:24
* @return array
* @throws \think\db\exception\DbException
*/
public function selectList(){
// 参数获取
$page = input('page',1);
$pageSize = input('page_size',PAGE_LIST_ROWS);
$search = input("search",'');
$ids = input('ids','');// 已经选中的商品id
// 列表获取
$field = [
'goods_id',
'goods_name',
'goods_image',
'price',
'market_price'
];
$result = $this
->field($field)
->where('site_id',$this->site_id)
->where('is_delete',0)
->when(!empty($search),function($query) use ($search){
$query->where('goods_name','like',"%{$search}%");
})
->when(!empty($ids),function($query) use ($ids){
$ids = explode(',',$ids);
$query->whereNotIn('goods_id',$ids);
})
->order('sort DESC,goods_id DESC')
->paginate(['list_rows' => $pageSize,'page' => $page]);
if($result) $result = $result->toArray();
$list = [
'count' => $result['total'],
'list' => $result['data'],
'page_count' => $result['last_page'],
];
return $this->success($list);
}
/**
* Common: 根据id数组获取商品信息
* Author: wu-hui
* Time: 2022/12/06 16:05
* @param $ids
* @param string[] $field
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getGoodsList($ids,$field = ['*']){
$list = $this
->field($field)
->where('site_id',$this->site_id)
->where('is_delete',0)
->whereIn('goods_id',$ids)
->order('sort DESC,goods_id DESC')
->select();
return $list ? $list->toArray() : [];
}
/**
* Common: 购买商品 奖励积分
* Author: wu-hui
* Time: 2022/12/09 17:11
* @param $orderId
* @param $memberId
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function buyGoodsRewardIntegral($orderId,$memberId){
Log::debug('购买商品获取积分 ------ START ------ 订单id:'.$orderId);
// 获取全部相关的商品
$orderGoodsList = Db::name('order_goods')
->field('goods_id,num,real_goods_money')
->where('order_id',$orderId)
->select();
if($orderGoodsList) $orderGoodsList = $orderGoodsList->toArray();
$orderGoodsIds = array_column($orderGoodsList,'goods_id');
$orderGoodsList = array_column($orderGoodsList,NULL,'goods_id');
$field = [
'goods_id',
'integral_switch',
'integral_type',
'integral',
'integral_proportion',
'parent_integral_proportion',
'parent_integral_switch',
'parent_integral_type',
'parent_integral',
];
$goodsList = Db::name('goods')
->field($field)
->whereIn('goods_id',$orderGoodsIds)
->select();
if($goodsList) $goodsList = $goodsList->toArray();
// 获取上级用户id
$parentMemberId = (new Fenxiao())->getParentMemberId($memberId);
// 循环处理积分奖励
$siteId = $this->site_id;
foreach($goodsList as $item){
// 基本参数
//Log::debug('购买商品获取积分 - 商品id:'.$item['goods_id']);
$buyNum = (int)$orderGoodsList[$item['goods_id']]['num'] <= 0 ? 1 : (int)(int)$orderGoodsList[$item['goods_id']]['num'];
$realGoodsMoney = (float)$orderGoodsList[$item['goods_id']]['real_goods_money'] <= 0 ? 1 : (float)$orderGoodsList[$item['goods_id']]['real_goods_money'];
// 下单用户奖励积分
if($item['integral_switch'] == 1) {
// 积分计算类型0=百分比1=固定金额
if($item['integral_type'] == 1) $integral = (float)$item['integral'] * $buyNum;
else $integral = $realGoodsMoney * (float)($item['integral_proportion'] / 100);
// 奖励发放
//Log::debug('购买商品获取积分 - 下单人奖励积分:'.$integral);
if($integral > 0){
$remark = '购买商品奖励积分';
(new MemberAccountModel())->addMemberAccount($siteId, $memberId, 'point', $integral, 'buy_goods', 0, $remark);
}
}
// 下单用户上级奖励积分
if($item['parent_integral_switch'] == 1 && $parentMemberId > 0) {
// 积分计算类型0=百分比1=固定金额
if($item['parent_integral_type'] == 1) $parentIntegral = (float)$item['parent_integral'] * $buyNum;
else $parentIntegral = $realGoodsMoney * (float)($item['parent_integral_proportion'] / 100);
// 奖励发放
//Log::debug('购买商品获取积分 - 上级奖励积分:'.$parentIntegral);
if($parentIntegral > 0){
$remark = '购买商品奖励积分';
(new MemberAccountModel())->addMemberAccount($siteId, $parentMemberId, 'point', $parentIntegral, 'buy_goods', 0, $remark);
}
}
}
Log::debug('购买商品获取积分 ------ END ------ 订单id:'.$orderId);
}
/**
* Common: 购买商品 赠送会员卡
* Author: wu-hui
* Time: 2022/12/13 10:38
* @param $orderId
* @param $memberId
* @return false|void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function buyGoodsGiveMemberLevel($orderId,$memberId){
Log::debug('购买商品赠送会员卡 - 订单id:'.$orderId);
// 获取全部的商品id
$orderGoodsIds = Db::name('order_goods')->where('order_id',$orderId)->column('goods_id');
$goodsMemberLevelList = Db::name('goods')
->field('goods_id,give_member_level_id,give_member_level_index')
->whereIn('goods_id',$orderGoodsIds)
->where('give_member_level_id','>',0)
->select();
if($goodsMemberLevelList) $goodsMemberLevelList = $goodsMemberLevelList->toArray();
else return false;
// 循环处理数据
$memberLevelOrderModel = new MemberLevelOrder();
foreach($goodsMemberLevelList as $item){
// 生成会员卡订单
$data = [
'level_id' => $item['give_member_level_id'],
'period_index' => $item['give_member_level_index'],
'member_id' => $memberId,
'site_id' => $this->site_id,
'purchase_method' => 'exchange',// 购买方式
'is_balance' => 0,
];
$res = $memberLevelOrderModel->create($data);
$result = $memberLevelOrderModel->offlinePay($res['data']['out_trade_no']);
}
}
}