StoreOrderController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace app\api\controller\admin;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StoreOrderStatus;
  5. use app\models\store\StorePink;
  6. use app\models\store\StoreService;
  7. use app\models\user\User;
  8. use app\models\user\UserBill;
  9. use app\Request;
  10. use crmeb\repositories\OrderRepository;
  11. use crmeb\services\MiniProgramService;
  12. use crmeb\services\UtilService;
  13. use crmeb\services\WechatService;
  14. /**
  15. * 订单类
  16. * Class StoreOrderController
  17. * @package app\api\controller\admin\order
  18. */
  19. class StoreOrderController
  20. {
  21. /**
  22. * 订单数据统计
  23. * @param Request $request
  24. * @return mixed
  25. */
  26. public function statistics(Request $request)
  27. {
  28. $uid = $request->uid();
  29. if(!StoreService::orderServiceStatus($uid))
  30. return app('json')->fail('权限不足');
  31. $dataCount = StoreOrder::getOrderDataAdmin();
  32. $dataPrice = StoreOrder::getOrderTimeData();
  33. $data = array_merge($dataCount, $dataPrice);
  34. return app('json')->successful($data);
  35. }
  36. /**
  37. * 订单每月统计数据
  38. * @param Request $request
  39. * @return mixed
  40. */
  41. public function data(Request $request)
  42. {
  43. $uid = $request->uid();
  44. if(!StoreService::orderServiceStatus($uid))
  45. return app('json')->fail('权限不足');
  46. list($page, $limit) = UtilService::getMore([['page',1],['limit',7]], $request, true);
  47. if(!$limit) return app('json')->successful([]);
  48. $data = StoreOrder::getOrderDataPriceCount($page, $limit);
  49. if($data) return app('json')->successful($data->toArray());
  50. return app('json')->successful([]);
  51. }
  52. /**
  53. * 订单列表
  54. * @param Request $request
  55. * @return mixed
  56. */
  57. public function lst(Request $request)
  58. {
  59. $uid = $request->uid();
  60. if(!StoreService::orderServiceStatus($uid))
  61. return app('json')->fail('权限不足');
  62. $where = UtilService::getMore([
  63. ['status',''],
  64. ['is_del',0],
  65. ['data',''],
  66. ['type',''],
  67. ['order',''],
  68. ['page',0],
  69. ['limit',0]
  70. ], $request);
  71. if(!$where['limit']) return app('json')->successful([]);
  72. return app('json')->successful(StoreOrder::orderList($where));
  73. }
  74. /**
  75. * 订单详情
  76. * @param Request $request
  77. * @param $orderId
  78. * @return mixed
  79. * @throws \think\Exception
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public function detail(Request $request, $orderId)
  85. {
  86. $uid = $request->uid();
  87. if(!StoreService::orderServiceStatus($uid))
  88. return app('json')->fail('权限不足');
  89. $order = StoreOrder::getAdminOrderDetail($orderId, 'id,uid,order_id,add_time,status,total_num,total_price,total_postage,pay_price,pay_postage,paid,refund_status,remark,pink_id,combination_id,mark,seckill_id,bargain_id,delivery_type,pay_type,real_name,user_phone,user_address,coupon_price,freight_price,delivery_name,delivery_type,delivery_id');
  90. if(!$order) return app('json')->fail('订单不存在');
  91. $order = $order->toArray();
  92. $nickname = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  93. $orderInfo = StoreOrder::tidyAdminOrder([$order], true)[0];
  94. unset($orderInfo['uid'], $orderInfo['seckill_id'], $orderInfo['pink_id'], $orderInfo['combination_id'], $orderInfo['bargain_id'], $orderInfo['status'], $orderInfo['total_postage']);
  95. $orderInfo['nickname'] = $nickname;
  96. return app('json')->successful('ok',$orderInfo);
  97. }
  98. /**
  99. * 订单发货获取订单信息
  100. * @param Request $request
  101. * @param $orderId
  102. * @return mixed
  103. * @throws \think\Exception
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. * @throws \think\exception\DbException
  107. */
  108. public function delivery_gain(Request $request, $orderId)
  109. {
  110. $uid = $request->uid();
  111. if(!StoreService::orderServiceStatus($uid))
  112. return app('json')->fail('权限不足');
  113. $order = StoreOrder::getAdminOrderDetail($orderId, 'real_name,user_phone,user_address,order_id,uid,status,paid');
  114. if(!$order) return app('json')->fail('订单不存在');
  115. if($order['paid']){
  116. $order['nickname'] = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  117. $order = $order->hidden(['uid','status','paid'])->toArray();
  118. return app('json')->successful('ok',$order);
  119. }
  120. return app('json')->fail('状态错误');
  121. }
  122. /**
  123. * 订单发货
  124. * @param Request $request
  125. * @return mixed
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. * @throws \think\exception\DbException
  129. */
  130. public function delivery_keep(Request $request)
  131. {
  132. $uid = $request->uid();
  133. if(!StoreService::orderServiceStatus($uid))
  134. return app('json')->fail('权限不足');
  135. list($order_id, $delivery_type, $delivery_name, $delivery_id) = UtilService::postMore([
  136. ['order_id',''],
  137. ['delivery_type', 0],
  138. ['delivery_name', ''],
  139. ['delivery_id', ''],
  140. ], $request, true);
  141. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,status,paid');
  142. if(!$order) return app('json')->fail('订单不存在');
  143. if(!$order['status'] && $order['paid']){
  144. $deliveryTypeArr = ['send','express','fictitious'];
  145. if(!strlen(trim($delivery_type))) return app('json')->fail('请填写发货方式');
  146. if(!in_array($delivery_type, $deliveryTypeArr)) return app('json')->fail('发货方式错误');
  147. if($delivery_type == 'express'){
  148. if(!strlen(trim($delivery_name))) return app('json')->fail('请选择快递公司');
  149. if(!strlen(trim($delivery_id))) return app('json')->fail('请填写快递单号');
  150. }
  151. if($delivery_type == 'send'){
  152. if(!strlen(trim($delivery_name))) return app('json')->fail('请填写发货人');
  153. if(!strlen(trim($delivery_id))) return app('json')->fail('请填写发货手机号');
  154. }
  155. $data['status'] = 1;
  156. $data['delivery_type'] = $delivery_type;
  157. $data['delivery_name'] = $delivery_name;
  158. $data['delivery_id'] = $delivery_id;
  159. $res = StoreOrder::edit($data, $order['id'], 'id');
  160. if($res){
  161. if($delivery_type == 'express'){
  162. StoreOrderStatus::status($order['id'],'delivery_goods','已发货 快递公司:'.$data['delivery_name'].' 快递单号:'.$data['delivery_id']);
  163. }else if($delivery_type == 'send'){
  164. StoreOrderStatus::status($order['id'],'delivery','已配送 发货人:'.$delivery_name.' 发货人电话:'.$delivery_id);
  165. }else if($delivery_type == 'fictitious'){
  166. StoreOrderStatus::status($order['id'],'delivery_fictitious','虚拟产品已发货');
  167. }
  168. }
  169. event('StoreProductOrderDeliveryGoodsAfter',[$data,$order['id']]);
  170. return app('json')->successful('发货成功!');
  171. }
  172. return app('json')->fail('状态错误');
  173. }
  174. /**
  175. * 订单改价
  176. * @param Request $request
  177. * @return mixed
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\ModelNotFoundException
  180. * @throws \think\exception\DbException
  181. */
  182. public function price(Request $request)
  183. {
  184. $uid = $request->uid();
  185. if(!StoreService::orderServiceStatus($uid))
  186. return app('json')->fail('权限不足');
  187. list($order_id, $price) = UtilService::postMore([
  188. ['order_id',''],
  189. ['price', '']
  190. ], $request, true);
  191. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,paid,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral');
  192. if(!$order) return app('json')->fail('订单不存在');
  193. $order = $order->toArray();
  194. if(!$order['paid']){
  195. if($price === '') return app('json')->fail('请填写实际支付金额');
  196. if($price < 0) return app('json')->fail('实际支付金额不能小于0元');
  197. if($order['pay_price'] == $price) return app('json')->successful('修改成功');
  198. if(!StoreOrder::edit(['pay_price'=>$price], $order['id'], 'id'))
  199. return app('json')->fail('状态错误');
  200. $order['pay_price'] = $price;
  201. event('StoreProductOrderEditAfter',[$order,$order['id']]);
  202. StoreOrderStatus::status($order['id'],'order_edit','修改实际支付金额'.$price);
  203. return app('json')->successful('修改成功');
  204. }
  205. return app('json')->fail('状态错误');
  206. }
  207. /**
  208. * 订单备注
  209. * @param Request $request
  210. * @return mixed
  211. * @throws \think\db\exception\DataNotFoundException
  212. * @throws \think\db\exception\ModelNotFoundException
  213. * @throws \think\exception\DbException
  214. */
  215. public function remark(Request $request)
  216. {
  217. $uid = $request->uid();
  218. if(!StoreService::orderServiceStatus($uid))
  219. return app('json')->fail('权限不足');
  220. list($order_id, $remark) = UtilService::postMore([
  221. ['order_id',''],
  222. ['remark', '']
  223. ], $request, true);
  224. $order = StoreOrder::getAdminOrderDetail($order_id, 'id');
  225. if(!$order) return app('json')->fail('订单不存在');
  226. $order = $order->toArray();
  227. if(!strlen(trim($remark))) return app('json')->fail('请填写备注内容');
  228. if(!StoreOrder::edit(['remark'=>$remark],$order['id']))
  229. return app('json')->fail('备注失败');
  230. return app('json')->successful('备注成功');
  231. }
  232. /**
  233. * 订单交易额/订单数量时间统计
  234. * @param Request $request
  235. * @return bool
  236. */
  237. public function time(Request $request)
  238. {
  239. $uid = $request->uid();
  240. if(!StoreService::orderServiceStatus($uid))
  241. return app('json')->fail('权限不足');
  242. list($start, $stop, $type) = UtilService::getMore([
  243. ['start',strtotime(date('Y-m'))],
  244. ['stop',time()],
  245. ['type',1]
  246. ], $request, true);
  247. if($start == $stop) return false;
  248. if($start > $stop){
  249. $middle = $stop;
  250. $stop = $start;
  251. $start = $middle;
  252. }
  253. $space = bcsub($stop, $start,0);//间隔时间段
  254. $front = bcsub($start, $space, 0);//第一个时间段
  255. if($type == 1){//销售额
  256. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($front, $start);
  257. $afterPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $stop);
  258. $chartInfo = StoreOrder::chartTimePrice($start, $stop);
  259. $data['chart'] = $chartInfo;//营业额图表数据
  260. $data['time'] = $afterPrice;//时间区间营业额
  261. $increase = (float)bcsub($afterPrice, $frontPrice, 2); //同比上个时间区间增长营业额
  262. $growthRate = abs($increase);
  263. if($growthRate == 0) $data['growth_rate'] = 0;
  264. else if($frontPrice == 0) $data['growth_rate'] = $growthRate;
  265. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontPrice, 2), 100, 0);//时间区间增长率
  266. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  267. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  268. }else{//订单数
  269. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($front, $start);
  270. $afterNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $stop);
  271. $chartInfo = StoreOrder::chartTimeNumber($start, $stop);
  272. $data['chart'] = $chartInfo;//订单数图表数据
  273. $data['time'] = $afterNumber;//时间区间订单数
  274. $increase = (int)bcsub($afterNumber, $frontNumber, 0); //同比上个时间区间增长订单数
  275. $growthRate = abs($increase);
  276. if($growthRate == 0) $data['growth_rate'] = 0;
  277. else if($frontNumber == 0) $data['growth_rate'] = $growthRate;
  278. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontNumber, 2), 100, 0);//时间区间增长率
  279. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  280. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  281. }
  282. return app('json')->successful($data);
  283. }
  284. /**
  285. * 订单支付
  286. * @param Request $request
  287. * @return mixed
  288. * @throws \think\db\exception\DataNotFoundException
  289. * @throws \think\db\exception\ModelNotFoundException
  290. * @throws \think\exception\DbException
  291. */
  292. public function offline(Request $request){
  293. list($orderId) = UtilService::postMore([['order_id', '']], $request, true);
  294. $orderInfo = StoreOrder::getAdminOrderDetail($orderId, 'id');
  295. if(!$orderInfo) return app('json')->fail('参数错误');
  296. $id = $orderInfo->id;
  297. $res = StoreOrder::updateOffline($id);
  298. if($res){
  299. event('StoreProductOrderOffline',[$id]);
  300. StoreOrderStatus::status($id,'offline','线下付款');
  301. return app('json')->successful('修改成功!');
  302. }
  303. return app('json')->fail(StoreOrder::getErrorInfo('修改失败!'));
  304. }
  305. /**
  306. * 订单退款
  307. * @param Request $request
  308. * @return mixed
  309. * @throws \think\Exception
  310. * @throws \think\db\exception\DataNotFoundException
  311. * @throws \think\db\exception\ModelNotFoundException
  312. * @throws \think\exception\DbException
  313. */
  314. public function refund(Request $request)
  315. {
  316. list($orderId, $price, $type) = UtilService::postMore([
  317. ['order_id',''],
  318. ['price', 0],
  319. ['type',1],
  320. ], $request, true);
  321. if(!strlen(trim($orderId))) return app('json')->fail('参数错误');
  322. $orderInfo = StoreOrder::getAdminOrderDetail($orderId);
  323. if(!$orderInfo) return app('json')->fail('数据不存在!');
  324. $orderInfo = $orderInfo->toArray();
  325. if($type == 1)
  326. $data['refund_status'] = 2;
  327. else if($type == 2)
  328. $data['refund_status'] = 0;
  329. else
  330. return app('json')->fail('退款修改状态错误');
  331. if($orderInfo['pay_price'] == 0 || $type == 2) {
  332. StoreOrder::update($data,['order_id'=>$orderId]);
  333. return app('json')->successful('修改退款状态成功!');
  334. }
  335. if($orderInfo['pay_price'] == $orderInfo['refund_price']) return app('json')->fail('已退完支付金额!不能再退款了');
  336. if(!$price) return app('json')->fail('请输入退款金额');
  337. $data['refund_price'] = bcadd($price, $orderInfo['refund_price'], 2);
  338. $bj = bccomp((float)$orderInfo['pay_price'],(float)$data['refund_price'],2);
  339. if($bj < 0) return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  340. $refundData['pay_price'] = $orderInfo['pay_price'];
  341. $refundData['refund_price'] = $price;
  342. if($orderInfo['pay_type'] == 'weixin'){
  343. if($orderInfo['is_channel'] == 1){// 小程序
  344. try{
  345. MiniProgramService::payOrderRefund($orderInfo['order_id'], $refundData);
  346. }catch(\Exception $e){
  347. return app('json')->fail($e->getMessage());
  348. }
  349. }else{// 公众号
  350. try{
  351. WechatService::payOrderRefund($orderInfo['order_id'], $refundData);
  352. }catch(\Exception $e){
  353. return app('json')->fail($e->getMessage());
  354. }
  355. }
  356. }else if($orderInfo['pay_type'] == 'yue'){//余额
  357. StoreOrder::beginTrans();
  358. $userInfo = User::getUserInfo($orderInfo['uid'], 'now_money');
  359. if(!$userInfo){
  360. StoreOrder::rollbackTrans();
  361. return app('json')->fail('订单用户不存在');
  362. }
  363. $res1 = User::bcInc($orderInfo['uid'],'now_money',$price,'uid');
  364. $res2 = $res2 = UserBill::income('商品退款',$orderInfo['uid'],'now_money','pay_product_refund', $price, $orderInfo['id'],bcadd($userInfo['now_money'], $price,2),'订单退款到余额'.floatval($price).'元');
  365. try{
  366. OrderRepository::storeOrderYueRefund($orderInfo, $refundData);
  367. }catch (\Exception $e){
  368. StoreOrder::rollbackTrans();
  369. return app('json')->fail($e->getMessage());
  370. }
  371. $res = $res1 && $res2;
  372. StoreOrder::checkTrans($res);
  373. if(!$res) return app('json')->fail('余额退款失败!');
  374. }
  375. $resEdit = StoreOrder::edit($data,$orderInfo['id'], 'id');
  376. if($resEdit){
  377. $data['type'] = $type;
  378. if($data['type'] == 1) StorePink::setRefundPink($orderInfo['id']);
  379. try{
  380. OrderRepository::storeProductOrderRefundY($data, $orderInfo['id']);
  381. }catch (\Exception $e){
  382. return app('json')->fail($e->getMessage());
  383. }
  384. StoreOrderStatus::status($orderInfo['id'],'refund_price','退款给用户'.$price.'元');
  385. return app('json')->successful('修改成功!');
  386. }else{
  387. StoreOrderStatus::status($orderInfo['id'],'refund_price','退款给用户'.$price.'元失败');
  388. return app('json')->successful('修改失败!');
  389. }
  390. }
  391. }