OrderService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/8/10
  6. * Time: 下午12:08
  7. */
  8. namespace app\main\service;
  9. use app\common\library\Redis;
  10. use app\common\model\Dda;
  11. use app\common\model\Orders;
  12. use app\common\model\OrdersCollect;
  13. use app\main\constants\ApiConstants;
  14. use app\main\constants\ErrorCodeConstants;
  15. use app\main\model\object\OrderObject;
  16. use app\main\model\object\ReturnObject;
  17. use app\main\model\object\UserObject;
  18. use think\Collection;
  19. use app\main\constants\OrderContents;
  20. /**
  21. * Class OrderService
  22. * @package app\main\service
  23. */
  24. class OrderService extends BaseService
  25. {
  26. private static $self = NULL;
  27. /**
  28. * @return $this|OrderService
  29. */
  30. public static function instance()
  31. {
  32. if (self::$self == NULL) {
  33. self::$self = new self();
  34. }
  35. return self::$self;
  36. }
  37. /**
  38. * @return Orders
  39. */
  40. public function getOrderModel()
  41. {
  42. return model('Orders');
  43. }
  44. /**
  45. * @return OrdersCollect
  46. */
  47. public function getOrderCollectModel()
  48. {
  49. return model('OrdersCollect');
  50. }
  51. /**
  52. * @param $user_id
  53. * @param $where
  54. * @param $vip bool 是否vip
  55. * @param $success bool 充值成功
  56. * @return ReturnObject
  57. */
  58. public function setOrderUserCountCache($user_id, $where, $vip, $success = true)
  59. {
  60. $fields = $total = array_values($where);
  61. if ($vip) {
  62. $fields[] = 'vip';
  63. } else {
  64. $fields[] = 'normal';
  65. }
  66. if ($success) {
  67. $fields[] = 'suc';
  68. $total[] = 'suc';
  69. }
  70. $cacheKey = 'ORU:' . implode(':', $fields);
  71. $cacheAllKey = 'ORU:' . implode(':', $total);
  72. LogService::info($cacheKey);
  73. Redis::instance()->pfAdd($cacheKey, $user_id);
  74. Redis::instance()->pfAdd($cacheAllKey, $user_id);
  75. Redis::instance()->expire($cacheKey, 86400);
  76. Redis::instance()->expire($cacheAllKey, 86400);
  77. $data = [
  78. 'user' => Redis::instance()->pfCount($cacheKey),
  79. 'total' => Redis::instance()->pfCount($cacheAllKey),
  80. ];
  81. return $this->setData($data)->getReturn();
  82. }
  83. /**
  84. * @param $payId
  85. * @param array $request
  86. * @param UserObject $user
  87. * @return \app\main\model\object\ReturnObject
  88. */
  89. public function createOrderByPayMethod($payId, array $request, UserObject $user)
  90. {
  91. $payment = CpsPay::getInstanceByPayId($payId);
  92. if ($payment == null) {
  93. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('支付类型不存在')->getReturn();
  94. }
  95. $params = [
  96. 'business' => '',
  97. 'goods_id' => '',
  98. 'act_id' => '',
  99. 'book_id' => '',
  100. 'chapter_id' => '',
  101. ];
  102. $params = array_merge($params, $request);
  103. if (!$user->id || !$params['goods_id']) {
  104. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('参数错误')->getReturn();
  105. }
  106. //获取渠道相关信息
  107. list($status, $message, $data) = $payment->createOrders($params, $user);
  108. if ($status) {
  109. $this->setMsg($message)->setData($data);
  110. } else {
  111. $this->setMsg($message)->setCode(ErrorCodeConstants::EXCEPTION);
  112. }
  113. return $this->getReturn();
  114. }
  115. /**
  116. * 获取服务类
  117. * @param $pay_method
  118. * @return BasePayService
  119. */
  120. public function getOrderService($pay_method)
  121. {
  122. $class = false;
  123. switch ($pay_method) {
  124. case ApiConstants::PAYMENT_METHOD_WECHATH5:
  125. $class = WechatH5Payservice::instance();
  126. break;
  127. case ApiConstants::PAYMENT_METHOD_ALIPAYH5:
  128. $class = AliH5PayService::instance();
  129. break;
  130. }
  131. return $class;
  132. }
  133. /**
  134. * 获取充值记录
  135. * @param $userId
  136. * @param int $page
  137. * @param int $pageSize
  138. * @return \app\main\model\object\ReturnObject
  139. */
  140. public function getRechargeRecord($userId, $page = 1, $pageSize = 10)
  141. {
  142. if (!$page) {
  143. $page = 1;
  144. }
  145. if (!$pageSize) {
  146. $pageSize = 1;
  147. }
  148. $result = [
  149. 'total' => 0,
  150. 'record_list' => [],
  151. ];
  152. $pageNo = $page;
  153. $startNo = ($pageNo - 1) * $pageSize;
  154. $recharge = model('Recharge')->setConnect($userId)->where('user_id', $userId)->order('createtime', 'desc')->limit($startNo, $pageSize)->select();
  155. $count = model('Recharge')->setConnect($userId)->where('user_id', $userId)->count();
  156. if ($recharge) {
  157. $result['record_list'] = Collection::make($recharge)->toArray();
  158. $result['total'] = $count;
  159. }
  160. return $this->setData($result)->getReturn();
  161. }
  162. /**
  163. * 订单缓存key
  164. * @param OrderObject $orderObject
  165. * @return \app\main\model\object\ReturnObject
  166. */
  167. public function getOrderCacheKey(OrderObject $orderObject)
  168. {
  169. $fields = ['goods_id', 'user_id', 'admin_id', 'wxpay_id', 'payment_method'];
  170. $keys = ["ORDER"];
  171. foreach ($fields as $field) {
  172. $keys[] = $orderObject->{$field};
  173. }
  174. $return = implode(':', $keys);
  175. return $this->setData($return)->getReturn();
  176. }
  177. public function getOrderKlData($start_time, $end_time)
  178. {
  179. $tmpStartPos = strtotime(date('Y-m-d', $start_time));
  180. $tmpEndPos = strtotime(date('Y-m-d', $end_time + 86400)) - 1;
  181. if ($tmpEndPos - $tmpStartPos > 864000) {
  182. $tmpEnd = $tmpStartPos + 864000 - 1;
  183. } else {
  184. $tmpEnd = $tmpEndPos;
  185. }
  186. LogService::info(date('Y-m-d H:i:s', $tmpStartPos) . '-' . date('Y-m-d H:i:s', $tmpEnd));
  187. $qds_ids = AdminService::instance()
  188. ->getAdminConfigModel()
  189. ->where('is_blacklist', 0)
  190. ->column('admin_id');
  191. $agent_ids = AdminService::instance()->getAdminExtendModel()
  192. ->alias('ae')
  193. ->join('admin_config ac', 'ae.create_by=ac.admin_id')
  194. ->where('ac.is_blacklist', 0)
  195. ->column('ae.admin_id');
  196. $all_ids = implode(',', array_merge($qds_ids, $agent_ids));
  197. $list = model('orders')
  198. ->where('state', OrderContents::ORDER_STATE_PAID)
  199. ->where('NOT FIND_IN_SET(IF(deduct=1,resource_id,admin_id),"' . $all_ids . '")')
  200. ->whereBetween('createtime', "$tmpStartPos, $tmpEnd")
  201. ->field([
  202. 'FROM_UNIXTIME(createtime,"%Y-%m-%d")' => 'date',
  203. 'sum(IF(deduct=1,1,0))' => 'dd_count',
  204. 'count(1)' => 'count',
  205. 'sum(IF(deduct=1,money,0))' => 'dd_money',
  206. 'sum(money)' => 'money',
  207. 'unix_timestamp()' => 'createtime',
  208. ])->group('FROM_UNIXTIME(createtime,"%Y-%m-%d")')
  209. ->select();
  210. $data = [];
  211. for ($i = $tmpStartPos; $i < $tmpEnd; $i += 86400) {
  212. $data[date('Y-m-d', $i)] = [
  213. 'date' => date('Y-m-d', $i),
  214. 'dd_count' => '0',
  215. 'count' => '0',
  216. 'dd_money' => '0',
  217. 'money' => '0',
  218. 'createtime' => time(),
  219. ];
  220. }
  221. if ($list) {
  222. foreach ($list as $item) {
  223. $data[$item['date']] = $item->getData();
  224. }
  225. }
  226. if ($tmpEnd != $tmpEndPos) {
  227. $tmpStartPos = $tmpEnd + 1;
  228. $data = array_merge($data, $this->getOrderKlData($tmpStartPos, $end_time)->data);
  229. }
  230. return $this->setData($data)->getReturn();
  231. }
  232. /**
  233. * 更新数据表
  234. * @param $data
  235. */
  236. public function updateKlTable($data)
  237. {
  238. $mDda = new Dda();
  239. $exists = $mDda->whereIn('date', array_keys($data))->column('date');
  240. foreach ($data as $date => $item) {
  241. if (in_array($date, $exists) || $date == date('Y-m-d')) {
  242. unset($data[$date]);
  243. }
  244. }
  245. $mDda->insertAll(array_values($data));
  246. }
  247. /**
  248. * @param $admin_id
  249. * @return ReturnObject
  250. */
  251. public function getWithdrawMonth($admin_id)
  252. {
  253. $etime = model('withdraw')->where('admin_id', $admin_id)->max('etime');
  254. $data = $this->getOrderCollectModel()
  255. ->field(
  256. [
  257. 'min(createdate)' => 'stime',
  258. 'max(createdate)' => 'etime',
  259. 'substr(createdate,1,6)' => 'cmonth',
  260. 'sum(recharge_money_benefit)' => 'cmoney',
  261. ]
  262. )->where(
  263. [
  264. 'admin_id' => $admin_id,
  265. 'type' => OrderContents::ORDER_COLLECT_TYPE_DAY,
  266. 'flag' => OrderContents::ORDER_COLLECT_FLAG_ALL,
  267. ]
  268. )->where('createdate', '>', $etime)
  269. ->group('cmonth')
  270. ->having('cmoney > 0')
  271. ->order('cmonth', 'asc')
  272. ->limit(1)
  273. ->find();
  274. if ($data) {
  275. if ($data['cmonth'] != date('Ym')) {
  276. $return = $data->getData();
  277. return $this->setData($return)->getReturn();
  278. }
  279. }
  280. return $this->setData(false)->getReturn();
  281. }
  282. }