MovieRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. private $premium_ratio = 0.2;//溢价比例
  27. private $yanglaojin_ratio = 0.3;
  28. private $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. $premium = round($value['cost_price'] * $this->premium_ratio, 2);
  91. $fileSchedValue[$key]['cost_price'] = bcadd((string)$value['cost_price'], (string)$premium, 2);
  92. $pv = bcmul((string)$premium, $this->pv_ratio, 2);
  93. $fileSchedValue[$key]['yanglaojin'] = bcmul((string)$premium, $this->yanglaojin_ratio, 2);
  94. $fileSchedValue[$key]['pv'] = $fileSchedValue[$key]['score'] = $fileSchedValue[$key]['gongxian'] = $pv;
  95. unset($fileSchedValue[$key]['channel_price']);
  96. }
  97. }
  98. return $cinemaSchedList;
  99. }
  100. /**
  101. * @param $relation_id
  102. * @param $sched_sign
  103. * @return mixed|string
  104. */
  105. public function getSchedSeat($relation_id = '', $sched_sign = '')
  106. {
  107. return DouHuoMallFilmApiRequest::getSchedSeat($relation_id, $sched_sign);
  108. }
  109. /**
  110. * 锁座订单
  111. * @param $sched_sign
  112. * @param $seat_sign
  113. * @param $channel_type
  114. * @param $third_order
  115. * @param $order_price
  116. * @param $phone
  117. * @return mixed|string
  118. */
  119. public function submitOrder($userinfo, $params)
  120. {
  121. /** @var StoreOrderRepository $storeOrder */
  122. $storeOrder = app()->make(StoreOrderRepository::class);
  123. $order_sn = $storeOrder->getNewOrderId() . '0';
  124. $order_price = round($params['number'] * $params['price'], 2);
  125. $submitOrder = DouHuoMallFilmApiRequest::submitOrder($userinfo->phone, $params['channel_type'], $params['sched_sign'], $order_sn, $order_price);
  126. if (!empty($submitOrder) && isset($submitOrder['code']) && ($submitOrder['code'] == 200) && isset($submitOrder['data']['plat_order'])) {
  127. $_order = [
  128. 'uid' => $userinfo->uid,
  129. 'mobile' => $userinfo->phone,
  130. 'order_sn' => $order_sn,
  131. 'plat_order' => $submitOrder['data']['plat_order'],
  132. 'price' => $params['price'],
  133. 'number' => $params['number'],
  134. 'order_price' => $order_price,
  135. 'channel_type' => $params['channel_type'],
  136. 'pay_type' => $params['pay_type'],
  137. 'city_sign' => $params['city_sign'],
  138. 'city_name' => $params['city_name'],
  139. 'area_code' => $params['area_code'],
  140. 'area_name' => $params['area_name'],
  141. 'cinema_sign' => $params['cinema_sign'],
  142. 'cinema_name' => $params['cinema_name'],
  143. 'address' => $params['address'],
  144. 'longitude' => $params['longitude'],
  145. 'latitude' => $params['latitude'],
  146. 'hall_sign' => $params['hall_sign'],
  147. 'hall_name' => $params['hall_name'],
  148. 'relation_id' => $params['relation_id'],
  149. 'sched_sign' => $params['sched_sign'],
  150. 'film_sign' => $params['film_sign'],
  151. 'film_name' => $params['film_name'],
  152. 'show_date' => $params['show_date'],
  153. 'show_time' => $params['show_time'],
  154. 'sched_seat' => $params['sched_seat'],
  155. 'create_time' => time()
  156. ];
  157. $group = Db::transaction(function () use ($userinfo, $_order) {
  158. $_order = $this->dao->create($_order);
  159. if ($_order) return ['code' => ApiResponseService::DEFAULT_SUCCESS_CODE, 'msg' => '下单成功', 'data' => ['order_sn' => $_order['order_sn']]];
  160. throw new ValidateException('下单失败');
  161. });
  162. return $group;
  163. } else {
  164. return $submitOrder;
  165. }
  166. }
  167. /**
  168. * @param $userinfo
  169. * @param $order_sn
  170. * @return mixed|string
  171. */
  172. public function confirmOrder($userinfo, $order_sn)
  173. {
  174. $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn, 'uid' => $userinfo->uid])->find();
  175. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  176. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 2) throw new ValidateException('订单已超时');
  177. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 3) throw new ValidateException('订单已取消');
  178. if ($orderinfo['paid'] == 1) throw new ValidateException('订单已支付');
  179. // return $this->pay(
  180. // $_order['pay_type'], $userinfo->wechat_user_id, $_order, $_order['film_name'], env('APP_URL') . "/api/hf_notify/movie",
  181. // 'delay');
  182. return DouHuoMallFilmApiRequest::confirmOrder($third_order, $order_price);
  183. }
  184. /**
  185. * 支付
  186. *
  187. * @param $payType
  188. * @param $openid
  189. * @param $_order
  190. * @param $title
  191. * @param $notify_url
  192. * @param $pay_mode
  193. * @param $appid
  194. * @return array|mixed
  195. */
  196. private function pay($payType, $openid, $_order, $title, $notify_url, $pay_mode = null, $appid = '')
  197. {
  198. switch ($payType) {
  199. case 'wx':
  200. $pay_wx = systemConfig('pay_wx');
  201. if ($pay_wx == 2) {
  202. $appid = env('WECHAT_MP.APPID');
  203. $new_secret = env('WECHAT_MP.SECRET');
  204. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  205. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  206. } elseif ($pay_wx == 3) {
  207. $appid = env('WECHAT_TDYL.APPID');
  208. $new_secret = env('WECHAT_TDYL.SECRET');
  209. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  210. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  211. }
  212. $type = '2';
  213. $params = [
  214. 'order_no' => $_order['order_sn'],
  215. 'app_id' => $app_id,
  216. 'pay_channel' => 'wx_lite',
  217. 'pay_amt' => (string)$_order['order_price'],
  218. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  219. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  220. 'device_info' => [
  221. 'device_ip' => app('request')->ip()
  222. ],
  223. 'expend' => json_encode(['open_id' => $openid, 'wx_app_id' => $appid]),
  224. 'fee_mode' => 'I',
  225. 'notify_url' => $notify_url,
  226. 'pay_mode' => $pay_mode
  227. ];
  228. break;
  229. case 'ALI':
  230. $type = '1';
  231. // 天地博爱科技通道
  232. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  233. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  234. $params = [
  235. 'order_no' => $_order['order_sn'],
  236. 'app_id' => $app_id,
  237. 'pay_channel' => 'alipay_qr',
  238. 'pay_amt' => (string)$_order['order_price'],
  239. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  240. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  241. 'device_info' => [
  242. 'device_ip' => app('request')->ip()
  243. ],
  244. 'fee_mode' => 'I',
  245. 'notify_url' => $notify_url
  246. ];
  247. break;
  248. default:
  249. throw new ValidateException('请选择支付方式');
  250. break;
  251. }
  252. return $this->Payment_create_traits_a($params, $type, $key);
  253. }
  254. /**
  255. * @param $userinfo
  256. * @param $order_sn
  257. * @return mixed|string
  258. */
  259. public function getOrderInfo($userinfo, $order_sn)
  260. {
  261. $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn, 'uid' => $userinfo->uid])->find();
  262. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  263. $result = DouHuoMallFilmApiRequest::getOrderInfo($order_sn);
  264. if (!empty($result) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) return $result['data'];
  265. throw new ValidateException('订单不存在');
  266. }
  267. /**
  268. * @return array
  269. * @throws \think\db\exception\DataNotFoundException
  270. * @throws \think\db\exception\DbException
  271. * @throws \think\db\exception\ModelNotFoundException
  272. */
  273. public function getLocalMovieCity()
  274. {
  275. $city_name = [];
  276. $city = Db::name('movie_city')->field('city_sign,city_name,initial')->where('is_show', 1)->order('id asc')->select();
  277. foreach ($city as $key => $value) {
  278. $city_name[$value['initial']][] = ['city_sign' => $value['city_sign'], 'city_name' => $value['city_name']];
  279. }
  280. $initial = !empty($city) ? array_values(array_column($city->toArray(), 'initial', 'initial')) : [];
  281. return compact('city_name', 'initial');
  282. }
  283. }