MovieRepository.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 $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 = substr($storeOrder->getNewOrderId(), 0, 20);
  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($params['phone'] ?? $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($params['cinema_sign']);
  172. /** @var FilmRepository $filmRepository */
  173. $filmRepository = app()->make(FilmRepository::class);
  174. $filminfo = $filmRepository->getFilmInfo($params['film_sign']);
  175. $_order = [
  176. 'uid' => $userinfo->uid,
  177. 'mobile' => $params['phone'] ?? $userinfo->phone,
  178. 'phone' => $params['phone'] ?? $userinfo->phone,
  179. 'order_sn' => $order_sn,
  180. 'plat_order' => $submitOrder['data']['plat_order'],
  181. 'price' => $order_price,
  182. 'number' => count($sched_seat),
  183. 'order_price' => bcadd((string)$order_price, bcmul((string)$order_price, (string)self::premium_ratio, 2), 2),
  184. 'channel_type' => $channel_sn['channel_type'],
  185. 'city_sign' => $cinema['city_sign'],
  186. 'city_name' => $cinema['city_name'],
  187. 'area_code' => $cinema['area_sign'],
  188. 'area_name' => $cinema['area_name'],
  189. 'cinema_sign' => $cinema['cinema_sign'],
  190. 'cinema_name' => $cinema['cinema_name'],
  191. 'address' => $cinema['address'],
  192. 'longitude' => $cinema['longitude'],
  193. 'latitude' => $cinema['latitude'],
  194. 'hall_sign' => $params['hall_sign'],
  195. 'hall_name' => $params['hall_name'],
  196. 'relation_id' => $params['relation_id'],
  197. 'sched_sign' => $params['sched_sign'],
  198. 'film_sign' => $filminfo['film_sign'],
  199. 'film_name' => $filminfo['film_name'],
  200. 'show_date' => $params['show_date'],
  201. 'show_time' => $params['show_time'],
  202. 'sched_seat' => $params['sched_seat'], // 场次座位
  203. 'create_time' => time()
  204. ];
  205. $order_sn = Db::transaction(function () use ($userinfo, $_order) {
  206. $_order = $this->dao->create($_order);
  207. if ($_order) return $_order['order_sn'];
  208. throw new ValidateException('锁座下单失败');
  209. });
  210. return $order_sn;
  211. } else {
  212. throw new ValidateException($submitOrder['msg']);
  213. }
  214. }
  215. /**
  216. * @param $userinfo
  217. * @param $order_sn
  218. * @param $pay_type
  219. * @return mixed
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\DbException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. */
  224. public function confirmOrder($userinfo, $order_sn, $pay_type)
  225. {
  226. $orderinfo = $this->dao->search(['order_sn' => $order_sn, 'uid' => $userinfo['uid']])->find();
  227. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  228. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 2) throw new ValidateException('订单已超时');
  229. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 3) throw new ValidateException('订单已取消');
  230. if ($orderinfo['paid'] == 1) throw new ValidateException('订单已支付');
  231. $res = $this->pay($pay_type, $userinfo['wechat_user_id'], $orderinfo, $orderinfo['film_name'], env('APP_URL') . "/api/hf_notify/movie", 'delay');
  232. Db::name('log')->insert(['data' => '汇付电影票订单支付--- 返回参数' . json_encode($res)]);
  233. if (isset($res['id'])) {
  234. $type = 0;
  235. if ($pay_type == 'wx') $type = 2;
  236. if (strtolower($pay_type) == 'ali') $type = 1;
  237. $orderinfo->pay_type = $type;
  238. $orderinfo->update_time = time();
  239. $orderinfo->save();
  240. return $res;
  241. }
  242. throw new ValidateException('支付失败');
  243. }
  244. /**
  245. * 支付
  246. *
  247. * @param $payType
  248. * @param $openid
  249. * @param $_order
  250. * @param $title
  251. * @param $notify_url
  252. * @param $pay_mode
  253. * @param $appid
  254. * @return array|mixed
  255. */
  256. private function pay($payType, $openid, $_order, $title, $notify_url, $pay_mode = null, $appid = '')
  257. {
  258. switch ($payType) {
  259. case 'wx':
  260. $pay_wx = systemConfig('pay_wx');
  261. if ($pay_wx == 2) {
  262. $appid = env('WECHAT_MP.APPID');
  263. $new_secret = env('WECHAT_MP.SECRET');
  264. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  265. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  266. } elseif ($pay_wx == 3) {
  267. $appid = env('WECHAT_TDYL.APPID');
  268. $new_secret = env('WECHAT_TDYL.SECRET');
  269. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  270. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  271. }
  272. $type = '2';
  273. $params = [
  274. 'order_no' => $_order['order_sn'],
  275. 'app_id' => $app_id,
  276. 'pay_channel' => 'wx_lite',
  277. 'pay_amt' => (string)$_order['order_price'],
  278. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  279. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  280. 'device_info' => [
  281. 'device_ip' => app('request')->ip()
  282. ],
  283. 'expend' => json_encode(['open_id' => $openid, 'wx_app_id' => $appid]),
  284. // 'fee_mode' => 'I',
  285. 'notify_url' => $notify_url,
  286. 'pay_mode' => $pay_mode
  287. ];
  288. break;
  289. case 'ALI':
  290. case 'ali':
  291. $type = '1';
  292. // 天地博爱科技通道
  293. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  294. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  295. $params = [
  296. 'order_no' => $_order['order_sn'],
  297. 'app_id' => $app_id,
  298. 'pay_channel' => 'alipay_qr',
  299. 'pay_amt' => (string)$_order['order_price'],
  300. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  301. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  302. 'device_info' => [
  303. 'device_ip' => app('request')->ip()
  304. ],
  305. // 'fee_mode' => 'I',
  306. 'notify_url' => $notify_url
  307. ];
  308. break;
  309. default:
  310. throw new ValidateException('请选择支付方式');
  311. break;
  312. }
  313. return $this->Payment_create_traits_a($params, $type, $key);
  314. }
  315. /**
  316. * @param $userinfo
  317. * @param $order_sn
  318. * @return mixed|string
  319. */
  320. public function getOrderInfo($userinfo, $order_sn)
  321. {
  322. $orderinfo = $this->dao->search(['order_sn' => $order_sn, 'uid' => $userinfo['uid']])->find();
  323. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  324. $result = DouHuoMallFilmApiRequest::getOrderInfo($order_sn);
  325. if (!empty($result) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
  326. $plat_orderinfo = $result['data'];
  327. $plat_orderinfo['order_price'] = $orderinfo['order_price'];
  328. $plat_orderinfo['order_sn'] = $order_sn;
  329. $plat_orderinfo['create_time'] = strtotime($orderinfo['create_time']);
  330. $plat_orderinfo['phone'] = $orderinfo['mobile'];
  331. $plat_orderinfo['sched_seat'] = json_decode($orderinfo['sched_seat'], true);
  332. $plat_orderinfo['cinema'] = [
  333. 'cinema_sign' => $orderinfo['cinema_sign'],
  334. 'cinema_name' => $orderinfo['cinema_name'],
  335. 'address' => $orderinfo['address'],
  336. 'longitude' => $orderinfo['longitude'],
  337. 'latitude' => $orderinfo['latitude'],
  338. 'hall_sign' => $orderinfo['hall_sign'],
  339. 'hall_name' => $orderinfo['hall_name'],
  340. 'tel' => Db::name('movie_cinema')->where('cinema_sign', $orderinfo['cinema_sign'])->value('tel')
  341. ];
  342. $film = Db::name('movie_film')->where('film_sign', $orderinfo['film_sign'])->find();
  343. $plat_orderinfo['film'] = [
  344. 'film_sign' => $orderinfo['film_sign'],
  345. 'film_name' => $orderinfo['film_name'],
  346. 'language' => $film['language'],
  347. 'cover_img' => $film['cover_img'],
  348. 'version' => $film['version'],
  349. ];
  350. $plat_orderinfo['sched_time'] = date('Y-m-d H:i', strtotime($result['data']['sched_time']));
  351. $plat_orderinfo['schedend_time'] = date('m-d H:i', strtotime($result['data']['sched_time']) + $film['duration'] * 60);
  352. if ($plat_orderinfo['status'] == 1) {
  353. $plat_orderinfo['status'] = 0;
  354. $plat_orderinfo['state'] = '待付款';
  355. }
  356. unset($plat_orderinfo['plat_order']);
  357. unset($plat_orderinfo['third_order']);
  358. return $plat_orderinfo;
  359. }
  360. throw new ValidateException('订单不存在');
  361. }
  362. /**
  363. * @return array
  364. * @throws \think\db\exception\DataNotFoundException
  365. * @throws \think\db\exception\DbException
  366. * @throws \think\db\exception\ModelNotFoundException
  367. */
  368. public function getLocalMovieCity()
  369. {
  370. $city_name = [];
  371. $city = Db::name('movie_city')->field('city_sign,city_name,initial')->where('is_show', 1)->order('id asc')->select();
  372. foreach ($city as $key => $value) {
  373. $city_name[$value['initial']][] = ['city_sign' => $value['city_sign'], 'city_name' => $value['city_name']];
  374. }
  375. $initial = !empty($city) ? array_values(array_column($city->toArray(), 'initial', 'initial')) : [];
  376. return compact('city_name', 'initial');
  377. }
  378. /**
  379. * 获取pv、养老金、售价、积分
  380. *
  381. * @param $cost_price
  382. * @return array
  383. */
  384. public static function getPvYanglaojinScore($cost_price)
  385. {
  386. $premium = round($cost_price * self::premium_ratio, 2);
  387. $cost_price = bcadd((string)$cost_price, (string)$premium, 2);
  388. $pv = $gongxian = $score = bcmul((string)$premium, self::pv_ratio, 2);
  389. $yanglaojin = bcmul((string)$premium, self::yanglaojin_ratio, 2);
  390. return compact('premium', 'cost_price', 'pv', 'yanglaojin', 'gongxian', 'score');
  391. }
  392. /**
  393. * 计算佣金分佣奖励
  394. *
  395. * @param $pay_type
  396. * @param $order_sn
  397. * @param $post_data
  398. * @return void
  399. * @throws \think\db\exception\DataNotFoundException
  400. * @throws \think\db\exception\DbException
  401. * @throws \think\db\exception\ModelNotFoundException
  402. */
  403. public function updateMovieOrderAndcalculationCommissionDividendAward($pay_type, $order_sn, $post_data)
  404. {
  405. $orderinfo = $this->dao->search(['order_sn' => $order_sn])->find();
  406. if ($orderinfo) throw new ValidateException('订单不存在');
  407. Db::startTrans();
  408. $orderinfo->paid = 1;
  409. $orderinfo->pay_time = time();
  410. $orderinfo->pay_type = $pay_type;
  411. $orderinfo->status = 1;
  412. $orderinfo->channel_trade_no = $post_data['id'];
  413. $orderinfo->pay_open_id = $post_data['expend']['open_id'];
  414. $orderinfo->update_time = time();
  415. // 支付方式 1支付宝 2微信
  416. if ($pay_type == 1) {
  417. $orderinfo->alipay_no = $post_data['out_trans_id'];
  418. }
  419. if ($pay_type == 2) {
  420. $orderinfo->weixin_no = $post_data['out_trans_id'];
  421. }
  422. $res = $orderinfo->save();
  423. if ($res) {
  424. $confirmOrder = DouHuoMallFilmApiRequest::confirmOrder($orderinfo['order_sn'], $orderinfo['price']);
  425. if (!empty($confirmOrder) && isset($confirmOrder['code']) && ($confirmOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
  426. // 计算佣金、分红奖励
  427. $this->calculationCommissionDividendAward($orderinfo);
  428. // 提交事务
  429. Db::commit();
  430. } else {
  431. Db::name('log')->insert(['data' => '电影票订单支付回调向【微唯宝】--- 告知电影订单已支付失败,订单编号:' . $order_sn]);
  432. // 回滚事务
  433. Db::rollback();
  434. throw new ValidateException('向微维宝确认订单失败');
  435. }
  436. } else {
  437. Db::name('log')->insert(['data' => '汇付电影票订单支付回调--- 更新电影订单表失败,订单编号:' . $order_sn]);
  438. // 回滚事务
  439. Db::rollback();
  440. throw new ValidateException('更新订单失败');
  441. }
  442. }
  443. /**
  444. * 计算佣金、分红奖励
  445. * @param $orderinfo
  446. * @return void
  447. * @throws \think\db\exception\DataNotFoundException
  448. * @throws \think\db\exception\DbException
  449. * @throws \think\db\exception\ModelNotFoundException
  450. */
  451. private function calculationCommissionDividendAward($orderinfo)
  452. {
  453. /** @var StoreOrderRepository $storeOrderRepository */
  454. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  455. // 电影分佣比例配置
  456. $movie_commission_rate_config = Db::name('system_config_value')->where('config_key', 'movie_commission_rate_config')->find();
  457. // 让利金额
  458. $concession_pri = bcmul((string)$orderinfo['price'], $movie_commission_rate_config['premium_rate'], 2);
  459. // 养老金
  460. $ylj_pri = bcmul($concession_pri, $movie_commission_rate_config['consumer_yanglaojin_rate'], 2);
  461. // 业绩PV
  462. $pv = bcmul($concession_pri, $movie_commission_rate_config['pv_rate'], 2);
  463. //-------------------消费者收益---------------------
  464. // PV值
  465. $storeOrderRepository->con_log_pv($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送PV值");
  466. // 养老金
  467. $this->distributionAnnuityEarnings($orderinfo['uid'], $ylj_pri, $orderinfo['order_id'], 5, $orderinfo['order_sn'], '购买电影票获得养老金' . $ylj_pri, 0, 0);
  468. // 消费者 - 红包 - 老用户发红包奖励
  469. $user = Db::name("user")->where('uid', $orderinfo["uid"])->find();
  470. if ($user['is_old'] == 1) {
  471. $storeOrderRepository->giveHongBao($orderinfo["uid"], $pv, $orderinfo['order_id'], '购买电影票赠送红包');
  472. }
  473. // 积分
  474. $storeOrderRepository->score_log($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送积分");
  475. // 贡献值
  476. $storeOrderRepository->con_log($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送贡献值");
  477. //-------------------消费者收益---------------------
  478. // 推广者
  479. if ($user['spread_uid'] != 0) {
  480. //查询{下单用户}的上级
  481. $spread_data = Db::name('user')->where('uid', $user['spread_uid'])->find();
  482. $rate = $storeOrderRepository->getUserGroupRate($spread_data['user_group']);
  483. //养老金
  484. $spread_yanglaojin = bcmul($pv, $movie_commission_rate_config['spread_yanglaojin_rate'], 2);
  485. $this->distributionAnnuityEarnings(
  486. $spread_data['uid'], $spread_yanglaojin, $orderinfo['order_id'], 5, $orderinfo['order_sn'], '电影票-推广者获得养老金' . $spread_yanglaojin, 0, 0
  487. );
  488. // 直推佣金
  489. $yj_amount_90 = round(bcmul($pv, $rate, 2) * $movie_commission_rate_config['commission_directthrust_rate'], 2);
  490. $this->distributionAnnuityEarnings(
  491. $spread_data['uid'], $yj_amount_90, $orderinfo['order_id'], 3, $orderinfo['order_sn'], '电影票-推广者获得直推佣金' . $yj_amount_90,
  492. 0, 0
  493. );
  494. // 复购金-推广者的佣金抽10%到个人复购金
  495. $fgj_amount_10 = round(bcmul($pv, $rate, 2) * $movie_commission_rate_config['repurchase_directthrust_rate'], 2);
  496. $storeOrderRepository->fugou_user_log($spread_data['uid'], $fgj_amount_10, $orderinfo['order_id'], 1);
  497. // 积分
  498. $score = bcmul($pv, $rate, 2);
  499. $storeOrderRepository->score_log($spread_data['uid'], $score, $orderinfo['order_id'], 13, "电影票-推广者获得赠送积分");
  500. // 业务员及以上身份
  501. if ($spread_data['user_group'] > 1) {
  502. // 贡献值
  503. $storeOrderRepository->con_log($spread_data['uid'], $pv, $orderinfo['order_id'], 13, "电影票-推广者获得赠送贡献值");
  504. }
  505. // 普通用户推广人
  506. if ($spread_data['user_group'] == 1) {
  507. // 贡献值
  508. $gongxianValue = bcmul($pv, $movie_commission_rate_config['contributionvalue_spread_user_rate'], 2);
  509. $storeOrderRepository->con_log($spread_data['uid'], $gongxianValue, $orderinfo['order_id'], 13, "电影票-推广者用户身份获得赠送贡献值");
  510. }
  511. }
  512. // 锁客收益
  513. if ($user['mer_id'] != 0) {
  514. $merchant_uid = Db::name('merchant')->where('mer_id', $user['mer_id'])->value('uid');
  515. $merchant_user = Db::name('user')->where('uid', $merchant_uid)->find();
  516. if (!empty($merchant_user)) {
  517. // 锁客佣金
  518. $yj_amount_90 = round(bcmul($pv, $movie_commission_rate_config['commission_lockpassenger_merid_rate'], 2) * $movie_commission_rate_config['commission_lockpassenger_rate'], 2);
  519. $this->distributionAnnuityEarnings(
  520. $merchant_uid, $yj_amount_90, $orderinfo['order_id'], 3, $orderinfo['order_sn'], '电影票-商户锁定用户获得佣金' . $yj_amount_90,
  521. $user['mer_id'], 0
  522. );
  523. // 锁客复购金
  524. $fgj_amount_10 = round(bcmul($pv, $movie_commission_rate_config['repurchase_lockpassenger_merid_rate'], 2) * $movie_commission_rate_config['repurchase_lockpassenger_rate'], 2);
  525. $storeOrderRepository->fugou_user_log($merchant_uid, $fgj_amount_10, $orderinfo['order_id'], 1);
  526. }
  527. }
  528. }
  529. /**
  530. * 直推奖、养老金
  531. *
  532. * @param $uid
  533. * @param $num
  534. * @param $order_id
  535. * @param $comm_order_type
  536. * @param $order_sn
  537. * @param $mark
  538. * @param $merid
  539. * @param $ds
  540. * @return void
  541. */
  542. private function distributionAnnuityEarnings($uid, $num, $order_id, $comm_order_type, $order_sn, $mark, $merid, $ds = 0)
  543. {
  544. $name = '';
  545. if ($comm_order_type == 3) {
  546. $name = '直推奖';
  547. } else if ($comm_order_type == 5) {
  548. $name = '养老金';
  549. }
  550. $bill = [
  551. 'uid' => $uid,
  552. 'link_id' => $order_id,//关联订单id
  553. 'pm' => 1,//0:支出,1:获得
  554. 'title' => $name,//账单标题
  555. 'category' => 'now_money',//明细种类
  556. 'type' => 'commission',//明细类型
  557. 'number' => $num,//明细数字
  558. 'balance' => 0,//剩余
  559. 'mark' => $mark,//备注
  560. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  561. 'status' => $ds,//0待确定,1有效,-1无效
  562. 'commission_type' => $comm_order_type,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  563. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  564. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  565. 'order_sn' => $order_sn,//订单号
  566. 'tripartite' => 0,//
  567. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  568. 'mer_id' => $merid,//商户id
  569. 'source' => 0,//1线下 0线上
  570. 'order_type' => 1,//1自营 2 第三方 订单类型
  571. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  572. 'gzc' => 0,//养老金是否已进入公证处log表0:未进入。1:已进入
  573. 'take_time' => '',//结算时间
  574. 'district_id' => '',//
  575. 'street_id' => '',//
  576. 'month' => '',//月份
  577. ];
  578. Db::name('user_bill')->insert($bill);
  579. }
  580. /**
  581. * 订单列表
  582. *
  583. * @param $userinfo
  584. * @param $page
  585. * @param $limit
  586. * @param $type
  587. * @return array
  588. * @throws \think\db\exception\DataNotFoundException
  589. * @throws \think\db\exception\DbException
  590. * @throws \think\db\exception\ModelNotFoundException
  591. */
  592. public function getOrderList($userinfo, $page, $limit, $type = -1)
  593. {
  594. $where['uid'] = $userinfo['uid'];
  595. switch ($type) {
  596. case 0:
  597. $where['status'] = [0];
  598. break;
  599. case 1:
  600. case 4:
  601. $where['status'] = [1, 4];
  602. break;
  603. case 2:
  604. case 3:
  605. $where['status'] = [2, 3];
  606. break;
  607. default:
  608. break;
  609. }
  610. $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';
  611. $movie_order = $this->dao->search($where)
  612. ->when($page && $limit, function ($query) use ($page, $limit) {
  613. $query->page($page, $limit);
  614. })->order('order_id desc')->field([$field])->select()->toArray();
  615. foreach ($movie_order as $key => $value) {
  616. $movie_order[$key]['sched_seat'] = json_decode($value['sched_seat'], true);
  617. $movie_order[$key]['cover_img'] = Db::name('movie_film')->where('film_sign', $value['film_sign'])->value('cover_img');
  618. }
  619. $count = $this->dao->search($where)->count();
  620. return compact('movie_order', 'count');
  621. }
  622. /**
  623. * 订单列表
  624. *
  625. * @param $userinfo
  626. * @param $page
  627. * @param $limit
  628. * @param $type
  629. * @return array
  630. * @throws \think\db\exception\DataNotFoundException
  631. * @throws \think\db\exception\DbException
  632. * @throws \think\db\exception\ModelNotFoundException
  633. */
  634. public function getOrderListAll($userinfo, $page, $limit, $type = -1)
  635. {
  636. $where['uid'] = $userinfo['uid'];
  637. switch ($type) {
  638. case 0:
  639. $where['status'] = [0];
  640. break;
  641. case 1:
  642. case 4:
  643. $where['status'] = [1, 4];
  644. break;
  645. case 2:
  646. case 3:
  647. $where['status'] = [2, 3];
  648. break;
  649. default:
  650. break;
  651. }
  652. $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';
  653. $movie_order = $this->dao->search($where)
  654. ->when($page && $limit, function ($query) use ($page, $limit) {
  655. $query->page($page, $limit);
  656. })->order('order_id desc')->field([$field])->select()->toArray();
  657. foreach ($movie_order as $key => $value) {
  658. $movie_order[$key]['sched_seat'] = json_decode($value['sched_seat'], true);
  659. $movie_order[$key]['cover_img'] = Db::name('movie_film')->where('film_sign', $value['film_sign'])->value('cover_img');
  660. }
  661. $count = $this->dao->search($where)->count();
  662. return compact('movie_order', 'count');
  663. }
  664. }