<?php
/**
 * User: Qinii
 * Date: 2020-06-05
 * Time: 09:41
 */

namespace app\controller\api\area;

use app\common\dao\store\order\StoreOrderDao;
use app\Request;
use think\App;
use app\common\repositories\article\ArticleRepository as repository;
use crmeb\basic\BaseController;
use think\facade\Db;

class Area extends BaseController
{
    /**
     * @var repository
     */
    protected $repository;

    /**
     * StoreBrand constructor.
     * @param App $app
     * @param repository $repository
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 区域详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {
        $uid = $this->request->uid();
        $data = [];
        $user = Db::name('user')->where('uid', $uid)->find();

        $month = $this->request->param('month', '') ?: date('Y-m');
        $startMonth = strtotime(date('Y-m-01 00:00:00', strtotime($month)));
        $endMonth = strtotime(date("Y-m-d 00:00:00", strtotime("first day of next month", strtotime($month))));

        if (!in_array($user['user_group'], [5, 6]))
            return app('json')->fail('您不是区域代理');
        $data['area_name'] = Db::name('regions')->field('type,code_list')->where('id', 'in', $user['manage_address'])->cache('code:' . $user['manage_address'], 7200)->column('name');
        $data['area_name'] = join($data['area_name']);

        $query1 = Db::name('store_order')
            ->where('uid', $uid)
            ->whereBetween('create_time', [$startMonth, $endMonth])
            ->where('paid', 1);
        $query2 = Db::name('store_order')
            ->where('uid', $uid)
            ->whereBetween('create_time', [$startMonth, $endMonth])
            ->where('paid', 1)
            ->where('mer_id', systemConfig('mer_365'));
        $query3 = Db::name('store_order')
            ->where('uid', $uid)
            ->whereBetween('create_time', [$startMonth, $endMonth])
            ->where('paid', 1)
            ->where('is_dacang', 1);
        $query4 = Db::name('store_order')
            ->where('uid', $uid)
            ->whereBetween('create_time', [$startMonth, $endMonth])
            ->where('paid', 1)
            ->where('is_dacang', 2);
        $query1_1 = clone $query1;
        $query2_1 = clone $query2;
        $query3_1 = clone $query3;
        $query4_1 = clone $query4;
        $data['success_order']['amount'] = $query1->sum('pay_price');
        $data['success_order']['shop_amount'] = $query2->sum('pay_price');
        $data['success_order']['gong_amount'] = $query3->sum('pay_price');
        $data['success_order']['self_amount'] = $query4->sum('pay_price');

        $data['clear_order']['amount'] = $query1_1->where('is_settlement', 1)->sum('pay_price');
        $data['clear_order']['shop_amount'] = $query2_1->where('is_settlement', 1)->sum('pay_price');
        $data['clear_order']['gong_amount'] = $query3_1->where('is_settlement', 1)->sum('pay_price');
        $data['clear_order']['self_amount'] = $query4_1->where('is_settlement', 1)->sum('pay_price');
        return app('json')->success($data);
    }

    /*收益*/
    public function recommend_agency()
    {
        $uid = $this->request->uid();
        $month = $this->request->param('month', '') ?: date('Y-m');

        $agency_list = Db::name('user')
            ->field('phone,user_group,up_agency_time,manage_address')
            ->whereIn('user_group', [5, 6])
            ->whereIn('spread_uid', $uid)
            ->select()
            ->toArray();
        foreach ($agency_list as &$agency){

            $agency['area_name'] = Db::name('regions')->field('type,code_list')->where('id', 'in', $agency['manage_address'])->cache('code:' . $agency['manage_address'], 7200)->column('name');
            $agency['area_name'] = join($agency['area_name']);
            $agency['area_award_money'] =Db::name('user_bill')
                ->where('uid',$uid)
                ->where('status',1)
                ->where('month',$month)
                ->whereIn('commission_type',[12])
                ->sum('number');
            $agency['area_award_money_sum'] =Db::name('user_bill')
                ->where('uid',$uid)
                ->where('status',1)
                ->where('month',$month)
                ->whereIn('commission_type',[7,12,19])
                ->sum('number');
            $agency['invite_agency_money'] =Db::name('user_bill')
                ->where('uid',$uid)
                ->where('status',1)
                ->where('month',$month)
                ->whereIn('commission_type',[14])
                ->sum('number');
        }
        return app('json')->success($agency_list);
    }

}
