<?php

namespace app\admin\service\document;

use app\admin\service\AdminBaseService;
use app\model\FaqModel;
use app\model\LanguageSettingModel;

class FaqService extends AdminBaseService
{

    public function index()
    {
        try {
            $rows = [];
            $list = FaqModel::where('is_delete', FaqModel::IS_DELETE_NO)
                ->field('id,lang,name,title,status,weight')
                ->order('name', 'asc')
                ->order('weight', 'desc')->select();
            if(!$list->isEmpty()){
                $rows = $list->toArray();
                $lang = LanguageSettingModel::where('id', '>', 0)->column('china_name', 'id');
                foreach ($rows as $key=>$item){
                    $rows[$key]['lang'] = $lang[$item['lang']] ?? '-';
                    $rows[$key]['status'] = FaqModel::STATUS_MAP[$item['status']] ?? '-';
                }
            }

            return $this->toData('0', 'SUCCESS', ['list' => $rows, 'total' => count($rows)]);
        }catch (\Exception $exception){
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }

    public function add($param)
    {
        try {
            $langIds = LanguageSettingModel::column('id');
            if(empty($param['lang']) || !in_array($param['lang'], $langIds)){
                return $this->toData('1', '语言类型无效');
            }

            if(empty($param['name']) || !is_string($param['name'])){
                return $this->toData('1', '名称无效');
            }

            if(empty($param['title']) || !is_string($param['title'])){
                return $this->toData('1', '标题无效');
            }

            if(empty($param['content']) || !is_string($param['content'])){
                return $this->toData('1', '内容无效');
            }

            if(!isset($param['weight']) || !is_numeric($param['weight']) || ceil($param['weight']) != $param['weight'] || $param['weight'] < 0){
                return $this->toData('1', '权重无效');
            }

            if(!isset($param['status']) || !in_array($param['status'], [1,2])){
                return $this->toData('1', '状态无效');
            }

            $docExists = FaqModel::where('is_delete', FaqModel::IS_DELETE_NO)
                ->where('name', $param['name'])
                ->where('lang', $param['lang'])->find();
            if(!empty($docExists)){
                return $this->toData('1', '已存在相同内容');
            }

            $doc = new FaqModel;
            $doc->lang = $param['lang'];
            $doc->title = $param['title'];
            $doc->content = $param['content'];
            $doc->status = $param['status'];
            $doc->weight = $param['weight'];
            $doc->create_time = date('Y-m-d H:i:s');
            $doc->update_time = date('Y-m-d H:i:s');
            $doc->name = $param['name'];
            $doc->save();

            return $this->toData('0', 'SUCCESS');
        }catch (\Exception $exception){
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }

    public function detail($param)
    {
        try {
            if(empty($param['id']) || !is_numeric($param['id'])){
                return $this->toData('1', '目标不存在');
            }

            $doc = FaqModel::where('id', $param['id'])
                ->where('is_delete', FaqModel::IS_DELETE_NO)
                ->find();
            if(empty($doc)){
                return $this->toData('1', '目标不存在');
            }

            $docArr = $doc->toArray();

            return $this->toData('0', 'SUCCESS', ['row' => $docArr,]);

        }catch (\Exception $exception){
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }

    public function edit($param)
    {
        try {
            if(empty($param['id']) || !is_numeric($param['id'])){
                return $this->toData('1', '目标不存在');
            }

            $doc = FaqModel::where('id', $param['id'])
                ->where('is_delete', FaqModel::IS_DELETE_NO)
                ->find();
            if(empty($doc)){
                return $this->toData('1', '目标不存在');
            }


            $langIds = LanguageSettingModel::column('id');
            if(empty($param['lang']) || !in_array($param['lang'], $langIds)){
                return $this->toData('1', '语言类型无效');
            }

            if(empty($param['name']) || !is_string($param['name'])){
                return $this->toData('1', '名称无效');
            }

            if(empty($param['title']) || !is_string($param['title'])){
                return $this->toData('1', '标题无效');
            }

            if(empty($param['content']) || !is_string($param['content'])){
                return $this->toData('1', '内容无效');
            }

            if(!isset($param['weight']) || !is_numeric($param['weight']) || ceil($param['weight']) != $param['weight'] || $param['weight'] < 0){
                return $this->toData('1', '权重无效');
            }

            if(!isset($param['status']) || !in_array($param['status'], [1,2])){
                return $this->toData('1', '状态无效');
            }

            $docExists = FaqModel::where('is_delete', FaqModel::IS_DELETE_NO)
                ->where('id', '<>', $param['id'])
                ->where('name', $param['name'])
                ->where('lang', $param['lang'])->find();
            if(!empty($docExists)){
                return $this->toData('1', '已存在相同内容');
            }

            $doc->lang = $param['lang'];
            $doc->title = $param['title'];
            $doc->content = $param['content'];
            $doc->status = $param['status'];
            $doc->weight = $param['weight'];
            $doc->update_time = date('Y-m-d H:i:s');
            $doc->name = $param['name'];
            $doc->save();

            return $this->toData('0', 'SUCCESS');
        }catch (\Exception $exception){
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }

    public function del($param)
    {
        try {
            if(empty($param['id']) || !is_numeric($param['id'])){
                return $this->toData('1', '目标不存在');
            }

            $doc = FaqModel::where('id', $param['id'])
                ->where('is_delete', FaqModel::IS_DELETE_NO)
                ->find();
            if(empty($doc)){
                return $this->toData('1', '目标不存在');
            }

            $doc->is_delete = FaqModel::IS_DELETE_YES;
            $doc->save();

            return $this->toData('0', 'SUCCESS');
        }catch (\Exception $exception){
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }
}