455 lines
16 KiB
PHP
455 lines
16 KiB
PHP
<?php
|
||
|
||
namespace app\api\logic\order;
|
||
|
||
use app\common\enum\OrderEnum;
|
||
use app\common\enum\PayEnum;
|
||
use app\common\enum\YesNoEnum;
|
||
use app\common\logic\BaseLogic;
|
||
use app\common\model\merchant\Merchant;
|
||
use app\common\model\order\Cart;
|
||
use app\common\model\store_branch_product\StoreBranchProduct;
|
||
use app\common\model\store_order\StoreOrder;
|
||
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
|
||
use app\common\model\store_product\StoreProduct;
|
||
use app\common\model\store_product_attr_value\StoreProductAttrValue;
|
||
use app\common\model\store_product_unit\StoreProductUnit;
|
||
use app\common\model\user\User;
|
||
use app\common\model\user\UserAddress;
|
||
use app\Request;
|
||
use support\Log;
|
||
use taoser\exception\ValidateException;
|
||
use think\Exception;
|
||
use think\facade\Db;
|
||
use Yansongda\Pay\Event\PayEnd;
|
||
|
||
/**
|
||
* 订单逻辑
|
||
* Class OrderLogic
|
||
* @package app\api\logic\order
|
||
*/
|
||
class OrderLogic extends BaseLogic
|
||
{
|
||
public static $total;
|
||
/**
|
||
* @notes 获取购物车商品信息
|
||
* @param $params
|
||
* @return array|bool
|
||
*/
|
||
static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [])
|
||
{
|
||
|
||
$where = ['is_pay' => 0, 'is_del' => 0];
|
||
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('product_id as goods,cart_num')->select()->toArray();
|
||
if (empty($cart_select)) {
|
||
self::setError('购物车为空');
|
||
return false;
|
||
}
|
||
try {
|
||
self::$total = 0;
|
||
/** 计算价格 */
|
||
foreach ($cart_select as $k => $v) {
|
||
$find = StoreBranchProduct::where(['id' => $v['goods']])->field('store_name,image,unit,price')->find();
|
||
if(!$find){
|
||
continue;
|
||
}
|
||
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['price'], 2);//钱
|
||
$cart_select[$k]['price'] = $find['price'];
|
||
$cart_select[$k]['product_id'] = $find['goods'];
|
||
$cart_select[$k]['old_cart_id'] = implode(',',$cartId);
|
||
$cart_select[$k]['cart_num'] = $v['cart_num'];
|
||
$cart_select[$k]['verify_code'] = $params['verify_code'];
|
||
//理论上每笔都是拆分了
|
||
// $cart_select[$k]['name'] = $find['store_name'];
|
||
// $cart_select[$k]['imgs'] = $find['image'];
|
||
// $cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name');
|
||
|
||
self::$total=bcadd(self::$total, $cart_select[$k]['total'], 2);
|
||
}
|
||
$order = [
|
||
'add_time' => time(),
|
||
'create_time' => time(),
|
||
'order_id' => getNewOrderId('PF'),
|
||
'total_price' => self::$total,//总价
|
||
'pay_price' => self::$total,//后期可能有降价抵扣
|
||
'total_num' => count($cart_select),//总数
|
||
'pay_type' => $params['pay_type'] ?? 0,
|
||
'cart_id' => implode(',', $cartId),
|
||
'store_id'=>$params['store_id'],
|
||
'shipping_type'=>$params['shipping_type']//配送方式 1=快递 ,2=门店自提
|
||
// 'delivery_msg'=>' 预计48小时发货 '
|
||
];
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
return ['order' => $order, 'cart_list' => $cart_select];
|
||
}
|
||
|
||
/**
|
||
* 创建新订单
|
||
* @return Object|bool|array
|
||
*/
|
||
static public function createOrder($cartId, $addressId, $user = null, $params = [])
|
||
{
|
||
$verify_code = generateUniqueVerificationCode();
|
||
$params['verify_code'] = $verify_code;
|
||
$orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params);
|
||
if(!$orderInfo){
|
||
return false;
|
||
}
|
||
// `delivery_name`快递名称/送货人姓名',
|
||
// `delivery_code`'快递公司编码',
|
||
// `delivery_type` '发货类型',
|
||
// `delivery_id'快递单号/手机号',
|
||
$_order = $orderInfo['order'];
|
||
if($orderInfo['order']['shipping_type'] == 1){
|
||
$_order['delivery_name'] = $params['delivery_name'];
|
||
$_order['delivery_id'] = $params['delivery_id'];
|
||
}
|
||
$_order['deduction_price'] = 0;
|
||
$_order['uid'] = request()->userId;
|
||
$user = User::where('id',\request()->userId)->find();
|
||
$_order['real_name'] = $user['real_name'];
|
||
$_order['mobile'] = $user['mobile'];
|
||
$_order['pay_type'] = $user['pay_type'];
|
||
$_order['verify_code'] = $verify_code;
|
||
|
||
if($addressId>0){
|
||
$address=UserAddress::where(['address_id'=>$addressId,'uid'=>Request()->userId])->find();
|
||
if($address){
|
||
$_order['real_name'] = $address['real_name'];
|
||
$_order['user_phone'] = $address['phone'];
|
||
$_order['user_address'] = $address['detail'];
|
||
}
|
||
}
|
||
// if($params['pay_type']==PayEnum::WECHAT_PAY_BARCODE){
|
||
// $_order['source']=1;
|
||
// }
|
||
// if($params['pay_type']==PayEnum::CASH_PAY){
|
||
// $_order['money']=$_order['total'];
|
||
// }
|
||
Db::startTrans();
|
||
try {
|
||
$order = StoreOrder::create($_order);
|
||
$goods_list = $orderInfo['cart_list'];
|
||
foreach ($goods_list as $k => $v) {
|
||
$goods_list[$k]['oid'] = $order->id;
|
||
$goods_list[$k]['uid'] = request()->userId;
|
||
$goods_list[$k]['cart_id'] = implode(',',$cartId);
|
||
$goods_list[$k]['delivery_id'] = $params['store_id'];//商家id
|
||
|
||
}
|
||
(new StoreOrderCartInfo())->saveAll($goods_list);
|
||
$where = ['is_pay' => 0, 'is_del' => 0];
|
||
Cart::whereIn('cart_id', $cartId)->where($where)->update(['is_pay'=>1]);
|
||
Db::commit();
|
||
return $order;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 获取购货订单购物车商品信息
|
||
* @param $params
|
||
* @return array
|
||
*/
|
||
static public function cartIdByPurchaseOrderInfo($user, $params)
|
||
{
|
||
if (!$user) {
|
||
self::setError('没有用户信息,请先登录');
|
||
return false;
|
||
}
|
||
$mer_id = $user['merchant']['mer_id'];
|
||
$where1 = ['paid' => 1,'is_opurchase'=>0];
|
||
$where1[] = ['pay_type','<>',9];
|
||
|
||
$arrs = Cashierclass::where('merchant', $mer_id)->whereDay('create_time')->where($where1)->column('cart_id,id,address_id');
|
||
// $order_id = Cashierclass::where('merchant', $mer_id)->whereDay('create_time')->where($where1)->column('id');
|
||
$cart_arr = [];
|
||
$order_id = [];
|
||
foreach ($arrs as $k => $v) {
|
||
if (empty($v['cart_id'])) {
|
||
self::setError('没有购物车信息');
|
||
return false;
|
||
}
|
||
// if (empty($v['address_id'])) {
|
||
// self::setError('请先设置配送地址');
|
||
// return false;
|
||
// }
|
||
$arr = explode(',',$v['cart_id']);
|
||
foreach ($arr as $kk => $vv) {
|
||
$cart_arr[] = $vv;
|
||
}
|
||
$order_id[] = $v['id'];
|
||
}
|
||
$where = ['is_pay' => 1, 'is_fail' => 0];
|
||
$cart_select = Cart::whereIn('cart_id', $cart_arr)->where($where)->field('goods_id as goods,cart_num')->select()->toArray();
|
||
if (empty($cart_select)) {
|
||
self::setError('购物车为空');
|
||
return false;
|
||
}
|
||
/** 计算价格 */
|
||
foreach ($cart_select as $k => $v) {
|
||
$sell = Goods::where(['id' => $v['goods']])->value('sell');
|
||
$cart_select[$k]['total'] = bcmul($v['cart_num'], $sell, 2);
|
||
$cart_select[$k]['price'] = $sell;
|
||
}
|
||
$order = [
|
||
'time' => time(),
|
||
'number' => static::getNewOrderId('CG'),
|
||
'total' => array_sum(array_column($cart_select, 'total')),
|
||
'pay_type' => $params['pay_type'] ?? 0,
|
||
'cart_id' => implode(',', $cart_arr),
|
||
'order_arr' => implode(',', $order_id)
|
||
];
|
||
return ['order' => $order, 'cart_list' => $cart_select];
|
||
}
|
||
/**
|
||
* 创建购货订单
|
||
* @return Object|bool
|
||
*/
|
||
static public function createOpurchaseOrder($user = null, $params = [])
|
||
{
|
||
|
||
if (!$user) {
|
||
self::setError('没有用户信息,请先登录');
|
||
return false;
|
||
}
|
||
$mer_id = $user['merchant']['mer_id'];
|
||
// $merchant = Merchant::where('mer_id', $mer_id)->find();
|
||
$orderInfo = self::cartIdByPurchaseOrderInfo($user, $params);
|
||
if (!$orderInfo) {
|
||
return false;
|
||
}
|
||
$_order = $orderInfo['order'];
|
||
// if ($_order['total'] < $merchant['mer_money']) {
|
||
// self::setError('商户余额不足');
|
||
// return false;
|
||
// }
|
||
$_order['merchant'] = $mer_id;
|
||
$_order['money'] = $_order['total'];
|
||
$_order['actual'] = $_order['total'];
|
||
$_order['paid'] = 1;
|
||
Db::startTrans();
|
||
try {
|
||
$order = Opurchaseclass::create($_order);
|
||
|
||
$goods_list = $orderInfo['cart_list'];
|
||
foreach ($goods_list as $k => $v) {
|
||
$goods_list[$k]['nums'] = $v['cart_num'];
|
||
$goods_list[$k]['pid'] = $order->id;
|
||
}
|
||
(new Opurchaseinfo())->saveAll($goods_list);
|
||
// $merchant->mer_money = bcsub($merchant->mer_money, $_order['total'], 2);
|
||
$order_arr = explode(',', $_order['order_arr']);
|
||
Cashierclass::where('id', 'in', $order_arr)->update(['is_opurchase' => 1]);
|
||
Db::commit();
|
||
return $order;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
/**
|
||
* @notes 获取订单号
|
||
* @param $type
|
||
* @return string
|
||
* @author likeadmin
|
||
* @date 2021/7/28 17:05
|
||
*/
|
||
static public function getNewOrderId($type)
|
||
{
|
||
list($msec, $sec) = explode(' ', microtime());
|
||
$msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
|
||
$orderId = $type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
|
||
return $orderId;
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 余额订单支付
|
||
* @param User $user
|
||
* @param $order
|
||
* @return bool
|
||
* @throws Exception
|
||
* @throws ValidateException
|
||
*/
|
||
static public function payBalance(User $user, $order)
|
||
{
|
||
if ($user['user_money'] < $order['actual']) {
|
||
self::setError('余额不足,请更换支付方式');
|
||
return false;
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
$user->user_money = bcsub($user->user_money, $order['actual'], 2);
|
||
$user->save();
|
||
Db::commit();
|
||
return true;
|
||
} catch (Exception $e) {
|
||
Db::rollback();
|
||
Log::error('余额支付失败' . $e->getMessage() . '。line:' . $e->getLine() . '。file:' . $e->getFile());
|
||
self::setError('余额支付失败' . $e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 订单支付成功
|
||
* @param $order 订单
|
||
* @param $CallbackData 回调数据
|
||
* @date 2021/7/8 00:40
|
||
*/
|
||
static function paySuccess($order, $CallbackData = [])
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$order->money = $CallbackData['money'];
|
||
$order->paid = 1;
|
||
$order->save();
|
||
Log::info('支付成功');
|
||
// 提交事务
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
// 回滚事务
|
||
Db::rollback();
|
||
Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine());
|
||
self::setError('支付失败' . $e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取用户常用购买记录
|
||
*/
|
||
public static function frequentlyPurchase($params)
|
||
{
|
||
try {
|
||
$goods_id = StoreOrderCartInfo::where('uid', Request()->userId)->page($params['page_no'])->limit(50)->column('product_id');
|
||
if(!$goods_id){
|
||
return [];
|
||
}
|
||
$goods_arr = array_unique($goods_id);
|
||
$select = StoreProduct::where('id', 'in', $goods_arr)->with('unitName')->field('id,name,sell,imgs,unit')->select();
|
||
return $select->toArray();
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static function purchaseAgain($order_id){
|
||
$arr= StoreOrderCartInfo::where('oid',$order_id)->field('product_id,cart_num,staff_id')->select();
|
||
$data=[];
|
||
foreach ($arr as $k=>$v){
|
||
$data[$k]['product_id']=$v['product_id'];
|
||
$unique = StoreProductAttrValue::where('product_id',$v['product_id'])->value('v');
|
||
$data[$k]['product_attr_unique']=$unique;
|
||
$data[$k]['cart_num']=$v['cart_num'];
|
||
$data[$k]['type']='';
|
||
$data[$k]['uid']=Request()->userId;
|
||
$store_id = StoreProduct::where('id',$v['product_id'])->value('store_id');
|
||
$data[$k]['store_id']=$store_id;
|
||
$data[$k]['staff_id']=$v['staff_id'];
|
||
$data[$k]['add_time']=time();
|
||
$data[$k]['combination_id']=0;
|
||
$data[$k]['seckill_id']=0;
|
||
$data[$k]['bargain_id']=0;
|
||
$data[$k]['discount_id']=0;
|
||
$data[$k]['status']=1;
|
||
$data[$k]['staff_id']=0;
|
||
$data[$k]['is_new']=0;
|
||
$data[$k]['is_del']=0;
|
||
$data[$k]['is_pay']=0;
|
||
}
|
||
if($data){
|
||
( new Cart())->saveAll($data);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
public static function detail($params): array
|
||
{
|
||
$find=StoreOrder::where($params)->findOrEmpty()->toArray();
|
||
if($find){
|
||
|
||
$find['goods_list']= StoreOrderCartInfo::where('oid',$find['id'])
|
||
->with('goodsName')
|
||
->field('product_id,cart_num nums')->select()->each(function($item){
|
||
$item['msg']='预计48小时发货';
|
||
$item['unit_name']=StoreProductUnit::where('id',$item['unit'])->value('name');
|
||
});
|
||
$merchant=Merchant::where('mer_id',$find['merchant'])->field('mer_id,uid,mer_name,service_phone,mer_address')->find();
|
||
$merchant['real_name']=User::where('id',$merchant['uid'])->value('real_name');
|
||
$find['merchant_info']=$merchant;
|
||
|
||
}
|
||
return $find;
|
||
}
|
||
|
||
|
||
//核销
|
||
|
||
/**
|
||
* @param $params
|
||
* @param $uid
|
||
* @return bool
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
* @author: codeliu
|
||
* @Time: 2024/6/3 22:42
|
||
*/
|
||
public static function writeOff($params,$uid): bool
|
||
{
|
||
$data = StoreOrderCartInfo::where([
|
||
'oid'=>$params['order_id'],
|
||
'verify_code'=>$params['verify_code'],
|
||
'uid'=>$uid
|
||
])->select()->toArray();
|
||
if (empty($data)){
|
||
return false;
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
$newArr = [];
|
||
$oid = [];
|
||
foreach ($data as $k =>$value){
|
||
$oid [] = $value['oid'];
|
||
$newArr[$k]['writeoff_time'] = time();
|
||
$newArr[$k]['is_writeoff'] = YesNoEnum::YES;
|
||
$newArr[$k]['update_time'] = time();
|
||
}
|
||
(new StoreOrderCartInfo())->saveAll($newArr);
|
||
$oidArr = array_values(array_unique($oid));
|
||
StoreOrder::whereIn('id',$oidArr)
|
||
->update([
|
||
'status' => OrderEnum::RECEIVED_GOODS,
|
||
'update_time' => time(),
|
||
]);
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|