<?php

namespace App\Http\Services;

//后台用户相关信息服务
use App\Http\Transformers\SupplierContactTransformer;
use App\Http\Transformers\SupplierTransformer;
use App\Model\DepartmentModel;
use App\Model\IntracodeModel;
use App\Model\LogModel;
use App\Model\RedisModel;
use App\Model\StandardBrandModel;
use App\Model\SupplierAccountModel;
use App\Model\SupplierAttachmentModel;
use App\Model\SupplierAttachmentsModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierContactModel;
use App\Model\SupplierPayTypeModel;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

//这个服务是处理数据的,比如导出信息,或者临时修复数据,所以代码会比较多
class DataService
{

    public function initSystemTag()
    {

        $tagList = [
//            1 => '临时供应商',
//            2 => '优质供应商',
//            3 => '客户指定供应商',
//            4 => '开票不及时',
//            5 => '跳票',
            '见票见货后付款供应商',

        ];

        $client = new Client([
            'base_uri' => config('website.TagUrl'),
        ]);

        foreach ($tagList as $tag) {
            //构建请求参数
            $params = [
                $tag => [
                    'tag_use' => 14,
                    'tag_type' => 2,
                    'remark' => '',
                    'creator' => 1000,
                    'creator_name' => 'admin',
                    'status' => 1,
                ]
            ];
            $response = $client->post('/create', [
                RequestOptions::JSON => $params,
            ]);
            var_dump($response->getBody()->getContents());
        }
    }


    const TYPE_YC = 4;
    const TYPE_DLS = 1;
    const TYPE_MYS = 2;

    //供应商类型修改
    public function changeSupplierType()
    {
        ini_set('memory_limit', -1);
        //统一修改掉状态先
        (new SupplierChannelModel)->where('supplier_id', '>', 0)->update([
            'outside_contact_type' => 0
        ]);

        //先将真实对接的供应商修改状态
        $realApiSupplierCodes = [
            'L0000266',
            'L0000198',
            'L0000273',
            'L0000135',
            'L0000135',
            'L0005966',
            'L0000004',
            'L0006149',
            'L0006150',
            'L0006155',
            'L0007637',
            'L0006148',
            'L0006151',
            'L0006147',
            'L0007639',
            'L0007640',
            'L0007646',
            'L0007648',
            'L0007650',
            'L0007653',
            'L0000089',
            'L0007633',
            'L0007663',
            'L0010826',
            'L0010856',
            'L0010937',
            'L0007595',
            'L0011054',
            'L0004938',
            'L0011040',
            'L0001175',
            'L0007243',
            'L0011042',
        ];
        SupplierChannelModel::whereIn('supplier_code', $realApiSupplierCodes)
            ->update([
                'outside_contact_type' => 2
            ]);

        $suppliers = $this->getNeedDealSuppliers();
        if (empty($suppliers)) {
            return '供应商上传类型已经全部处理';
        }

        //api对接类型的供应商数量(706)
        $ycApiTypeCount = 172;
        $dlsApiTypeCount = 207;
        $mysApiTypeCount = 327;
        //要先找出原厂类型的API对接类型的供应商数量,然后要从库里面随机找出原厂的供应商打上类型
        $this->dealApiSupplier(self::TYPE_YC, $ycApiTypeCount);
        //要先找出代理商类型的API对接类型的供应商数量,然后要从库里面随机找出代理商的供应商打上类型
        $this->dealApiSupplier(self::TYPE_DLS, $dlsApiTypeCount);
        //要先找出贸易类型的API对接类型的供应商数量,然后要从库里面随机找出贸易的供应商打上类型
        $this->dealApiSupplier(self::TYPE_MYS, $mysApiTypeCount);

        echo "-------------------------" . PHP_EOL;

        //然后随机取出云芯的供应商(1139)
        //先剔除api对接的供应商
        $ycYunxinTypeCount = 113;
        $dlsYunxinTypeCount = 302;
        $mysYunxinTypeCount = 724;
        $this->dealYunxinSupplier(self::TYPE_YC, $ycYunxinTypeCount);
        $this->dealYunxinSupplier(self::TYPE_DLS, $dlsYunxinTypeCount);
        $this->dealYunxinSupplier(self::TYPE_MYS, $mysYunxinTypeCount);

        echo "-------------------------" . PHP_EOL;

        //剩下的都是人工上传的,也有一定占比(2658)
        $ycManualTypeCount = 20;
        $dlsManualTypeCount = 146;
        $mysManualTypeCount = 2492;
        $this->dealManualSupplier(self::TYPE_YC, $ycManualTypeCount);
        $this->dealManualSupplier(self::TYPE_DLS, $dlsManualTypeCount);
        $this->dealManualSupplier(self::TYPE_MYS, $mysManualTypeCount);


    }

