<?php

namespace App\Models\Api\Promoter;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Models\Api\Promoter\Configuration as ConfigurationModel;
use App\Models\Api\Promoter\Users as UsersModel;

/**
 * 推广员申请表模型
 * @author 唐远望
 * @version 1.0
 * @date 2025-09-04
 */
class ApplicationForm extends Model
{
    use HasFactory;
    // 与模型关联的表名
    protected $table = 'promoter_application_form';
    // 是否主动维护时间戳
    public $timestamps = false;
    protected   $connection = 'company';
    // 定义时间戳字段名
    // const CREATED_AT = 'insert_time';
    // const UPDATED_AT = 'update_time';


    /**
     * 添加
     * @author 唐远望
     * @version 1.0
     * @date 2025-05-27
     */
    public function addApplicationForm_content($data)
    {
        $insert_data = [
            'company_id' => $data['company_id'],
            'superior_custom_uid' => $data['superior_custom_uid'],
            'custom_uid' => $data['custom_uid'],
            'full_name' => $data['full_name'],
            'mobile_phone' => $data['mobile_phone'],
            'email' => $data['email'],
            'city' => $data['city'],
            'company_name' => $data['company_name'],
            'other_parameters' => json_encode($data['other_parameters']),
            'insert_time' => time(),
            'status' => $data['status'],
            'audit_remark' => $this->isNeedAudit($data['company_id']) ? '' : '',
        ];
        $ApplicationForm_id = $this->insertGetId($insert_data);
        return $ApplicationForm_id;
    }


    /**
     * 是否需要审核
     * @author 唐远望
     * @version 1.0
     * @date 2025-09-05
     */
    public function isNeedAudit($company_id)
    {
        $is_need_audit = true; //默认需要审核
        $ConfigurationModel = new ConfigurationModel();
        //获取推广员申请条件
        $application_requirements_data = $ConfigurationModel->where(['code_name' => 'application_requirements', 'company_id' => $company_id])->first();
        if (!$application_requirements_data) {
            return $is_need_audit;
        }
        $application_requirements_content_data = $application_requirements_data->content;
        if ($application_requirements_content_data['is_review'] == 0) {
            $is_need_audit = false;//不需要审核
        }
        return $is_need_audit;
    }


    /**
     * 写入数据
     * @author 唐远望
     * @version 1.0
     * @date 2025-05-27
     * @param $data
     * @return bool
     */
    public function addApplicationForm($data)
    {
        DB::beginTransaction();
        try {
            $status = $this->isNeedAudit($data['company_id']) ? 1 : 2;
            $data['status'] = $status;
            $insert_data = [
                'company_id' => $data['company_id'],
                'superior_custom_uid' => $data['superior_custom_uid'],
                'custom_uid' => $data['custom_uid'],
                'full_name' => $data['full_name'],
                'mobile_phone' => $data['mobile_phone'],
                'email' => isset($data['email'])?$data['email']:'',
                'city' => isset($data['city'])?$data['city']:'',
                'company_name' => isset($data['company_name'])?$data['company_name']:'',
                'other_parameters' => json_encode($data['other_parameters']),
                'insert_time' => time(),
                'status' => $data['status'],
                'audit_remark' => $this->isNeedAudit($data['company_id']) ? '' : '',
            ];
            $ApplicationForm_id = $this->insertGetId($insert_data);
            if ($status == 2) { //审核状态1=待审核,2=审核通过,3=审核拒绝
                $insert_data = [
                    'company_id' => $data['company_id'],
                    'superior_custom_uid' => $data['superior_custom_uid'],
                    'custom_uid' => $data['custom_uid'],
                    'full_name' => $data['full_name'],
                    'mobile_phone' => $data['mobile_phone'],
                    'email' => isset($data['email'])?$data['email']:'',
                    'city' => isset($data['city'])?$data['city']:'',
                    'company_name' => isset($data['company_name'])?$data['company_name']:'',
                    'other_parameters' => json_encode($data['other_parameters']),
                    'insert_time' => time(),
                    'promoter_remark' => '',
                    'status' => '1'
                ];
                (new UsersModel())->insertGetId($insert_data);
            }
            DB::commit();
            return $ApplicationForm_id;
            // 成功处理...
        } catch (\Exception $e) {
            DB::rollBack();
            // 错误处理...
            return ['code' => 'error', 'msg' => $e->getMessage()];
        }
    }


    /**
     * 编辑内容
     * @author 唐远望
     * @version 1.0
     * @date 2025-05-27
     * @param $data
     * @return bool
     */
    public function editApplicationForm_content($where, $data)
    {
        $ApplicationForm = $this->where($where)->first();
        if (!$ApplicationForm) {
            return false;
        }
        $ApplicationForm->full_name = $data['full_name'];
        $ApplicationForm->mobile_phone = $data['mobile_phone'];
        $ApplicationForm->email = $data['email'];
        $ApplicationForm->city = $data['city'];
        $ApplicationForm->company_name = $data['company_name'];
        $ApplicationForm->other_parameters = json_encode($data['other_parameters']);
        $ApplicationForm->update_time = time();
        $ApplicationForm->save();
        return true;
    }



    /**
     * 更新数据
     * @author 唐远望
     * @version 1.0
     * @date 2025-05-27
     * @param $data
     * @return bool
     */
    public function updateApplicationForm($where, $data)
    {
        DB::beginTransaction();
        try {
            $this->editApplicationForm_content($where, $data);
            DB::commit();
            return true;
            // 成功处理...
        } catch (\Exception $e) {
            DB::rollBack();
            // 错误处理...
            return false;
        }
    }

    /**
     * 修改状态
     * @author 唐远望
     * @version 1.0
     * @date 2025-05-27
     * @param $id
     * @param $status
     * @return bool
     */
    public function changeStatus($where, $status)
    {
        $ApplicationForm = $this->where($where)->first();
        if (!$ApplicationForm) {
            return false;
        }
        $ApplicationForm->status = $status;
        $ApplicationForm->update_time = time();
        $ApplicationForm->save();
        return true;
    }

    /**
     * 删除数据
     * @author 唐远望
     * @version 1.0
     * @date 2025-05-27
     * @param $id
     * @return bool
     */
    public function deleteApplicationForm($where)
    {
        $ApplicationForm = $this->where($where)->first();
        if (!$ApplicationForm) {
            return false;
        }
        $ApplicationForm->delete();
        return true;
    }
}
