<?php

namespace app\admin\controller\ContactUs;

use app\BaseController;
use app\common\model\ContactUs\ConsultingHotline as ModelConsultingHotline;
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 ConsultingHotline extends BaseController
{
    /**
     * 获取咨询热线列表
     */
    public function getConsultingHotlineList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['consulting_hotline_user', 'LIKE'], ['consulting_hotline_phone', 'LIKE'],['consulting_hotline_order','='],);

        $query = ModelConsultingHotline::where($con)
            ->field([
                'consulting_hotline_id',
                'consulting_hotline_guid',
                'consulting_hotline_user',
                'consulting_hotline_phone',
                'consulting_hotline_order'
            ])
            ->order('consulting_hotline_order', 'asc');

        return msg("获取咨询热线列表成功!", $query);
    }

    /**
     * 编辑咨询热线
     */
    public function editConsultingHotline(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'consulting_hotline_user|资讯留言人' => 'require',
            'consulting_hotline_phone|资讯热线方式' => 'require',
            'consulting_hotline_order|资讯热线排序'=>'require'
        ]);
        $model = ModelConsultingHotline::where('consulting_hotline_guid', $params['consulting_hotline_guid'])->find();
        if (!$model) throwErrorMsg("该咨询热线不存在", 1);
        $model->allowField([
            'consulting_hotline_update_user_guid',
            'consulting_hotline_user',
            'consulting_hotline_phone',
            'consulting_hotline_order'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加咨询热线
     */
    public function addConsultingHotline(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'consulting_hotline_user|资讯留言人' => 'require',
            'consulting_hotline_phone|资讯热线方式' => 'require',
            'consulting_hotline_order|资讯热线排序'=>'require'
        ]);
        $model = ModelConsultingHotline::create($params, [
            'consulting_hotline_guid',
            'consulting_hotline_create_user_guid',
            'consulting_hotline_update_user_guid',
            'consulting_hotline_user',
            'consulting_hotline_phone',
            'consulting_hotline_order'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除咨询热线
     */
    public function deleteConsultingHotline(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'consulting_hotline_guid' => 'require',
        ]);
        $consulting_hotline = ModelConsultingHotline::where([
            'consulting_hotline_guid' => explode(',', $params['consulting_hotline_guid'])
        ])->select();
        $consulting_hotline->delete();
        return msg('删除成功!');
    }
}