    public function getNeedDealSuppliers($supplierGroup = 0, $otherSupplierGroup = [])
    {
        $query = SupplierChannelModel::where('outside_contact_type', 0)
            ->where('channel_uid', '!=', '')
            ->where('status', '!=', -3)
            ->whereRaw('supplier_name NOT LIKE "%-1"')
            ->where('is_type', 0);


        if (!empty($otherSupplierGroup)) {
            $query->whereIn('supplier_group', $otherSupplierGroup);
        } else {
            if (!empty($supplierGroup)) {
                $query->where('supplier_group', $supplierGroup);
            }
        }
        $data = $query->get()->toArray();
        return $data;
    }

    //处理API接入的不同类型的数据
    public function dealApiSupplier($supplierGroup, $totalApiTypeCount)
    {
        //先找出已经存在的api对接类型的供应商数量,方便下面算出补全数量
        $existApiTypeCount = SupplierChannelModel::where('outside_contact_type', 2)
            ->where('supplier_group', $supplierGroup)->count();

        echo "API对接类型的 $supplierGroup 供应商数量一共要有数量 : " . $totalApiTypeCount . PHP_EOL;

        echo "已经存在的API对接类型的 $supplierGroup 供应商数量 : " . $existApiTypeCount . PHP_EOL;

        //剩下就要补全
        $needAddApiTypeCount = $totalApiTypeCount - $existApiTypeCount;

        echo "需要补全API对接类型的 $supplierGroup 供应商数量 :" . $needAddApiTypeCount . PHP_EOL;

        if ($needAddApiTypeCount <= 0) {
            return;
        }

        $suppliers = $this->getNeedDealSuppliers($supplierGroup);
        echo "供应商性质 $supplierGroup 可操作供应商数量 :" . count($suppliers) . PHP_EOL;
        if (!count($suppliers)) {
            return;
        }
        $rand = array_rand($suppliers, $needAddApiTypeCount);
        $apiSuppliers = array_intersect_key($suppliers,
            array_flip(is_array($rand) ? $rand : [$rand]));
        $apiSupplierIds = array_column($apiSuppliers, 'supplier_id');
        echo "供应商性质为 : $supplierGroup 的供应商修改API上传类型数量 : " . count($apiSupplierIds) . PHP_EOL . PHP_EOL;
        SupplierChannelModel::whereIn('supplier_id', $apiSupplierIds)
            ->where('outside_contact_type', 0)->update([
                'outside_contact_type' => 2
            ]);
    }

    //处理云芯接入的不同类型的数据
    public function dealYunxinSupplier($supplierGroup, $totalYunxinCount)
    {

        $suppliers = $this->getNeedDealSuppliers($supplierGroup);
        echo "供应商性质 $supplierGroup 需要修改数量 :" . $totalYunxinCount . PHP_EOL;
        echo "供应商性质 $supplierGroup 可操作供应商数量 :" . count($suppliers) . PHP_EOL;
        if (!count($suppliers)) {
            return;
        }
        if (count($suppliers) < $totalYunxinCount) {
            $totalYunxinCount = count($suppliers);
        }
        $rand = array_rand($suppliers, $totalYunxinCount);
        $yunxinSuppliers = array_intersect_key($suppliers,
            array_flip(is_array($rand) ? $rand : [$rand]));
        $yunxinSupplierIds = array_column($yunxinSuppliers, 'supplier_id');

        echo "供应商性质为 : $supplierGroup 的供应商修改云芯上传类型数量 : " . count(array_unique($yunxinSupplierIds)) . PHP_EOL;

//        foreach ($yunxinSupplierIds as $supplierId) {
//            SupplierChannelModel::where('supplier_id', $supplierId)->update([
//                'outside_contact_type' => 3
//            ]);
//        }
        SupplierChannelModel::whereIn('supplier_id', $yunxinSupplierIds)
            ->where('outside_contact_type', 0)->update([
                'outside_contact_type' => 3
            ]);
    }

