MovieRepository.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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\store\order\StoreOrderRepository;
  12. use app\traits\HuiPay;
  13. use crmeb\services\ApiResponseService;
  14. use think\exception\ValidateException;
  15. use think\facade\Db;
  16. use think\facade\Log;
  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 $relation_id
  104. * @param $sched_sign
  105. * @return mixed|string
  106. */
  107. public function getSchedSeat($relation_id = '', $sched_sign = '')
  108. {
  109. $result = DouHuoMallFilmApiRequest::getSchedSeat($relation_id, $sched_sign);
  110. $seat = [];
  111. $schedSeatList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) ? $result['data'] : [];
  112. foreach ($schedSeatList as $key => $value) {
  113. $tmpprice = [];
  114. foreach ($value['price'] as $pKey => $pValue) {
  115. $tmpprice[$pKey] = ['channel_type' => $pKey, 'price' => $pValue];
  116. }
  117. $channel_price = [];
  118. if (isset($tmpprice['P2'])) {
  119. $channel_price = $tmpprice['P2'];
  120. } elseif (isset($tmpprice['P1'])) {
  121. $channel_price = $tmpprice['P1'];
  122. } elseif (isset($tmpprice['P0'])) {
  123. $channel_price = $tmpprice['P0'];
  124. }
  125. unset($schedSeatList[$key]['price']);
  126. $pcYanglaojinGongxian = self::getPvYanglaojinScore($channel_price['price']);
  127. $schedSeatList[$key]['channel_sn'] = base64_encode(json_encode($channel_price, JSON_FORCE_OBJECT));
  128. $schedSeatList[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  129. $schedSeatList[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  130. $schedSeatList[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  131. $schedSeatList[$key]['score'] = $pcYanglaojinGongxian['score'];
  132. $schedSeatList[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  133. $schedSeatList[$key]['relation_id'] = $relation_id; // 场次ID
  134. $schedSeatList[$key]['sched_sign'] = $sched_sign; // 场次编码
  135. $seat[$value['rowNo']][] = $schedSeatList[$key];
  136. }
  137. if ($seat) ksort($seat);
  138. return array_values($seat);
  139. }
  140. /**
  141. * 锁座订单
  142. *
  143. * @param $userinfo
  144. * @param $params
  145. * @return mixed
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\DbException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. */
  150. public function submitOrder($userinfo, $params)
  151. {
  152. /** @var StoreOrderRepository $storeOrder */
  153. $storeOrder = app()->make(StoreOrderRepository::class);
  154. $order_sn = $storeOrder->getNewOrderId() . '0';
  155. /** @var CinemaSchedRepository $cinemaSchedRepository */
  156. $cinemaSchedRepository = app()->make(CinemaSchedRepository::class);
  157. $sched = $cinemaSchedRepository->getCinemaSchedInfo($params['relation_id'], $params['sched_sign']);
  158. if (empty($sched)) throw new ValidateException('观影场次不存在');
  159. $channel_sn = json_decode(base64_decode($params['channel_sn']), true);
  160. if (empty($channel_sn) || !isset($channel_sn['channel_type']) || empty($channel_sn['channel_type']) || !isset($channel_sn['price'])
  161. || empty($channel_sn['price']) || json_last_error()) {
  162. throw new ValidateException('请选择观影场次');
  163. }
  164. $sched_seat = json_decode($params['sched_seat'], true);
  165. if (json_last_error()) throw new ValidateException('锁座下单失败');
  166. $order_price = bcmul((string)$channel_sn['price'], (string)count($sched_seat), 2);
  167. $submitOrder = DouHuoMallFilmApiRequest::submitOrder($userinfo->phone, $channel_sn['channel_type'], $params['sched_sign'], array_column($sched_seat, 'seat_sign'), $order_sn, $order_price);
  168. if (!empty($submitOrder) && isset($submitOrder['code']) && ($submitOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE) && isset($submitOrder['data']['plat_order'])) {
  169. /** @var CinemaRepository $cinemaRepository */
  170. $cinemaRepository = app()->make(CinemaRepository::class);
  171. $cinema = $cinemaRepository->getCinemaInfo($sched['cinema_sign']);
  172. $_order = [
  173. 'uid' => $userinfo->uid,
  174. 'mobile' => $userinfo->phone,
  175. 'order_sn' => $order_sn,
  176. 'plat_order' => $submitOrder['data']['plat_order'],
  177. 'price' => $order_price,
  178. 'number' => count($sched_seat),
  179. 'order_price' => bcadd((string)$order_price, bcmul((string)$order_price, (string)self::premium_ratio, 2), 2),
  180. 'channel_type' => $channel_sn['channel_type'],
  181. 'city_sign' => $cinema['city_sign'],
  182. 'city_name' => $cinema['city_name'],
  183. 'area_code' => $cinema['area_sign'],
  184. 'area_name' => $cinema['area_name'],
  185. 'cinema_sign' => $cinema['cinema_sign'],
  186. 'cinema_name' => $cinema['cinema_name'],
  187. 'address' => $cinema['address'],
  188. 'longitude' => $cinema['longitude'],
  189. 'latitude' => $cinema['latitude'],
  190. 'hall_sign' => $sched['hall_sign'],
  191. 'hall_name' => $sched['hall_name'],
  192. 'relation_id' => $sched['relation_id'],
  193. 'sched_sign' => $sched['sched_sign'],
  194. 'film_sign' => $sched['film_sign'],
  195. 'film_name' => $sched['film_name'],
  196. 'show_date' => $sched['show_date'],
  197. 'show_time' => $sched['show_time'],
  198. 'sched_seat' => $params['sched_seat'], // 场次座位
  199. 'create_time' => time()
  200. ];
  201. $order_sn = Db::transaction(function () use ($userinfo, $_order) {
  202. $_order = $this->dao->create($_order);
  203. if ($_order) return $_order['order_sn'];
  204. throw new ValidateException('锁座下单失败');
  205. });
  206. return $order_sn;
  207. } else {
  208. throw new ValidateException('锁座下单失败');
  209. }
  210. }
  211. /**
  212. * @param $userinfo
  213. * @param $order_sn
  214. * @param $pay_type
  215. * @return mixed
  216. * @throws \think\db\exception\DataNotFoundException
  217. * @throws \think\db\exception\DbException
  218. * @throws \think\db\exception\ModelNotFoundException
  219. */
  220. public function confirmOrder($userinfo, $order_sn, $pay_type)
  221. {
  222. $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn, 'uid' => $userinfo->uid])->find();
  223. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  224. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 2) throw new ValidateException('订单已超时');
  225. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 3) throw new ValidateException('订单已取消');
  226. if ($orderinfo['paid'] == 1) throw new ValidateException('订单已支付');
  227. $res = $this->pay($pay_type, $userinfo->wechat_user_id, $orderinfo, $orderinfo['film_name'], env('APP_URL') . "/api/hf_notify/movie", 'delay');
  228. Db::name('log')->insert(['data' => '汇付电影票订单支付--- 返回参数' . json_encode($res)]);
  229. if (isset($res['id'])) {
  230. $type = 0;
  231. if ($pay_type == 'wx') $type = 2;
  232. if (strtolower($pay_type) == 'ali') $type = 1;
  233. $orderinfo->pay_type = $type;
  234. $orderinfo->update_time = time();
  235. $orderinfo->save();
  236. return app('json')->success($res);
  237. }
  238. throw new ValidateException('支付失败');
  239. }
  240. /**
  241. * 支付
  242. *
  243. * @param $payType
  244. * @param $openid
  245. * @param $_order
  246. * @param $title
  247. * @param $notify_url
  248. * @param $pay_mode
  249. * @param $appid
  250. * @return array|mixed
  251. */
  252. private function pay($payType, $openid, $_order, $title, $notify_url, $pay_mode = null, $appid = '')
  253. {
  254. switch ($payType) {
  255. case 'wx':
  256. $pay_wx = systemConfig('pay_wx');
  257. if ($pay_wx == 2) {
  258. $appid = env('WECHAT_MP.APPID');
  259. $new_secret = env('WECHAT_MP.SECRET');
  260. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  261. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  262. } elseif ($pay_wx == 3) {
  263. $appid = env('WECHAT_TDYL.APPID');
  264. $new_secret = env('WECHAT_TDYL.SECRET');
  265. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  266. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  267. }
  268. $type = '2';
  269. $params = [
  270. 'order_no' => $_order['order_sn'],
  271. 'app_id' => $app_id,
  272. 'pay_channel' => 'wx_lite',
  273. 'pay_amt' => (string)$_order['order_price'],
  274. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  275. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  276. 'device_info' => [
  277. 'device_ip' => app('request')->ip()
  278. ],
  279. 'expend' => json_encode(['open_id' => $openid, 'wx_app_id' => $appid]),
  280. 'fee_mode' => 'I',
  281. 'notify_url' => $notify_url,
  282. 'pay_mode' => $pay_mode
  283. ];
  284. break;
  285. case 'ALI':
  286. case 'ali':
  287. $type = '1';
  288. // 天地博爱科技通道
  289. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  290. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  291. $params = [
  292. 'order_no' => $_order['order_sn'],
  293. 'app_id' => $app_id,
  294. 'pay_channel' => 'alipay_qr',
  295. 'pay_amt' => (string)$_order['order_price'],
  296. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  297. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  298. 'device_info' => [
  299. 'device_ip' => app('request')->ip()
  300. ],
  301. 'fee_mode' => 'I',
  302. 'notify_url' => $notify_url
  303. ];
  304. break;
  305. default:
  306. throw new ValidateException('请选择支付方式');
  307. break;
  308. }
  309. return $this->Payment_create_traits_a($params, $type, $key);
  310. }
  311. /**
  312. * @param $userinfo
  313. * @param $order_sn
  314. * @return mixed|string
  315. */
  316. public function getOrderInfo($userinfo, $order_sn)
  317. {
  318. $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn, 'uid' => $userinfo->uid])->find();
  319. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  320. $result = DouHuoMallFilmApiRequest::getOrderInfo($order_sn);
  321. if (!empty($result) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) return $result['data'];
  322. throw new ValidateException('订单不存在');
  323. }
  324. /**
  325. * @return array
  326. * @throws \think\db\exception\DataNotFoundException
  327. * @throws \think\db\exception\DbException
  328. * @throws \think\db\exception\ModelNotFoundException
  329. */
  330. public function getLocalMovieCity()
  331. {
  332. $city_name = [];
  333. $city = Db::name('movie_city')->field('city_sign,city_name,initial')->where('is_show', 1)->order('id asc')->select();
  334. foreach ($city as $key => $value) {
  335. $city_name[$value['initial']][] = ['city_sign' => $value['city_sign'], 'city_name' => $value['city_name']];
  336. }
  337. $initial = !empty($city) ? array_values(array_column($city->toArray(), 'initial', 'initial')) : [];
  338. return compact('city_name', 'initial');
  339. }
  340. /**
  341. * 获取pv、养老金、售价、积分
  342. *
  343. * @param $cost_price
  344. * @return array
  345. */
  346. public static function getPvYanglaojinScore($cost_price)
  347. {
  348. $premium = round($cost_price * self::premium_ratio, 2);
  349. $cost_price = bcadd((string)$cost_price, (string)$premium, 2);
  350. $pv = $gongxian = $score = bcmul((string)$premium, self::pv_ratio, 2);
  351. $yanglaojin = bcmul((string)$premium, self::yanglaojin_ratio, 2);
  352. return compact('premium', 'cost_price', 'pv', 'yanglaojin', 'gongxian', 'score');
  353. }
  354. /**
  355. * 计算佣金分佣奖励
  356. *
  357. * @param $pay_type
  358. * @param $order_sn
  359. * @param $post_data
  360. * @return void
  361. * @throws \think\db\exception\DataNotFoundException
  362. * @throws \think\db\exception\DbException
  363. * @throws \think\db\exception\ModelNotFoundException
  364. */
  365. public function updateMovieOrderAndcalculationCommissionDividendAward($pay_type, $order_sn, $post_data)
  366. {
  367. $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn])->find();
  368. if ($orderinfo) throw new ValidateException('订单不存在');
  369. Db::startTrans();
  370. $orderinfo->paid = 1;
  371. $orderinfo->pay_time = time();
  372. $orderinfo->pay_type = $pay_type;
  373. $orderinfo->status = 1;
  374. $orderinfo->channel_trade_no = $post_data['id'];
  375. $orderinfo->pay_open_id = $post_data['expend']['open_id'];
  376. $orderinfo->update_time = time();
  377. // 支付方式 1支付宝 2微信
  378. if ($pay_type == 1) {
  379. $orderinfo->alipay_no = $post_data['out_trans_id'];
  380. }
  381. if ($pay_type == 2) {
  382. $orderinfo->weixin_no = $post_data['out_trans_id'];
  383. }
  384. $res = $orderinfo->save();
  385. if ($res) {
  386. $confirmOrder = DouHuoMallFilmApiRequest::confirmOrder($orderinfo['order_sn'], $orderinfo['price']);
  387. if (!empty($confirmOrder) && isset($confirmOrder['code']) && ($confirmOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
  388. // 提交事务
  389. Db::commit();
  390. } else {
  391. Db::name('log')->insert(['data' => '电影票订单支付回调向【微唯宝】--- 告知电影订单已支付失败,订单编号:' . $order_sn]);
  392. // 回滚事务
  393. Db::rollback();
  394. }
  395. } else {
  396. Db::name('log')->insert(['data' => '汇付电影票订单支付回调--- 更新电影订单表失败,订单编号:' . $order_sn]);
  397. // 回滚事务
  398. Db::rollback();
  399. }
  400. }
  401. }