MovieRepository.php 37 KB

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