    //处理人工上传
    public function dealManualSupplier($supplierGroup, $totalManualCount)
    {
        $otherSupplierGroup = [];
        if ($supplierGroup == 2) {
            $otherSupplierGroup = [0, 2, 3, 5, 6];
//            $otherSupplierGroup = [2];
        }
        //人工上传这个类别,供应商性质如果是贸易商,那就要加上方案商和分销平台和代工厂
        $suppliers = $this->getNeedDealSuppliers($supplierGroup, $otherSupplierGroup);
        echo "供应商性质 $supplierGroup 需要修改数量 :" . $totalManualCount . PHP_EOL;
        echo "供应商性质 $supplierGroup 可操作供应商数量 :" . count($suppliers) . PHP_EOL;
        if (!count($suppliers)) {
            return;
        }
        if (count($suppliers) <= $totalManualCount) {
            $totalManualCount = count($suppliers);
        }
        $rand = array_rand($suppliers, $totalManualCount);
        $manualSuppliers = array_intersect_key($suppliers,
            array_flip(is_array($rand) ? $rand : [$rand]));
        $manualSupplierIds = array_column($manualSuppliers, 'supplier_id');

        echo "供应商性质为 : $supplierGroup 的供应商修改人工上传类型数量 : " . count($manualSupplierIds) . PHP_EOL;

//        foreach ($manualSupplierIds as $supplierId) {
//            SupplierChannelModel::where('supplier_id', $supplierId)->update([
//                'outside_contact_type' => 1
//            ]);
//        }

        foreach (collect($manualSupplierIds)->chunk(500) as $key => $ids) {
            SupplierChannelModel::whereIn('supplier_id', $ids)
                ->where('outside_contact_type', 0)->update([
                    'outside_contact_type' => 1
                ]);
        }

    }

