<?php

namespace app\admin\service\document;

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

class BannerService extends AdminBaseService
{

    public function index()
    {
        try {
            $rows = [];
            $list = BannerModel::where('is_delete', BannerModel::IS_DELETE_NO)
                ->field('id,lang,path,name,title,status')
                ->order('name', 'asc')
                ->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'] = BannerModel::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(empty($param['path']) || !is_string($param['path'])){
                return $this->toData('1', '图片路径无效');
            }


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

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

            $doc = new BannerModel();
            $doc->lang = $param['lang'];
            $doc->path = $param['path'];
            $doc->title = $param['title'];
            $doc->content = $param['content'];
            $doc->status = $param['status'];
            $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 = BannerModel::where('id', $param['id'])
                ->where('is_delete', BannerModel::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 = BannerModel::where('id', $param['id'])
                ->where('is_delete', BannerModel::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(empty($param['path']) || !is_string($param['path'])){
                return $this->toData('1', '图片路径无效');
            }

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

            $docExists = BannerModel::where('is_delete', BannerModel::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->path = $param['path'];
            $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 = BannerModel::where('id', $param['id'])
                ->where('is_delete', BannerModel::IS_DELETE_NO)
                ->find();
            if(empty($doc)){
                return $this->toData('1', '目标不存在');
            }

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

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