<?php

namespace App\Http\Services\V1_1;

use App\Generated\V1_1\DTOs\AdminPartnerDTO;
use App\Generated\V1_1\DTOs\PageDTO;
use App\Generated\V1_1\Messages\AdminPartner\UnSelectPartnerMessage;
use App\Generated\V1_1\Messages\AdminPartner\Change2NewMessage;
use App\Generated\V1_1\Messages\AdminPartner\LowListMessage;
use App\Generated\V1_1\Messages\AdminPartner\IsSelectedListMessage;
use App\Models\PartnerModel;
use Illuminate\Database\Eloquent\Builder;

class AdminPartnerService
{
    public function isSelectedList(IsSelectedListMessage $message)
    {
        $isFeedPush = $message->getIsFeedPush();
        $sex = $message->getSex();
        $page = $message->getPages();
        $limit = 20;
        if (1 == $page) {
            $total = PartnerModel::where('is_select', 1)
                ->when($isFeedPush, function (/** @var Builder $query */ $query) {
                    return $query->where('is_sell', 1)->where('score', '>=', 0);
                })
                ->when($sex, function (/** @var Builder $query */ $query) use ($sex) {
                    return $query->where('sex', $sex);
                })
                ->where(function (/** @var Builder $query */ $query) {
                    return $query->where('check_photo', 1)->orWhere('photo_1_check', 1)
                        ->orWhere('photo_2_check', 1)->orWhere('photo_3_check', 1)
                        ->orWhere('photo_4_check', 1);
                })->count();
        } else {
            $total = 10000;
        }
        $datas = PartnerModel::where('is_select', 1)
            ->when($isFeedPush, function (/** @var Builder $query */ $query) {
                return $query->where('is_sell', 1)->where('score', '>=', 0);
            })
            ->when($sex, function (/** @var Builder $query */ $query) use ($sex) {
                return $query->where('sex', $sex);
            })
            ->where(function (/** @var \Illuminate\Database\Eloquent\Builder $query */ $query) {
                return $query->where('check_photo', 1)->orWhere('photo_1_check', 1)
                    ->orWhere('photo_2_check', 1)->orWhere('photo_3_check', 1)
                    ->orWhere('photo_4_check', 1);
            })->orderByDesc('select_at')->skip(($page - 1) * $limit)->take($limit)->get();
        foreach ($datas as &$data) {
            /** @var PartnerModel $data */
            if ($data->check_photo == 1) {
                $data->photo_src = "http://oss.pocketuniversity.cn{$data->photo_src}";
            } else {
                unset($data->photo_src);
            }
            if ($data->photo_1_check == 1) {
                $data->photo_1 = "http://oss.pocketuniversity.cn{$data->photo_1}";
            } else {
                unset($data->photo_1);
            }
            if ($data->photo_2_check == 1) {
                $data->photo_2 = "http://oss.pocketuniversity.cn{$data->photo_2}";
            } else {
                unset($data->photo_2);
            }
            if ($data->photo_3_check == 1) {
                $data->photo_3 = "http://oss.pocketuniversity.cn{$data->photo_3}";
            } else {
                unset($data->photo_3);
            }
            if ($data->photo_4_check == 1) {
                $data->photo_4 = "http://oss.pocketuniversity.cn{$data->photo_4}";
            } else {
                unset($data->photo_4);
            }
        }
        $message->setResponse(PageDTO::initFromEloquent([
            'page' => (int)$page,
            'limit' => $limit,
            'total' => $total
        ]), AdminPartnerDTO::initFromEloquents($datas));
    }

    public function lowList(LowListMessage $message)
    {
        $page = $message->getPages();
        $limit = 20;
        if (1 == $page) {
            $total = PartnerModel::where([
                ['feed_pool_level', 'low'],
                ['wait_low_pool', '!=', 2]
            ])->where(function (/** @var \Illuminate\Database\Eloquent\Builder $query */ $query) {
                return $query->where('check_photo', 1)->orWhere('photo_1_check', 1)
                    ->orWhere('photo_2_check', 1)->orWhere('photo_3_check', 1)
                    ->orWhere('photo_4_check', 1);
            })->count();
        } else {
            $total = 10000;
        }
        $datas = PartnerModel::where([
            ['feed_pool_level', 'low'],
            ['wait_low_pool', '!=', 2]
        ])->where(function (/** @var \Illuminate\Database\Eloquent\Builder $query */ $query) {
            return $query->where('check_photo', 1)->orWhere('photo_1_check', 1)
                ->orWhere('photo_2_check', 1)->orWhere('photo_3_check', 1)
                ->orWhere('photo_4_check', 1);
        })->orderByDesc('select_at')->skip(($page - 1) * $limit)->take($limit)->get();
        foreach ($datas as &$data) {
            /** @var PartnerModel $data */
            if ($data->check_photo == 1) {
                $data->photo_src = "http://oss.pocketuniversity.cn{$data->photo_src}";
            } else {
                unset($data->photo_src);
            }
            if ($data->photo_1_check == 1) {
                $data->photo_1 = "http://oss.pocketuniversity.cn{$data->photo_1}";
            } else {
                unset($data->photo_1);
            }
            if ($data->photo_2_check == 1) {
                $data->photo_2 = "http://oss.pocketuniversity.cn{$data->photo_2}";
            } else {
                unset($data->photo_2);
            }
            if ($data->photo_3_check == 1) {
                $data->photo_3 = "http://oss.pocketuniversity.cn{$data->photo_3}";
            } else {
                unset($data->photo_3);
            }
            if ($data->photo_4_check == 1) {
                $data->photo_4 = "http://oss.pocketuniversity.cn{$data->photo_4}";
            } else {
                unset($data->photo_4);
            }
        }

        $message->setResponse(PageDTO::initFromEloquent([
            'page' => (int)$page,
            'limit' => $limit,
            'total' => $total
        ]), AdminPartnerDTO::initFromEloquents($datas));
    }

    public function change2New(Change2NewMessage $message)
    {
        $reqs = $message->getReq();
        foreach ($reqs as $req) {
            if ($req->getToType()) {
                PartnerModel::where('id', $req->getPartnerId())->update([
                    'feed_tag_cnt' => 0,
                    'feed_tag_like' => 0,
                    'feed_pool_level' => 'new',
                    'wait_low_pool' => 0
                ]);
            } else {
                PartnerModel::where('id', $req->getPartnerId())->update([
                    'wait_low_pool' => 2
                ]);
            }
        }
    }

    public function unSelectPartner(UnSelectPartnerMessage $message)
    {
        $partnerId = $message->getPartnerId();
        PartnerModel::where('id', $partnerId)->update(['is_select' => 0]);
    }
}
