MovieRepository.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. <?php
  2. /**
  3. * @package merchant
  4. * @Author: Qinii
  5. * @Date: 2020/5/8
  6. */
  7. namespace app\common\repositories\movie;
  8. use app\common\dao\movie\order\MovieOrderDao;
  9. use app\common\model\movie\order\MovieOrder;
  10. use app\common\repositories\BaseRepository;
  11. use app\common\repositories\merchant\order\OrderMerchantRepository;
  12. use app\common\repositories\store\order\StoreOrderRepository;
  13. use app\traits\HuiPay;
  14. use crmeb\services\ApiResponseService;
  15. use think\exception\ValidateException;
  16. use think\facade\Db;
  17. /**
  18. * Class MovieRepository
  19. * @package app\common\repositories\movie
  20. * @author xaboy
  21. * @mixin MovieOrderDao
  22. */
  23. class MovieRepository extends BaseRepository
  24. {
  25. use HuiPay;
  26. protected $dao;
  27. const premium_ratio = 0.2;//溢价比例
  28. const yanglaojin_ratio = 0.3;
  29. const pv_ratio = 0.7;
  30. const tenMinute = 600;
  31. /**
  32. * ProductRepository constructor.
  33. * @param MovieOrderDao $dao
  34. */
  35. public function __construct(MovieOrderDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * @param $page
  41. * @param $limit
  42. * @param $initial
  43. * @param $city_name
  44. * @return false|int|mixed
  45. */
  46. public function getMovieCity($page = 1, $limit = 10, $initial = '', $city_name = '')
  47. {
  48. return DouHuoMallFilmApiRequest::getMovieCity($page, $limit, $initial = '', $city_name);
  49. }
  50. /**
  51. * @param $city_sign
  52. * @return false|int|mixed
  53. */
  54. public function getMovieArea($city_sign = '')
  55. {
  56. return DouHuoMallFilmApiRequest::getMovieArea($city_sign);
  57. }
  58. /**
  59. * @param $page
  60. * @param $limit
  61. * @param $city_sign
  62. * @param $area_sign
  63. * @return false|int|mixed
  64. */
  65. public function getCinema($page = 1, $limit = 10, $city_sign = '', $area_sign = '')
  66. {
  67. return DouHuoMallFilmApiRequest::getCinema($page, $limit, $city_sign, $area_sign);
  68. }
  69. /**
  70. * @param $page
  71. * @param $limit
  72. * @param $cinema_sign
  73. * @param $film_sign
  74. * @return false|int|mixed
  75. */
  76. public function getFilm($page = 1, $limit = 10, $cinema_sign = '', $film_sign = '')
  77. {
  78. return DouHuoMallFilmApiRequest::getFilm($page, $limit, $cinema_sign, $film_sign);
  79. }
  80. /**
  81. * @param $film_sign
  82. * @param $cinema_sign
  83. * @return mixed|string
  84. */
  85. public function getCinemaSched($film_sign = '', $cinema_sign = '')
  86. {
  87. $result = DouHuoMallFilmApiRequest::getCinemaSched($film_sign, $cinema_sign);
  88. $cinemaSchedList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
  89. ? $result['data']['list'] : [];
  90. foreach ($cinemaSchedList as $dateDay => &$fileSchedValue) {
  91. foreach ($fileSchedValue as $key => $value) {
  92. $pcYanglaojinGongxian = static::getPvYanglaojinScore($value['cost_price']);
  93. $fileSchedValue[$key]['price'] = $pcYanglaojinGongxian['cost_price']; // 销售价
  94. $fileSchedValue[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  95. $fileSchedValue[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  96. $fileSchedValue[$key]['score'] = $pcYanglaojinGongxian['score'];
  97. $fileSchedValue[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  98. // unset($fileSchedValue[$key]['channel_price']);
  99. }
  100. }
  101. return $cinemaSchedList;
  102. }
  103. /**
  104. * @param $page
  105. * @param $limit
  106. * @param $film_sign
  107. * @param $longitude
  108. * @param $latitude
  109. * @param $city_sign
  110. * @return mixed|string
  111. */
  112. public function getFilmSched($page = 1, $limit = 10, $film_sign = '', $longitude = '', $latitude = '', $city_sign = '')
  113. {
  114. return DouHuoMallFilmApiRequest::getFilmSched($page, $limit, $film_sign, $longitude, $latitude, $city_sign);
  115. }
  116. /**
  117. * @param $relation_id
  118. * @param $sched_sign
  119. * @return mixed|string
  120. */
  121. public function getSchedSeat($relation_id = '', $sched_sign = '')
  122. {
  123. $result = DouHuoMallFilmApiRequest::getSchedSeat($relation_id, $sched_sign);
  124. $seat = [];
  125. $schedSeatList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) ? $result['data'] : [];
  126. foreach ($schedSeatList as $key => $value) {
  127. $tmpprice = [];
  128. foreach ($value['price'] as $pKey => $pValue) {
  129. $tmpprice[$pKey] = ['channel_type' => $pKey, 'price' => $pValue];
  130. }
  131. $channel_price = [];
  132. if (isset($tmpprice['P2'])) {
  133. $channel_price = $tmpprice['P2'];
  134. } elseif (isset($tmpprice['P1'])) {
  135. $channel_price = $tmpprice['P1'];
  136. } elseif (isset($tmpprice['P0'])) {
  137. $channel_price = $tmpprice['P0'];
  138. }
  139. unset($schedSeatList[$key]['price']);
  140. $pcYanglaojinGongxian = self::getPvYanglaojinScore($channel_price['price']);
  141. $schedSeatList[$key]['channel_sn'] = base64_encode(json_encode($channel_price, JSON_FORCE_OBJECT));
  142. $schedSeatList[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  143. $schedSeatList[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  144. $schedSeatList[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  145. $schedSeatList[$key]['score'] = $pcYanglaojinGongxian['score'];
  146. $schedSeatList[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  147. $schedSeatList[$key]['relation_id'] = $relation_id; // 场次ID
  148. $schedSeatList[$key]['sched_sign'] = $sched_sign; // 场次编码
  149. $seat[$value['rowNo']][] = $schedSeatList[$key];
  150. }
  151. if ($seat) ksort($seat);
  152. return array_values($seat);
  153. }
  154. /**
  155. * 锁座订单
  156. *
  157. * @param $userinfo
  158. * @param $params
  159. * @return mixed
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function submitOrder($userinfo, $params)
  165. {
  166. /** @var StoreOrderRepository $storeOrder */
  167. $storeOrder = app()->make(StoreOrderRepository::class);
  168. $order_sn = substr($storeOrder->getNewOrderId(), 0, 20);
  169. $channel_sn = json_decode(base64_decode($params['channel_sn']), true);
  170. if (empty($channel_sn) || !isset($channel_sn['channel_type']) || empty($channel_sn['channel_type']) || !isset($channel_sn['price'])
  171. || empty($channel_sn['price']) || json_last_error()) {
  172. throw new ValidateException('请选择观影场次');
  173. }
  174. $sched_seat = json_decode($params['sched_seat'], true);
  175. if (json_last_error()) throw new ValidateException('锁座下单失败');
  176. $order_price = bcmul((string)$channel_sn['price'], (string)count($sched_seat), 2);
  177. $submitOrder = DouHuoMallFilmApiRequest::submitOrder($params['phone'] ?? $userinfo->phone, $channel_sn['channel_type'], $params['sched_sign'], array_column($sched_seat, 'seat_sign'), $order_sn, $order_price);
  178. if (!empty($submitOrder) && isset($submitOrder['code']) && ($submitOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE) && isset($submitOrder['data']['plat_order'])) {
  179. /** @var CinemaRepository $cinemaRepository */
  180. $cinemaRepository = app()->make(CinemaRepository::class);
  181. $cinema = $cinemaRepository->getCinemaInfo($params['cinema_sign']);
  182. /** @var FilmRepository $filmRepository */
  183. $filmRepository = app()->make(FilmRepository::class);
  184. $filminfo = $filmRepository->getFilmInfo($params['film_sign']);
  185. $_order = [
  186. 'uid' => $userinfo->uid,
  187. 'mobile' => $params['phone'] ?? $userinfo->phone,
  188. 'phone' => $params['phone'] ?? $userinfo->phone,
  189. 'order_sn' => $order_sn,
  190. 'plat_order' => $submitOrder['data']['plat_order'],
  191. 'price' => $order_price,
  192. 'number' => count($sched_seat),
  193. 'order_price' => bcadd((string)$order_price, bcmul((string)$order_price, (string)self::premium_ratio, 2), 2),
  194. 'channel_type' => $channel_sn['channel_type'],
  195. 'city_sign' => $cinema['city_sign'],
  196. 'city_name' => $cinema['city_name'],
  197. 'area_code' => $cinema['area_sign'],
  198. 'area_name' => $cinema['area_name'],
  199. 'cinema_sign' => $cinema['cinema_sign'],
  200. 'cinema_name' => $cinema['cinema_name'],
  201. 'address' => $cinema['address'],
  202. 'longitude' => $cinema['longitude'],
  203. 'latitude' => $cinema['latitude'],
  204. 'hall_sign' => $params['hall_sign'],
  205. 'hall_name' => $params['hall_name'],
  206. 'relation_id' => $params['relation_id'],
  207. 'sched_sign' => $params['sched_sign'],
  208. 'film_sign' => $filminfo['film_sign'],
  209. 'film_name' => $filminfo['film_name'],
  210. 'show_date' => $params['show_date'],
  211. 'show_time' => $params['show_time'],
  212. 'sched_seat' => $params['sched_seat'], // 场次座位
  213. 'create_time' => time()
  214. ];
  215. $order_id = Db::transaction(function () use ($userinfo, $_order) {
  216. $orderModel = $this->dao->create($_order);
  217. if ($orderModel) return $orderModel['order_id'];
  218. throw new ValidateException('锁座下单失败');
  219. });
  220. return compact('order_id', 'order_sn');
  221. } else {
  222. throw new ValidateException($submitOrder['msg']);
  223. }
  224. }
  225. /**
  226. * @param $userinfo
  227. * @param $order_sn
  228. * @param $pay_type
  229. * @return mixed
  230. * @throws \think\db\exception\DataNotFoundException
  231. * @throws \think\db\exception\DbException
  232. * @throws \think\db\exception\ModelNotFoundException
  233. */
  234. public function confirmOrder($userinfo, $order_id, $pay_type, $code)
  235. {
  236. $orderinfo = $this->dao->search(['order_id' => $order_id])->find();
  237. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  238. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 2) throw new ValidateException('订单已超时');
  239. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 3) throw new ValidateException('订单已取消');
  240. if ($orderinfo['paid'] == 1) throw new ValidateException('订单已支付');
  241. $res = $this->movie_pay(strtolower($pay_type), $code, $userinfo['wechat_user_id'], $orderinfo, $orderinfo['film_name'], env('APP_URL') . "/api/hf_notify/movie", 'delay');
  242. Db::name('log')->insert(['data' => '汇付电影票订单支付--- 返回参数' . json_encode($res)]);
  243. if (isset($res['id'])) {
  244. $type = 0;
  245. if ($pay_type == 'wx') $type = 2;
  246. if (strtolower($pay_type) == 'ali') $type = 1;
  247. $orderinfo->pay_type = $type;
  248. $orderinfo->update_time = time();
  249. $orderinfo->save();
  250. return $res;
  251. }
  252. throw new ValidateException('支付失败');
  253. }
  254. /**
  255. * 支付
  256. *
  257. * @param $payType
  258. * @param $openid
  259. * @param $_order
  260. * @param $title
  261. * @param $notify_url
  262. * @param $pay_mode
  263. * @param $appid
  264. * @return array|mixed
  265. */
  266. private function pay($payType, $openid, $_order, $title, $notify_url, $pay_mode = null, $appid = '')
  267. {
  268. switch ($payType) {
  269. case 'wx':
  270. $pay_wx = systemConfig('pay_wx');
  271. if ($pay_wx == 2) {
  272. $appid = env('WECHAT_MP.APPID');
  273. $new_secret = env('WECHAT_MP.SECRET');
  274. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  275. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  276. } elseif ($pay_wx == 3) {
  277. $appid = env('WECHAT_TDYL.APPID');
  278. $new_secret = env('WECHAT_TDYL.SECRET');
  279. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  280. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  281. }
  282. $type = '2';
  283. $params = [
  284. 'order_no' => $_order['order_sn'],
  285. 'app_id' => $app_id,
  286. 'pay_channel' => 'wx_lite',
  287. 'pay_amt' => (string)$_order['order_price'],
  288. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  289. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  290. 'device_info' => [
  291. 'device_ip' => app('request')->ip()
  292. ],
  293. 'expend' => json_encode(['open_id' => $openid, 'wx_app_id' => $appid]),
  294. // 'fee_mode' => 'I',
  295. 'notify_url' => $notify_url,
  296. 'pay_mode' => $pay_mode
  297. ];
  298. break;
  299. case 'ALI':
  300. case 'ali':
  301. $type = '1';
  302. // 天地博爱科技通道
  303. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  304. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  305. $params = [
  306. 'order_no' => $_order['order_sn'],
  307. 'app_id' => $app_id,
  308. 'pay_channel' => 'alipay_qr',
  309. 'pay_amt' => (string)$_order['order_price'],
  310. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  311. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  312. 'device_info' => [
  313. 'device_ip' => app('request')->ip()
  314. ],
  315. // 'fee_mode' => 'I',
  316. 'notify_url' => $notify_url
  317. ];
  318. break;
  319. default:
  320. throw new ValidateException('请选择支付方式');
  321. break;
  322. }
  323. return $this->Payment_create_traits_a($params, $type, $key);
  324. }
  325. /**
  326. * 电影票支付
  327. *
  328. * @param $payType
  329. * @param $code
  330. * @param $wechat_user_id
  331. * @param $orderinfo
  332. * @param $title
  333. * @param $notify_url
  334. * @param $pay_mode
  335. * @param $appid
  336. * @return array|mixed|void
  337. */
  338. private function movie_pay($payType, $code, $wechat_user_id, $orderinfo, $title, $notify_url, $pay_mode = null, $appid = '')
  339. {
  340. if ($payType == 'wx') {
  341. if (!$code || $code == '' || $code == 'undefined') {
  342. throw new ValidateException('授权失败,code为空');
  343. }
  344. if ($appid == '') {
  345. $pay_wx = systemConfig('pay_wx');
  346. if ($pay_wx == 2) {
  347. $appid = env('WECHAT_MP.APPID');
  348. $new_secret = env('WECHAT_MP.SECRET');
  349. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  350. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  351. } elseif ($pay_wx == 3) {
  352. $appid = env('WECHAT_TDYL.APPID');
  353. $new_secret = env('WECHAT_TDYL.SECRET');
  354. Db::name('log')->insert(['data' => 'appid---' . $appid]);
  355. Db::name('log')->insert(['data' => 'new_secret---' . $new_secret]);
  356. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  357. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  358. }
  359. } else {
  360. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  361. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  362. $new_secret = "c8cbe24fa3b83f77a88b79554b3aeab4";
  363. }
  364. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $new_secret . "&js_code=" . $code . "&grant_type=authorization_code";
  365. $get = curlGet($url);
  366. Db::name('log')->insert(['data' => 'open_id---' . json_encode($get)]);
  367. if (!isset($get['openid']) || empty($get['openid'])) {
  368. throw new ValidateException('授权失败');
  369. }
  370. $member_id = 0;
  371. $type = '2';
  372. $pay_channel = 'wx_lite';
  373. $expend = ['open_id' => $get['openid'], 'wx_app_id' => $appid];
  374. if (false) {
  375. // if ($orderinfo['order_type'] = "movie") {
  376. $cfb = sprintf("%01.2f", round(bcmul($orderinfo['order_price'], '0.094', 5), 2));//9.4%的分账
  377. $cfb_member = "jsdk_member_1680162243019";
  378. // 平台
  379. $cxb = sprintf("%01.2f", round(bcsub($orderinfo['order_price'], $cfb, 5), 2));//剩下的归平台
  380. $div_members = [
  381. // [
  382. // 'member_id' => $member_id,
  383. // 'amount' => $cxb,
  384. // 'fee_flag' => 'Y'
  385. // ],
  386. // [
  387. // 'member_id' => $cfb_member,
  388. // 'amount' => $cfb,
  389. // 'fee_flag' => 'N'
  390. // ]
  391. ];
  392. } else {
  393. $div_members = [
  394. [
  395. 'member_id' => $member_id,
  396. 'amount' => $orderinfo['order_price'],
  397. 'fee_flag' => 'Y'
  398. ]
  399. ];
  400. }
  401. $params = [
  402. 'order_no' => $orderinfo['order_sn'],
  403. 'app_id' => $app_id,
  404. 'pay_channel' => $pay_channel,
  405. 'pay_amt' => (string)$orderinfo['order_price'],
  406. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  407. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  408. 'device_info' => [
  409. 'device_ip' => app('request')->ip()
  410. ],
  411. 'expend' => json_encode($expend),
  412. 'div_members' => json_encode($div_members),
  413. 'fee_mode' => 'I',
  414. 'notify_url' => $notify_url
  415. ];
  416. if ($pay_mode == 'delay') {
  417. $params['pay_mode'] = 'delay';
  418. unset($params['div_members']);
  419. unset($params['fee_mode']);
  420. }
  421. return $this->Payment_create_traits_b($params, $type, $key);
  422. } elseif ($payType == 'ali') {
  423. $member_id = 0;
  424. $type = '1';
  425. $pay_channel = 'alipay_qr';
  426. // 天地博爱科技通道
  427. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  428. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  429. // 天地博爱通道
  430. // $app_id='app_0827b960-b932-45ec-b800-4e850091b419';
  431. // $key='api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  432. $div_members = [
  433. [
  434. 'member_id' => $member_id,
  435. 'amount' => $orderinfo['order_price'],
  436. 'fee_flag' => 'Y'
  437. ]
  438. ];
  439. $params = [
  440. 'order_no' => $orderinfo['order_sn'],
  441. 'app_id' => $app_id,
  442. 'pay_channel' => $pay_channel,
  443. 'pay_amt' => (string)$orderinfo['order_price'],
  444. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  445. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  446. 'device_info' => [
  447. 'device_ip' => app('request')->ip()
  448. ],
  449. 'div_members' => json_encode($div_members),
  450. 'fee_mode' => 'I',
  451. 'notify_url' => $notify_url
  452. ];
  453. if ($pay_mode == 'delay') {
  454. $params['pay_mode'] = 'delay';
  455. unset($params['div_members']);
  456. unset($params['fee_mode']);
  457. }
  458. return $this->Payment_create_traits_a($params, $type, $key);
  459. }
  460. }
  461. /**
  462. * @param $userinfo
  463. * @param $order_sn
  464. * @return mixed|string
  465. */
  466. public function getOrderInfo($userinfo, $order_sn)
  467. {
  468. $orderinfo = $this->dao->search(['order_sn' => $order_sn, 'uid' => $userinfo['uid']])->find();
  469. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  470. $time = time();
  471. $result = DouHuoMallFilmApiRequest::getOrderInfo($order_sn);
  472. if (!empty($result) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
  473. $plat_orderinfo = $result['data'];
  474. $plat_orderinfo['oid'] = $orderinfo['order_id'];
  475. $plat_orderinfo['type'] = ($orderinfo['pay_type'] == 1) ? 'wx' : ($orderinfo['pay_type'] == 2 ? 'ali' : 'wx');
  476. $plat_orderinfo['order_price'] = $orderinfo['order_price'];
  477. $plat_orderinfo['order_sn'] = $order_sn;
  478. $plat_orderinfo['create_time'] = strtotime($orderinfo['create_time']);// 下单时间戳
  479. $plat_orderinfo['phone'] = $orderinfo['mobile'];
  480. $plat_orderinfo['sched_seat'] = json_decode($orderinfo['sched_seat'], true);
  481. $plat_orderinfo['cinema'] = [
  482. 'cinema_sign' => $orderinfo['cinema_sign'],
  483. 'cinema_name' => $orderinfo['cinema_name'],
  484. 'address' => $orderinfo['address'],
  485. 'longitude' => $orderinfo['longitude'],
  486. 'latitude' => $orderinfo['latitude'],
  487. 'hall_sign' => $orderinfo['hall_sign'],
  488. 'hall_name' => $orderinfo['hall_name'],
  489. 'tel' => Db::name('movie_cinema')->where('cinema_sign', $orderinfo['cinema_sign'])->value('tel')
  490. ];
  491. $film = Db::name('movie_film')->where('film_sign', $orderinfo['film_sign'])->find();
  492. $plat_orderinfo['film'] = [
  493. 'film_sign' => $orderinfo['film_sign'],
  494. 'film_name' => $orderinfo['film_name'],
  495. 'language' => $film['language'],
  496. 'cover_img' => $film['cover_img'],
  497. 'version' => $film['version'],
  498. ];
  499. $plat_orderinfo['sched_time'] = date('Y-m-d H:i', strtotime($result['data']['sched_time']));
  500. $plat_orderinfo['schedend_time'] = date('m-d H:i', strtotime($result['data']['sched_time']) + $film['duration'] * 60);
  501. // 订单状态 0:待支付;1:已出票;3:已取消
  502. $statuspayinfo = $this->getOrderStatus($plat_orderinfo['create_time'], $plat_orderinfo['status']);
  503. $plat_orderinfo['status'] = $statuspayinfo['status'];
  504. $plat_orderinfo['state'] = $statuspayinfo['state'];
  505. $plat_orderinfo['time_remaining'] = $statuspayinfo['time_remaining'];// 支付倒计时
  506. unset($plat_orderinfo['plat_order']);
  507. unset($plat_orderinfo['third_order']);
  508. return $plat_orderinfo;
  509. }
  510. throw new ValidateException('订单不存在');
  511. }
  512. /**
  513. * 获取订单状态
  514. *
  515. * @param $create_time
  516. * @param $plat_status
  517. * @return array
  518. */
  519. private function getOrderStatus($create_time, $plat_status = 0)
  520. {
  521. $time_remaining = 0;
  522. $time = time();
  523. // 订单状态 0:待支付;1:已出票;3:已取消
  524. $create_time = $create_time + self::tenMinute;
  525. if ($plat_status == 1 && ($create_time < $time)) {
  526. $status = 0;
  527. $state = '待付款';
  528. $time_remaining = $time - $create_time;
  529. } elseif ($plat_status == 5) {
  530. $status = 3;
  531. $state = '支付超时,已取消';
  532. } elseif ($plat_status == 6) {
  533. $status = 1;
  534. $state = '已出票';
  535. } else {
  536. $status = 3;
  537. $state = '支付超时,已取消';
  538. }
  539. return compact('status', 'state', 'time_remaining');
  540. }
  541. /**
  542. * @return array
  543. * @throws \think\db\exception\DataNotFoundException
  544. * @throws \think\db\exception\DbException
  545. * @throws \think\db\exception\ModelNotFoundException
  546. */
  547. public function getLocalMovieCity()
  548. {
  549. $city_name = [];
  550. $city = Db::name('movie_city')->field('city_sign,city_name,initial')->where('is_show', 1)->order('id asc')->select();
  551. foreach ($city as $key => $value) {
  552. $city_name[$value['initial']][] = ['city_sign' => $value['city_sign'], 'city_name' => $value['city_name']];
  553. }
  554. $initial = !empty($city) ? array_values(array_column($city->toArray(), 'initial', 'initial')) : [];
  555. return compact('city_name', 'initial');
  556. }
  557. /**
  558. * 获取pv、养老金、售价、积分
  559. *
  560. * @param $cost_price
  561. * @return array
  562. */
  563. public static function getPvYanglaojinScore($cost_price)
  564. {
  565. $premium = round($cost_price * self::premium_ratio, 2);
  566. $cost_price = bcadd((string)$cost_price, (string)$premium, 2);
  567. $pv = $gongxian = $score = bcmul((string)$premium, self::pv_ratio, 2);
  568. $yanglaojin = bcmul((string)$premium, self::yanglaojin_ratio, 2);
  569. return compact('premium', 'cost_price', 'pv', 'yanglaojin', 'gongxian', 'score');
  570. }
  571. /**
  572. * 计算佣金分佣奖励
  573. *
  574. * @param $pay_type
  575. * @param $order_sn
  576. * @param $post_data
  577. * @return void
  578. * @throws \think\db\exception\DataNotFoundException
  579. * @throws \think\db\exception\DbException
  580. * @throws \think\db\exception\ModelNotFoundException
  581. */
  582. public function updateMovieOrderAndcalculationCommissionDividendAward($pay_type, $order_sn, $post_data)
  583. {
  584. $orderinfo = $this->dao->search(['order_sn' => $order_sn])->find();
  585. if ($orderinfo) throw new ValidateException('订单不存在');
  586. Db::startTrans();
  587. $orderinfo->paid = 1;
  588. $orderinfo->pay_time = time();
  589. $orderinfo->pay_type = $pay_type;
  590. $orderinfo->status = 1;
  591. $orderinfo->channel_trade_no = $post_data['id'];
  592. $orderinfo->pay_open_id = $post_data['expend']['open_id'];
  593. $orderinfo->update_time = time();
  594. // 支付方式 1支付宝 2微信
  595. if ($pay_type == 1) {
  596. $orderinfo->alipay_no = $post_data['out_trans_id'];
  597. }
  598. if ($pay_type == 2) {
  599. $orderinfo->weixin_no = $post_data['out_trans_id'];
  600. }
  601. $res = $orderinfo->save();
  602. if ($res) {
  603. $confirmOrder = DouHuoMallFilmApiRequest::confirmOrder($orderinfo['order_sn'], $orderinfo['price']);
  604. if (!empty($confirmOrder) && isset($confirmOrder['code']) && ($confirmOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
  605. // 计算佣金、分红奖励
  606. $this->calculationCommissionDividendAward($orderinfo);
  607. // 提交事务
  608. Db::commit();
  609. } else {
  610. Db::name('log')->insert(['data' => '电影票订单支付回调向【微唯宝】--- 告知电影订单已支付失败,订单编号:' . $order_sn]);
  611. // 回滚事务
  612. Db::rollback();
  613. throw new ValidateException('向微维宝确认订单失败');
  614. }
  615. } else {
  616. Db::name('log')->insert(['data' => '汇付电影票订单支付回调--- 更新电影订单表失败,订单编号:' . $order_sn]);
  617. // 回滚事务
  618. Db::rollback();
  619. throw new ValidateException('更新订单失败');
  620. }
  621. }
  622. /**
  623. * 计算佣金、分红奖励
  624. * @param $orderinfo
  625. * @return void
  626. * @throws \think\db\exception\DataNotFoundException
  627. * @throws \think\db\exception\DbException
  628. * @throws \think\db\exception\ModelNotFoundException
  629. */
  630. private function calculationCommissionDividendAward($orderinfo)
  631. {
  632. /** @var StoreOrderRepository $storeOrderRepository */
  633. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  634. // 电影分佣比例配置
  635. $movie_commission_rate_config = Db::name('system_config_value')->where('config_key', 'movie_commission_rate_config')->find();
  636. // 让利金额
  637. $concession_pri = bcmul((string)$orderinfo['price'], $movie_commission_rate_config['premium_rate'], 2);
  638. // 养老金
  639. $ylj_pri = bcmul($concession_pri, $movie_commission_rate_config['consumer_yanglaojin_rate'], 2);
  640. // 业绩PV
  641. $pv = bcmul($concession_pri, $movie_commission_rate_config['pv_rate'], 2);
  642. //-------------------消费者收益---------------------
  643. // PV值
  644. $storeOrderRepository->con_log_pv($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送PV值");
  645. // 养老金
  646. $this->distributionAnnuityEarnings($orderinfo['uid'], $ylj_pri, $orderinfo['order_id'], 5, $orderinfo['order_sn'], '购买电影票获得养老金' . $ylj_pri, 0, 0);
  647. // 消费者 - 红包 - 老用户发红包奖励
  648. $user = Db::name("user")->where('uid', $orderinfo["uid"])->find();
  649. if ($user['is_old'] == 1) {
  650. $storeOrderRepository->giveHongBao($orderinfo["uid"], $pv, $orderinfo['order_id'], '购买电影票赠送红包');
  651. }
  652. // 积分
  653. $storeOrderRepository->score_log($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送积分");
  654. // 贡献值
  655. $storeOrderRepository->con_log($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送贡献值");
  656. //-------------------消费者收益---------------------
  657. // 推广者
  658. if ($user['spread_uid'] != 0) {
  659. //查询{下单用户}的上级
  660. $spread_data = Db::name('user')->where('uid', $user['spread_uid'])->find();
  661. $rate = $storeOrderRepository->getUserGroupRate($spread_data['user_group']);
  662. //养老金
  663. $spread_yanglaojin = bcmul($pv, $movie_commission_rate_config['spread_yanglaojin_rate'], 2);
  664. $this->distributionAnnuityEarnings(
  665. $spread_data['uid'], $spread_yanglaojin, $orderinfo['order_id'], 5, $orderinfo['order_sn'], '电影票-推广者获得养老金' . $spread_yanglaojin, 0, 0
  666. );
  667. // 直推佣金
  668. $yj_amount_90 = round(bcmul($pv, $rate, 2) * $movie_commission_rate_config['commission_directthrust_rate'], 2);
  669. $this->distributionAnnuityEarnings(
  670. $spread_data['uid'], $yj_amount_90, $orderinfo['order_id'], 3, $orderinfo['order_sn'], '电影票-推广者获得直推佣金' . $yj_amount_90,
  671. 0, 0
  672. );
  673. // 复购金-推广者的佣金抽10%到个人复购金
  674. $fgj_amount_10 = round(bcmul($pv, $rate, 2) * $movie_commission_rate_config['repurchase_directthrust_rate'], 2);
  675. $storeOrderRepository->fugou_user_log($spread_data['uid'], $fgj_amount_10, $orderinfo['order_id'], 1);
  676. // 积分
  677. $score = bcmul($pv, $rate, 2);
  678. $storeOrderRepository->score_log($spread_data['uid'], $score, $orderinfo['order_id'], 13, "电影票-推广者获得赠送积分");
  679. // 业务员及以上身份
  680. if ($spread_data['user_group'] > 1) {
  681. // 贡献值
  682. $storeOrderRepository->con_log($spread_data['uid'], $pv, $orderinfo['order_id'], 13, "电影票-推广者获得赠送贡献值");
  683. }
  684. // 普通用户推广人
  685. if ($spread_data['user_group'] == 1) {
  686. // 贡献值
  687. $gongxianValue = bcmul($pv, $movie_commission_rate_config['contributionvalue_spread_user_rate'], 2);
  688. $storeOrderRepository->con_log($spread_data['uid'], $gongxianValue, $orderinfo['order_id'], 13, "电影票-推广者用户身份获得赠送贡献值");
  689. }
  690. }
  691. // 锁客收益
  692. if ($user['mer_id'] != 0) {
  693. $merchant_uid = Db::name('merchant')->where('mer_id', $user['mer_id'])->value('uid');
  694. $merchant_user = Db::name('user')->where('uid', $merchant_uid)->find();
  695. if (!empty($merchant_user)) {
  696. // 锁客佣金
  697. $yj_amount_90 = round(bcmul($pv, $movie_commission_rate_config['commission_lockpassenger_merid_rate'], 2) * $movie_commission_rate_config['commission_lockpassenger_rate'], 2);
  698. $this->distributionAnnuityEarnings(
  699. $merchant_uid, $yj_amount_90, $orderinfo['order_id'], 3, $orderinfo['order_sn'], '电影票-商户锁定用户获得佣金' . $yj_amount_90,
  700. $user['mer_id'], 0
  701. );
  702. // 锁客复购金
  703. $fgj_amount_10 = round(bcmul($pv, $movie_commission_rate_config['repurchase_lockpassenger_merid_rate'], 2) * $movie_commission_rate_config['repurchase_lockpassenger_rate'], 2);
  704. $storeOrderRepository->fugou_user_log($merchant_uid, $fgj_amount_10, $orderinfo['order_id'], 1);
  705. }
  706. }
  707. }
  708. /**
  709. * 直推奖、养老金
  710. *
  711. * @param $uid
  712. * @param $num
  713. * @param $order_id
  714. * @param $comm_order_type
  715. * @param $order_sn
  716. * @param $mark
  717. * @param $merid
  718. * @param $ds
  719. * @return void
  720. */
  721. private function distributionAnnuityEarnings($uid, $num, $order_id, $comm_order_type, $order_sn, $mark, $merid, $ds = 0)
  722. {
  723. $name = '';
  724. if ($comm_order_type == 3) {
  725. $name = '直推奖';
  726. } else if ($comm_order_type == 5) {
  727. $name = '养老金';
  728. }
  729. $bill = [
  730. 'uid' => $uid,
  731. 'link_id' => $order_id,//关联订单id
  732. 'pm' => 1,//0:支出,1:获得
  733. 'title' => $name,//账单标题
  734. 'category' => 'now_money',//明细种类
  735. 'type' => 'commission',//明细类型
  736. 'number' => $num,//明细数字
  737. 'balance' => 0,//剩余
  738. 'mark' => $mark,//备注
  739. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  740. 'status' => $ds,//0待确定,1有效,-1无效
  741. 'commission_type' => $comm_order_type,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  742. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  743. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  744. 'order_sn' => $order_sn,//订单号
  745. 'tripartite' => 0,//
  746. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  747. 'mer_id' => $merid,//商户id
  748. 'source' => 0,//1线下 0线上
  749. 'order_type' => 1,//1自营 2 第三方 订单类型
  750. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  751. 'gzc' => 0,//养老金是否已进入公证处log表0:未进入。1:已进入
  752. 'take_time' => '',//结算时间
  753. 'district_id' => '',//
  754. 'street_id' => '',//
  755. 'month' => '',//月份
  756. ];
  757. Db::name('user_bill')->insert($bill);
  758. }
  759. /**
  760. * 订单列表
  761. *
  762. * @param $userinfo
  763. * @param $page
  764. * @param $limit
  765. * @param $type
  766. * @return array
  767. * @throws \think\db\exception\DataNotFoundException
  768. * @throws \think\db\exception\DbException
  769. * @throws \think\db\exception\ModelNotFoundException
  770. */
  771. public function getOrderList($userinfo, $page, $limit, $type = -1)
  772. {
  773. $where['uid'] = $userinfo['uid'];
  774. switch ($type) {
  775. case 0:
  776. $where['status'] = [0];
  777. break;
  778. case 1:
  779. case 4:
  780. $where['status'] = [1, 4];
  781. break;
  782. case 2:
  783. case 3:
  784. $where['status'] = [2, 3];
  785. break;
  786. default:
  787. break;
  788. }
  789. $film = [];
  790. $field = 'order_sn,order_price,city_name,area_name,cinema_name,address,hall_name,film_sign,film_name,show_date,show_time,sched_seat,number,status,create_time';
  791. $movie_order = $this->dao->search($where)
  792. ->when($page && $limit, function ($query) use ($page, $limit) {
  793. $query->page($page, $limit);
  794. })->order('order_id desc')->field([$field])->select()->toArray();
  795. foreach ($movie_order as $key => $value) {
  796. if (!isset($film[$value['film_sign']])) $film[$value['film_sign']] = Db::name('movie_film')->where('film_sign', $value['film_sign'])->value('cover_img');
  797. $movie_order[$key]['sched_seat'] = json_decode($value['sched_seat'], true);
  798. $movie_order[$key]['cover_img'] = $film[$value['film_sign']];
  799. $movie_order[$key]['time_remaining'] = 0;
  800. // 订单状态 0:待支付;1:已出票;3:已取消
  801. $statuspayinfo = $this->getOrderStatus(strtotime($value['create_time']), $value['status']);
  802. $movie_order[$key]['status'] = $statuspayinfo['status'];
  803. $movie_order[$key]['state'] = $statuspayinfo['state'];
  804. $movie_order[$key]['time_remaining'] = $statuspayinfo['time_remaining'];// 支付倒计时
  805. }
  806. $count = $this->dao->search($where)->count();
  807. return compact('movie_order', 'count');
  808. }
  809. /**
  810. * 所有未支付订单列表
  811. *
  812. * @param $status
  813. * @param $paid
  814. * @return array
  815. * @throws \think\db\exception\DataNotFoundException
  816. * @throws \think\db\exception\DbException
  817. * @throws \think\db\exception\ModelNotFoundException
  818. */
  819. public function getOrderListAll($status = [], $paid = 0)
  820. {
  821. $where = ['status' => $status, 'paid' => $paid];
  822. $field = 'order_id,order_price,city_name,area_name,cinema_name,address,hall_name,film_sign,film_name,show_date,show_time,sched_seat,number,status,create_time';
  823. // foreach ($movie_order as $key => $value) {
  824. // $movie_order[$key]['sched_seat'] = json_decode($value['sched_seat'], true);
  825. // $movie_order[$key]['cover_img'] = Db::name('movie_film')->where('film_sign', $value['film_sign'])->value('cover_img');
  826. // }
  827. return $this->dao->search($where)->order('create_time asc')->field([$field])->select()->toArray();
  828. }
  829. }