123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/10/23
- * Time: 上午10:28
- */
- namespace app\main\service;
- use app\common\model\OrdersCollect;
- use app\main\constants\AdminConstants;
- use think\Log;
- class OrderCollectService
- {
- //数据标识 自身
- const FLAG_SELF = 1;
- //数据标识 属下
- const FLAG_SUB = 2;
- //数据标识 全部
- const FLAG_ALL = 3;
- /**
- * @var OrderCollectService
- */
- protected static $self = NULL;
- /**
- * @return OrderCollectService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return OrdersCollect
- */
- public function getOrdersCollectModel()
- {
- return model('OrdersCollect');
- }
- /**
- * 创建订单时添加数据统计
- * @param $admin_id
- * @param $is_vip
- * @param $channel_id
- * @param $group_id
- * @param $business
- * @param $is_activity
- */
- public function addCreateOrderCollect($user_id, $admin_id,$is_vip,$channel_id,$group_id,$business, $is_activity){
- $date = date('Ymd', time());
- //渠道商数据统计
- if($group_id == AdminConstants::ADMIN_GROUP_ID_CHANNEL){
- //添加渠道本身订单统计
- $this->addChannelOrdersCollect($user_id, self::FLAG_SELF,$date,$is_vip,$admin_id,$business, $is_activity);
- //添加渠道汇总订单统计
- $this->addChannelOrdersCollect($user_id, self::FLAG_ALL,$date,$is_vip,$admin_id,$business, $is_activity);
- }
- //代理商数据统计
- if($group_id == AdminConstants::ADMIN_GROUP_ID_AGENT){
- //添加代理商本身统计
- $this->addAgentOrdersCollect($user_id, self::FLAG_SELF,$date,$is_vip,$admin_id,$business, $is_activity);
- //添加代理商上级渠道下属统计
- $this->addChannelOrdersCollect($user_id, self::FLAG_SUB,$date,$is_vip,$channel_id,$business, $is_activity);
- //添加代理商上级渠道汇总统计
- $this->addChannelOrdersCollect($user_id, self::FLAG_ALL,$date,$is_vip,$channel_id,$business, $is_activity);
- }
- //添加管理员下属统计
- $this->addManagerOrdersCollect($user_id,self::FLAG_SUB,$date,$is_vip, $business, $is_activity);
- }
- /**
- * 添加代理商创建订单时数据统计
- * @param $user_id
- * @param $flag
- * @param $date
- * @param $is_vip
- * @param $admin_id
- * @param $business
- */
- private function addAgentOrdersCollect($user_id, $flag, $date, $is_vip, $admin_id, $business, $is_activity)
- {
- //代理商今日的数据
- $map_day = ['admin_id' => $admin_id, 'type' => 1, 'flag' => $flag, 'createdate' => $date, 'business_line'=>$business];
- try {
- $agentResDay = $this->getOrdersCollectModel()->where($map_day)->find();
- $user_count = OrderService::instance()->setOrderUserCountCache($user_id, $map_day, $is_vip, false)->data;
- if ($agentResDay) {
- if ($is_vip) { //VIP充值
- $update_data = [
- 'vip_recharge_orders_count' => ['exp', "`vip_recharge_orders_count`+1"],
- 'vip_recharge_user_count' => $user_count['user'],
- 'recharge_user_count'=>$user_count['total']
- ];
- } else { //普通充值
- $update_data = [
- 'normal_recharge_orders_count' => ['exp', "`normal_recharge_orders_count`+1"],
- 'normal_recharge_user_count' => $user_count['user'],
- 'recharge_user_count'=>$user_count['total']
- ];
- if (!$is_activity) {
- $update_data['kandian_recharge_orders_count'] = ['exp', "`kandian_recharge_orders_count`+1"];
- }
- }
- $this->getOrdersCollectModel()->where('id', $agentResDay->id)->update($update_data);
- } else {
- if ($is_vip) { //VIP充值
- $map_day['vip_recharge_orders_count'] = 1;
- $map_day['vip_recharge_user_count'] = $user_count['user'];
- $map_day['recharge_user_count'] = $user_count['total'];
- } else { //普通充值
- $map_day['normal_recharge_orders_count'] = 1;
- $map_day['normal_recharge_user_count'] = $user_count['user'];
- $map_day['recharge_user_count'] = $user_count['total'];
- if (!$is_activity) {
- $map_day['kandian_recharge_orders_count'] = 1;
- }
- }
- $this->getOrdersCollectModel()->insert($map_day);
- }
- } catch (\Exception $exception) {
- Log::error("添加代理订单统计(Flag:{$flag})数据错误:" . json_encode(['errcode' => $exception->getCode(), 'errmsg' => $exception->getMessage()]));
- }
- }
- /**
- * 添加渠道商创建订单时的数据统计
- * @param $user_id
- * @param $flag
- * @param $date
- * @param $is_vip
- * @param $admin_id
- */
- private function addChannelOrdersCollect($user_id, $flag,$date,$is_vip,$admin_id,$business, $is_activity){
- //渠道商今天自身的数据
- $map_day = ['admin_id' => $admin_id, 'type' => 1, 'flag' => $flag, 'createdate' => $date, 'business_line' => $business];
- try {
- $channelResDay = $this->getOrdersCollectModel()->where($map_day)->find();
- $user_count = OrderService::instance()->setOrderUserCountCache($user_id, $map_day, $is_vip, false)->data;
- if ($channelResDay) { //如果有渠道商今天自身的数据
- if ($is_vip) { //充值vip
- $update_data = [
- 'vip_recharge_orders_count' => ['exp', "`vip_recharge_orders_count`+1"],
- 'updatetime' => time(),
- 'vip_recharge_user_count' => $user_count['user'],
- 'recharge_user_count' => $user_count['total'],
- ];
- } else { //普通充值
- $update_data = [
- 'normal_recharge_orders_count' => ['exp', "`normal_recharge_orders_count`+1"],
- 'updatetime' => time(),
- 'normal_recharge_user_count' => $user_count['user'],
- 'recharge_user_count' => $user_count['total'],
- ];
- if (!$is_activity) {
- $update_data['kandian_recharge_orders_count'] = ['exp', "`kandian_recharge_orders_count`+1"];
- }
- }
- $this->getOrdersCollectModel()->where('id',$channelResDay->id)->update($update_data);
- } else { //渠道商自身当天的订单数据插入
- if ($is_vip) { //VIP充值
- $map_day['vip_recharge_orders_count'] = 1;
- $map_day['vip_recharge_user_count'] = $user_count['user'];
- $map_day['recharge_user_count'] = $user_count['total'];
- $map_day['createtime'] = time();
- $map_day['updatetime'] = time();
- } else { //普通充值
- $map_day['normal_recharge_orders_count'] = 1;
- $map_day['normal_recharge_user_count'] = $user_count['user'];
- $map_day['recharge_user_count'] = $user_count['total'];
- $map_day['createtime'] = time();
- $map_day['updatetime'] = time();
- if (!$is_activity) {
- $map_day['kandian_recharge_orders_count'] = 1;
- }
- }
- $map_day['business_line'] = $business;
- $this->getOrdersCollectModel()->insert($map_day);
- }
- } catch (\Exception $exception) {
- Log::error("添加渠道订单统计(Flag:{$flag})数据错误:".json_encode(['errcode' => $exception->getCode(), 'errmsg' => $exception->getMessage()]));
- }
- }
- /**
- * 添加管理员OrdersCollect统计
- * @param $user_id
- * @param $flag
- * @param $date
- * @param $is_vip
- * @param $business
- */
- private function addManagerOrdersCollect($user_id, $flag,$date,$is_vip,$business, $is_activity){
- $map_day = ['admin_id' => 0, 'type' => 1, 'flag' => $flag, 'createdate' => $date, 'business_line' => $business];
- //管理员今天下级的数据
- try {
- $manageResDay = $this->getOrdersCollectModel()->where($map_day)->find();
- $user_count = OrderService::instance()->setOrderUserCountCache($user_id, $map_day, $is_vip, false)->data;
- if ($manageResDay) { //更新管理员下级的数据
- if ($is_vip) {
- $this->getOrdersCollectModel()->update([
- 'vip_recharge_orders_count' => ['exp', "`vip_recharge_orders_count`+1"],
- 'id' => $manageResDay->id,
- 'vip_recharge_user_count' => $user_count['user'],
- 'recharge_user_count'=>$user_count['total']
- ]);
- } else {
- $update_data = [
- 'normal_recharge_orders_count' => ['exp', "`normal_recharge_orders_count`+1"],
- 'id' => $manageResDay->id,
- 'normal_recharge_user_count' => $user_count['user'],
- 'recharge_user_count'=>$user_count['total']
- ];
- if (!$is_activity) {
- $update_data['kandian_recharge_orders_count'] = ['exp', "`kandian_recharge_orders_count`+1"];
- }
- $this->getOrdersCollectModel()->update($update_data);
- }
- } else { //插入管理员下级的数据
- if ($is_vip) {
- $map_day['vip_recharge_orders_count'] = 1;
- $map_day['vip_recharge_user_count'] = $user_count['user'];
- $map_day['recharge_user_count'] = $user_count['total'];
- $this->getOrdersCollectModel()->insert($map_day);
- } else {
- $map_day['normal_recharge_orders_count'] = 1;
- $map_day['normal_recharge_user_count'] = $user_count['user'];
- $map_day['recharge_user_count'] = $user_count['total'];
- if (!$is_activity) {
- $map_day['kandian_recharge_orders_count'] = 1;
- }
- $this->getOrdersCollectModel()->insert($map_day);
- }
- }
- } catch (\Exception $exception) {
- Log::error("添加管理员订单统计(Flag:{$flag})数据错误:".json_encode(['errcode' => $exception->getCode(), 'errmsg' => $exception->getMessage()]));
- }
- }
- }
|