<?php

namespace app\admin\controller\AboutUs;

use app\BaseController;
use app\common\model\AboutUs\DevelopmentHistory as ModelDevelopmentHistory;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;


class DevelopmentHistory extends BaseController
{
    /**
     * 获取发展历程列表接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-24
     * @author xjh
     * @since 1.0.0
     */
    public function getDevelopmentHistoryList(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'sort|排序' => 'require',
        ]);
        $select = ModelDevelopmentHistory::field([
            'development_history_id',
            'development_history_guid',
            'development_history_year',
            'development_history_title',
            'development_history_content'
        ])
            ->order('development_history_year', $params['sort'])
            ->select();

        return msg(0, "获取发展历程列表成功!", [
            'count' => count($select),
            'data' => $select,
        ]);
    }

    /**
     * 编辑发展历程接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-24
     * @author xjh
     * @since 1.0.0
     */
    public function editDevelopmentHistory(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'development_history_year|年份' => 'require',
            'development_history_title|标题' => 'require',
            'development_history_content|内容' => 'require'
        ]);
        $model = ModelDevelopmentHistory::where('development_history_guid', $params['development_history_guid'])->find();
        if (!$model) throwErrorMsg("该发展历程不存在", 1);
        $model->allowField([
            'development_history_update_user_guid',
            'development_history_year',
            'development_history_title',
            'development_history_content'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加发展历程接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-24
     * @author xjh
     * @since 1.0.0
     */
    public function addDevelopmentHistory(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'development_history_year|年份' => 'require',
            'development_history_title|标题' => 'require',
            'development_history_content|内容' => 'require'
        ]);
        $model = ModelDevelopmentHistory::create($params, [
            'development_history_guid',
            'development_history_create_user_guid',
            'development_history_update_user_guid',
            'development_history_year',
            'development_history_title',
            'development_history_content'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除发展历程接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-24
     * @author xjh
     * @since 1.0.0
     */
    public function deleteDevelopmentHistory(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'development_history_guid' => 'require',
        ]);
        $development_history = ModelDevelopmentHistory::where([
            'development_history_guid' => explode(',', $params['development_history_guid'])
        ])->select();
        $development_history->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-24
     * @author xjh
     * @since 1.0.0
     */
    public function exportExcel(Request $request): void
    {
        $params = $request->param();
        $this->validate($params, [
            'sort|排序' => 'require',
        ]);
        $select = ModelDevelopmentHistory::field([
            'development_history_year',
            'development_history_title',
            'development_history_content'
        ])
            ->order('development_history_year', $params['sort'])
            ->select()->toArray();

        ModelDevelopmentHistory::exportExcel($select);
    }

    /**
     * 下载导入模板接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-24
     * @author xjh
     * @since 1.0.0
     */
    public function downloadTemplate(Request $request): void
    {
        $data = [
            array_values(ModelDevelopmentHistory::EXCELFIELD),
            [date('Y'), '公司成立', '公司成立']
        ];
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('发展历程导入模板.xlsx');
    }

    /**
     * 导入excel接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-24
     * @author xjh
     * @since 1.0.0
     */
    public function importExcel(Request $request): array
    {
        $file = new UploadFile('uploads', 'fileExt:xlsx');
        $file->putFile('development_history');

        $msg = ModelDevelopmentHistory::importExcel($file);
        return msg($msg);
    }
}
