<?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\adminapi\controller\user;

use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\user\UserLists;
use app\adminapi\logic\user\UserLogic;
use app\adminapi\validate\user\AdjustUserMoney;
use app\adminapi\validate\user\UserValidate;
use think\facade\Db;
use think\facade\Request;
use app\api\logic\SmsLogic;
use app\api\controller\JunziqianController;
use app\common\model\contract\Contract;

/**
 * 用户控制器
 * Class UserController
 * @package app\adminapi\controller\user
 */
class UserController extends BaseAdminController
{

    /**
     * @notes 用户列表
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/22 16:16
     */
    public function lists()
    {
        return $this->dataLists(new UserLists());
    }


    /**
     * @notes 获取用户详情
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/22 16:34
     */
    public function detail()
    {
        $params = (new UserValidate())->goCheck('detail');
        $detail = UserLogic::detail($params['id']);
        return $this->success('', $detail);
    }


    /**
     * @notes 编辑用户信息
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/22 16:34
     */
    public function edit()
    {
        $params = (new UserValidate())->post()->goCheck('setInfo');
        UserLogic::setUserInfo($params);
        return $this->success('操作成功', [], 1, 1);
    }


    /**
     * @notes 调整用户余额
     * @return \think\response\Json
     * @author 段誉
     * @date 2023/2/23 14:33
     */
    public function adjustMoney()
    {
        $params = (new AdjustUserMoney())->post()->goCheck();
        $res = UserLogic::adjustUserMoney($params);
        if (true === $res) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail($res);
    }
    //**发起合同 */
    public function initiate_contract(){
        $params = Request::param();
        $params['party_a']=Db::name('company')->where('admin_id',$this->adminId)->value('id');
        if($params['party_a']<=0){
            return $this->fail('甲方不存在,请联系平台绑定公司');
        }
        $res=Contract::Initiate_contract($params);
        if (true === $res) {
            return $this->success('发起成功,等待平台风控部上传合同');
        }
        return $this->fail('发起失败');
    }
        // /**生成合同 */
        public function Draftingcontracts()
        {
            $params = Request::param();
            $result = UserLogic::detail($params['id']);
            halt($result);
            if ($result && $result['contract'] && $result['contract']['file'] != '') {
                $data = [
                    'name' => $result['nickname'] . '的合同',
                    'signatories' => [['fullName' => $result['nickname'], 'identityType' => 1, 'identityCard' => $result['id_card'], 'mobile' => $result['mobile'],  'noNeedVerify' => 1, 'signLevel' => 1]],
                    'url' => $result['contract']['file']
                ];
                $res = app(JunziqianController::class)->Signing($data,$result['contract']['id']);
                if ($res->success == true) {
                    Db::name('contract')->where('id', $result['contract']['id'])->update(['contract_no' => $res->data]);
                    $data = array(
                        "applyNo" => $res->data, //TODO *
                        "fullName" => $result['nickname'], //TODO *
                        "identityCard" => $result['id_card'], //TODO *
                        "identityType" => 1, //TODO *
                    );
                    $res = app(JunziqianController::class)->SigningLink($data);
                    if ($res->success == true) {
                        Db::name('contract')->where('id', $result['contract']['id'])->update(['url' => $res->data]);
                        //发送短信
                        $sms = [
                            'mobile' => $result['mobile'],
                            'name' => $result['nickname'],
                            'type' => '《' . $result['contract']['contract_type_name'] . '》',
                            'code' => 'api/Hetong/url?id=' . $result['contract']['id'],
                            'scene' => 'WQ'
                        ];
                        $result = SmsLogic::contractUrl($sms);
                        if (true === $result) {
                            return $this->success('发送成功');
                        } else {
                            return $this->fail(SmsLogic::getError());
                        }
                    } else {
                        return $this->fail($res->msg);
                    }
                    return $this->success('生成合同成功', [], 1, 1);
                } else {
                    return $this->fail($res->msg);
                }
            } else {
                return $this->fail('生成合同成功失败,联系管理员');
            }
        }
}