<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/3/22
 * Time: 下午6:00
 */

namespace App\Modules\Channel\Services;


use App\Modules\Channel\Models\Company;
use DB;

class CompanyService
{

    /**
     * 添加一个公司
     * @param array $data
     * @return mixed
     */
    public static function addCompany($data = [])
    {
        return Company::create($data);
    }

    /**
     * 更新公司
     * @param $id
     * @param array $data [name, is_important, distribution_manages_id]
     * @return mixed
     */
    public static function updateCompany($id, $data = [])
    {
        \Log::info('updateCompany---------------------------------');
        \Log::info($data);
        $company = self::getCompany($id);
        if ($company) {
            if (isset($data['name']) && $data['name']) {
                $company->name = $data['name'];
            }
            if (isset($data['is_important']) && is_numeric($data['is_important'])) {
                $company->is_important = $data['is_important'];
            }
            if (isset($data['distribution_manages_id']) && is_numeric($data['distribution_manages_id'])) {
                $company->distribution_manages_id = $data['distribution_manages_id'];
            }
            if (isset($data['fans_gender']) && $data['fans_gender']) {
                $company->fans_gender = $data['fans_gender'];
            }
            if (isset($data['city']) && $data['city']) {
                $company->city = $data['city'];
            }
            $company->save();
        }
        return self::getCompany($id);
    }

    /**
     * 根据ID查询公司
     * @param $id
     * @return mixed
     */
    public static function getCompany($id)
    {
        $sqlObj = Company::select(
            'companies.id',
            'companies.name',
            'companies.city',
            'companies.fans_gender',
            'companies.is_important',
            'companies.distribution_manages_id',
            'companies.created_at',
            'companies.updated_at',
            DB::raw('IFNULL(distribution_manages.nickname, "") as distribution_manages_nickname')
        )
            ->leftjoin('distribution_manages', 'distribution_manages.id', '=', 'companies.distribution_manages_id')
            ->where('companies.id', $id);
        return $sqlObj->first();

        //return Company::find($id);
    }

    /**
     * 根据名称查找公司
     * @param $name
     * @return mixed
     */
    public static function findByName($name)
    {
        return Company::where('name', $name)->first();
    }

    /**
     * 通过 distribution_manages_id 获取商务下的公司数量
     * @param $distribution_manages_id
     * @return mixed
     */
    public static function getCompanyCountByManageId($distribution_manages_id)
    {
        return Company::where('distribution_manages_id', $distribution_manages_id)->count();
    }

    /**
     * 获取公司列表
     * @param array $data
     * @param bool $isAll
     * @return mixed
     */
    public static function getList($data = [], $isAll = false, $manager_id = '')
    {
        $search_object = Company::select(
            'companies.id',
            'companies.name',
            'companies.city',
            'companies.is_important',
            'companies.distribution_manages_id',
            'companies.fans_gender',
            'companies.created_at',
            'companies.updated_at',
            DB::raw('IFNULL(distribution_manages.nickname, "") as distribution_manages_nickname')
        )
            ->leftjoin('distribution_manages', 'distribution_manages.id', '=', 'companies.distribution_manages_id')
            ->orderBy('created_at', 'desc');

        // $search_object = Company::orderBy('created_at', 'desc');

        if ($manager_id) {
            $search_object->where('distribution_manages.id', $manager_id);
        }

        if (isset($data['search_name']) && $data['search_name']) {
            $search_object->where(function ($query) use ($data) {
                $query->where('companies.name', 'like', "%" . $data['search_name'] . "%");
            });
        }

        if (isset($data['is_important']) && is_numeric($data['is_important'])) {
            $search_object->where('companies.is_important', $data['is_important']);
        }

        if (isset($data['distribution_manages_id']) && is_numeric($data['distribution_manages_id'])) {
            $search_object->where('companies.distribution_manages_id', $data['distribution_manages_id']);
        }

        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }

    public static function getCompanyServiceAccounts($company_id)
    {
        return Company::leftjoin('channel_users', 'channel_users.company_id', '=', 'companies.id')
            ->leftjoin('distribution_channels', 'distribution_channels.channel_user_id', '=', 'channel_users.id')
            ->leftjoin('official_accounts', 'official_accounts.distribution_channel_id', '=', 'distribution_channels.id')
            ->select(['official_accounts.id', 'official_accounts.nickname as official_account_name', 'distribution_channels.id as distribution_channel_id', 'distribution_channels.nickname as channel_name'])
            ->where('companies.id', $company_id)
            ->get();
    }
}