MovieRepository.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. $plat_orderinfo['time_remaining'] = $time - $plat_orderinfo['create_time'];
  512. } elseif ($orderinfo['status'] == 1) {
  513. $plat_orderinfo['status'] = 1;
  514. $plat_orderinfo['state'] = '已支付';
  515. } else {
  516. $plat_orderinfo['status'] = 3;
  517. $plat_orderinfo['state'] = '支付超时,已取消';
  518. }
  519. unset($plat_orderinfo['plat_order']);
  520. unset($plat_orderinfo['third_order']);
  521. return $plat_orderinfo;
  522. }
  523. throw new ValidateException('订单不存在');
  524. }
  525. /**
  526. * @return array
  527. * @throws \think\db\exception\DataNotFoundException
  528. * @throws \think\db\exception\DbException
  529. * @throws \think\db\exception\ModelNotFoundException
  530. */
  531. public function getLocalMovieCity()
  532. {
  533. $city_name = [];
  534. $city = Db::name('movie_city')->field('city_sign,city_name,initial')->where('is_show', 1)->order('id asc')->select();
  535. foreach ($city as $key => $value) {
  536. $city_name[$value['initial']][] = ['city_sign' => $value['city_sign'], 'city_name' => $value['city_name']];
  537. }
  538. $initial = !empty($city) ? array_values(array_column($city->toArray(), 'initial', 'initial')) : [];
  539. return compact('city_name', 'initial');
  540. }
  541. /**
  542. * 获取pv、养老金、售价、积分
  543. *
  544. * @param $cost_price
  545. * @return array
  546. */
  547. public static function getPvYanglaojinScore($cost_price)
  548. {
  549. $premium = round($cost_price * self::premium_ratio, 2);
  550. $cost_price = bcadd((string)$cost_price, (string)$premium, 2);
  551. $pv = $gongxian = $score = bcmul((string)$premium, self::pv_ratio, 2);
  552. $yanglaojin = bcmul((string)$premium, self::yanglaojin_ratio, 2);
  553. return compact('premium', 'cost_price', 'pv', 'yanglaojin', 'gongxian', 'score');
  554. }
  555. /**
  556. * 计算佣金分佣奖励
  557. *
  558. * @param $pay_type
  559. * @param $order_sn
  560. * @param $post_data
  561. * @return void
  562. * @throws \think\db\exception\DataNotFoundException
  563. * @throws \think\db\exception\DbException
  564. * @throws \think\db\exception\ModelNotFoundException
  565. */
  566. public function updateMovieOrderAndcalculationCommissionDividendAward($pay_type, $order_sn, $post_data)
  567. {
  568. $orderinfo = $this->dao->search(['order_sn' => $order_sn])->find();
  569. if ($orderinfo) throw new ValidateException('订单不存在');
  570. Db::startTrans();
  571. $orderinfo->paid = 1;
  572. $orderinfo->pay_time = time();
  573. $orderinfo->pay_type = $pay_type;
  574. $orderinfo->status = 1;
  575. $orderinfo->channel_trade_no = $post_data['id'];
  576. $orderinfo->pay_open_id = $post_data['expend']['open_id'];
  577. $orderinfo->update_time = time();
  578. // 支付方式 1支付宝 2微信
  579. if ($pay_type == 1) {
  580. $orderinfo->alipay_no = $post_data['out_trans_id'];
  581. }
  582. if ($pay_type == 2) {
  583. $orderinfo->weixin_no = $post_data['out_trans_id'];
  584. }
  585. $res = $orderinfo->save();
  586. if ($res) {
  587. $confirmOrder = DouHuoMallFilmApiRequest::confirmOrder($orderinfo['order_sn'], $orderinfo['price']);
  588. if (!empty($confirmOrder) && isset($confirmOrder['code']) && ($confirmOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
  589. // 计算佣金、分红奖励
  590. $this->calculationCommissionDividendAward($orderinfo);
  591. // 提交事务
  592. Db::commit();
  593. } else {
  594. Db::name('log')->insert(['data' => '电影票订单支付回调向【微唯宝】--- 告知电影订单已支付失败,订单编号:' . $order_sn]);
  595. // 回滚事务
  596. Db::rollback();
  597. throw new ValidateException('向微维宝确认订单失败');
  598. }
  599. } else {
  600. Db::name('log')->insert(['data' => '汇付电影票订单支付回调--- 更新电影订单表失败,订单编号:' . $order_sn]);
  601. // 回滚事务
  602. Db::rollback();
  603. throw new ValidateException('更新订单失败');
  604. }
  605. }
  606. /**
  607. * 计算佣金、分红奖励
  608. * @param $orderinfo
  609. * @return void
  610. * @throws \think\db\exception\DataNotFoundException
  611. * @throws \think\db\exception\DbException
  612. * @throws \think\db\exception\ModelNotFoundException
  613. */
  614. private function calculationCommissionDividendAward($orderinfo)
  615. {
  616. /** @var StoreOrderRepository $storeOrderRepository */
  617. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  618. // 电影分佣比例配置
  619. $movie_commission_rate_config = Db::name('system_config_value')->where('config_key', 'movie_commission_rate_config')->find();
  620. // 让利金额
  621. $concession_pri = bcmul((string)$orderinfo['price'], $movie_commission_rate_config['premium_rate'], 2);
  622. // 养老金
  623. $ylj_pri = bcmul($concession_pri, $movie_commission_rate_config['consumer_yanglaojin_rate'], 2);
  624. // 业绩PV
  625. $pv = bcmul($concession_pri, $movie_commission_rate_config['pv_rate'], 2);
  626. //-------------------消费者收益---------------------
  627. // PV值
  628. $storeOrderRepository->con_log_pv($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送PV值");
  629. // 养老金
  630. $this->distributionAnnuityEarnings($orderinfo['uid'], $ylj_pri, $orderinfo['order_id'], 5, $orderinfo['order_sn'], '购买电影票获得养老金' . $ylj_pri, 0, 0);
  631. // 消费者 - 红包 - 老用户发红包奖励
  632. $user = Db::name("user")->where('uid', $orderinfo["uid"])->find();
  633. if ($user['is_old'] == 1) {
  634. $storeOrderRepository->giveHongBao($orderinfo["uid"], $pv, $orderinfo['order_id'], '购买电影票赠送红包');
  635. }
  636. // 积分
  637. $storeOrderRepository->score_log($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送积分");
  638. // 贡献值
  639. $storeOrderRepository->con_log($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送贡献值");
  640. //-------------------消费者收益---------------------
  641. // 推广者
  642. if ($user['spread_uid'] != 0) {
  643. //查询{下单用户}的上级
  644. $spread_data = Db::name('user')->where('uid', $user['spread_uid'])->find();
  645. $rate = $storeOrderRepository->getUserGroupRate($spread_data['user_group']);
  646. //养老金
  647. $spread_yanglaojin = bcmul($pv, $movie_commission_rate_config['spread_yanglaojin_rate'], 2);
  648. $this->distributionAnnuityEarnings(
  649. $spread_data['uid'], $spread_yanglaojin, $orderinfo['order_id'], 5, $orderinfo['order_sn'], '电影票-推广者获得养老金' . $spread_yanglaojin, 0, 0
  650. );
  651. // 直推佣金
  652. $yj_amount_90 = round(bcmul($pv, $rate, 2) * $movie_commission_rate_config['commission_directthrust_rate'], 2);
  653. $this->distributionAnnuityEarnings(
  654. $spread_data['uid'], $yj_amount_90, $orderinfo['order_id'], 3, $orderinfo['order_sn'], '电影票-推广者获得直推佣金' . $yj_amount_90,
  655. 0, 0
  656. );
  657. // 复购金-推广者的佣金抽10%到个人复购金
  658. $fgj_amount_10 = round(bcmul($pv, $rate, 2) * $movie_commission_rate_config['repurchase_directthrust_rate'], 2);
  659. $storeOrderRepository->fugou_user_log($spread_data['uid'], $fgj_amount_10, $orderinfo['order_id'], 1);
  660. // 积分
  661. $score = bcmul($pv, $rate, 2);
  662. $storeOrderRepository->score_log($spread_data['uid'], $score, $orderinfo['order_id'], 13, "电影票-推广者获得赠送积分");
  663. // 业务员及以上身份
  664. if ($spread_data['user_group'] > 1) {
  665. // 贡献值
  666. $storeOrderRepository->con_log($spread_data['uid'], $pv, $orderinfo['order_id'], 13, "电影票-推广者获得赠送贡献值");
  667. }
  668. // 普通用户推广人
  669. if ($spread_data['user_group'] == 1) {
  670. // 贡献值
  671. $gongxianValue = bcmul($pv, $movie_commission_rate_config['contributionvalue_spread_user_rate'], 2);
  672. $storeOrderRepository->con_log($spread_data['uid'], $gongxianValue, $orderinfo['order_id'], 13, "电影票-推广者用户身份获得赠送贡献值");
  673. }
  674. }
  675. // 锁客收益
  676. if ($user['mer_id'] != 0) {
  677. $merchant_uid = Db::name('merchant')->where('mer_id', $user['mer_id'])->value('uid');
  678. $merchant_user = Db::name('user')->where('uid', $merchant_uid)->find();
  679. if (!empty($merchant_user)) {
  680. // 锁客佣金
  681. $yj_amount_90 = round(bcmul($pv, $movie_commission_rate_config['commission_lockpassenger_merid_rate'], 2) * $movie_commission_rate_config['commission_lockpassenger_rate'], 2);
  682. $this->distributionAnnuityEarnings(
  683. $merchant_uid, $yj_amount_90, $orderinfo['order_id'], 3, $orderinfo['order_sn'], '电影票-商户锁定用户获得佣金' . $yj_amount_90,
  684. $user['mer_id'], 0
  685. );
  686. // 锁客复购金
  687. $fgj_amount_10 = round(bcmul($pv, $movie_commission_rate_config['repurchase_lockpassenger_merid_rate'], 2) * $movie_commission_rate_config['repurchase_lockpassenger_rate'], 2);
  688. $storeOrderRepository->fugou_user_log($merchant_uid, $fgj_amount_10, $orderinfo['order_id'], 1);
  689. }
  690. }
  691. }
  692. /**
  693. * 直推奖、养老金
  694. *
  695. * @param $uid
  696. * @param $num
  697. * @param $order_id
  698. * @param $comm_order_type
  699. * @param $order_sn
  700. * @param $mark
  701. * @param $merid
  702. * @param $ds
  703. * @return void
  704. */
  705. private function distributionAnnuityEarnings($uid, $num, $order_id, $comm_order_type, $order_sn, $mark, $merid, $ds = 0)
  706. {
  707. $name = '';
  708. if ($comm_order_type == 3) {
  709. $name = '直推奖';
  710. } else if ($comm_order_type == 5) {
  711. $name = '养老金';
  712. }
  713. $bill = [
  714. 'uid' => $uid,
  715. 'link_id' => $order_id,//关联订单id
  716. 'pm' => 1,//0:支出,1:获得
  717. 'title' => $name,//账单标题
  718. 'category' => 'now_money',//明细种类
  719. 'type' => 'commission',//明细类型
  720. 'number' => $num,//明细数字
  721. 'balance' => 0,//剩余
  722. 'mark' => $mark,//备注
  723. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  724. 'status' => $ds,//0待确定,1有效,-1无效
  725. 'commission_type' => $comm_order_type,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  726. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  727. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  728. 'order_sn' => $order_sn,//订单号
  729. 'tripartite' => 0,//
  730. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  731. 'mer_id' => $merid,//商户id
  732. 'source' => 0,//1线下 0线上
  733. 'order_type' => 1,//1自营 2 第三方 订单类型
  734. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  735. 'gzc' => 0,//养老金是否已进入公证处log表0:未进入。1:已进入
  736. 'take_time' => '',//结算时间
  737. 'district_id' => '',//
  738. 'street_id' => '',//
  739. 'month' => '',//月份
  740. ];
  741. Db::name('user_bill')->insert($bill);
  742. }
  743. /**
  744. * 订单列表
  745. *
  746. * @param $userinfo
  747. * @param $page
  748. * @param $limit
  749. * @param $type
  750. * @return array
  751. * @throws \think\db\exception\DataNotFoundException
  752. * @throws \think\db\exception\DbException
  753. * @throws \think\db\exception\ModelNotFoundException
  754. */
  755. public function getOrderList($userinfo, $page, $limit, $type = -1)
  756. {
  757. $where['uid'] = $userinfo['uid'];
  758. switch ($type) {
  759. case 0:
  760. $where['status'] = [0];
  761. break;
  762. case 1:
  763. case 4:
  764. $where['status'] = [1, 4];
  765. break;
  766. case 2:
  767. case 3:
  768. $where['status'] = [2, 3];
  769. break;
  770. default:
  771. break;
  772. }
  773. $time = time();
  774. $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';
  775. $movie_order = $this->dao->search($where)
  776. ->when($page && $limit, function ($query) use ($page, $limit) {
  777. $query->page($page, $limit);
  778. })->order('order_id desc')->field([$field])->select()->toArray();
  779. foreach ($movie_order as $key => $value) {
  780. $movie_order[$key]['sched_seat'] = json_decode($value['sched_seat'], true);
  781. $movie_order[$key]['cover_img'] = Db::name('movie_film')->where('film_sign', $value['film_sign'])->value('cover_img');
  782. $movie_order[$key]['time_remaining']= 0;
  783. // 0 待支付 1 已支付 2已超时 3已取消 4已成功
  784. $create_time = strtotime($value['create_time']) + self::tenMinute;
  785. if ($value['status'] == 0 && ($create_time < $time)) {
  786. $movie_order[$key]['status'] = 0;
  787. $movie_order[$key]['state'] = '待付款';
  788. $movie_order[$key]['time_remaining'] = $time - strtotime($value['create_time']);
  789. } elseif ($value['status'] == 1) {
  790. $movie_order[$key]['status'] = 1;
  791. $movie_order[$key]['state'] = '已支付';
  792. } else {
  793. $movie_order[$key]['status'] = 3;
  794. $movie_order[$key]['state'] = '支付超时,已取消';
  795. }
  796. }
  797. $count = $this->dao->search($where)->count();
  798. return compact('movie_order', 'count');
  799. }
  800. /**
  801. * 所有未支付订单列表
  802. *
  803. * @param $status
  804. * @param $paid
  805. * @return array
  806. * @throws \think\db\exception\DataNotFoundException
  807. * @throws \think\db\exception\DbException
  808. * @throws \think\db\exception\ModelNotFoundException
  809. */
  810. public function getOrderListAll($status = [], $paid = 0)
  811. {
  812. $where = ['status' => $status, 'paid' => $paid];
  813. $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';
  814. // foreach ($movie_order as $key => $value) {
  815. // $movie_order[$key]['sched_seat'] = json_decode($value['sched_seat'], true);
  816. // $movie_order[$key]['cover_img'] = Db::name('movie_film')->where('film_sign', $value['film_sign'])->value('cover_img');
  817. // }
  818. return $this->dao->search($where)->order('create_time asc')->field([$field])->select()->toArray();
  819. }
  820. }