123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/8/10
- * Time: 下午12:08
- */
- namespace app\main\service;
- use app\common\library\Redis;
- use app\common\model\Dda;
- use app\common\model\Orders;
- use app\common\model\OrdersCollect;
- use app\main\constants\ApiConstants;
- use app\main\constants\ErrorCodeConstants;
- use app\main\model\object\OrderObject;
- use app\main\model\object\ReturnObject;
- use app\main\model\object\UserObject;
- use think\Collection;
- use app\main\constants\OrderContents;
- /**
- * Class OrderService
- * @package app\main\service
- */
- class OrderService extends BaseService
- {
- private static $self = NULL;
- /**
- * @return $this|OrderService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return Orders
- */
- public function getOrderModel()
- {
- return model('Orders');
- }
- /**
- * @return OrdersCollect
- */
- public function getOrderCollectModel()
- {
- return model('OrdersCollect');
- }
- /**
- * @param $user_id
- * @param $where
- * @param $vip bool 是否vip
- * @param $success bool 充值成功
- * @return ReturnObject
- */
- public function setOrderUserCountCache($user_id, $where, $vip, $success = true)
- {
- $fields = $total = array_values($where);
- if ($vip) {
- $fields[] = 'vip';
- } else {
- $fields[] = 'normal';
- }
- if ($success) {
- $fields[] = 'suc';
- $total[] = 'suc';
- }
- $cacheKey = 'ORU:' . implode(':', $fields);
- $cacheAllKey = 'ORU:' . implode(':', $total);
- LogService::info($cacheKey);
- Redis::instance()->pfAdd($cacheKey, $user_id);
- Redis::instance()->pfAdd($cacheAllKey, $user_id);
- Redis::instance()->expire($cacheKey, 86400);
- Redis::instance()->expire($cacheAllKey, 86400);
- $data = [
- 'user' => Redis::instance()->pfCount($cacheKey),
- 'total' => Redis::instance()->pfCount($cacheAllKey),
- ];
- return $this->setData($data)->getReturn();
- }
- /**
- * @param $payId
- * @param array $request
- * @param UserObject $user
- * @return \app\main\model\object\ReturnObject
- */
- public function createOrderByPayMethod($payId, array $request, UserObject $user)
- {
- $payment = CpsPay::getInstanceByPayId($payId);
- if ($payment == null) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('支付类型不存在')->getReturn();
- }
- $params = [
- 'business' => '',
- 'goods_id' => '',
- 'act_id' => '',
- 'book_id' => '',
- 'chapter_id' => '',
- ];
- $params = array_merge($params, $request);
- if (!$user->id || !$params['goods_id']) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('参数错误')->getReturn();
- }
- //获取渠道相关信息
- list($status, $message, $data) = $payment->createOrders($params, $user);
- if ($status) {
- $this->setMsg($message)->setData($data);
- } else {
- $this->setMsg($message)->setCode(ErrorCodeConstants::EXCEPTION);
- }
- return $this->getReturn();
- }
- /**
- * 获取服务类
- * @param $pay_method
- * @return BasePayService
- */
- public function getOrderService($pay_method)
- {
- $class = false;
- switch ($pay_method) {
- case ApiConstants::PAYMENT_METHOD_WECHATH5:
- $class = WechatH5Payservice::instance();
- break;
- case ApiConstants::PAYMENT_METHOD_ALIPAYH5:
- $class = AliH5PayService::instance();
- break;
- }
- return $class;
- }
- /**
- * 获取充值记录
- * @param $userId
- * @param int $page
- * @param int $pageSize
- * @return \app\main\model\object\ReturnObject
- */
- public function getRechargeRecord($userId, $page = 1, $pageSize = 10)
- {
- if (!$page) {
- $page = 1;
- }
- if (!$pageSize) {
- $pageSize = 1;
- }
- $result = [
- 'total' => 0,
- 'record_list' => [],
- ];
- $pageNo = $page;
- $startNo = ($pageNo - 1) * $pageSize;
- $recharge = model('Recharge')->setConnect($userId)->where('user_id', $userId)->order('createtime', 'desc')->limit($startNo, $pageSize)->select();
- $count = model('Recharge')->setConnect($userId)->where('user_id', $userId)->count();
- if ($recharge) {
- $result['record_list'] = Collection::make($recharge)->toArray();
- $result['total'] = $count;
- }
- return $this->setData($result)->getReturn();
- }
- /**
- * 订单缓存key
- * @param OrderObject $orderObject
- * @return \app\main\model\object\ReturnObject
- */
- public function getOrderCacheKey(OrderObject $orderObject)
- {
- $fields = ['goods_id', 'user_id', 'admin_id', 'wxpay_id', 'payment_method'];
- $keys = ["ORDER"];
- foreach ($fields as $field) {
- $keys[] = $orderObject->{$field};
- }
- $return = implode(':', $keys);
- return $this->setData($return)->getReturn();
- }
- public function getOrderKlData($start_time, $end_time)
- {
- $tmpStartPos = strtotime(date('Y-m-d', $start_time));
- $tmpEndPos = strtotime(date('Y-m-d', $end_time + 86400)) - 1;
- if ($tmpEndPos - $tmpStartPos > 864000) {
- $tmpEnd = $tmpStartPos + 864000 - 1;
- } else {
- $tmpEnd = $tmpEndPos;
- }
- LogService::info(date('Y-m-d H:i:s', $tmpStartPos) . '-' . date('Y-m-d H:i:s', $tmpEnd));
- $qds_ids = AdminService::instance()
- ->getAdminConfigModel()
- ->where('is_blacklist', 0)
- ->column('admin_id');
- $agent_ids = AdminService::instance()->getAdminExtendModel()
- ->alias('ae')
- ->join('admin_config ac', 'ae.create_by=ac.admin_id')
- ->where('ac.is_blacklist', 0)
- ->column('ae.admin_id');
- $all_ids = implode(',', array_merge($qds_ids, $agent_ids));
- $list = model('orders')
- ->where('state', OrderContents::ORDER_STATE_PAID)
- ->where('NOT FIND_IN_SET(IF(deduct=1,resource_id,admin_id),"' . $all_ids . '")')
- ->whereBetween('createtime', "$tmpStartPos, $tmpEnd")
- ->field([
- 'FROM_UNIXTIME(createtime,"%Y-%m-%d")' => 'date',
- 'sum(IF(deduct=1,1,0))' => 'dd_count',
- 'count(1)' => 'count',
- 'sum(IF(deduct=1,money,0))' => 'dd_money',
- 'sum(money)' => 'money',
- 'unix_timestamp()' => 'createtime',
- ])->group('FROM_UNIXTIME(createtime,"%Y-%m-%d")')
- ->select();
- $data = [];
- for ($i = $tmpStartPos; $i < $tmpEnd; $i += 86400) {
- $data[date('Y-m-d', $i)] = [
- 'date' => date('Y-m-d', $i),
- 'dd_count' => '0',
- 'count' => '0',
- 'dd_money' => '0',
- 'money' => '0',
- 'createtime' => time(),
- ];
- }
- if ($list) {
- foreach ($list as $item) {
- $data[$item['date']] = $item->getData();
- }
- }
- if ($tmpEnd != $tmpEndPos) {
- $tmpStartPos = $tmpEnd + 1;
- $data = array_merge($data, $this->getOrderKlData($tmpStartPos, $end_time)->data);
- }
- return $this->setData($data)->getReturn();
- }
- /**
- * 更新数据表
- * @param $data
- */
- public function updateKlTable($data)
- {
- $mDda = new Dda();
- $exists = $mDda->whereIn('date', array_keys($data))->column('date');
- foreach ($data as $date => $item) {
- if (in_array($date, $exists) || $date == date('Y-m-d')) {
- unset($data[$date]);
- }
- }
- $mDda->insertAll(array_values($data));
- }
- /**
- * @param $admin_id
- * @return ReturnObject
- */
- public function getWithdrawMonth($admin_id)
- {
- $etime = model('withdraw')->where('admin_id', $admin_id)->max('etime');
- $data = $this->getOrderCollectModel()
- ->field(
- [
- 'min(createdate)' => 'stime',
- 'max(createdate)' => 'etime',
- 'substr(createdate,1,6)' => 'cmonth',
- 'sum(recharge_money_benefit)' => 'cmoney',
- ]
- )->where(
- [
- 'admin_id' => $admin_id,
- 'type' => OrderContents::ORDER_COLLECT_TYPE_DAY,
- 'flag' => OrderContents::ORDER_COLLECT_FLAG_ALL,
- ]
- )->where('createdate', '>', $etime)
- ->group('cmonth')
- ->having('cmoney > 0')
- ->order('cmonth', 'asc')
- ->limit(1)
- ->find();
- if ($data) {
- if ($data['cmonth'] != date('Ym')) {
- $return = $data->getData();
- return $this->setData($return)->getReturn();
- }
- }
- return $this->setData(false)->getReturn();
- }
- }
|