<?php

namespace App\Models\News;

use App\Helper\Arr;
use App\Models\Base;

class News extends Base
{
    const STATUS_DRAFT = 0;
    const STATUS_ONE = 1;

    protected $table = 'gl_news';
    //连接数据库
    protected $connection = 'custom_mysql';

    public static function getNumByProjectId($project_id){
        return self::where('project_id', $project_id)->where('status', 1)->count();
    }

    public function getReleaseAtAttribute($value){
        if(!$value){
            return date('Y-m-d H:i:s', strtotime($this->getAttribute('created_at')));
        }
        return $value;
    }

    public function getRelatedNewsIdAttribute($value){
        return Arr::setToArr($value);
    }

    public function getRelatedProductIdAttribute($value){
        return Arr::setToArr($value);
    }

    /**
     * @remark :扩展字段根据type返回类型
     * @name   :setTypValues
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 14:43
     */
    public function getExtendInfo($news_id){
        $extendModel = new NewsExtend();
        $list = $extendModel->list(['status'=>1],'id',['id','type','key','title']);
        if(empty($list)){
            return [];
        }
        $extendInfoModel = new NewsExtendInfo();
        $infoList = $extendInfoModel->list(['news_id'=>$news_id],'created_at');
        foreach ($list as $k=>$v){
            if($v['type'] == 3 || $v['type'] == 4){
                $v['values'] = [];
            }else{
                $v['values'] = '';
            }
            if(!empty($infoList)){
                foreach ($infoList as $values){
                    if($v['key'] == $values['key']){
                        $v = $this->setTypValues($v,$values);
                        break;
                    }
                }
            }
            $list[$k] = $v;
        }
        return $list;
    }


    /**
     * @remark :扩展字段根据type返回类型
     * @name   :setTypValues
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 14:43
     */
    public function setTypValues($v,$info){
        if($v['type'] == 3){
            $arr = json_decode($info['values']);
            foreach ($arr as $k1=>$v1){
                $v1 = (array)$v1;
                $v1['url'] = getImageUrl($v1['url']);
                $arr[$k1] = $v1;
            }
            $v['values'] = $arr;
        }elseif($v['type'] == 4){
            $arr1 = json_decode($info['values']);
            foreach ($arr1 as $k1=>$v1){
                $v1 = (array)$v1;
                if(isset($v1['url'])){
                    $v1['url'] = getFileUrl($v1['url']);
                }else{
                    $v1 = getFileUrl($v1);
                }
                $arr1[$k1] = $v1;
            }
            $v['values'] = $arr1;
        }else{
            $v['values'] = $info['values'];
        }
        return $v;
    }

    /**
     * @remark :保存扩展字段
     * @name   :saveExtend
     * @author :lyh
     * @method :post
     * @time   :2023/11/9 15:02
     */
    public function saveExtendInfo($news_id,$extend,$project_id){
        //先删除以前的数据
        $extendInfoModel = new NewsExtendInfo();
        $extendInfoModel->del(['news_id'=>$news_id]);
        if(empty($extend)) {
            return true;
        }
        foreach ($extend as $k => $v){
            if(empty($v['values'])){
                continue;
            }
            $v = $this->saveHandleExtend($v,$news_id,$project_id);
            $extendInfoModel->add($v);
        }
        return true;
    }

    /**
     * @remark :保存扩展字段时处理数据
     * @name   :saveHandleExtend
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 15:11
     */
    public function saveHandleExtend(&$v,$news_id,$project_id){
        unset($v['title']);
        if($v['type'] == 3){
            foreach ($v['values'] as $k1=>$v1){
                $v1['url'] = str_replace_url($v1['url']);
                $v['values'][$k1] = $v1;
            }
            $v['values'] = json_encode($v['values']);
        }elseif ($v['type'] == 4){
            foreach ($v['values'] as $k1=>$v1){
                $v1['url'] = str_replace_url($v1['url']);
                $v['values'][$k1] = $v1;
            }
            $v['values'] = json_encode($v['values']);
        }
        $v['project_id'] = $project_id;
        $v['news_id'] = $news_id;
        return $v;
    }
}