|
|
@@ -11,8 +11,15 @@
|
|
|
|
|
|
namespace App\Services\Common;
|
|
|
|
|
|
+use App\Models\AccountModel;
|
|
|
+use App\Models\GoodsModel;
|
|
|
+use App\Models\MemberModel;
|
|
|
use App\Models\TradeModel;
|
|
|
use App\Services\BaseService;
|
|
|
+use App\Services\ConfigService;
|
|
|
+use \App\Services\Common\FinanceService;
|
|
|
+use App\Services\RedisService;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
/**
|
|
|
* 交易管理-服务类
|
|
|
@@ -49,6 +56,177 @@ class TradeService extends BaseService
|
|
|
return self::$instance;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $params
|
|
|
+ * @param int $pageSize
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDataList($params, $pageSize = 15)
|
|
|
+ {
|
|
|
+ $where = ['a.mark' => 1];
|
|
|
+ $status = isset($params['status']) ? $params['status'] : 0;
|
|
|
+ $userId = isset($params['user_id']) ? $params['user_id'] : 0;
|
|
|
+ $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
|
|
|
+ $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
|
|
|
+ $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
|
|
|
+ $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
|
|
|
+ if ($shopId > 0) {
|
|
|
+ $where['a.shop_id'] = $shopId;
|
|
|
+ }
|
|
|
+ if ($parentId > 0) {
|
|
|
+ $where['b.parent_id'] = $parentId;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($isAppeal > 0) {
|
|
|
+ $where['a.is_appeal'] = $isAppeal;
|
|
|
+ }
|
|
|
+ if ($status > 0) {
|
|
|
+ $where['a.status'] = $status;
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = $this->model->from('trade as a')
|
|
|
+ ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
|
|
|
+ ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
|
|
|
+ ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
|
|
|
+ ->where($where)
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
+ $keyword = isset($params['keyword']) ? $params['keyword'] : '';
|
|
|
+ if ($keyword) {
|
|
|
+ $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")->orWhere('g.goods_name', 'like', "%{$keyword}%");
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
+ $time = isset($params['time']) ? $params['time'] : 0;
|
|
|
+ if ($time) {
|
|
|
+ $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->where(function ($query) use ($userId,$sellUid, $status) {
|
|
|
+
|
|
|
+ if ($sellUid && $userId) {
|
|
|
+ $query->where(function($query) use($userId, $sellUid){
|
|
|
+ $query->where('a.user_id', $userId)->orWhere('a.sell_uid', $sellUid);
|
|
|
+ })->where('a.status','<=', 2);
|
|
|
+ }else if($userId){
|
|
|
+ $query->where('a.user_id', '=', $userId);
|
|
|
+ }else if($sellUid){
|
|
|
+ $query->where('a.sell_uid', '=', $sellUid);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'c.nickname as sell_nickname', 'c.mobile as sell_mobile', 'g.goods_name', 'g.code', 'g.thumb'])
|
|
|
+ ->orderBy('a.pay_time', 'desc')
|
|
|
+ ->orderBy('a.id', 'desc')
|
|
|
+ ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+ $list = $list ? $list->toArray() : [];
|
|
|
+ if ($list) {
|
|
|
+ foreach ($list['data'] as &$item) {
|
|
|
+ $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
|
|
|
+ $item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
|
|
|
+ $item['confirm_time'] = $item['confirm_time'] ? datetime($item['confirm_time'], 'Y-m-d H:i:s') : '';
|
|
|
+ $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
|
|
|
+ $item['price'] = intval($item['price']);
|
|
|
+ $item['real_price'] = intval($item['real_price']);
|
|
|
+ $item['fee'] = intval($item['fee']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'pageSize' => $pageSize,
|
|
|
+ 'total' => isset($list['total']) ? $list['total'] : 0,
|
|
|
+ 'counts' => [
|
|
|
+ ''
|
|
|
+ ],
|
|
|
+ 'list' => isset($list['data']) ? $list['data'] : []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 详情
|
|
|
+ * @param $id
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getInfo($id)
|
|
|
+ {
|
|
|
+ $info = $this->model->from('trade as a')
|
|
|
+ ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
|
|
|
+ ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
|
|
|
+ ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
|
|
|
+ ->where(['a.id'=>$id,'a.mark'=>1])
|
|
|
+ ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'c.nickname as sell_nickname', 'c.mobile as sell_mobile', 'g.goods_name', 'g.code', 'g.thumb'])
|
|
|
+ ->first();
|
|
|
+ if($info){
|
|
|
+ $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
|
|
|
+ $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
|
|
|
+ $info['confirm_time'] = $info['confirm_time'] ? datetime($info['confirm_time'], 'Y-m-d H:i:s') : '';
|
|
|
+ $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
|
|
|
+ $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
|
|
|
+ $info['price'] = intval($info['price']);
|
|
|
+ $info['real_price'] = intval($info['real_price']);
|
|
|
+ $info['fee'] = intval($info['fee']);
|
|
|
+
|
|
|
+ $info['bank_info'] = MemberBankService::make()->getBindInfo($info['sell_uid']);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计
|
|
|
+ * @param $params
|
|
|
+ * @return int[]
|
|
|
+ */
|
|
|
+ public function getCounts($params)
|
|
|
+ {
|
|
|
+ $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
|
|
|
+ $where = ['a.mark' => 1];
|
|
|
+ $status = isset($params['status']) ? $params['status'] : 0;
|
|
|
+ $userId = isset($params['user_id']) ? $params['user_id'] : 0;
|
|
|
+ $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
|
|
|
+ $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
|
|
|
+ $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
|
|
|
+ $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
|
|
|
+ if ($shopId > 0) {
|
|
|
+ $where['a.shop_id'] = $shopId;
|
|
|
+ }
|
|
|
+ if ($parentId > 0) {
|
|
|
+ $where['b.parent_id'] = $parentId;
|
|
|
+ }
|
|
|
+ if ($userId > 0) {
|
|
|
+ $where['a.user_id'] = $userId;
|
|
|
+ }
|
|
|
+ if ($sellUid > 0) {
|
|
|
+ $where['a.sell_uid'] = $sellUid;
|
|
|
+ }
|
|
|
+ if ($isAppeal > 0) {
|
|
|
+ $where['a.is_appeal'] = $isAppeal;
|
|
|
+ }
|
|
|
+ if ($status > 0) {
|
|
|
+ $where['a.status'] = $status;
|
|
|
+ }
|
|
|
+ $type = isset($params['type']) ? $params['type'] : 0;
|
|
|
+
|
|
|
+ $counts['total'] = intval($this->model->from('trade as a')
|
|
|
+ ->where($where)
|
|
|
+ ->sum('real_price'));
|
|
|
+
|
|
|
+
|
|
|
+ $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
|
|
|
+ $bonusRate = $bonusRate ? $bonusRate : 5;
|
|
|
+ $counts['bonus'] = intval($counts['total'] * $bonusRate / 100);
|
|
|
+ $counts['fee'] = intval($this->model->from('trade as a')
|
|
|
+ ->where($where)
|
|
|
+ ->sum('fee'));
|
|
|
+
|
|
|
+ $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
|
|
|
+ $serviceRate = $serviceRate ? $serviceRate : 8;
|
|
|
+ $counts['service_fee'] = intval($counts['total'] * $serviceRate / 100);
|
|
|
+
|
|
|
+
|
|
|
+ return $counts;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 添加或编辑
|
|
|
* @return array
|
|
|
@@ -64,14 +242,13 @@ class TradeService extends BaseService
|
|
|
/**
|
|
|
* 获取店铺交易统计
|
|
|
* @param $shopId
|
|
|
- * @param int $type
|
|
|
- * @param int $coinType
|
|
|
+ * @param int $status
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function getShopTradeTotal($shopId, $status=0)
|
|
|
+ public function getShopTradeTotal($shopId, $status = 0)
|
|
|
{
|
|
|
- $where = ['shop_id'=>$shopId,'mark'=>1];
|
|
|
- if($status){
|
|
|
+ $where = ['shop_id' => $shopId, 'mark' => 1];
|
|
|
+ if ($status) {
|
|
|
$where['status'] = $status;
|
|
|
}
|
|
|
return $this->model->where($where)
|
|
|
@@ -81,14 +258,13 @@ class TradeService extends BaseService
|
|
|
/**
|
|
|
* 获取交易数
|
|
|
* @param $shopId
|
|
|
- * @param int $type
|
|
|
- * @param int $coinType
|
|
|
+ * @param int $status
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function getShopTradeCount($shopId, $status = 0)
|
|
|
{
|
|
|
- $where = ['shop_id'=>$shopId,'mark'=>1];
|
|
|
- if($status){
|
|
|
+ $where = ['shop_id' => $shopId, 'mark' => 1];
|
|
|
+ if ($status) {
|
|
|
$where['status'] = $status;
|
|
|
}
|
|
|
return $this->model->where($where)
|
|
|
@@ -96,24 +272,446 @@ class TradeService extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 抢拍交易订单数
|
|
|
- * @param $userId 用户ID
|
|
|
- * @param int $status 状态
|
|
|
+ * 获取用户交易统计
|
|
|
+ * @param $userId
|
|
|
+ * @param int $status
|
|
|
+ * @param int $time
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function getNewTradeCountByStatus($userId, $status=0, $time=30)
|
|
|
+ public function getUserTradeTotal($userId, $status = 0, $time = 0)
|
|
|
{
|
|
|
- $where = ['user_id'=>$userId,'mark'=>1,'is_read'=>0];
|
|
|
+ $where = ['user_id' => $userId, 'mark' => 1];
|
|
|
return $this->model->where($where)
|
|
|
- ->where(function($query) use($status){
|
|
|
- $query->whereIn('status',is_array($status)? $status:[$status]);
|
|
|
+ ->where(function ($query) use ($status, $time) {
|
|
|
+ $query->whereIn('status', is_array($status) ? $status : [$status]);
|
|
|
+
|
|
|
+ // 本月
|
|
|
+ if ($time == 1) {
|
|
|
+ $query->where('pay_time', '>=', strtotime(date('Y-m-01')));
|
|
|
+ } // 今日
|
|
|
+ else if ($time == 2) {
|
|
|
+ $query->where('pay_time', '>=', strtotime(date('Y-m-d')));
|
|
|
+ }
|
|
|
})
|
|
|
- ->where(function($query) use($time){
|
|
|
- if($time){
|
|
|
- $query->where('update_time','>', $time);
|
|
|
+ ->sum('price');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户团队交易统计
|
|
|
+ * @param $userId
|
|
|
+ * @param int $status
|
|
|
+ * @param int $time
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getTeamTradeTotal($userId, $status = 0, $time = 0)
|
|
|
+ {
|
|
|
+ $where = ['b.parent_id' => $userId, 'a.mark' => 1];
|
|
|
+ return $this->model->from('trade as a')
|
|
|
+ ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
|
|
|
+ ->where($where)
|
|
|
+ ->where(function ($query) use ($status, $time) {
|
|
|
+ $query->whereIn('a.status', is_array($status) ? $status : [$status]);
|
|
|
+
|
|
|
+ // 本月
|
|
|
+ if ($time == 1) {
|
|
|
+ $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
|
|
|
+ } // 今日
|
|
|
+ else if ($time == 2) {
|
|
|
+ $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
|
|
|
}
|
|
|
})
|
|
|
+ ->sum('a.price');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 抢拍交易订单数
|
|
|
+ * @param $userId 用户ID
|
|
|
+ * @param int $status 状态
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getNewTradeCountByStatus($userId, $status = 0)
|
|
|
+ {
|
|
|
+ $where = ['user_id' => $userId, 'mark' => 1, 'is_read' => 0];
|
|
|
+ $counts = $this->model->where($where)
|
|
|
+ ->where(function ($query) use ($status) {
|
|
|
+ $query->whereIn('status', is_array($status) ? $status : [$status]);
|
|
|
+ })
|
|
|
+ ->select(['status', DB::raw('count(*) as count')])
|
|
|
->groupBy('status')
|
|
|
- ->count();
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ if ($counts) {
|
|
|
+ $temps = ['status1' => 0, 'status2' => 0, 'status3' => 0];
|
|
|
+ foreach ($counts as $v) {
|
|
|
+ $temps['status' . $v['status']] = $v['count'];
|
|
|
+ }
|
|
|
+ $counts = $temps;
|
|
|
+ } else {
|
|
|
+ $counts = ['status1' => 0, 'status2' => 0, 'status3' => 0];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $counts;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 抢购
|
|
|
+ * @param $params
|
|
|
+ * @param $userId
|
|
|
+ * @param $shopId
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function buy($params, $userId, $shopId)
|
|
|
+ {
|
|
|
+ $goodsId = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $info = GoodsService::make()->getInfo(['id' => $goodsId, 'status' => 1, 'mark' => 1]);
|
|
|
+ $goodsUserId = isset($info['user_id']) ? $info['user_id'] : 0;
|
|
|
+ $isTrade = isset($info['is_trade']) ? $info['is_trade'] : 0;
|
|
|
+ if (empty($info)) {
|
|
|
+ $this->error = 2031;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($isTrade == 1) {
|
|
|
+ $this->error = 2032;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($goodsUserId == $userId) {
|
|
|
+ $this->error = 2036;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $shopInfo = ShopService::make()->getInfo($shopId);
|
|
|
+ if (empty($shopInfo)) {
|
|
|
+ $this->error = 2033;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 营业时间
|
|
|
+ $curTime = time();
|
|
|
+ $startTime = isset($shopInfo['start_time']) ? $shopInfo['start_time'] : '';
|
|
|
+ $endTime = isset($shopInfo['end_time']) ? $shopInfo['end_time'] : '';
|
|
|
+
|
|
|
+ // 店长自己抢
|
|
|
+ if ($shopInfo['user_id'] == $userId) {
|
|
|
+ $snapTime = ConfigService::make()->getConfigByCode('snap_time');
|
|
|
+ $snapTime = $snapTime ? $snapTime : 5;
|
|
|
+ $curTime = strtotime(date('Y-m-d') . ' ' . $startTime) + 1;
|
|
|
+ if (time() < strtotime(date('Y-m-d') . ' ' . $startTime) - $snapTime * 60) {
|
|
|
+ $this->error = 2034;
|
|
|
+ return false;
|
|
|
+ } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
|
|
|
+ $this->error = 2035;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (time() < strtotime(date('Y-m-d') . ' ' . $startTime)) {
|
|
|
+ $this->error = 2034;
|
|
|
+ return false;
|
|
|
+ } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
|
|
|
+ $this->error = 2035;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 验证收款账号
|
|
|
+ if (!MemberBankService::make()->getBindInfo($userId)) {
|
|
|
+ $this->error = 2037;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
|
|
|
+ $feeRate = $feeRate ? $feeRate : '2.5';
|
|
|
+ $realPrice = intval($info['price'] - $info['sell_price'] + $info['source_price']);
|
|
|
+ $fee = round($realPrice * $feeRate / 100, 0);
|
|
|
+
|
|
|
+ $lockCacheKey = "caches:trade:lock:{$goodsId}";
|
|
|
+ if (RedisService::get($lockCacheKey)) {
|
|
|
+ $this->error = 2032;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ RedisService::set($lockCacheKey, $info, rand(1, 2));
|
|
|
+ $data = [
|
|
|
+ 'order_sn' => get_order_num('T'),
|
|
|
+ 'goods_id' => $goodsId,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'shop_id' => $shopId,
|
|
|
+ 'sell_uid' => $info['user_id'],
|
|
|
+ 'num' => 1,
|
|
|
+ 'price' => $info['price'],
|
|
|
+ 'source_price' => $info['source_price'],
|
|
|
+ 'sell_price' => $info['sell_price'],
|
|
|
+ 'real_price' => $realPrice,
|
|
|
+ 'fee' => $fee,
|
|
|
+ 'new_price' => $info['price'],
|
|
|
+ 'remark' => '抢拍交易',
|
|
|
+ 'create_time' => $curTime,
|
|
|
+ 'update_time' => $curTime,
|
|
|
+ 'status' => 1
|
|
|
+ ];
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ if (!TradeModel::insertGetId($data)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2040;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!GoodsModel::where(['id' => $goodsId])->update(['is_trade' => 1, 'update_time' => time()])) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2040;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ RedisService::keyDel($lockCacheKey);
|
|
|
+ $this->error = 2041;
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 付款
|
|
|
+ * @param $params
|
|
|
+ * @param $userId
|
|
|
+ * @param $shopId
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function pay($params, $userId, $shopId)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $payImg = isset($params['pay_img']) ? $params['pay_img'] : '';
|
|
|
+ if (empty($payImg)) {
|
|
|
+ $this->error = 2043;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
|
|
|
+ if (empty($id) || empty($info)) {
|
|
|
+ $this->error = 2042;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($info['status'] != 1) {
|
|
|
+ $this->error = 2044;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($info['user_id'] != $userId) {
|
|
|
+ $this->error = 2045;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->model->where(['id' => $id, 'mark' => 1])->update(['pay_time' => time(), 'pay_img' => $payImg, 'status' => 2, 'update_time' => time()])) {
|
|
|
+ $this->error = 2046;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2047;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 确认收款
|
|
|
+ * @param $params
|
|
|
+ * @param $userId
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function confirm($params, $userId)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
|
|
|
+ if (empty($id) || empty($info)) {
|
|
|
+ $this->error = 2042;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!in_array($info['status'], [1, 2])) {
|
|
|
+ $this->error = 2044;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+// if($info['sell_uid'] != $userId){
|
|
|
+// $this->error = 2045;
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ $data = ['confirm_time' => time(), 'status' => 3, 'update_time' => time()];
|
|
|
+ if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
|
|
|
+ $this->error = 2050;
|
|
|
+ DB::rollBack();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更改商品归属人
|
|
|
+ if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id'=> $info['user_id'],'update_time'=> time()])) {
|
|
|
+ $this->error = 2050;
|
|
|
+ DB::rollBack();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 佣金结算
|
|
|
+ $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
|
|
|
+ $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
|
|
|
+ $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
|
|
|
+ if ($memberInfo && $parentId && $parentInfo) {
|
|
|
+ $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
|
|
|
+ $bonusRate = $bonusRate ? $bonusRate : 5;
|
|
|
+ $bonus = $info['real_price'] * $bonusRate / 100;
|
|
|
+
|
|
|
+ if ($bonus > 0) {
|
|
|
+ if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update(['bonus' => $parentInfo['bonus'] + $bonus, 'update_time' => time()])) {
|
|
|
+ $this->error = 2051;
|
|
|
+ DB::rollBack();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'user_id' => $parentId,
|
|
|
+ 'shop_id' => $info['shop_id'],
|
|
|
+ 'source_uid' => $userId,
|
|
|
+ 'source_order_sn' => $info['order_sn'],
|
|
|
+ 'type' => 2,
|
|
|
+ 'coin_type' => 2,
|
|
|
+ 'money' => $bonus,
|
|
|
+ 'balance' => $parentInfo['bonus'],
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'update_time' => time(),
|
|
|
+ 'remark' => '交易成功佣金结算',
|
|
|
+ 'status' => 1,
|
|
|
+ 'mark' => 1
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (!AccountModel::insertGetId($data)) {
|
|
|
+ $this->error = 2051;
|
|
|
+ DB::rollBack();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 结算统计
|
|
|
+// FinanceService::make()->settle($bonus, 1);
|
|
|
+ FinanceService::make()->settleBonus($bonus, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ $this->error = 2049;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 申请待售
|
|
|
+ * @param $params
|
|
|
+ * @param $userId
|
|
|
+ * @return false
|
|
|
+ */
|
|
|
+ public function sell($params, $userId)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
|
|
|
+ if (empty($id) || empty($info)) {
|
|
|
+ $this->error = 2042;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($info['status'] != 3) {
|
|
|
+ $this->error = 2053;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($info['user_id'] != $userId) {
|
|
|
+ $this->error = 2045;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 1, 'update_time' => time()])) {
|
|
|
+ $this->error = 2054;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2055;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 申请待售审核
|
|
|
+ * @param $params
|
|
|
+ * @param $userId
|
|
|
+ * @return false
|
|
|
+ */
|
|
|
+ public function sellConfirm($params)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
|
|
|
+ if (empty($id) || empty($info)) {
|
|
|
+ $this->error = 2042;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($info['status'] != 3) {
|
|
|
+ $this->error = 2053;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ $priceRate = ConfigService::make()->getConfigByCode('price_rate');
|
|
|
+ $priceRate = $priceRate? $priceRate : 4;
|
|
|
+
|
|
|
+ $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
|
|
|
+ $stopSplitPrice = $stopSplitPrice? $stopSplitPrice : 500;
|
|
|
+
|
|
|
+ // 判断是否可以上架或拆分
|
|
|
+ $realPrice = $info['real_price'];
|
|
|
+ $price = $info['price']; // 特价
|
|
|
+ $addPrice = $realPrice*$priceRate/100;
|
|
|
+
|
|
|
+ // 满足涨价上架
|
|
|
+ if($price+$addPrice < $info['split_price']){
|
|
|
+ if (!$this->model->where(['id' => $id])->update(['status' => 4,'new_price'=> $price+$addPrice,'real_price'=> $realPrice + $addPrice, 'is_sell' => 2, 'update_time' => time()])) {
|
|
|
+ $this->error = 2056;
|
|
|
+ DB::rollBack();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!GoodsModel::where(['id' => $info['goods_id']])->update(['last_sell_time'=>time(),'real_price' => $realPrice + $addPrice, 'price' => $price+$addPrice,'is_trade'=> 2, 'update_time' => time()])) {
|
|
|
+ $this->error = 2056;
|
|
|
+ DB::rollBack();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ $this->error = 2057;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ // 停止拆分
|
|
|
+ else if($info['sell_price'] == $stopSplitPrice){
|
|
|
+ if (!GoodsModel::where(['id' => $id])->update(['status' => 2,'stop_split'=>1, 'update_time' => time()])) {
|
|
|
+ $this->error = 2054;
|
|
|
+ DB::rollBack();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 满足拆分
|
|
|
+ else {
|
|
|
+ if(!GoodsService::make()->split($info['goods_id'])){
|
|
|
+ $this->error = 2058;
|
|
|
+ DB::rollBack();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2059;
|
|
|
+ DB::commit();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $this->error = 2056;
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|