<?php


class CommonClassModel extends Model
{
    //客户列表接口
    public function getcustomer()
    {
        $uid = $this->getuid();
        $data = $this->db->getall('select `contract_number` as value,name from 
                                    oa_customer 
                                    where (uid = ' . $uid . '
                                        or department_id=(select deptname from oa_userinfo where id=' . $uid . ') 
                                    or department_id like concat((select deptname from oa_userinfo where id=' . $uid . '),",%") 
                                    or department_id like concat("%,",(select deptname from oa_userinfo where id=' . $uid . ')) 
                                    or department_id like concat("%,",(select deptname from oa_userinfo where id=' . $uid . '),",%"))
                                    and status = 1 
                                    order by id desc');
        return $data;
    }

    //获取客户公海池列表接口
    public function getgongcustomer()
    {
        $data = $this->db->getall('select id as value,name from [Q]customer where isgh = 1 order by id desc');
        return $data;
    }

    public function getbusilist()
    {
        $data = m('option')->getall('pid = 484','id as value,name');

        return $data;
    }

    //合同列表接口
    public function getcontractList($param)
    {
        $data = m('c_contract')->getall('status = ' . $param, 'id as value,  CONCAT(title, "（",numb,"）") as name');
        return $data;
    }

    //合同列表接口
    public function getcontractlistfk()
    {
        $uid = $this->getuid();
        $sql = '';
        if ($uid != 1)
            $sql .= ' and (c.uid = ' . $uid . ' or c.staff_id = ' . $uid . ')';
            $where='select c.id as value, CONCAT(c.title, "(",c.numb,")") as name from [Q]c_contract as c inner join  [Q]flow_bill as f  on c.id=f.mid where (f.nowcourseid =752  or c.status=1) '.$sql;
        
            $data = $this->db->getall($where);
        return $data;
    }

    //客户列表接口
    public function getsupplier()
    {
        $uid = $this->getuid();

        $data = $this->db->getall('select `number` as value,name from 
                                    oa_supplier 
                                    where (uid = ' . $uid . ' 
                                    or department_id=(select deptname from oa_userinfo where id=' . $uid . ') 
                                    or department_id like concat((select deptname from oa_userinfo where id=' . $uid . '),",%") 
                                    or department_id like concat("%,",(select deptname from oa_userinfo where id=' . $uid . ')) 
                                    or department_id like concat("%,",(select deptname from oa_userinfo where id=' . $uid . '),",%"))
                                    and status = 1 
                                    order by id desc');
        return $data;
    }
   //账户列表接口
    public function getaccountlist()
    {
        $uid = $this->getuid();
        $data = $this->db->getall('select `id` as value,name from 
        oa_c_fund_account 
        where (uid = ' . $uid . '  or uid=' . $uid . '   or uid like concat(' . $uid . ',",%")   or uid like concat("%,",' . $uid . ')        or uid like concat("%,",' . $uid . ',",%"))
        and status = 1 ');
        return $data;
    }
    //成本中心列表接口
    public function getcbzx()
    {
        $data = m('c_cbzx')->getall('status = 1', 'id as value,CONCAT(code, "（",name,"）") as name');
        return $data;
    }

    //成本中心列表接口
    public function getcbzxnoxmbm()
    {
        $data = m('c_cbzx')->getall('status = 1 and iscbzx = 1', 'id as value,CONCAT(code, "（",name,"）") as name');
        return $data;
    }

    //付款类型-科目列表接口
    public function getbxlx()
    {
        $data = m('c_bxlx')->getall('status = 1', 'id as value,name');
        return $data;
    }

    //获取部门列表接口
    public function getdept()
    {
        $data = m('c_department')->getall('status = 1', 'id as value,name');
        return $data;
    }

    public function getuid()
    {
        return (new Action())->adminid;
    }


      //获取合同供应商列表
    public function getcontract_supplier()
    {
        $uid = $this->getuid();

        $data = $this->db->getall('select `number` as value,CONCAT(name, "（","供应商","）") as name from 
                                    oa_supplier 
                                    where (uid = ' . $uid . ' 
                                    or department_id=(select deptname from oa_userinfo where id=' . $uid . ') 
                                    or department_id like concat((select deptname from oa_userinfo where id=' . $uid . '),",%") 
                                    or department_id like concat("%,",(select deptname from oa_userinfo where id=' . $uid . ')) 
                                    or department_id like concat("%,",(select deptname from oa_userinfo where id=' . $uid . '),",%"))
                                    and status = 1 
                                    order by id desc');
        return $data;
    }

        //获取合同客户列表
    public function getcontract_customer()
    {
        $uid = $this->getuid();
        $data = $this->db->getall('select `contract_number` as value,CONCAT(name, "（","客户","）") as name from 
                                    oa_customer 
                                    where (uid = ' . $uid . '
                                        or department_id=(select deptname from oa_userinfo where id=' . $uid . ') 
                                    or department_id like concat((select deptname from oa_userinfo where id=' . $uid . '),",%") 
                                    or department_id like concat("%,",(select deptname from oa_userinfo where id=' . $uid . ')) 
                                    or department_id like concat("%,",(select deptname from oa_userinfo where id=' . $uid . '),",%"))
                                    and status = 1 
                                    order by id desc');
        return $data;
    }

         //固定资产申领
    public function getassetmuse()
    {
        $data = m('assetm')->getall('state = 0', 'id as value,  CONCAT(num, "（",title,"）") as name');
        return $data;
    }

     //项目付款单成本中心列表接口
    public function getprojectcbzx()
    {
        $data = m('c_cbzx')->getall(' type=0  and status = 1', 'id as value,CONCAT(code, "（",name,"）") as name');
        return $data;
    }

      public function getprojcbzx()
    {
        $aDt = $this->getAccessData();
        $map = '';
        if(is_array($aDt['data']) && !empty($aDt['data'])){
            foreach ($aDt['data'] as $k => $value) {
                $and = ' and ';
                if($k == count($aDt['data'])- 1) $and = '';
                $map .= $value[0].' '.$value[1].' ('.$value[2].') '.$and;
            }
        }
      $data = m('c_project_numbs')->getall($map, 'id as value,CONCAT(project_numb, "（",project_name,"）") as name');
      return $data;
    }


    private function getToken(){
        $url = 'http://jz.gonn.com.cn/v1/token?app_id=b617866c20482d133d5de66fceb37da3&timestamp=1588055274&sign=91ffa6ef3eef7ec6231ddb16532c39e1';
        $res = curlGet($url);
        return json_decode($res,true);
    }

    private function getAccessData(){
        $tData = $this->getToken();
        $uid = $this->getJzUid();
        if($tData['message'] == "SUCCESS") $url = 'http://jz.gonn.com.cn/v1/permission?uid='.$uid.'&token='.$tData['data']['token'];

        $res = curlGet($url);

        return json_decode($res,true);
    }

    private function getJzUid(){
        $adminid = $this->adminid;
        $user = m('admin')->getone(' id = '.$adminid,'user');
        $email = $user['user'].'@gonn.com.cn';

        $tData = $this->getToken();
        if($tData['message'] == "SUCCESS") $url = 'http://jz.gonn.com.cn/v1/email?email='.$email.'&token='. $tData['data']['token'];

        $res = curlGet($url);
            $uData = json_decode($res,true);

        return $uData['data']['id'];
    }
}
