<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/10 0010
 * Time: 10:20
 */

namespace App\Http\Logics\Admin;

use Illuminate\Support\Facades\DB;


//    +"id": 95
//    +"code": "R2017-028"
//    +"name": "广州海山娱乐科技有限公司"
//    +"thumb": "/uploads/image/20180123/19/11bf2157ed922de5110a9279d65047af.jpg"
//    +"domain": "hswaterslide.com"
//    +"webmaster_domain": null
//    +"cloud_ip": ""
//    +"company_name": "广州海山娱乐科技有限公司"
//    +"company_type": "中小型企业"
//    +"company_addr": ""
//    +"area_id": 440100
//    +"area_text": "广东省 > 广州市"
//    +"province": null
//    +"city": null
//    +"trade": "其他行业"
//    +"linkman": ""
//    +"phone": ""
//    +"remark": ""
//    +"rank": 36
//    +"is_enabled": 0
//    +"reach_num": 20
//    +"reach_days": 620
//    +"status_text": "实施期"
//    +"google_domain": "google.com"
//    +"allow_googlerank": 1
//    +"allow_allintitle": 0
//    +"sign_time": 0
//    +"start_time": 1462550400
//    +"sync_time": 1539765018
//    +"reach_time": 1470672000
//    +"expire_time": 1557158400
//    +"create_time": 1516169486
//    +"number": 1
//    +"report_day": 0

class SiteSyncLogic
{

    public function insertGetId(array $data)
    {
        return DB::connection('rank')->table('project')->insertGetId($data + ['create_time' => time()]);
    }

    public function update($oldId, array $data)
    {
        if ($oldId) { //如果有关联的则更新
            DB::connection('rank')->table('project')->where(['id' => $oldId])->update($data);
        }
    }

//    public function updateOrInsert($attributes, $values)
//    {
//        DB::connection('rank')->table('project')->updateOrInsert($attributes, $values);
//    }

    /**
     *
     * @param  $ids
     */
    public function delete($ids)
    {
        $filter = is_array($ids) ? $ids : [$ids];
        DB::connection('rank')->table('project')->whereIn('id', $filter)->update([
            'is_enabled' => 0,
            'allow_googlerank' => 0
        ]);
    }

    /**
     * 新老平台数据映射
     * @param array $origins
     * @return array
     */
    public function fieldMap(array $origins)
    {
//        $map = [
//            'id' => 'id',
//            'code' => 'identifier',
//            'name' => 'cn_title',
//            'thumb' => '',
//            'domain' => 'domain',
//            'webmaster_domain' => '',//
//            'cloud_ip' => '',//
//            'company_name' => '',
//            'company_addr' => '',
//            'area_id' => '',
//            'area_text' => '',
//            'province' => '',
//            'city' => '',
//            'trade' => '',
//            'linkman' => '',
//            'phone' => '',
//            'remark' => '',
//            'rank' => '',
//            'is_enabled' => '',
//            'reach_num' => '',
//            'reach_days' => '',
//            'status_text' => '',
//            'google_domain' => '',
//            'allow_googlerank' => '',
//            'allow_allintitle' => '',
//            'sign_time' => '',
//            'start_time' => '',
//            'sync_time' => '',
//            'reach_time' => '',
//            'expire_time' => '',
//            'create_time' => '',
//            'number' => 1,
//            'report_day' => ''
//        ];
        $map = [
//            'id' => 'id',
            'identifier' => 'code',
            'cn_title' => 'name',
            'domain' => 'domain',
            'report_day' => 'report_day',
            'webmaster_domain' => 'webmaster_domain',
            'server_ip' => 'cloud_ip',
            'keyword_goal' => 'reach_num',
            'online_at' => 'start_time'
//            'cloud_type' => 'number'
        ];
        $result = [];
        foreach ($origins as $key => $item) {
            if (empty($map[$key]))
                continue;
            if (empty($item))
                continue;


            $result[$map[$key]] = $item;
            if (in_array($key, ['online_at'])) {
                $result[$map[$key]] = strtotime($item);
            }

        }
        return $result;
    }


}