OrderService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. namespace app\common\service;
  3. use addons\epay\library\Service;
  4. use fast\Random;
  5. use think\Db;
  6. use think\Exception;
  7. class OrderService
  8. {
  9. private $model = null;
  10. /**
  11. * 初始化方法
  12. */
  13. public function __construct()
  14. {
  15. $this->model = Db::name('order');
  16. }
  17. /**
  18. * 套餐支付
  19. * @param $params
  20. * @return array
  21. */
  22. public function orderPay($params=[])
  23. {
  24. $result = [
  25. 'status' => 1,
  26. 'msg' => '操作成功',
  27. 'data' => [],
  28. ];
  29. try {
  30. $payType = isset($params['pay_type']) ? $params['pay_type'] : 'wechat';
  31. $platform = isset($params['platform']) ? $params['platform'] : 'miniapp';
  32. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  33. $carId = isset($params['car_id']) ? $params['car_id'] : 0;
  34. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  35. $packageId = isset($params['package_id']) ? $params['package_id'] : 0;
  36. $miniOpenId = isset($params['mini_openid']) ? $params['mini_openid'] : '';
  37. //验证车辆
  38. $userCarWhere['user_id'] = $userId;
  39. $userCarWhere['id'] = $carId;
  40. $userCar = Db::name('user_car')->where($userCarWhere)->find();
  41. if (empty($userCar)) {
  42. throw new Exception('未找到车辆信息');
  43. }
  44. //验证套餐
  45. $packageWhere['id'] = $packageId;
  46. $packageWhere['status'] = 1;
  47. $packageWhere['num'] = ['gt',0];
  48. $package = Db::name('package')->where($packageWhere)->find();
  49. if (empty($package)) {
  50. throw new Exception('未找到套餐信息');
  51. }
  52. /*if ($package['company_id'] != $companyId) {
  53. throw new Exception('您绑定的门店和购买套餐门店不一致');
  54. }*/
  55. if ($payType == 'wallet') {//验证余额
  56. $userWalletWhere['user_id'] = $userId;
  57. $userWalletWhere['company_id'] = $companyId;
  58. $userWallet = Db::name('user_wallet')->where($userWalletWhere)->find();
  59. if (empty($userWallet)) {
  60. throw new Exception('钱包异常');
  61. }
  62. if ($userWallet['money'] < $package['price']) {
  63. throw new Exception('您的余额不足,请充值。');
  64. }
  65. }
  66. $gift_amount = 0.00;
  67. $order_amount = $package['price'];
  68. $extData = ['car_id'=>$carId];
  69. $time = time();
  70. //创建订单
  71. $data['company_id'] = $companyId;
  72. $data['user_id'] = $userId;
  73. $data['out_trade_no'] = createUniqueNo('O',$userId); // 数据库订单号加密
  74. $data['order_amount'] = $order_amount;
  75. $data['gift_amount'] = $gift_amount;
  76. $data['createtime'] = time();
  77. $data['pay_type'] = $payType;
  78. $data['order_status'] = 0;
  79. $data['table_name'] = 'package';
  80. $data['table_id'] = $packageId;
  81. $data['ext_data'] = json_encode($extData);
  82. $payOrderId = Db::name('pay_order')->insertGetId($data);
  83. //支付方式:wallet=钱包,wechat=微信
  84. if ($payType == 'wechat') {//微信支付 记录日志,生成订单,商家端操作完成增加收入
  85. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  86. //下单
  87. $payTest = config('param.pay_test');
  88. $orderAmount = $payTest == 1 ? 0.01 : $data['order_amount'];
  89. $paramsData = [
  90. 'type' => $payType,
  91. 'orderid' => $data['out_trade_no'],
  92. 'title' => '购买套餐',
  93. 'amount' => $orderAmount,
  94. 'method' => $platform,
  95. 'openid' => $miniOpenId,
  96. 'notifyurl' => $httpStr.'/api/pay/order_notify_base/paytype/'.$payType.'/func/package_do',
  97. 'returnurl' => '',
  98. ];
  99. $resData = Service::submitOrder($paramsData);
  100. $resData = json_decode($resData,true);
  101. } else {//余额支付 扣减余额,记录日志,生成订单 商家端不变
  102. //扣减金额
  103. $oldMoney = $userWallet['money'];
  104. $newMoney = bcsub($oldMoney,$package['price'],2);
  105. $userWalletData['money'] = $newMoney;
  106. $userWalletData['updatetime'] = $time;
  107. $userWalletWhere['user_id'] = $userId;
  108. $userWalletWhere['company_id'] = $companyId;
  109. $userWalletRes = Db::name('user_wallet')->where($userWalletWhere)->update($userWalletData);
  110. if (!$userWalletRes) {
  111. throw new Exception('支付失败');
  112. }
  113. //余额明细记录
  114. $userMoneyLogData = [
  115. 'company_id' => $companyId,
  116. 'user_id' => $userId,
  117. 'log_type' => 103,//购买套餐
  118. 'before' => $oldMoney,
  119. 'change_value' => $package['price'],
  120. 'remain' => $newMoney,
  121. 'table' => 'pay_order',
  122. 'table_id' => $payOrderId,
  123. 'remark' => '购买套餐',
  124. 'createtime' => $time,
  125. 'updatetime' => $time,
  126. ];
  127. $userMoneyLogRes = Db::name('user_money_log')->insertGetId($userMoneyLogData);
  128. if (!$userMoneyLogRes) {
  129. throw new Exception('余额明细记录失败');
  130. }
  131. //生成订单
  132. $params['order_paytype'] = 2;//支付方式:1=线下,2=余额,3=微信
  133. $params['pay_order_id'] = $payOrderId;
  134. $orderRes = $this->addOrder($params);
  135. if (!$orderRes['status']) {
  136. throw new Exception($orderRes['msg']);
  137. }
  138. //更新支付状态
  139. $payOrderData['order_status'] = 1;
  140. $payOrderWhere['id'] = $payOrderId;
  141. $payOrderRes = Db::name('pay_order')->where($payOrderWhere)->update($payOrderData);
  142. if (!$payOrderRes) {
  143. throw new Exception('支付失败');
  144. }
  145. }
  146. $resData['order_id'] = isset($orderRes['data']['order_id']) ? $orderRes['data']['order_id'] : 0;
  147. $resData['pay_order_id'] = $payOrderId;
  148. $resData['template_ids'] = config('param.wechat_template_ids');//微信消息模版ID
  149. $result['data'] = $resData;
  150. } catch (Exception $e) {
  151. $result['status'] = 0;
  152. $result['msg'] = $e->getMessage();
  153. }
  154. return $result;
  155. }
  156. /**
  157. * 生成订单
  158. * @return void
  159. */
  160. public function addOrder($params)
  161. {
  162. $result = [
  163. 'status' => 1,
  164. 'msg' => '操作成功',
  165. 'data' => [],
  166. ];
  167. try {
  168. $packageId = isset($params['package_id']) ? $params['package_id'] : 0;
  169. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  170. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  171. $carId = isset($params['car_id']) ? $params['car_id'] : 0;
  172. $payOrderId= isset($params['pay_order_id']) ? $params['pay_order_id'] : 0;
  173. $orderPayType = isset($params['order_paytype']) ? $params['order_paytype'] : 3;//支付方式:1=线下,2=余额,3=微信
  174. //生成订单
  175. $time = time();
  176. $packageWhere['id'] = $packageId;
  177. $packageWhere['num'] = ['gt',0];
  178. $package = Db::name('package')->where($packageWhere)->lock(true)->find();
  179. if (empty($package)) {
  180. throw new Exception('未找到套餐信息');
  181. }
  182. $userWhere['id'] = $userId;
  183. $user = Db::name('user')->where($userWhere)->find();
  184. //绑定门店
  185. $userService = new UserService();
  186. $userParams = [
  187. 'user_id' => $userId,
  188. 'company_id' => $companyId,
  189. 'comefrom' => '平台引流',//来源
  190. ];
  191. $userBindRes = $userService->userWallet($userParams);
  192. if (!$userBindRes['status']) {
  193. throw new Exception($userBindRes['msg']);
  194. }
  195. $userCarWhere['id'] = $carId;
  196. $userCar = Db::name('user_car')->where($userCarWhere)->find();
  197. //生成订单
  198. $checkCode[] = Random::alnum(8);
  199. $orderService = new OrderService();//防重复
  200. $checkCodeRes = $orderService->getCheckOrder($checkCode);
  201. if ($checkCodeRes['status']) {
  202. $checkCodeStr = isset($checkCodeRes['data'][0]) ? $checkCodeRes['data'][0] : '';
  203. } else {
  204. $checkCodeStr = Random::alnum(8);
  205. }
  206. if ($package['type'] == 2) {//卡券类型直接已完成
  207. $orderStatus = 3;
  208. $finishTime = $time;
  209. $userCouponsParams = [
  210. 'package_id' => $packageId,
  211. 'company_id' => $companyId,
  212. 'user_id' => $userId,
  213. 'payorder_id'=> $payOrderId,
  214. ];
  215. $userCouponsRes = $userService->packageCoupons($userCouponsParams);
  216. if (!$userCouponsRes['status']) {
  217. throw new Exception($userCouponsRes['msg']);
  218. }
  219. } else {
  220. $orderStatus = 1;
  221. $finishTime = 0;
  222. }
  223. $orderData = [
  224. 'check_code' => $checkCodeStr,
  225. 'orderno' => createUniqueNo('O', $userId),//订单号
  226. 'ordertype' => 3,//类型:1=预约下单,2=在线下单,3=套餐订单
  227. 'company_id' => $companyId,//门店ID
  228. 'user_id' => $userId,//用户ID
  229. 'user_name' => isset($user['nickname']) ? $user['nickname'] : '',//用户姓名
  230. 'user_car_id' => isset($userCar['id']) ? $userCar['id'] : 0,//车辆ID
  231. 'user_car_number' => isset($userCar['car_number']) ? $userCar['car_number'] : '',//用户车牌
  232. 'user_mobile' => isset($user['mobile']) ? $user['mobile'] : '',//用户手机
  233. 'pre_order_id' => 0,//预约单ID
  234. 'package_id' => isset($package['id']) ? $package['id'] : 0,//套餐ID
  235. 'servicetype_id' => isset($package['servicetype_id']) ? $package['servicetype_id'] : 0,//服务类型
  236. 'server_info' => isset($package['title']) ? $package['title'] : '',//套餐主标题
  237. 'server_images' => isset($package['images']) ? $package['images'] : '',//套餐图片
  238. 'pay_fee' => isset($package['price']) ? $package['price'] : 0.00,//套餐价格
  239. 'status' => $orderStatus,//状态:1=待核销,2=已接单,3=已完成,4=已取消
  240. 'pay_time' => $time,
  241. 'paytype' => $orderPayType,//支付方式:1=线下,2=余额,3=微信
  242. 'total_fee' => isset($package['price']) ? $package['price'] : 0.00,//支付总额
  243. 'createtime' => $time,//下单时间
  244. 'pay_order_id' => $payOrderId,//支付ID
  245. 'finish_time' => $finishTime,//完成时间
  246. ];
  247. $orderRes = Db::name('order')->insertGetId($orderData);
  248. if (!$orderRes) {
  249. throw new Exception('生成订单失败');
  250. }
  251. $newNum = $package['num'] - 1;
  252. $packageRes = Db::name('package')->where($packageWhere)->update(['num'=>$newNum]);
  253. if (!$packageRes) {
  254. throw new Exception('扣减库存失败');
  255. }
  256. $result['data'] = [
  257. 'order_id' => $orderRes,
  258. 'pay_order_id' => $payOrderId,
  259. ];
  260. } catch (Exception $e) {
  261. $result['status'] = 0;
  262. $result['msg'] = $e->getMessage();
  263. }
  264. return $result;
  265. }
  266. /**
  267. * 获取车辆预计下次保养时间
  268. * @return void
  269. */
  270. public function getCarNextDate($params=[])
  271. {
  272. $result = [
  273. 'status' => 1,
  274. 'msg' => '获取成功',
  275. 'data' => [],
  276. ];
  277. try {
  278. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  279. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  280. $carId = isset($params['car_id']) ? $params['car_id'] : [];
  281. $carNumber = isset($params['car_number']) ? $params['car_number'] : [];
  282. if (!empty($carNumber)) {
  283. $orderWhere['user_car_number'] = ['in',$carNumber];
  284. } else {
  285. $orderWhere['user_car_id'] = ['in',$carId];
  286. }
  287. $orderWhere['user_id'] = $userId;
  288. $orderWhere['company_id'] = $companyId;
  289. $orderWhere['next_date'] = ['neq',''];
  290. $orderWhere['status'] = 3;//状态:2=已接单,3=已完成,4=已取消
  291. $subQuery = Db::name('order')->alias('o')->where($orderWhere)
  292. ->order(['finish_time' => 'desc'])
  293. ->buildSql(); //构建查询语句
  294. $order = Db::table($subQuery)->alias('o')->field('id,user_car_id,user_car_number,next_date')
  295. ->group('user_car_id')->order('finish_time desc')->select();
  296. $row = [];
  297. if (!empty($order)) {
  298. foreach ($order as $key => $value) {
  299. $row[$value['user_car_number']]['next_date'] = $value['next_date'];
  300. }
  301. }
  302. $result['data'] = $row;
  303. } catch (Exception $e) {
  304. $result['status'] = 0;
  305. $result['msg'] = $e->getMessage();
  306. }
  307. return $result;
  308. }
  309. /**
  310. * 查看是否重复-卡券
  311. * @return void
  312. */
  313. public function getCheckCoupons($checkCode=[])
  314. {
  315. try {
  316. if (!empty($checkCode)) {
  317. $where['check_code'] = ['in', $checkCode];
  318. $userCoupons = Db::name('user_coupon')->field('id,check_code')->where($where)->select();
  319. if (!empty($userCoupons)) {
  320. $checkCodeDbArr = array_column($userCoupons,'check_code');
  321. foreach ($checkCode as $key => $code) {
  322. if (in_array($code,$checkCodeDbArr)) {
  323. $checkCode[$key] = Random::alnum(8);
  324. }
  325. }
  326. $checkCodeRes = $this->getCheckCoupons($checkCode);
  327. if ($checkCodeRes['status']) {
  328. $checkCode = $checkCodeRes['data'];
  329. }
  330. }
  331. }
  332. $result['data'] = $checkCode;
  333. } catch (Exception $e) {
  334. $result['status'] = 0;
  335. $result['msg'] = $e->getMessage();
  336. }
  337. return $result;
  338. }
  339. /**
  340. * 查看是否重复-订单
  341. * @return void
  342. */
  343. public function getCheckOrder($checkCode=[])
  344. {
  345. $result = [
  346. 'status' => 1,
  347. 'msg' => '',
  348. 'data' => [],
  349. ];
  350. try {
  351. if (!empty($checkCode)) {
  352. $where['check_code'] = ['in', $checkCode];
  353. $userCoupons = Db::name('order')->field('id,check_code')->where($where)->select();
  354. if (!empty($userCoupons)) {
  355. $checkCodeDbArr = array_column($userCoupons,'check_code');
  356. foreach ($checkCode as $key => $code) {
  357. if (in_array($code,$checkCodeDbArr)) {
  358. $checkCode[$key] = Random::alnum(8);
  359. }
  360. }
  361. $checkCodeRes = $this->getCheckOrder($checkCode);
  362. if ($checkCodeRes['status']) {
  363. $checkCode = $checkCodeRes['data'];
  364. }
  365. }
  366. }
  367. $result['data'] = $checkCode;
  368. } catch (Exception $e) {
  369. $result['status'] = 0;
  370. $result['msg'] = $e->getMessage();
  371. }
  372. return $result;
  373. }
  374. }