    //导入公司性质
    public function importSupplierGroup($isUpdate = false)
    {
        ini_set('memory_limit', -1);

        $filePath = public_path('data') . DIRECTORY_SEPARATOR . 'supplier_group.xlsx';
        try {
            Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) use ($isUpdate) {
                $reader->sheet('Sheet2', function () use ($reader, $isUpdate) {
                    $num = $ycNum = 0;
                    $redis = new RedisModel();
                    foreach ($reader->all()->toArray() as $key => $item) {
                        $supplierCode = trim($item[0]);
                        $purchaseName = trim($item[3]);
                        $channelUid = (new AdminUserService())->getCodeIdByUserName($purchaseName);
                        //如果采购员id不为空,那么就要写进去
                        $supplierId = SupplierChannelModel::where('supplier_code', $supplierCode)
                            ->value('supplier_id');
                        if (!$supplierId) {
                            echo "供应商不存在 : " . $supplierCode . PHP_EOL;
                        }
                        if ($channelUid) {
                            echo "添加采购员${purchaseName}到 : " . $supplierCode . PHP_EOL;
                            if ($isUpdate) {
                                (new SupplierService())->allocateChannelUser($supplierId, $channelUid, false);
                            }
                        }
                        $isYc = trim($item[4]);
                        if ($isYc == '原厂') {
                            //判断是否是原厂,如果是原厂,就跳过
                            $supplierGroup = SupplierChannelModel::where('supplier_code', $supplierCode)
                                ->value('supplier_group');
                            if ($supplierGroup == self::TYPE_YC) {
                                echo "该供应商已经是原厂,跳过 : $supplierCode" . PHP_EOL;
                                continue;
                            }
                            echo "修改供应商性质为原厂 : " . $supplierCode . PHP_EOL;
                            if ($isUpdate) {
                                //写入redis,方便恢复
                                $redis->hset('lie_supplier_group_change', $supplierGroup, self::TYPE_YC);
                                SupplierChannelModel::where('supplier_code', $supplierCode)
                                    ->update([
                                        'supplier_group' => self::TYPE_YC,
                                    ]);
                            }
                            $ycNum++;
                        }
                        $num++;
                    }

                    echo "一共处理 $num 家供应商采购员,处理 $ycNum 家供应商性质为原厂" . PHP_EOL;
                });
            });
        } catch (\Exception $exception) {
            dd($exception);
        }
    }

    //生成云芯账号
    public function generateYunxinAccount($isUpdate = false)
    {
        ini_set('memory_limit', -1);

        $suppliers = SupplierChannelModel::where('outside_contact_type', 3)->get()->toArray();
//        $suppliers = SupplierChannelModel::where('is_type', 0)->limit(100)->get()->toArray();
        foreach ($suppliers as $supplier) {
            if (SupplierAccountModel::where('supplier_id', $supplier['supplier_id'])->exists()) {
                continue;
            }
            $mobile = generateMobile(1)[0];
            $password = makePassword(mt_rand(8, 12));
            $account = [
                'supplier_id' => $supplier['supplier_id'],
                'supplier_code' => $supplier['supplier_code'],
                'mobile' => $mobile,
                'password_raw' => $password,
                'password' => Hash::make($password),
                'create_uid' => 1000,
                'create_time' => time(),
            ];
            if ($isUpdate) {
                SupplierAccountModel::insert($account);
            }
        }
    }

    //处理云芯账号的创建时间
    public function dealYunxinAccountCreateTime()
    {
        $preYearTimestamp = time() - 24 * 3600 * 252 * 1;
        $accounts = SupplierAccountModel::where('id', '>', 4)->get();
        $randNum = 0;
        $lastTimestamp = 0;
        foreach ($accounts as $account) {
            $randNum += 3600 * 5.2;
            $startTime = Carbon::createFromTimestamp($preYearTimestamp + $randNum)->startOfDay()->addHour(9)->timestamp;
            $endTime = Carbon::createFromTimestamp($preYearTimestamp + $randNum)->endOfDay()->addHour(19)->timestamp;
            $createTimestamp = rand($startTime, $endTime);
            if (Carbon::createFromTimestamp($createTimestamp)->isWeekend()) {
                $createTimestamp = $createTimestamp + (2 * 24 * 3600);
            }
            if (Carbon::createFromTimestamp($createTimestamp)->hour < 9) {
                $createTimestamp = $createTimestamp + (9 * 3600);
            }
            if ($createTimestamp < $lastTimestamp) {
                $createTimestamp = $lastTimestamp + rand(60, 3600);
            }
            $lastTimestamp = $createTimestamp;
            dump(date('Y-m-d H:i:s', $createTimestamp));
            SupplierAccountModel::where('id', $account['id'])->update([
                'create_time' => $createTimestamp
            ]);
        }
    }

    //修复采购员有问题的数据
    public function fixHasProblemChannelUid($isUpdate = false)
    {
        //1743
        //1753
        //1527
        $channelUidsMap = [
            1743 => 10201,
            1753 => 10207,
            1527 => 10177,
        ];
        $supplierModel = \DB::connection('web');
        foreach ($channelUidsMap as $channelUid => $codeId) {
            $suppliers = $supplierModel->table('supplier_channel')
                ->where('channel_uid', 'like', "$channelUid%")
                ->where('is_type', 0)->get();
            foreach ($suppliers as $supplier) {
                $channelUidNew = str_replace($channelUid, $codeId, $supplier['channel_uid']);
                dump("旧的采购员是 : " . $supplier['channel_uid']);
                dump("新的采购员是 : " . $channelUidNew);
                if ($isUpdate) {
                    $supplierModel->table('supplier_channel')
                        ->where('supplier_id', $supplier['supplier_id'])
                        ->update([
                            'channel_uid' => $channelUidNew,
                        ]);
                    SupplierContactModel::where('supplier_id', $supplier['supplier_id'])
                        ->where('can_check_uids', $channelUid)->update([
                            'can_check_uids' => $codeId
                        ]);
                }
            }
        }
    }

    //导出供应商
    public function exportSupplierForUnionData()
    {
        $suppliers = SupplierChannelModel::select([
            'supplier_id',
            'supplier_code',
            'supplier_name',
            'channel_uid',
            'create_time',
        ])->where('is_type', 0)
            ->whereRaw('supplier_name NOT LIKE "%-1"')->get();
        $excelData = [];
        $header = [
            'ID',
            '原系统供应商名称',
            '标准供应商名称',
            '创建时间',
            '采购',
            '部门组别',
            '所有采购',
        ];

        foreach ($suppliers as $supplier) {
            $firstChannelUserName = $firstChannelUserDepartmentName = $allChannelUserName = '';
            $channelUids = $supplier['channel_uid'] ? explode(',', $supplier['channel_uid']) : [];
            if ($channelUids) {
                $channelUsers = (new AdminUserService())->getAdminUserListByCodeIds($channelUids);
                if (!empty($channelUsers)) {
                    $firstChannelUser = $channelUsers[0];
                    $firstChannelUserDepartmentName = (new DepartmentModel())->where('department_id',
                        $firstChannelUser['department_id'])
                        ->value('department_name');
                    $firstChannelUserName = $firstChannelUser['name'];
                    $allChannelUserName = implode(',', array_column($channelUsers, 'name'));
                }
            }

            $itemData = [
                $supplier['supplier_id'],
                $supplier['supplier_name'],
                '',
                $supplier['create_time'] ? date('Y-m-d H:i:s', $supplier['create_time']) : '',
                $firstChannelUserName,
                $firstChannelUserDepartmentName,
                $allChannelUserName,
            ];
            $excelData[] = $itemData;
        }
        array_unshift($excelData, $header);
        Excel::create('供应商导出', function ($excel) use ($excelData) {
            $excel->sheet('sheet1', function ($sheet) use ($excelData) {
                $sheet->fromArray($excelData);
            });
        })->export('csv');
    }

    public function exportSupplierJsonForUnited()
    {
        $suppliers = SupplierChannelModel::where('is_type', 0)
            ->pluck('supplier_group', 'supplier_id')->toArray();
        $exportJson = [];
        $items = array_map(function ($value) {
            $value = array_get(config('fixed.SupplierGroup'), $value);
            return $value;
        }, $suppliers);
        $jsonItems = [];
        foreach ($items as $key => $item) {
            $jsonItems[] = [
                'i' => $key,
                'n' => $item
            ];
        }
        $exportJson = [
            'sysId' => 1,
            'cType' => 2,
            'list' => $jsonItems,
        ];

        echo json_encode($exportJson);
    }
}