<?php
namespace Base\Service;

use Base\Facade\Request;

class PartnerService
{
    public function checkRelation($params)
    {
        if (empty($params['self_company_id'])) {
            throw new \Exception('请选择己方公司');
        }
    }

    public function adminAgree($application)
    {
        $adminInfo = $_SESSION['onethink_admin']['user_auth'];
        $application['verify_log'] = json_decode($application['verify_log'], true);
        $application['verify_log']['verify_user'] = $adminInfo["username"];
        $application['verify_log']['verify_time'] = date("Y-m-d H:i:s");
        $application['verify_log'] = json_encode($application['verify_log']);
        $application['verify_status'] = 1;
        $application["create_time"] = time();
        $partnerInfo = json_decode($application['partner_info'], true);

        $relation = $partnerInfo['relation'];
        unset($partnerInfo['relation']);
        
        if($application['partner_id'] == 0){
            //新增
            $application['partner_id'] = $partnerInfo['id'] = M('partner', 'tab_')->add($partnerInfo);
        }else{
            //编辑
            M('partner', 'tab_')->save($partnerInfo);
        }

        $this->saveRelation($relation, $partnerInfo);

        M('partner_verify', 'tab_')->save($application);
        addOperationLog(['op_type' => 1, 'key'=>$application['partner_id'], "op_name"=>"管理员审核", 'url'=>U('index')]);
    }

    protected function saveRelation($relation, $company)
    {
        $selfCompany = M('company_info', 'tab_')->where(['id' => $relation['self_company_id']])->find();
        $where = '(first_company_type=1 and first_company_id=' . $company['id'] .
            ') or (second_company_type=1 and second_company_id=' . $company['id'] . ')';
        $oldRelation =  M('company_relation', 'tab_')->where($where)->find();

        $data = [];
        if ($relation['collaborate_way'] == 1) {
            $data['first_company_id'] = $company['id'];
            $data['first_company_name'] = $company['partner'];
            $data['first_company_type'] = 1;
            $data['second_company_id'] = $selfCompany['id'];
            $data['second_company_name'] = $selfCompany['partner'];
            $data['second_company_type'] = 0;
        } else {
            $data['first_company_id'] = $selfCompany['id'];
            $data['first_company_name'] = $selfCompany['partner'];
            $data['first_company_type'] = 0;
            $data['second_company_id'] = $company['id'];
            $data['second_company_name'] = $company['partner'];
            $data['second_company_type'] = 1;
        }

        $data['settlement_type'] = $relation['settlement_type'];
        $data['invoice_type'] = $relation['invoice_type'];
        $data['collection'] = $relation['collection'];
        $data['is_payment'] = $relation['is_payment'];
        $data['invoice_content'] = $relation['invoice_content'];
        if ($oldRelation) {
            M('company_relation', 'tab_')->where(['id' => $oldRelation['id']])->save($data);
        } else {
            M('company_relation', 'tab_')->add($data);
        }
    }
}