| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AccountModel;
- use App\Models\GoodsModel;
- use App\Models\MemberModel;
- use App\Models\OrderModel;
- use App\Models\ScoreGoodsModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use Illuminate\Support\Facades\DB;
- /**
- * 积分商城订单管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class OrderService
- * @package App\Services\Common
- */
- class OrderService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * OrderService constructor.
- */
- public function __construct()
- {
- $this->model = new OrderModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- 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;
- if ($shopId > 0) {
- $where['a.shop_id'] = $shopId;
- }
- if ($parentId > 0) {
- $where['b.parent_id'] = $parentId;
- }
- if ($status > 0) {
- $where['a.status'] = $status;
- }
- $list = $this->model->from('orders as a')
- ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
- ->leftJoin('score_goods as g', 'g.id', '=', 'a.goods_id')
- ->leftJoin('score_goods_cate as c', 'c.id', '=', 'g.cate_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.company_name', 'like', "%{$keyword}%");
- }
- })
- ->where(function ($query) use ($userId) {
- if($userId){
- $query->where('a.user_id', '=', $userId);
- }
- })
- ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'c.name as cate_name', 'g.goods_name', 'g.company_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['pay_img'] = isset($item['pay_img']) && $item['pay_img'] ? get_image_url($item['pay_img']) : '';
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'counts' => [
- ''
- ],
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 抢拍交易订单数
- * @param $userId 用户ID
- * @param int $status 状态
- * @return mixed
- */
- public function getNewTradeCount($userId, $status=0)
- {
- $where = ['user_id'=>$userId,'mark'=>1,'is_read'=>0];
- return $this->model->where($where)
- ->where(function($query) use($status){
- $query->whereIn('status',is_array($status)? $status:[$status]);
- })
- ->count('id');
- }
- /**
- * 详情
- * @param $id
- * @return mixed
- */
- public function getInfo($id)
- {
- $info = $this->model->from('orders as a')
- ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
- ->leftJoin('score_goods as g', 'g.id', '=', 'a.goods_id')
- ->where(['a.id'=>$id,'a.mark'=>1])
- ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'g.goods_name','g.source_price','g.company_name','g.qrcode', 'g.code','g.score', '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['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
- $info['qrcode'] = isset($info['qrcode']) && $info['qrcode'] ? get_image_url($info['qrcode']) : '';
- $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
- $address = MemberAddressService::make()->getBindInfo($info['user_id']);
- $info['address'] = $address? $address : [];
- // 积分
- // $scoreRate = MemberModel::where(['id'=> $info['user_id']])->value('score_rate');
- // $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
- // $info['score'] = round($info['total']/$scoreRate, 0);
- $bankInfo = ConfigService::make()->getConfigByGroupValue('8');
- $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
- }
- return $info;
- }
- /**
- * 兑换
- * @param $userId
- */
- public function exchange($userId, $shopId)
- {
- $params = request()->all();
- $goodsId = isset($params['id'])? $params['id'] : 0;
- $info = ScoreGoodsModel::where(['id'=> $goodsId,'mark'=>1,'status'=>1])->first();
- if(empty($info)){
- $this->error = 2061;
- return false;
- }
- $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->select(['id','member_level','nickname'])->first();
- $memberLevel = isset($memberInfo['member_level'])? $memberInfo['member_level'] : 0;
- if(empty($memberInfo)){
- $this->error = 2019;
- return false;
- }
- $total = $memberLevel? $info['price'] : $info['source_price'];
- $order = [
- 'user_id'=> $userId,
- 'shop_id'=> $shopId,
- 'goods_id'=> $goodsId,
- 'order_sn'=> get_order_num('S'),
- 'price'=> $info['price'],
- 'num'=> 1,
- 'total'=> $total,
- 'score'=> $info['score'],
- 'pay_money'=> 0,
- 'pay_score'=> 0,
- 'status'=>1,
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'mark'=>1
- ];
- return $this->model->insertGetId($order);
- }
- /**
- * 支付兑换
- * @param $userId
- * @return bool
- */
- public function pay($userId)
- {
- $params = request()->all();
- $id = isset($params['id'])? $params['id'] : 0;
- $info = $this->model->where(['id'=> $id,'mark'=>1,'status'=>1])->first();
- $total = isset($info['total'])? $info['total'] : 0;
- $payScore = isset($info['score'])? $info['score'] : 0;
- if(!$id || empty($info)){
- $this->error = 2073;
- return false;
- }
- $addressInfo = isset($params['address'])? $params['address'] : [];
- $payImg = isset($params['pay_img'])? $params['pay_img'] : '';
- if(empty($addressInfo)){
- $this->error = 2075;
- return false;
- }
- if(empty($payImg)){
- $this->error = 2074;
- return false;
- }
- $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->first();
- $score = isset($memberInfo['score'])? $memberInfo['score'] : 0;
- // $scoreRate = isset($memberInfo['score_rate'])? $memberInfo['score_rate'] : 0;
- // $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
- if(empty($memberInfo)){
- $this->error = 2019;
- return false;
- }
- if($payScore > $score){
- $this->error = 2077;
- return false;
- }
- // 所需积分
- /*$needScore = round($total/$scoreRate, 0);
- if($payScore<$needScore && empty($payImg)){
- $this->error = 2076;
- return false;
- }*/
- /*$scorePayMoney = intval($payScore*$scoreRate, 0);
- $payMoney = round($total, 0);*/
- $payMoney = round($total, 0);
- $address = [];
- $address[] = isset($addressInfo['province'])? $addressInfo['province'] : '';
- $address[] = isset($addressInfo['city'])? $addressInfo['city'] : '';
- $address[] = isset($addressInfo['district'])? $addressInfo['district'] : '';
- $address[] = isset($addressInfo['address'])? $addressInfo['address'] : '';
- $address = array_filter($address);
- $data = [
- 'pay_img'=> $payImg,
- 'pay_time'=> time(),
- 'user_address'=> $address? implode(' ', $address) : '',
- 'realname'=> isset($addressInfo['realname'])? $addressInfo['realname'] : '',
- 'user_mobile'=> isset($addressInfo['mobile'])? $addressInfo['mobile'] : '',
- 'pay_score'=> $payScore,
- 'pay_money'=> $payMoney,
- 'status'=> 2,
- 'update_time'=>time()
- ];
- DB::beginTransaction();
- if(!$this->model->where(['id'=> $id])->update($data)){
- $this->error = 2079;
- Db::rollBack();
- return false;
- }
- if($payScore>0){
- if(!MemberModel::where(['id'=>$userId,'mark'=>1])->update(['score'=> max(0, $score - $payScore),'update_time'=>time()])){
- $this->error = 2079;
- DB::rollBack();
- return false;
- }
- $data = [
- 'user_id'=> $userId,
- 'shop_id'=>$info['shop_id'],
- 'source_order_sn'=> $info['order_sn'],
- 'type'=> 1,
- 'coin_type'=> 3,
- 'money'=> -$payScore,
- 'balance'=> $score,
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'remark'=> '积分兑换商品',
- 'status'=>1,
- 'mark'=>1,
- ];
- if(!AccountModel::insertGetId($data)){
- $this->error = 2079;
- DB::rollBack();
- return false;
- }
- }
- Db::commit();
- $this->error = 2078;
- return true;
- }
- /**
- * 已收款
- * @return bool
- */
- public function received()
- {
- $params = request()->all();
- $id = isset($params['id'])? $params['id'] : 0;
- $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
- $status = isset($info['status'])? $info['status'] : 0;
- if(!$id || empty($info)){
- $this->error = 2081;
- return false;
- }
- if($status != 2){
- $this->error = 2090;
- return false;
- }
- if($this->model->where(['id'=> $id])->update(['status'=> 3,'confirm_time'=>time(), 'update_time'=> time()])){
- $this->error = 2092;
- return true;
- }else{
- $this->error = 2093;
- return false;
- }
- }
- /**
- * 已发货
- * @return bool
- */
- public function deliver()
- {
- $params = request()->all();
- $id = isset($params['id'])? $params['id'] : 0;
- $express = isset($params['express'])? $params['express'] : '';
- $expressNo = isset($params['express_no'])? $params['express_no'] : '';
- $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
- $status = isset($info['status'])? $info['status'] : 0;
- if(!$id || empty($info)){
- $this->error = 2081;
- return false;
- }
- if(!in_array($status, [2,3])){
- $this->error = 2090;
- return false;
- }
- if($this->model->where(['id'=> $id])->update(['status'=> 4,'express'=> $express,'express_no'=> $expressNo, 'update_time'=> time()])){
- $this->error = 2094;
- return true;
- }else{
- $this->error = 2095;
- return false;
- }
- }
- /**
- * 已收货
- * @return bool
- */
- public function receive()
- {
- $params = request()->all();
- $id = isset($params['id'])? $params['id'] : 0;
- $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
- $status = isset($info['status'])? $info['status'] : 0;
- if(!$id || empty($info)){
- $this->error = 2081;
- return false;
- }
- if(!in_array($status, [3,4,5])){
- $this->error = 2082;
- return false;
- }
- if($this->model->where(['id'=> $id])->update(['status'=> 5, 'update_time'=> time()])){
- $this->error = 2083;
- return true;
- }else{
- $this->error = 2084;
- return false;
- }
- }
- }
|