<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\api\controller;


use app\api\logic\IndexLogic;
use app\common\model\contract\Contract;
use app\common\model\vehicle\Vehicle;
use app\common\model\vehicle\VehicleRent;
use think\response\Json;


/**
 * index
 * Class IndexController
 * @package app\api\controller
 */
class IndexController extends BaseApiController
{
    public array $notNeedLogin = ['index', 'config', 'policy', 'decorate','notifyUrl'];

    /**
     * @notes 首页数据
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 段誉
     * @date 2022/9/21 19:15
     */
    public function index()
    {
        $result = IndexLogic::getIndexData();
        return $this->data($result);
    }


    /**
     * @notes 全局配置
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 段誉
     * @date 2022/9/21 19:41
     */
    public function config()
    {
        $result = IndexLogic::getConfigData();
        return $this->data($result);
    }


    /**
     * @notes 政策协议
     * @return Json
     * @author 段誉
     * @date 2022/9/20 20:00
     */
    public function policy()
    {
        $type = $this->request->get('type/s', '');
        $result = IndexLogic::getPolicyByType($type);
        return $this->data($result);
    }


    /**
     * @notes 装修信息
     * @return Json
     * @author 段誉
     * @date 2022/9/21 18:37
     */
    public function decorate()
    {
        $id = $this->request->get('id/d');
        $result = IndexLogic::getDecorate($id);
        return $this->data($result);
    }

    public function notifyUrl()
    {
        $params = Request()->get(['id']);
        if(empty($params['id'])){
            return json(['success' => false, 'msg' => '缺少参数']);
        }
        //获取合同数据
        $contract = Contract::where('id',$params['id'])->find();
        if(empty($contract)){
            return json(['success' => false, 'msg' => '参数错误']);
        }
        if($contract['status'] != 2){
            return json(['success' => false, 'msg' => '合同状态错误']);
        }
        if ($contract['signing_timer'] == 0) {
            //更新本地合同状态
            $updateLocalRes = Contract::where('id',$contract['id'])->update(['signing_timer'=>1]);
            //更新远程
            $updateSverRes =curl_post(env('project.notify_domain').'/api/index/notifyVehicleContractUpdate',[],[
                'contract_logistic_id' => $contract['id'],
                'signing_timer' => 1,
            ]);
            if(!$updateLocalRes || $updateSverRes['code']==0){
                return json(['success' => false, 'msg' => '更新失败']);
            }
        }else if($contract['signing_timer'] == 1) {
            $vehicle = json_decode($contract['cars_info'],true);
            if(empty($vehicle)){
                return json(['success' => false, 'msg' => '更新失败']);
            }
            //获取签约后的合同
            $signContractFile = app(JunziqianController::class)->download_file($contract['contract_no']);
            $signContractFile = $signContractFile ?? '';
            //获取签约后的证据
            $signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
            $signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
            $signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
            $signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
            $contractEvidence = json_encode([
                'party_a' => $signContractEvidenceToPartyA,
                'party_b' => $signContractEvidenceToPartyB
            ]);
            //更新本地合同状态
            Contract::where('id',$params['id'])->update(['signing_timer'=>2,'status'=>3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
            //更新本地车辆状态
            Vehicle::where('id','in',array_column($vehicle,'id'))->update(['status'=>2]);
            //添加车辆到租赁列表
            $data = [];
            foreach($vehicle as $v){
                $data[] = [
                    'car_id' => $v['id'],
                    'contract_id' => $contract['id'],
                    'company_id' => $contract['company_b_id'],
                    'company_name' => $contract['company_b_name'],
                    'company_user' => $contract['company_b_user'],
                    'company_phone' => $contract['company_b_phone'],
                    'create_time' => time(),
                    'status' => 0,
                    'use_user_id' => 0,
                    'use_user_name' => '',
                    'use_user_phone' => ''
                ];
            }
            $vehicleRent = new VehicleRent();
            $vehicleRent -> saveAll($data);
            //更新远程
            curl_post(env('project.notify_domain').'/api/index/notifyVehicleContractUpdate',[],[
                'contract_logistic_id' => $contract['id'],
                'signing_timer' => 2,
                'status' => 3,
                'contract_url'=>$signContractFile,
                'contract_evidence'=>$contractEvidence
            ]);
        }
        return json(['success' => true, 'msg' => '成功']);
    }


}