<?php
/**
 * ${CARET}
 * @file:CpListController.php
 * @Created by gnitif
 * @Date: 2023/3/20
 * @Time: 16:25
 */


namespace Modules\CpManage\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Catch\Exceptions\FailedException;
use Illuminate\Http\Request;
use Modules\CpManage\Http\Requests\CpRequest;
use Modules\CpManage\Models\Cp\CpCollection;
use Modules\CpManage\Models\Cp\Cps;
use Modules\CpManage\Services\CpManage\CpService;

class CpListController extends Controller
{
    /**
     * @param Cps $model
     */
    public function __construct(protected readonly Cps $model,protected readonly CpCollection $cpCollectionModel)
    {

    }

    /**
     * cp列表
     * name: index
     * @param CpRequest $request
     * @return mixed
     * date 2023/03/20 16:26
     */
    public function index(Request $request)
    {
        $where =[ ['is_deleted','=',0]];
        if (!empty($request->get("cp_name", ''))){
            $where[] = ['cp_name','like',"%".$request->get("cp_name", ''). "%"];
        }
        if (!empty($request->get("cp_company", ''))) {
            $where[] = ['cp_company','like',"%".$request->get("cp_company", ''). "%"];
        }

        return  CpService::getCpList($where,$request->get('limit',15));
    }

    /**
     *  添加
     * name: store
     * @param CpRequest $request
     * @return mixed
     * date 2023/03/23 13:50
     */
    public function store(CpRequest $request)
    {
        $params = $request->all();
        return $this->model->storeBy($params);
    }

    /**
     *  获取cp信息
     * name: show
     * @param $id
     * @return mixed
     * date 2023/03/23 13:50
     */
    public function show($id)
    {

        $cp =  $this->model->where('cp_id', $id)->first();
        // [$cp['share_per_before'],$cp['share_per_after']] = explode(':',$cp['share_per']);
        return $cp;
    }

    /**
     *  编辑
     * name: update
     * @param $id
     * @param CpRequest $request
     * @return mixed
     * date 2023/03/23 13:51
     */
    public function update($id, CpRequest $request)
    {
        $params = $request->all();
        // $params['share_per'] = $params['share_per_before'] .":" .$params['share_per_after'];
        if(isset($params['cp_name'])){
            unset($params['cp_name']);
        }
        return $this->model->updateBy($id, $params);
    }

    /**
     *  删除
     * name: destroy
     * @param $id
     * date 2023/03/23 13:51
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }

    /**
     * 获取cp现在选
     * name: selectOptions
     * @param Request $request
     * @return mixed
     * date 2023/03/23 13:51
     */
    public function selectOptions(Request $request)
    {
        $cpName = $request->input('cp_name','');
        return CpService::selectOptions($cpName);

    }


    /**
     * 采集
     * @param Request $request
     * @return void
     */
    public function cpCollection(Request $request){
        $cpName = $request->get('cp_name');
        $cpId = $request->get('cp_id');

        if(empty($cpName) || empty($cpId) ){
            throw new FailedException('缺少参数');
        }


        $status = $this->cpCollectionModel->where('cp_id',$cpId)->whereIn('spider_status',[0,1])->count();
        if($status >0){
            throw new FailedException('正在采集中');
        }

        $this->cpCollectionModel->create(['cp_id'=>$cpId,'cp_name'=>$cpName,'spider_status'=>0,'spider_result'=>'']);

        return [];

    }

}
