MovieRepository.php 37 KB

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