OrderService.php 16 KB

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