Contract.php 25.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
<?php

namespace addons\qingdong\controller;

use addons\qingdong\model\AuthGroup;
use addons\qingdong\model\Contract as ContractModel;
use addons\qingdong\model\ContractFile;
use addons\qingdong\model\ContractOther;
use addons\qingdong\model\ExamineRecord;
use addons\qingdong\model\FormField;
use addons\qingdong\model\Ratio;
use addons\qingdong\model\Receivables;
use addons\qingdong\model\Message;
use addons\qingdong\model\Staff;
use app\admin\model\AuthGroupAccess;
use addons\qingdong\model\Customer;
use addons\qingdong\model\Business;
use think\Db;
use think\Exception;

/**
 * 合同接口
 */
class Contract extends StaffApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = [];







    //合同排行榜
    public function getRank(){

        $startTime = input('startTime','');
        $endTime = input('endTime','');
        $type = input('type',1);//1=合同金额;2=存量客户;3=存量项目
        
        if($type==1){
            //个人
            $list = Staff::field('id,name,group_ids')->where('id','not in',[1,2,54,55,56,57,58,52])->where('status',1)->order('id asc')->select();
            foreach ($list as &$v){
                $v['money'] = ContractModel::where(function ($query)use($startTime,$endTime){
                    if($startTime!='' && $endTime!=''){
                        $query->whereTime('createtime','between',[$startTime,$endTime]);
                    }
                })->where('order_staff_id',$v['id'])->sum('money');
            }
            usort($list, function($a, $b) {
                $moneyComparison =   $b['money'] <=> $a['money'] ;
                if ($moneyComparison === 0) {
                    return $a['id'] <=> $b['id'];
                }
                return $moneyComparison;
            });
            //前三人排行
            $top3 = array_slice($list,0,3);
            $last3 = array_slice($list,-3);
            
            
            //分公司
            $suids = \addons\qingdong\model\Staff::getLowerStaffId2(5);
            $list = Staff::field('id,name,group_ids')->where('id','not in',[1,2,54,55,56,57,58,52])->whereIn('id',$suids)->where('status',1)->order('id asc')->select();
            foreach ($list as &$v){
                $v['money'] = ContractModel::where(function ($query)use($startTime,$endTime){
                    if($startTime!='' && $endTime!=''){
                        $query->whereTime('createtime','between',[$startTime,$endTime]);
                    }
                })->where('order_staff_id',$v['id'])->sum('money');
            }
            usort($list, function($a, $b) {
                $moneyComparison =   $b['money'] <=> $a['money'] ;
                if ($moneyComparison === 0) {
                    return $a['id'] <=> $b['id'];
                }
                return $moneyComparison;
            });
            //前三人排行
            $ltop3 = array_slice($list,0,3);
            $llast3 = array_slice($list,-3);
            
            $this->success('合同排行榜',['staff'=>['top'=>$top3,'last'=>$last3],'group'=>['top'=>$ltop3,'last'=>$llast3]]);
        }elseif($type==2){
            //存量客户
            $list = Staff::field('id,name,group_ids')->where('id','not in',[1,2,54,55,56,57,58,52])->where('status',1)->order('id asc')->select();
            foreach ($list as &$v){
                $v['money'] = Customer::where(function ($query)use($startTime,$endTime){
                    if($startTime!=''){
                        $query->whereTime('createtime','<',$startTime);
                    }else{
                        $query->whereTime('createtime','<',0);
                    }
                })->whereNull('deletetime')->where('owner_staff_id',$v['id'])->count();
            }
            usort($list, function($a, $b) {
                $moneyComparison =   $b['money'] <=> $a['money'] ;
                if ($moneyComparison === 0) {
                    return $a['id'] <=> $b['id'];
                }
                return $moneyComparison;
            });
            //前三人排行
            $top3 = array_slice($list,0,3);
            $last3 = array_slice($list,-3);
            
            
            $suids = \addons\qingdong\model\Staff::getLowerStaffId2(5);
            $list = Staff::field('id,name,group_ids')->where('id','not in',[1,2,54,55,56,57,58,52])->whereIn('id',$suids)->where('status',1)->order('id asc')->select();
            foreach ($list as &$v){
                $v['money'] = Customer::where(function ($query)use($startTime,$endTime){
                    if($startTime!=''){
                        $query->whereTime('createtime','<',$startTime);
                    }else{
                        $query->whereTime('createtime','<',0);
                    }
                })->whereNull('deletetime')->where('owner_staff_id',$v['id'])->count();
            }
            usort($list, function($a, $b) {
                $moneyComparison =   $b['money'] <=> $a['money'] ;
                if ($moneyComparison === 0) {
                    return $a['id'] <=> $b['id'];
                }
                return $moneyComparison;
            });
            //前三人排行
            $ltop3 = array_slice($list,0,3);
            $llast3 = array_slice($list,-3);
            
            
            $this->success('存量客户',['staff'=>['top'=>$top3,'last'=>$last3],'group'=>['top'=>$ltop3,'last'=>$llast3]]);
        }elseif($type==3){
            //存量客户
            $list = Staff::field('id,name,group_ids')->where('id','not in',[1,2,54,55,56,57,58,52])->where('status',1)->order('id asc')->select();
            foreach ($list as &$v){
                $v['money'] = Business::where(function ($query)use($startTime,$endTime){
                    if($startTime!=''){
                        $query->where('createtime','<',$startTime);
                    }else{
                        $query->where('createtime','<',0);
                    }
                })->where('customer_id',$v['id'])->whereNull('deletetime')->count();
            }
            usort($list, function($a, $b) {
                $moneyComparison =   $b['money'] <=> $a['money'] ;
                if ($moneyComparison === 0) {
                    return $a['id'] <=> $b['id'];
                }
                return $moneyComparison;
            });
            //前三人排行
            $top3 = array_slice($list,0,3);
            $last3 = array_slice($list,-3);
            
            
            $suids = \addons\qingdong\model\Staff::getLowerStaffId2(5);
            $list = Staff::field('id,name,group_ids')->where('id','not in',[1,2,54,55,56,57,58,52])
            ->whereIn('id',$suids)->where('status',1)
            ->order('id asc')->select();
            foreach ($list as &$v){
                $v['money'] = Business::where(function ($query)use($startTime,$endTime){
                    if($startTime!=''){
                        $query->whereTime('createtime','<',$startTime);
                    }else{
                        $query->whereTime('createtime','<',0);
                    }
                })->whereNull('deletetime')->where('customer_id',$v['id'])
                ->count();
            }
            // halt($list);
            usort($list, function($a, $b) {
                $moneyComparison =   $b['money'] <=> $a['money'] ;
                if ($moneyComparison === 0) {
                    return $a['id'] <=> $b['id'];
                }
                return $moneyComparison;
            });
            //前三人排行
            $ltop3 = array_slice($list,0,3);
            $llast3 = array_slice($list,-3);
            
            
            $this->success('存量项目',['staff'=>['top'=>$top3,'last'=>$last3],'group'=>['top'=>$ltop3,'last'=>$llast3]]);
        }
        
        
        
        
        
//        //自己数据
//        $myself = Staff::field('id,name')->where('status',1)->where('id',$this->auth->id)->find();
//
//        $rank = null;
//        foreach ($list as $index => $item) {
//            if ($item['money'] == $myself['money']) {
//                $rank = $index + 1; // 排名从1开始
//                break;
//            }
//        }
//        $myself['rank'] = $rank;
//
//        $data1 = [
////            'myself' => $myself,
//            'list' => $top3,
//        ];

        //分公司
        $list2 = AuthGroup::where('id','not in',[1,2,3,4,8])->field('id,pid,name')->select();
        foreach ($list2 as &$item){
            $satff_ids = AuthGroupAccess::where('group_id' ,$item['id'])->column('uid');
            $item['money'] = ContractModel::where(function ($query)use($startTime,$endTime){
                if($startTime!='' && $endTime!=''){
                    $query->whereTime('createtime','between',[$startTime,$endTime]);
                }
            })->where('owner_staff_id','in',$satff_ids)->sum('money');
        }

        usort($list2, function($a, $b) {
            $moneyComparison =   $b['money'] <=> $a['money'] ;
            if ($moneyComparison === 0) {
                return $a['id'] <=> $b['id'];
            }
            return $moneyComparison;
        });
        $list2 = array_slice($list2, 0, 3);
        $data1['list2'] = $list2;

        $this->success('合同排行榜',['staff'=>$top3,'group'=>$list2]);
        
        
        
        //return;
        //$startTime = input('startTime','');
        //$endTime = input('endTime','');
        //$list = ContractModel::with('staff')->where(function ($query)use($startTime,$endTime){
        //    if($startTime!='' && $endTime!=''){
        //        $query->whereTime('createtime','between',[$startTime,$endTime]);
        //    }
        //})->group('owner_staff_id')->order('money', 'desc')->limit(3)->select();
        //
        //
        //$data = AuthGroup::where('id','not in',[1,2,3,4,8])->field('id,pid,name')->select();
        //
        //$ContractModel = new ContractModel;
        //
        //foreach ($data as $item){
        //    $satff_ids = AuthGroupAccess::where('group_id' ,$item['id'])->column('uid');
        //    $item['money'] = $ContractModel->where('owner_staff_id','in',$satff_ids)->sum('money');
        //}
        //
        //usort($data, function($a, $b) {
        //    return  $b['money'] <=> $a['money'] ;
        //});
        //
        //$list2 = array_slice($data, 0, 3);
        ////halt($data);
        ////$list2 = ContractModel::alias('c')
        ////    ->join('ats_qingdong_staff s', 'c.owner_staff_id = s.id')
        ////    ->where(function ($query)use($startTime,$endTime){
        ////        if($startTime!='' && $endTime!=''){
        ////            $query->whereTime('createtime','between',[$startTime,$endTime]);
        ////        }
        ////    })
        ////    ->field('s.group_ids, COUNT(*) as count')
        ////    ->group('s.group_ids')
        ////    ->order('count', 'desc')
        ////    ->limit(3)
        ////    ->select();

        $this->success('合同排行榜',['staff'=>$list,'group'=>$list2]);
    }

    //创建合同
    public function addContract()
    {
        $params = $this->request->post();

        // 表单验证
        if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
            $this->error($result);
        }
        $result = FormField::checkFields(FormField::CONTRACT_TYPE, $params);
        if ($result !== true) {
            $this->error($result);
        }
        if (isset($params['num'])) {
            if (empty($params['num'])) {
                $params['num'] = ContractModel::getNum();
            }
            $contract = ContractModel::where(['num' => $params['num']])->find();
            if ($contract) {
                $this->error('合同编号已存在');
            }
        } else {
            $params['num'] = ContractModel::getNum();
        }
        Db::startTrans();
        try {
            //halt($params);
            $result = ContractModel::createContract($params);
            $id = Db::name('xtuqll')->getLastInsID();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('添加合同成功',['id'=>$result]);
        }
    }


    //获取select合同列表
    public function getSelectList()
    {
        $customer_id = input('customer_id');
        $name = input('name','');
        $where = [];
        if ($customer_id) {
            $where['customer_id'] = $customer_id;
        }
        if ($name) {
            $where['name'] = ["like","%$name%"];
        }
        $records = ContractModel::where($where)->where(['check_status'=>2])->with(['ownerStaff'])->field('id,name,num,order_date,money')->order('id desc')->select();
        $this->success('请求成功', $records);
    }


    //获取合同列表
    public function getList()
    {
        $limit = input("limit/d", 10);
        $customer_id = input('customer_id');
        $contacts_id = input('contacts_id');
        $status = input('status',0);

        $params = $this->request->post();

        $whereT =[];
        $where= FormField::updateWhereField(FormField::CONTRACT_TYPE,$params);
        if (isset($params['createtime']) && $params['createtime']) {//跟进状态
            $createtime = $params['createtime'];
            $createtime = explode(',', $createtime);
            $where['order_date'] = ['between', [date('Y-m-d 00:00:00',strtotime($createtime[0])), date('Y-m-d 23:59:59',strtotime($createtime[1]) + 86400 - 1)]];
        }
        if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
            $where['owner_staff_id'] = $params['staff_id'];
        } else {
            $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
            if (isset($params['type']) && $params['type']) {//客户分类
                if ($params['type'] == 1) {//我的客户
                    $where['owner_staff_id'] = $this->auth->id;
                } elseif ($params['type'] == 2) {//下属负责的客户
                    $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
                }
            }
        }

        if ($status) {
            if($status == 1){//待审核
                $where['check_status']=['in',[0,1]];
            }elseif($status == 2){//待回款
                $where['check_status']=2;
                $where['contract_status']=0;
            }elseif($status == 3){//已回款
                $where['check_status']=2;
                $where['contract_status']=1;
            }
        }
        if ($customer_id) {
            $where['customer_id'] = $customer_id;
        }
        if ($contacts_id) {
            $where['contacts_id'] = $contacts_id;
        }
        $records = ContractModel::where($where)->with([
            'customer',
            'contacts',
            'ownerStaff',
            'orderStaff',
            'receivables'
        ])->order('id desc')->paginate($limit)->toArray();
        $data = $records['data'];

        foreach ($data as $k => $v) {
            if (empty($v['receivables'])) {
                $v['receivables'] = [
                    'repayment_money' => 0,
                    'be_money' => $v['money'],
                    'ratio' => 0
                ];
            } else {
                $be_money = $v['money'] - $v['receivables']['repayment_money'];
                $be_money =   ($be_money > 0) ? $be_money : 0;
                $ratio_m = 0;
                if($v['money'] >0){
                    $ratio_m = round($v['receivables']['repayment_money'] / $v['money'] * 100, 2);
                }
                $v['receivables'] = [
                    'repayment_money' => $v['receivables']['repayment_money'],
                    'be_money' =>$be_money,
                    'ratio' => $ratio_m
                ];
            }
            $data[$k] = $v;
        }

        $whereT['owner_staff_id']=$where['owner_staff_id'];
        $noMoney = ContractModel::where($whereT)->where(array('check_status'=>2))->sum('money');
        $moneyinfo['repayment_money'] = Receivables::where($whereT)->where(array('check_status'=>2))->sum('money'); //已回款
        $moneyinfo['be_money'] = sprintf("%.2f",$noMoney)-sprintf("%.2f",$moneyinfo['repayment_money']);//未回款
        $moneyinfo['allmoney'] = $noMoney;//合同总金额
        $this->success('请求成功', ['moneyinfo' => $moneyinfo, 'total' => $records['total'], 'per_page' => $records['per_page'], 'current_page' => $records['current_page'], 'last_page' => $records['last_page'], 'data' => $data]);
    }




    //获取我的合同列表
    public function getMyList(){

        $limit = input("limit/d", 10);

        $where['owner_staff_id'] = $this->auth->id;

        $records = ContractModel::where($where)->with([
            'customer',
            'contacts',
            'ownerStaff',
            'orderStaff',
            'receivables'
        ])->order('id desc')->paginate($limit)->toArray();
        $data = $records['data'];

        foreach ($data as $k => $v) {
            if (empty($v['receivables'])) {
                $v['receivables'] = [
                    'repayment_money' => 0,
                    'be_money' => $v['money'],
                    'ratio' => 0
                ];
            } else {
                $be_money = $v['money'] - $v['receivables']['repayment_money'];
                $be_money =   ($be_money > 0) ? $be_money : 0;
                $ratio_m = 0;
                if($v['money'] >0){
                    $ratio_m = round($v['receivables']['repayment_money'] / $v['money'] * 100, 2);
                }
                $v['receivables'] = [
                    'repayment_money' => $v['receivables']['repayment_money'],
                    'be_money' =>$be_money,
                    'ratio' => $ratio_m
                ];
            }
            $data[$k] = $v;
        }

        $whereT['owner_staff_id']=$where['owner_staff_id'];
        $noMoney = ContractModel::where($whereT)->where(array('check_status'=>2))->sum('money');
        $moneyinfo['repayment_money'] = Receivables::where($whereT)->where(array('check_status'=>2))->sum('money'); //已回款
        $moneyinfo['be_money'] = sprintf("%.2f",$noMoney)-sprintf("%.2f",$moneyinfo['repayment_money']);//未回款
        $moneyinfo['allmoney'] = $noMoney;//合同总金额
        //$this->success('请求成功', ['moneyinfo' => $moneyinfo, 'total' => $records['total'], 'per_page' => $records['per_page'], 'current_page' => $records['current_page'], 'last_page' => $records['last_page'], 'data' => $data]);
        $this->success('请求成功',$data);
    }



    //获取客户相关合同列表
    public function getCustomerContacts()
    {
        $customer_id = input('customer_id');
        $contacts_id = input('contacts_id');
        $where = [];
        if ($customer_id) {
            $where['customer_id'] = $customer_id;
        }
        if ($contacts_id) {
            $where['contacts_id'] = $contacts_id;
        }
        $records = ContractModel::where($where)->with(['receivables'])->field('id,name,money,num,check_status,order_date')->order('id desc')->select();
        $records = collection($records)->toArray();
        foreach ($records as $k => $v) {
            if (empty($v['receivables'])) {
                $v['receivables'] = [
                    'repayment_money' => 0,
                    'be_money' => $v['money'],
                    'ratio' => 0
                ];
            } else {
                $be_money = $v['money'] - $v['receivables']['repayment_money'];
                $v['receivables'] = [
                    'repayment_money' => $v['receivables']['repayment_money'],
                    'be_money' => ($be_money > 0) ? $be_money : 0,
                    'ratio' => round($v['receivables']['repayment_money'] / $v['money'] * 100, 2)
                ];
            }
            $records[$k] = $v;
        }
        $this->success('请求成功', $records);
    }


    //获取合同详情
    public function getDetail()
    {
        $id = input('id');
        $contract = ContractModel::where(['id' => $id])->with([
            'customer',
            'contacts',
            'ownerStaff',
            'orderStaff',
            'product',
            'receivables',
            'business'
        ])->find();
        if (empty($contract)) {
            //标记通知已读
            Message::setRead(Message::CONTRACT_TYPE, $id);
            $this->error('合同不存在');
        }
        $contract = $contract->toArray();
        $contract = ContractOther::getOther($contract);
        $receivablesMoney = Receivables::where(['contract_id' => $contract['id'], 'check_status' => 2])->sum('money');
        //回款金额
        $contract['receivables_money'] = $receivablesMoney;
        $contract['ratios'] = json_decode($contract['ratios'],true);
        if($contract['ratios']){
            foreach ($contract['ratios'] as $k => $v) {
                $v['staff'] = Staff::where(['id' => $v['staff_id']])->field('id,name,img,post')->find()->toArray();
                $v['money'] = $contract['money'] * ($v['ratio'] / 100);
                $contract['ratios'][$k] = $v;
            }
        }
        if (empty($contract['receivables'])) {
            $contract['receivables'] = [
                'repayment_money' => 0,
                'be_money' => $contract['money'],
                'ratio' => 0
            ];
        } else {
            $be_money = $contract['money'] - $contract['receivables']['repayment_money'];
            $be_ratio = 0;
            if($contract['money'] > 0){
                $be_ratio =round($contract['receivables']['repayment_money'] / $contract['money'] * 100, 2);
            }

            $contract['receivables'] = [
                'repayment_money' => $contract['receivables']['repayment_money'],
                'be_money' => ($be_money > 0) ? $be_money : 0,
                'ratio' => $be_ratio
            ];
        }
        //产品删除不显示
        if(isset($contract['product']) && $contract['product']){
            foreach($contract['product'] as $k=>$v){
                if(!$v['name'] && !$v['num']){
                    unset($contract['product'][$k]);
                }
            }
        }
        //标记通知已读
        Message::setRead(Message::CONTRACT_TYPE, $id, $this->auth->id);

        $this->success('请求成功', $contract);
    }


    //获取附件列表
    public function getFilesList()
    {
        $id = input('contract_id');

        $files = ContractFile::where(['contract_id' => $id])->field('file_id')->with(['file'])->select();
        $this->success('请求成功', $files);
    }


    //撤回审核
    public function cancel()
    {
        $id = input('id');
        $customer = ContractModel::where(['id' => $id, 'check_status' => ['in', [0, 1]]])->find();
        if (empty($customer)) {
            $this->error('合同信息不存在');
        }
        $record = ExamineRecord::where([
            'relation_type' => ExamineRecord::CONTRACT_TYPE,
            'relation_id' => $id,
            'status' => 0
        ])->find();
        Db::startTrans();
        try {
            if ($message = Message::where(['relation_type' => 'examine', 'relation_id' => $record['id'], 'from_staff_id' => $this->auth->id])->find()) {
                Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
            }
            ContractModel::where(['id' => $id])->update(['check_status' => 4]);
            ExamineRecord::where([
                'relation_type' => ExamineRecord::CONTRACT_TYPE,
                'relation_id' => $id,
                'status' => 0
            ])->update(['status' => 3]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();;
            $this->error($e->getMessage());
        }


        $this->success('撤回成功');
    }


    //修改合同
    public function editContract()
    {
        $id = input('id');
        $params = $this->request->post();
        $row = ContractModel::where(['id' => $id, 'check_status' => ['in', [3, 4]]])->find();
        if (empty($row)) {
            $this->error('合同信息不存在');
        }
        // 表单验证
        if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
            $this->error($result);
        }
        $result = FormField::checkFields(FormField::CONTRACT_TYPE, $params,$id);
        if ($result !== true) {
            $this->error($result);
        }
        Db::startTrans();
        try {
            $params['owner_staff_id'] = $row['owner_staff_id'];
            $result = ContractModel::updateContract($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('修改合同信息成功');
    }
    //获取合同编号
    public function getContractNumber()
    {
        $this->success('请求成功', ['number' => ContractModel::getNum()]);
    }

    /**
     * 获取业绩分割比例
     */
    public function getRatio()
    {
        $ratios = Ratio::where(['status' => 1])->field('name,ratio')->select();
        $ratios = collection($ratios)->toArray();
        foreach ($ratios as $k => $v) {
            $v['ratio'] = json_decode($v['ratio'], true);
            $ratios[$k] = $v;
        }
        $this->success('请求成功', $ratios);
    }
}