<?php

namespace app\admin\controller\AboutUs;

use app\BaseController;
use app\common\model\AboutUs\CompanyProfile as ModelCompanyProfile;
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 CompanyProfile extends BaseController
{
    /**
     * 获取公司简介列表接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-19
     * @author xjh
     * @since 1.0.0
     */
    public function getCompanyProfileList(Request $request): array
    {
        $con = Tool::getOptionalQuery(
            ['company_profile_content', 'LIKE'],
            ['company_profile_order']
        );
        $query = ModelCompanyProfile::where($con)
            ->field([
                'company_profile_id',
                'company_profile_guid',
                'company_profile_img',
                'company_profile_color',
                'company_profile_content',
                'company_profile_order'
            ])
            ->order('company_profile_order', 'asc');

        return msg("获取公司简介列表成功!", $query);
    }

    /**
     * 编辑公司简介接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-19
     * @author xjh
     * @since 1.0.0
     */
    public function editCompanyProfile(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'company_profile_img|简介图片' => 'require',
                'company_profile_content|简介内容' => 'require',
                'company_profile_order|简介排序' => 'require'
            ]);
            $model = ModelCompanyProfile::where('company_profile_guid', $params['company_profile_guid'])->find();
            if (!$model) throwErrorMsg("该公司简介不存在", 1);
            $model->allowField([
                'company_profile_update_user_guid',
                'company_profile_img',
                'company_profile_color',
                'company_profile_content',
                'company_profile_order'
            ])->save($params);
            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 添加公司简介接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-19
     * @author xjh
     * @since 1.0.0
     */
    public function addCompanyProfile(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'company_profile_img|简介图片' => 'require',
                'company_profile_content|简介内容' => 'require',
                'company_profile_order|简介排序' => 'require'
            ]);
            ModelCompanyProfile::create($params, [
                'company_profile_guid',
                'company_profile_create_user_guid',
                'company_profile_update_user_guid',
                'company_profile_img',
                'company_profile_color',
                'company_profile_content',
                'company_profile_order'
            ]);
            Db::commit();
            return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 删除公司简介接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-19
     * @author xjh
     * @since 1.0.0
     */
    public function deleteCompanyProfile(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'company_profile_guid' => 'require',
            ]);
            $company_profile = ModelCompanyProfile::where([
                'company_profile_guid' => explode(',', $params['company_profile_guid'])
            ])->select();
            $company_profile->delete();
            Db::commit();
            return msg('删除成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }
}
