| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- <?php
- /**
- * @package merchant
- * @Author: Qinii
- * @Date: 2020/5/8
- */
- namespace app\common\repositories\movie;
- use app\common\dao\movie\order\MovieOrderDao;
- use app\common\repositories\BaseRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\traits\HuiPay;
- use crmeb\services\ApiResponseService;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\Log;
- /**
- * Class MovieRepository
- * @package app\common\repositories\movie
- * @author xaboy
- * @mixin MovieOrderDao
- */
- class MovieRepository extends BaseRepository
- {
- use HuiPay;
- protected $dao;
- const premium_ratio = 0.2;//溢价比例
- const yanglaojin_ratio = 0.3;
- const pv_ratio = 0.7;
- /**
- * ProductRepository constructor.
- * @param MovieOrderDao $dao
- */
- public function __construct(MovieOrderDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * @param $page
- * @param $limit
- * @param $initial
- * @param $city_name
- * @return false|int|mixed
- */
- public function getMovieCity($page = 1, $limit = 10, $initial = '', $city_name = '')
- {
- return DouHuoMallFilmApiRequest::getMovieCity($page, $limit, $initial = '', $city_name);
- }
- /**
- * @param $city_sign
- * @return false|int|mixed
- */
- public function getMovieArea($city_sign = '')
- {
- return DouHuoMallFilmApiRequest::getMovieArea($city_sign);
- }
- /**
- * @param $page
- * @param $limit
- * @param $city_sign
- * @param $area_sign
- * @return false|int|mixed
- */
- public function getCinema($page = 1, $limit = 10, $city_sign = '', $area_sign = '')
- {
- return DouHuoMallFilmApiRequest::getCinema($page, $limit, $city_sign, $area_sign);
- }
- /**
- * @param $page
- * @param $limit
- * @param $cinema_sign
- * @param $film_sign
- * @return false|int|mixed
- */
- public function getFilm($page = 1, $limit = 10, $cinema_sign = '', $film_sign = '')
- {
- return DouHuoMallFilmApiRequest::getFilm($page, $limit, $cinema_sign, $film_sign);
- }
- /**
- * @param $film_sign
- * @param $cinema_sign
- * @return mixed|string
- */
- public function getCinemaSched($film_sign = '', $cinema_sign = '')
- {
- $result = DouHuoMallFilmApiRequest::getCinemaSched($film_sign, $cinema_sign);
- $cinemaSchedList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
- ? $result['data']['list'] : [];
- foreach ($cinemaSchedList as $dateDay => &$fileSchedValue) {
- foreach ($fileSchedValue as $key => $value) {
- $pcYanglaojinGongxian = static::getPvYanglaojinScore($value['cost_price']);
- $fileSchedValue[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
- $fileSchedValue[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
- $fileSchedValue[$key]['pv'] = $pcYanglaojinGongxian['pv'];
- $fileSchedValue[$key]['score'] = $pcYanglaojinGongxian['score'];
- $fileSchedValue[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
- // unset($fileSchedValue[$key]['channel_price']);
- }
- }
- return $cinemaSchedList;
- }
- /**
- * @param $relation_id
- * @param $sched_sign
- * @return mixed|string
- */
- public function getSchedSeat($relation_id = '', $sched_sign = '')
- {
- $result = DouHuoMallFilmApiRequest::getSchedSeat($relation_id, $sched_sign);
- $schedSeatList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
- ? $result['data']['list'] : [];
- foreach ($schedSeatList as $key => $value) {
- $tmpprice = json_decode($value['price'], true);
- if (isset($tmpprice['P2'])) {
- $price = $tmpprice['P2'];
- } elseif (isset($tmpprice['P1'])) {
- $price = $tmpprice['P1'];
- } elseif (isset($tmpprice['P0'])) {
- $price = $tmpprice['P0'];
- } else {
- $price = 0.00;
- }
- unset($schedSeatList[$key]['price']);
- $pcYanglaojinGongxian = self::getPvYanglaojinScore($price);
- $schedSeatList[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
- $schedSeatList[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
- $schedSeatList[$key]['pv'] = $pcYanglaojinGongxian['pv'];
- $schedSeatList[$key]['score'] = $pcYanglaojinGongxian['score'];
- $schedSeatList[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
- $schedSeatList[$key]['relation_id'] = $relation_id; // 场次ID
- $schedSeatList[$key]['sched_sign'] = $sched_sign; // 场次编码
- }
- return $schedSeatList;
- }
- /**
- * 锁座订单
- *
- * @param $userinfo
- * @param $params
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function submitOrder($userinfo, $params)
- {
- /** @var StoreOrderRepository $storeOrder */
- $storeOrder = app()->make(StoreOrderRepository::class);
- $order_sn = $storeOrder->getNewOrderId() . '0';
- /** @var CinemaSchedRepository $cinemaSchedRepository */
- $cinemaSchedRepository = app()->make(CinemaSchedRepository::class);
- $sched = $cinemaSchedRepository->getCinemaSchedInfo($params['relation_id'], $params['sched_sign']);
- if (empty($sched)) throw new ValidateException('观影场次不存在');
- $seat_sign = json_decode($params['seat_sign'], true);
- $order_price = round($sched['price'] * count($seat_sign), 2);
- $submitOrder = DouHuoMallFilmApiRequest::submitOrder($userinfo->phone, $params['channel_type'], $params['sched_sign'], $seat_sign, $order_sn, $order_price);
- if (!empty($submitOrder) && isset($submitOrder['code']) && ($submitOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE) && isset($submitOrder['data']['plat_order'])) {
- /** @var CinemaRepository $cinemaRepository */
- $cinemaRepository = app()->make(CinemaRepository::class);
- $cinema = $cinemaRepository->getCinemaInfo($sched['cinema_sign']);
- $_order = [
- 'uid' => $userinfo->uid,
- 'mobile' => $userinfo->phone,
- 'order_sn' => $order_sn,
- 'plat_order' => $submitOrder['data']['plat_order'],
- 'price' => $sched['price'],
- 'number' => count($seat_sign),
- 'order_price' => $order_price,
- 'channel_type' => $params['channel_type'],
- 'city_sign' => $cinema['city_sign'],
- 'city_name' => $cinema['city_name'],
- 'area_code' => $cinema['area_sign'],
- 'area_name' => $cinema['area_name'],
- 'cinema_sign' => $cinema['cinema_sign'],
- 'cinema_name' => $cinema['cinema_name'],
- 'address' => $cinema['address'],
- 'longitude' => $cinema['longitude'],
- 'latitude' => $cinema['latitude'],
- 'hall_sign' => $sched['hall_sign'],
- 'hall_name' => $sched['hall_name'],
- 'relation_id' => $sched['relation_id'],
- 'sched_sign' => $sched['sched_sign'],
- 'film_sign' => $sched['film_sign'],
- 'film_name' => $sched['film_name'],
- 'show_date' => $sched['show_date'],
- 'show_time' => $sched['show_time'],
- 'sched_seat' => $sched['sched_seat'],
- 'create_time' => time()
- ];
- $order_sn = Db::transaction(function () use ($userinfo, $_order) {
- $_order = $this->dao->create($_order);
- if ($_order) return $_order['order_sn'];
- throw new ValidateException('锁座下单失败');
- });
- return $order_sn;
- } else {
- throw new ValidateException('锁座下单失败');
- }
- }
- /**
- * @param $userinfo
- * @param $order_sn
- * @return mixed|string
- */
- public function confirmOrder($userinfo, $order_sn)
- {
- $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn, 'uid' => $userinfo->uid])->find();
- if (empty($orderinfo)) throw new ValidateException('订单不存在');
- if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 2) throw new ValidateException('订单已超时');
- if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 3) throw new ValidateException('订单已取消');
- if ($orderinfo['paid'] == 1) throw new ValidateException('订单已支付');
- // return $this->pay(
- // $_order['pay_type'], $userinfo->wechat_user_id, $_order, $_order['film_name'], env('APP_URL') . "/api/hf_notify/movie",
- // 'delay');
- return DouHuoMallFilmApiRequest::confirmOrder($orderinfo['order_sn'], $orderinfo['order_price']);
- }
- /**
- * 支付
- *
- * @param $payType
- * @param $openid
- * @param $_order
- * @param $title
- * @param $notify_url
- * @param $pay_mode
- * @param $appid
- * @return array|mixed
- */
- private function pay($payType, $openid, $_order, $title, $notify_url, $pay_mode = null, $appid = '')
- {
- switch ($payType) {
- case 'wx':
- $pay_wx = systemConfig('pay_wx');
- if ($pay_wx == 2) {
- $appid = env('WECHAT_MP.APPID');
- $new_secret = env('WECHAT_MP.SECRET');
- $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
- $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
- } elseif ($pay_wx == 3) {
- $appid = env('WECHAT_TDYL.APPID');
- $new_secret = env('WECHAT_TDYL.SECRET');
- $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
- $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
- }
- $type = '2';
- $params = [
- 'order_no' => $_order['order_sn'],
- 'app_id' => $app_id,
- 'pay_channel' => 'wx_lite',
- 'pay_amt' => (string)$_order['order_price'],
- 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
- 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
- 'device_info' => [
- 'device_ip' => app('request')->ip()
- ],
- 'expend' => json_encode(['open_id' => $openid, 'wx_app_id' => $appid]),
- 'fee_mode' => 'I',
- 'notify_url' => $notify_url,
- 'pay_mode' => $pay_mode
- ];
- break;
- case 'ALI':
- $type = '1';
- // 天地博爱科技通道
- $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
- $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
- $params = [
- 'order_no' => $_order['order_sn'],
- 'app_id' => $app_id,
- 'pay_channel' => 'alipay_qr',
- 'pay_amt' => (string)$_order['order_price'],
- 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
- 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
- 'device_info' => [
- 'device_ip' => app('request')->ip()
- ],
- 'fee_mode' => 'I',
- 'notify_url' => $notify_url
- ];
- break;
- default:
- throw new ValidateException('请选择支付方式');
- break;
- }
- return $this->Payment_create_traits_a($params, $type, $key);
- }
- /**
- * @param $userinfo
- * @param $order_sn
- * @return mixed|string
- */
- public function getOrderInfo($userinfo, $order_sn)
- {
- $orderinfo = $this->dao->getWhere(['order_sn' => $order_sn, 'uid' => $userinfo->uid])->find();
- if (empty($orderinfo)) throw new ValidateException('订单不存在');
- $result = DouHuoMallFilmApiRequest::getOrderInfo($order_sn);
- if (!empty($result) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) return $result['data'];
- throw new ValidateException('订单不存在');
- }
- /**
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getLocalMovieCity()
- {
- $city_name = [];
- $city = Db::name('movie_city')->field('city_sign,city_name,initial')->where('is_show', 1)->order('id asc')->select();
- foreach ($city as $key => $value) {
- $city_name[$value['initial']][] = ['city_sign' => $value['city_sign'], 'city_name' => $value['city_name']];
- }
- $initial = !empty($city) ? array_values(array_column($city->toArray(), 'initial', 'initial')) : [];
- return compact('city_name', 'initial');
- }
- /**
- * 获取pv、养老金、售价、积分
- *
- * @param $cost_price
- * @return array
- */
- public static function getPvYanglaojinScore($cost_price)
- {
- $premium = round($cost_price * self::premium_ratio, 2);
- $cost_price = bcadd((string)$cost_price, (string)$premium, 2);
- $pv = $gongxian = $score = bcmul((string)$premium, self::pv_ratio, 2);
- $yanglaojin = bcmul((string)$premium, self::yanglaojin_ratio, 2);
- return compact('premium', 'cost_price', 'pv', 'yanglaojin', 'gongxian', 'score');
- }
- }
|