OrderService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\GoodsModel;
  13. use App\Models\MemberModel;
  14. use App\Models\OrderModel;
  15. use App\Models\ScoreGoodsModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. /**
  19. * 积分商城订单管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * Class OrderService
  23. * @package App\Services\Common
  24. */
  25. class OrderService extends BaseService
  26. {
  27. // 静态对象
  28. protected static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * OrderService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new OrderModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return static|null
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = (new static());
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * @param $params
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getDataList($params, $pageSize = 15)
  56. {
  57. $where = ['a.mark' => 1];
  58. $status = isset($params['status']) ? $params['status'] : 0;
  59. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  60. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  61. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  62. if ($shopId > 0) {
  63. $where['a.shop_id'] = $shopId;
  64. }
  65. if ($parentId > 0) {
  66. $where['b.parent_id'] = $parentId;
  67. }
  68. if ($status > 0) {
  69. $where['a.status'] = $status;
  70. }
  71. $list = $this->model->from('orders as a')
  72. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  73. ->leftJoin('score_goods as g', 'g.id', '=', 'a.goods_id')
  74. ->leftJoin('score_goods_cate as c', 'c.id', '=', 'g.cate_id')
  75. ->where($where)
  76. ->where(function ($query) use ($params) {
  77. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  78. if ($keyword) {
  79. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")->orWhere('g.goods_name', 'like', "%{$keyword}%");
  80. }
  81. })
  82. ->where(function ($query) use ($userId) {
  83. if($userId){
  84. $query->where('a.user_id', '=', $userId);
  85. }
  86. })
  87. ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'c.name as cate_name', 'g.goods_name', 'g.code', 'g.thumb'])
  88. ->orderBy('a.pay_time', 'desc')
  89. ->orderBy('a.id', 'desc')
  90. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  91. $list = $list ? $list->toArray() : [];
  92. if ($list) {
  93. foreach ($list['data'] as &$item) {
  94. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  95. $item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
  96. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  97. }
  98. }
  99. return [
  100. 'pageSize' => $pageSize,
  101. 'total' => isset($list['total']) ? $list['total'] : 0,
  102. 'counts' => [
  103. ''
  104. ],
  105. 'list' => isset($list['data']) ? $list['data'] : []
  106. ];
  107. }
  108. /**
  109. * 添加或编辑
  110. * @return array
  111. * @since 2020/11/11
  112. * @author laravel开发员
  113. */
  114. public function edit()
  115. {
  116. $data = request()->all();
  117. return parent::edit($data); // TODO: Change the autogenerated stub
  118. }
  119. /**
  120. * 抢拍交易订单数
  121. * @param $userId 用户ID
  122. * @param int $status 状态
  123. * @return mixed
  124. */
  125. public function getNewTradeCount($userId, $status=0)
  126. {
  127. $where = ['user_id'=>$userId,'mark'=>1,'is_read'=>0];
  128. return $this->model->where($where)
  129. ->where(function($query) use($status){
  130. $query->whereIn('status',is_array($status)? $status:[$status]);
  131. })
  132. ->count('id');
  133. }
  134. /**
  135. * 详情
  136. * @param $id
  137. * @return mixed
  138. */
  139. public function getInfo($id)
  140. {
  141. $info = $this->model->from('orders as a')
  142. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  143. ->leftJoin('score_goods as g', 'g.id', '=', 'a.goods_id')
  144. ->where(['a.id'=>$id,'a.mark'=>1])
  145. ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'g.goods_name','g.source_price', 'g.code', 'g.thumb'])
  146. ->first();
  147. if($info){
  148. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  149. $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
  150. $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
  151. $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  152. $address = MemberAddressService::make()->getBindInfo($info['user_id']);
  153. $info['address'] = $address? $address : [];
  154. // 积分
  155. $scoreRate = MemberModel::where(['id'=> $info['user_id']])->value('score_rate');
  156. $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  157. $info['score'] = round($info['total']/$scoreRate, 0);
  158. $bankInfo = ConfigService::make()->getConfigByGroupValue('8');
  159. $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
  160. }
  161. return $info;
  162. }
  163. /**
  164. * 兑换
  165. * @param $userId
  166. */
  167. public function exchange($userId, $shopId)
  168. {
  169. $params = request()->all();
  170. $goodsId = isset($params['id'])? $params['id'] : 0;
  171. $info = ScoreGoodsModel::where(['id'=> $goodsId,'mark'=>1,'status'=>1])->first();
  172. if(empty($info)){
  173. $this->error = 2061;
  174. return false;
  175. }
  176. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->select(['id','member_level','nickname'])->first();
  177. $memberLevel = isset($memberInfo['member_level'])? $memberInfo['member_level'] : 0;
  178. if(empty($memberInfo)){
  179. $this->error = 2019;
  180. return false;
  181. }
  182. $total = $memberLevel? $info['price'] : $info['source_price'];
  183. $order = [
  184. 'user_id'=> $userId,
  185. 'shop_id'=> $shopId,
  186. 'goods_id'=> $goodsId,
  187. 'order_sn'=> get_order_num('S'),
  188. 'price'=> $info['price'],
  189. 'num'=> 1,
  190. 'total'=> $total,
  191. 'pay_money'=> $total,
  192. 'status'=>1,
  193. 'create_time'=> time(),
  194. 'update_time'=> time(),
  195. 'mark'=>1
  196. ];
  197. return $this->model->insertGetId($order);
  198. }
  199. /**
  200. * 支付兑换
  201. * @param $userId
  202. * @return bool
  203. */
  204. public function pay($userId)
  205. {
  206. $params = request()->all();
  207. $id = isset($params['id'])? $params['id'] : 0;
  208. $info = $this->model->where(['id'=> $id,'mark'=>1,'status'=>1])->first();
  209. $total = isset($info['total'])? $info['total'] : 0;
  210. if(!$id || empty($info)){
  211. $this->error = 2073;
  212. return false;
  213. }
  214. $payScore = isset($params['score'])? intval($params['score']) : 0;
  215. $addressInfo = isset($params['address'])? $params['address'] : [];
  216. $payImg = isset($params['pay_img'])? $params['pay_img'] : '';
  217. if(empty($addressInfo)){
  218. $this->error = 2075;
  219. return false;
  220. }
  221. if($payScore<=0 && empty($payImg)){
  222. $this->error = 2074;
  223. return false;
  224. }
  225. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->first();
  226. $score = isset($memberInfo['score'])? $memberInfo['score'] : 0;
  227. $scoreRate = isset($memberInfo['score_rate'])? $memberInfo['score_rate'] : 0;
  228. $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  229. if(empty($memberInfo)){
  230. $this->error = 2019;
  231. return false;
  232. }
  233. if($payScore > $score){
  234. $this->error = 2077;
  235. return false;
  236. }
  237. // 所需积分
  238. $needScore = round($total/$scoreRate, 0);
  239. if($payScore<$needScore && empty($payImg)){
  240. $this->error = 2076;
  241. return false;
  242. }
  243. $scorePayMoney = intval($payScore*$scoreRate, 0);
  244. $payMoney = round($total - $scorePayMoney, 0);
  245. $address = [];
  246. $address[] = isset($addressInfo['province'])? $addressInfo['province'] : '';
  247. $address[] = isset($addressInfo['city'])? $addressInfo['city'] : '';
  248. $address[] = isset($addressInfo['district'])? $addressInfo['district'] : '';
  249. $address[] = isset($addressInfo['address'])? $addressInfo['address'] : '';
  250. $address = array_filter($address);
  251. $data = [
  252. 'pay_img'=> $payImg,
  253. 'pay_time'=> time(),
  254. 'user_address'=> $address? implode(' ', $address) : '',
  255. 'realname'=> isset($addressInfo['realname'])? $addressInfo['realname'] : '',
  256. 'user_mobile'=> isset($addressInfo['mobile'])? $addressInfo['mobile'] : '',
  257. 'pay_score'=> $payScore,
  258. 'pay_money'=> $payMoney,
  259. 'status'=> 2,
  260. 'update_time'=>time()
  261. ];
  262. if($this->model->where(['id'=> $id])->update($data)){
  263. $this->error = 2078;
  264. return true;
  265. }else{
  266. $this->error = 2079;
  267. return false;
  268. }
  269. }
  270. /**
  271. *
  272. * @return bool
  273. */
  274. public function receive()
  275. {
  276. $params = request()->all();
  277. $id = isset($params['id'])? $params['id'] : 0;
  278. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  279. $status = isset($info['status'])? $info['status'] : 0;
  280. if(!$id || empty($info)){
  281. $this->error = 2081;
  282. return false;
  283. }
  284. if(!in_array($status, [3,4,5])){
  285. $this->error = 2082;
  286. return false;
  287. }
  288. if($this->model->where(['id'=> $id])->update(['status'=> 5, 'update_time'=> time()])){
  289. $this->error = 2083;
  290. return true;
  291. }else{
  292. $this->error = 2084;
  293. return false;
  294. }
  295. }
  296. }