<?php
namespace Base\Service;

use Base\Facade\Request;

class MarketService
{
    public function shift($marketShift)
    {
        $fromId = $marketShift['from_id'];
        $promoteId = $marketShift['promote_id'];
        $toId = $marketShift['to_id'];
        $time = $marketShift['split_time'] == 0 ? null : $marketShift['split_time'];
        
        $promote = M('promote', 'tab_')->field(['chain', 'id', 'level', 'admin_id'])->where(['id' => $promoteId, 'admin_id' => $fromId])->find();

        if (!$promote) {
            throw new \Exception('会长不存在');
        }
        if ($promote['level'] != 1) {
            throw new \Exception('该账号非会长');
        }
        if ($fromId == $toId) {
            throw new \Exception('原市场专员与新市场专员相同,不可转换。');
        }
        if ($promote['admin_id'] == $toId) {
            throw new \Exception('新市场专员与现市场专员相同,不可转换。');
        }
        $promotes = (new PromoteService)->getAllChildren($promote, 0, ['id']);
        $promoteIds = [$promote['id']];
        $promoteIds = array_merge(array_column($promotes, 'id'), $promoteIds);

        $this->shiftPromote($promote, $fromId, $toId);
        $this->shiftSpend($promoteIds, $fromId, $toId, $time);
        $this->shiftDeposit($promoteIds, $fromId, $toId, $time);
    }

    public function shiftPromote($promote, $fromId, $toId, $time = null)
    {
        $map = [];
        $map['id'] = $promote['id'];
        $map['admin_id'] = $fromId;
        M('promote', 'tab_')->where($map)->save(['admin_id' => $toId]);
        M('promote', 'tab_')->where(['chain' => ['like', $promote['chain'] . $promote['id'] . '/%']])->save(['admin_id' => $toId]);
        
        $authGroup = M('auth_group', 'sys_')->where(['title'=>'市场总监'])->find();
        $dataPresident = explode(',', $authGroup['data_president']);
        if (!in_array($promote['id'], $dataPresident)) {
            M('auth_group', 'sys_')->where(['title'=>'市场总监'])->save(['data_president' => $authGroup['data_president'] . ',' . $promote['id']]);
        }
    }

    public function shiftSpend($promoteIds, $fromId, $toId, $time = null)
    {
        $map = [];
        $map['promote_id'] = ['in', $promoteIds];
        $map['market_admin_id'] = $fromId;
        if ($time) {
            $map['pay_time'] = ['egt', $time];
        }

        $count = M('spend', 'tab_')->where($map)->count();
        $limit = 500;
        $pageCount = ceil($count/$limit);
        for ($page = 0; $page <= $pageCount; $page ++) {
            M('spend', 'tab_')->where($map)->limit($limit)->save(['market_admin_id' => $toId]);
        }
        M('spend', 'tab_')->where($map)->save(['market_admin_id' => $toId]);
    }

    public function shiftDeposit($promoteIds, $fromId, $toId, $time = null)
    {
        $map = [];
        $map['promote_id'] = ['in', $promoteIds];
        $map['market_admin_id'] = $fromId;
        if ($time) {
            $map['create_time'] = ['egt', $time];
        }

        $count = M('deposit', 'tab_')->where($map)->count();
        $limit = 500;
        $pageCount = ceil($count/$limit);
        for ($page = 0; $page <= $pageCount; $page ++) {
            M('deposit', 'tab_')->where($map)->limit($limit)->save(['market_admin_id' => $toId]);
        }
        M('deposit', 'tab_')->where($map)->save(['market_admin_id' => $toId]);
    }
}