MovieRepository.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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\repositories\BaseRepository;
  10. use app\common\repositories\store\order\StoreOrderRepository;
  11. use app\traits\HuiPay;
  12. use crmeb\services\ApiResponseService;
  13. use think\exception\ValidateException;
  14. use think\facade\Db;
  15. use think\facade\Log;
  16. /**
  17. * Class MovieRepository
  18. * @package app\common\repositories\movie
  19. * @author xaboy
  20. * @mixin MovieOrderDao
  21. */
  22. class MovieRepository extends BaseRepository
  23. {
  24. use HuiPay;
  25. protected $dao;
  26. const premium_ratio = 0.2;//溢价比例
  27. const yanglaojin_ratio = 0.3;
  28. const pv_ratio = 0.7;
  29. /**
  30. * ProductRepository constructor.
  31. * @param MovieOrderDao $dao
  32. */
  33. public function __construct(MovieOrderDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * @param $page
  39. * @param $limit
  40. * @param $initial
  41. * @param $city_name
  42. * @return false|int|mixed
  43. */
  44. public function getMovieCity($page = 1, $limit = 10, $initial = '', $city_name = '')
  45. {
  46. return DouHuoMallFilmApiRequest::getMovieCity($page, $limit, $initial = '', $city_name);
  47. }
  48. /**
  49. * @param $city_sign
  50. * @return false|int|mixed
  51. */
  52. public function getMovieArea($city_sign = '')
  53. {
  54. return DouHuoMallFilmApiRequest::getMovieArea($city_sign);
  55. }
  56. /**
  57. * @param $page
  58. * @param $limit
  59. * @param $city_sign
  60. * @param $area_sign
  61. * @return false|int|mixed
  62. */
  63. public function getCinema($page = 1, $limit = 10, $city_sign = '', $area_sign = '')
  64. {
  65. return DouHuoMallFilmApiRequest::getCinema($page, $limit, $city_sign, $area_sign);
  66. }
  67. /**
  68. * @param $page
  69. * @param $limit
  70. * @param $cinema_sign
  71. * @param $film_sign
  72. * @return false|int|mixed
  73. */
  74. public function getFilm($page = 1, $limit = 10, $cinema_sign = '', $film_sign = '')
  75. {
  76. return DouHuoMallFilmApiRequest::getFilm($page, $limit, $cinema_sign, $film_sign);
  77. }
  78. /**
  79. * @param $film_sign
  80. * @param $cinema_sign
  81. * @return mixed|string
  82. */
  83. public function getCinemaSched($film_sign = '', $cinema_sign = '')
  84. {
  85. $result = DouHuoMallFilmApiRequest::getCinemaSched($film_sign, $cinema_sign);
  86. $cinemaSchedList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
  87. ? $result['data']['list'] : [];
  88. foreach ($cinemaSchedList as $dateDay => &$fileSchedValue) {
  89. foreach ($fileSchedValue as $key => $value) {
  90. $pcYanglaojinGongxian = static::getPvYanglaojinScore($value['cost_price']);
  91. $fileSchedValue[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  92. $fileSchedValue[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  93. $fileSchedValue[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  94. $fileSchedValue[$key]['score'] = $pcYanglaojinGongxian['score'];
  95. $fileSchedValue[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  96. // unset($fileSchedValue[$key]['channel_price']);
  97. }
  98. }
  99. return $cinemaSchedList;
  100. }
  101. /**
  102. * @param $relation_id
  103. * @param $sched_sign
  104. * @return mixed|string
  105. */
  106. public function getSchedSeat($relation_id = '', $sched_sign = '')
  107. {
  108. $result = DouHuoMallFilmApiRequest::getSchedSeat($relation_id, $sched_sign);
  109. $schedSeatList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
  110. ? $result['data']['list'] : [];
  111. foreach ($schedSeatList as $key => $value) {
  112. $tmpprice = json_decode($value['price'], true);
  113. if (isset($tmpprice['P2'])) {
  114. $price = $tmpprice['P2'];
  115. } elseif (isset($tmpprice['P1'])) {
  116. $price = $tmpprice['P1'];
  117. } elseif (isset($tmpprice['P0'])) {
  118. $price = $tmpprice['P0'];
  119. } else {
  120. $price = 0.00;
  121. }
  122. unset($schedSeatList[$key]['price']);
  123. $pcYanglaojinGongxian = self::getPvYanglaojinScore($price);
  124. $schedSeatList[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  125. $schedSeatList[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  126. $schedSeatList[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  127. $schedSeatList[$key]['score'] = $pcYanglaojinGongxian['score'];
  128. $schedSeatList[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  129. $schedSeatList[$key]['relation_id'] = $relation_id; // 场次ID
  130. $schedSeatList[$key]['sched_sign'] = $sched_sign; // 场次编码
  131. }
  132. return $schedSeatList;
  133. }
  134. /**
  135. * 锁座订单
  136. *
  137. * @param $userinfo
  138. * @param $params
  139. * @return mixed
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function submitOrder($userinfo, $params)
  145. {
  146. /** @var StoreOrderRepository $storeOrder */
  147. $storeOrder = app()->make(StoreOrderRepository::class);
  148. $order_sn = $storeOrder->getNewOrderId() . '0';
  149. /** @var CinemaSchedRepository $cinemaSchedRepository */
  150. $cinemaSchedRepository = app()->make(CinemaSchedRepository::class);
  151. $sched = $cinemaSchedRepository->getCinemaSchedInfo($params['relation_id'], $params['sched_sign']);
  152. if (empty($sched)) throw new ValidateException('观影场次不存在');
  153. $seat_sign = json_decode($params['seat_sign'], true);
  154. $order_price = round($sched['price'] * count($seat_sign), 2);
  155. $submitOrder = DouHuoMallFilmApiRequest::submitOrder($userinfo->phone, $params['channel_type'], $params['sched_sign'], $seat_sign, $order_sn, $order_price);
  156. if (!empty($submitOrder) && isset($submitOrder['code']) && ($submitOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE) && isset($submitOrder['data']['plat_order'])) {
  157. /** @var CinemaRepository $cinemaRepository */
  158. $cinemaRepository = app()->make(CinemaRepository::class);
  159. $cinema = $cinemaRepository->getCinemaInfo($sched['cinema_sign']);
  160. $_order = [
  161. 'uid' => $userinfo->uid,
  162. 'mobile' => $userinfo->phone,
  163. 'order_sn' => $order_sn,
  164. 'plat_order' => $submitOrder['data']['plat_order'],
  165. 'price' => $sched['price'],
  166. 'number' => count($seat_sign),
  167. 'order_price' => $order_price,
  168. 'channel_type' => $params['channel_type'],
  169. 'city_sign' => $cinema['city_sign'],
  170. 'city_name' => $cinema['city_name'],
  171. 'area_code' => $cinema['area_sign'],
  172. 'area_name' => $cinema['area_name'],
  173. 'cinema_sign' => $cinema['cinema_sign'],
  174. 'cinema_name' => $cinema['cinema_name'],
  175. 'address' => $cinema['address'],
  176. 'longitude' => $cinema['longitude'],
  177. 'latitude' => $cinema['latitude'],
  178. 'hall_sign' => $sched['hall_sign'],
  179. 'hall_name' => $sched['hall_name'],
  180. 'relation_id' => $sched['relation_id'],
  181. 'sched_sign' => $sched['sched_sign'],
  182. 'film_sign' => $sched['film_sign'],
  183. 'film_name' => $sched['film_name'],
  184. 'show_date' => $sched['show_date'],
  185. 'show_time' => $sched['show_time'],
  186. 'sched_seat' => $sched['sched_seat'],
  187. 'create_time' => time()
  188. ];
  189. $order_sn = Db::transaction(function () use ($userinfo, $_order) {
  190. $_order = $this->dao->create($_order);
  191. if ($_order) return $_order['order_sn'];
  192. throw new ValidateException('锁座下单失败');
  193. });
  194. return $order_sn;
  195. } else {
  196. throw new ValidateException('锁座下单失败');
  197. }
  198. }
  199. /**
  200. * @param $userinfo
  201. * @param $order_sn
  202. * @return mixed|string
  203. */
  204. public function confirmOrder($userinfo, $order_sn)
  205. {
  206. $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn, 'uid' => $userinfo->uid])->find();
  207. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  208. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 2) throw new ValidateException('订单已超时');
  209. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 3) throw new ValidateException('订单已取消');
  210. if ($orderinfo['paid'] == 1) throw new ValidateException('订单已支付');
  211. // return $this->pay(
  212. // $_order['pay_type'], $userinfo->wechat_user_id, $_order, $_order['film_name'], env('APP_URL') . "/api/hf_notify/movie",
  213. // 'delay');
  214. return DouHuoMallFilmApiRequest::confirmOrder($orderinfo['order_sn'], $orderinfo['order_price']);
  215. }
  216. /**
  217. * 支付
  218. *
  219. * @param $payType
  220. * @param $openid
  221. * @param $_order
  222. * @param $title
  223. * @param $notify_url
  224. * @param $pay_mode
  225. * @param $appid
  226. * @return array|mixed
  227. */
  228. private function pay($payType, $openid, $_order, $title, $notify_url, $pay_mode = null, $appid = '')
  229. {
  230. switch ($payType) {
  231. case 'wx':
  232. $pay_wx = systemConfig('pay_wx');
  233. if ($pay_wx == 2) {
  234. $appid = env('WECHAT_MP.APPID');
  235. $new_secret = env('WECHAT_MP.SECRET');
  236. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  237. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  238. } elseif ($pay_wx == 3) {
  239. $appid = env('WECHAT_TDYL.APPID');
  240. $new_secret = env('WECHAT_TDYL.SECRET');
  241. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  242. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  243. }
  244. $type = '2';
  245. $params = [
  246. 'order_no' => $_order['order_sn'],
  247. 'app_id' => $app_id,
  248. 'pay_channel' => 'wx_lite',
  249. 'pay_amt' => (string)$_order['order_price'],
  250. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  251. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  252. 'device_info' => [
  253. 'device_ip' => app('request')->ip()
  254. ],
  255. 'expend' => json_encode(['open_id' => $openid, 'wx_app_id' => $appid]),
  256. 'fee_mode' => 'I',
  257. 'notify_url' => $notify_url,
  258. 'pay_mode' => $pay_mode
  259. ];
  260. break;
  261. case 'ALI':
  262. $type = '1';
  263. // 天地博爱科技通道
  264. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  265. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  266. $params = [
  267. 'order_no' => $_order['order_sn'],
  268. 'app_id' => $app_id,
  269. 'pay_channel' => 'alipay_qr',
  270. 'pay_amt' => (string)$_order['order_price'],
  271. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  272. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  273. 'device_info' => [
  274. 'device_ip' => app('request')->ip()
  275. ],
  276. 'fee_mode' => 'I',
  277. 'notify_url' => $notify_url
  278. ];
  279. break;
  280. default:
  281. throw new ValidateException('请选择支付方式');
  282. break;
  283. }
  284. return $this->Payment_create_traits_a($params, $type, $key);
  285. }
  286. /**
  287. * @param $userinfo
  288. * @param $order_sn
  289. * @return mixed|string
  290. */
  291. public function getOrderInfo($userinfo, $order_sn)
  292. {
  293. $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn, 'uid' => $userinfo->uid])->find();
  294. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  295. $result = DouHuoMallFilmApiRequest::getOrderInfo($order_sn);
  296. if (!empty($result) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) return $result['data'];
  297. throw new ValidateException('订单不存在');
  298. }
  299. /**
  300. * @return array
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\DbException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. */
  305. public function getLocalMovieCity()
  306. {
  307. $city_name = [];
  308. $city = Db::name('movie_city')->field('city_sign,city_name,initial')->where('is_show', 1)->order('id asc')->select();
  309. foreach ($city as $key => $value) {
  310. $city_name[$value['initial']][] = ['city_sign' => $value['city_sign'], 'city_name' => $value['city_name']];
  311. }
  312. $initial = !empty($city) ? array_values(array_column($city->toArray(), 'initial', 'initial')) : [];
  313. return compact('city_name', 'initial');
  314. }
  315. /**
  316. * 获取pv、养老金、售价、积分
  317. *
  318. * @param $cost_price
  319. * @return array
  320. */
  321. public static function getPvYanglaojinScore($cost_price)
  322. {
  323. $premium = round($cost_price * self::premium_ratio, 2);
  324. $cost_price = bcadd((string)$cost_price, (string)$premium, 2);
  325. $pv = $gongxian = $score = bcmul((string)$premium, self::pv_ratio, 2);
  326. $yanglaojin = bcmul((string)$premium, self::yanglaojin_ratio, 2);
  327. return compact('premium', 'cost_price', 'pv', 'yanglaojin', 'gongxian', 'score');
  328. }
  329. }