<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/12/2
 * Time: 15:56
 */

namespace App\Modules\Channel\Services;

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

class ChannelUserService
{
    /**
     * 创建渠道
     * @param  array $params ['password','name','pay_type','nickname','latest_login_ip','latest_login_time','remark','register_ip']
     * @return object
     */
    static function createUser($params)
    {
        return ChannelUser::createUser($params);
    }

    /**
     * 登陆
     */
    static function login($phone, $password)
    {
        return ChannelUser::login($phone, $password);
    }

    /**
     * 修改密码
     * @param  string $phone 手机号码
     * @param  string $password 加密后密码
     */
    static function modifyPassword($phone, $password)
    {
        return ChannelUser::modifyPassword($phone, $password);
    }

    /**
     * 判断是否注册
     */
    static function isRegistered($phone)
    {
        return ChannelUser::isRegistered($phone);
    }

    /**
     * 通过手机号码获取
     */
    static function getByPhone($phone)
    {
        return ChannelUser::getByPhone($phone);
    }

    /**
     * 通过ID获取
     */
    static function getById($id)
    {
        return ChannelUser::getById((int)$id);
    }

    /**
     * 通过ID获取 is_important
     */
    static function getImpotantById($id)
    {
        if (empty($id)) {
            return 0;
        }
        return ChannelUser::getImpotantById($id);
    }


    /**
     * 修改渠道信息
     * @param $id 渠道ID
     * @param $params []
     *                  name:渠道名称 可选
     *                  nickname:渠道昵称 可选
     *                  pay_merchant_id: 支付通道 可选
     *                  distribution_manages_id: 管理员 可选
     *                  phone:电话 可选
     *                  person_in_charge_name:负责人 可选
     *                  remark:备注 可选
     *                  is_enabled: 是否开通 0:未审核； 1:审核通过
     *                  company_id:
     *                  company_name:
     * @return mixed
     */
    public static function updateChannelData($id, $params = [])
    {
        return ChannelUser::updateChannelUserData($id, $params);
    }


    public static function updateChannelUserManager($distribution_manages_id, $company_id)
    {

        $channelUserList = ChannelUser::where('company_id', $company_id)->get();
        if ($channelUserList && count($channelUserList) > 0) {
            foreach ($channelUserList as $channelUser) {
                //更新 ChannelUser 对应 渠道 的商务 ID
                $distribution_manage = DB::table('distribution_manages')->find($distribution_manages_id);
                $manager = $distribution_manage?$distribution_manage->nickname:'';
                $affectedAA = DB::update('UPDATE distribution_channels SET distribution_manages_id=:distribution_manages_id,person_in_charge_name=:manager WHERE channel_user_id=:channel_user_id', ['distribution_manages_id' => $distribution_manages_id, 'channel_user_id' => $channelUser->id,'manager'=>$manager]);
            }
        }
        $company = DB::table('companies')->find($company_id);
        $company_name = $company?$company->name:'';
        $affected = DB::update('UPDATE channel_users SET distribution_manages_id=:distribution_manages_id,company_name=:company_name WHERE company_id=:company_id', ['distribution_manages_id' => $distribution_manages_id, 'company_id' => $company_id,'company_name'=>$company_name]);

    }

    /**
     * 获取渠道列表
     * @param $params []
     *                  channel_id:渠道ID 可选
     *                  channel_name:渠道名称 可选
     *                  search_name: 搜索名称
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  is_enabled: 是否开通 0:未审核； 1:审核通过
     *                  distribution_manages_id: 管理员 可选
     * @param $isAll
     * @return mixed
     */
    public static function getChannelList($params = [], $isAll = '')
    {
        return ChannelUser::getChannelUserList($params, $isAll);
    }


    /**
     * 获取公司对应的所有账户ID
     * @param $companyId
     * @return mixed
     */
    public static function getChannelUserIdListByCompany($companyId)
    {
        return ChannelUser::where('company_id', $companyId)->pluck('id');
    }

    /**
     * 获取渠道列表
     * @param $channelUserIds
     * @return mixed
     */
    public static function getChannelListByChannelUserIds($channelUserIds = [])
    {
        $search_object = Channel::select([
            'distribution_channels.id',
            'distribution_channels.name',
            'distribution_channels.pay_merchant_id',
            'distribution_channels.phone',
            'distribution_channels.nickname',
            'distribution_channels.person_in_charge_name',
            'distribution_channels.latest_login_time',
            'distribution_channels.remark',
            'distribution_channels.latest_login_ip',
            'distribution_channels.password',
            'distribution_channels.register_ip',
            'distribution_channels.is_enabled',
            'distribution_channels.distribution_manages_id',
            'distribution_channels.created_at',
            'distribution_channels.channel_user_id',
            'distribution_manages.account as distribution_manages_account',
            'distribution_manages.number as distribution_manages_number',
            'distribution_manages.nickname as distribution_manages_nickname',
        ])
            ->leftjoin('distribution_manages', 'distribution_manages.id', '=', 'distribution_channels.distribution_manages_id')
            ->orderBy('distribution_channels.id', 'desc')
            ->orderBy('distribution_channels.created_at', 'desc');
        return $search_object->whereIn('distribution_channels.channel_user_id', $channelUserIds)->get();
    }
}