<?php

/**
 * 合同接口
 * Class opencatClassAction
 */
class opencatClassAction extends openapiAction
{
    public function __call($name, $arguments)
    {
        $msg = $name.' action not found';
        return $this->showreturn([],$msg,'201');
    }

    //通过合同名称返回合同编号
    public function infoAction(){
        $post = $_POST;

        if($post['numbs'] == '') return $this->showreturn([],'Invalid parameter','400');
        $post['numbs'] = explode(',',$post['numbs']);
        $numbStr = join("','", array_values($post['numbs']));

        $sql  = 'select id,title,finance_type,busi_type,numb,department_id,busi_line,part_a,part_b,part_c,
                sign_date,staff_id,end_date,cycle,info,file_id,adddt,optdt,status,archive,remark,pid,busi_type_temp,amount,create_date
                 from [Q]c_contract as c where c.numb in ('.$numbStr.') order by c.id desc';
        $data = $this->db->getall($sql);
        $data = $this->flowrsreplace($data);

        $sql = 'select * from [Q]c_fee as f where f.mid = '.$data[0]['id'];
        $data['fee'] = $this->db->getall($sql);
        $sql = 'select * from [Q]c_rhythm as r where r.mid = '.$data[0]['id'];
        $data['rhythm'] = $this->db->getall($sql);

        return $this->showreturn($data);
    }


    //合同列表
    public function listAction(){
          //通过邮箱  查找uid

        $post = $_GET;
          $email=$post['email'];
        if(empty($email)){
            $data=array(
                'code' => 400,
                'message'=>'Mailbox cannot be empty'
            );
            return json_encode($data);
            die;
        }
        // $email='xiakun@gonn.com.cn';
        $sql  = "select u.id,u.email from [Q]userinfo u where u.email = '$email'";  
        $data = $this->db->getall($sql);
        $id=$data[0]['id'];//用户id
        $ids[]=$id;
        $sql  = "select u.id from [Q]admin u where u.superid = $id";  
        $data = $this->db->getall($sql);
        if($data){
        foreach ($data as $key => $value) {
           $arr[]['id']=$value['id'];
           $ids[]=$value['id'];
         }
        }
        if($arr){
            foreach ($arr as $key => $v) {
             $sql  = "select u.id from [Q]admin u where u.superid = {$v['id']}";  
             $datalist = $this->db->getall($sql);//三级

                  if($datalist){
                       foreach ($datalist as $k => $vo) {
                        $three[]['id']=$vo['id'];
                        $ids[]=$vo['id'];
                       }
                   }

            }
           if($three){
           foreach ($three as $ky => $vlue) {
             $sql  = "select u.id from [Q]admin u where u.superid = {$vlue['id']}";  
             $datathree = $this->db->getall($sql);//三级下属
             if($datathree){
                foreach ($datathree as $key => $value) {
                     $ids[]=$value['id'];
                }
             }
           }
          }
        }
           $idslist=implode(',',$ids);
        $post = $_POST;
        if(!isset($post['size'])) $post['size'] = 0;
        if(!isset($post['count'])) $post['count'] = 20;

       
        $sql  = 'select * from [Q]c_contract c where c.staff_id in '.'('.$idslist.')'.' order by c.id desc limit '.$post['size'].','.$post['count'];  //limit 2,20
   
        $data = $this->db->getall($sql);
        $data = $this->flowrsreplace($data);

        foreach ($data as $k => $d){
            $sql = 'select * from [Q]c_fee as f where f.mid = '.$d['id'];
            $fee = $this->db->getall($sql);

            $fdata = [];
            // //转换字段
            if($fee){
                foreach ($fee as $key => $vo){
                    $fdata[] = $this->flowsubdata($vo,'fee');
                }
            }
            $data[$k]['fee'] = $fdata;

            $sql = 'select * from [Q]c_rhythm as r where r.mid = '.$d['id'];
            $rdata = $this->db->getall($sql);
            $data[$k]['rhythm'] =$rdata;
        }

        return $this->showreturn($data);
    }

    public function flowrsreplace($rs)
    {
        foreach ($rs as $k => &$v) {
            switch ($k) {
                case 'finance_type':
                    $v = $this->getOptionType($v);
                    continue;
                    break;
                case 'departmentname':
                    if($v == '') $v = $rs['deptname'];
                    continue;
                    break;
                case 'staff':
                    if($v == '') {
                        $v = $v['staffname'];
                    }
                    continue;
                    break;
                case 'receive':
                    if($v == 1) {
                        $v = '甲方';
                    } else if($v == 2) {
                        $v = '乙方';
                    } else {
                        $v = '';
                    }
                    continue;
                    break;
                case 'part_a':
                    $v = $v['parta_name'];
                    continue;
                    break;
                case 'part_b':
                    $v = $v['partb_name'];
                    continue;
                    break;
                case 'part_c':
                    $v = $v['partc_name'];
                    continue;
                    break;
                case 'busi_line':
                    if($v == '') {
                        $v = $rs['businame'];
                        continue;
                        break;
                    }
                    else {
                        $v= $this->getOptionType($v);
                        continue;
                        break;
                    }
                case 'archive':
                    $v = $v == 1 ? '已归档' : '未归档';
                    continue;
                    break;
                case 'amount':
                    $v = $this->farmatMoney($v);
                    continue;
                    break;
            }
        }
        return $rs;
    }

    /**
     * 子表数据替换
     * @param $rD
     * @param int $lx   =0编辑,1显示
     * @return mixed
     */
    public function flowsubdata($r,$lx){
        foreach ($r as $k => &$v){
            if(is_array($v)){
                foreach ($v as $a => $b){
                    if($lx == 'fee') {
                        if ($a == 'title' && !empty($b)) {
                            $v[$a] = $this->getfeename($b);
                            continue;
                        }
                        if ($a == 'rate' && !empty($b)) {
                            $v[$a] = $this->getrate($b);
                            continue;
                        }
                        if ($a == 'costmodel' && !empty($b)) {
                            $v[$a] = $this->getcostname($b);
                            continue;
                        }
                        if($a == 'invoicetype' && !empty($b)){
                            $v[$a] = $this->getinvoicename($b);
                            continue;
                        }
                    }
                }
            }
        }
        return $r;
    }

    private function getOptionType($typeId){
        if(is_numeric($typeId)){
            $data = $this->db->getone('oa_option', 'id = '.$typeId);
        }
        else {
            $data['name'] = $typeId;
        }
        return $data['name'];
    }

    private function farmatMoney($money,$max_length = 2){
        if($money == '') return '';
        $is_dot = stripos($money,'.');
        if($is_dot){
            $tai = substr($money,$is_dot+1);
            $length = strlen($tai);
            $fill_num = $max_length - $length;
            if($fill_num > 0) {
                $money .= str_repeat('0',$fill_num); //补0
            }
            else if($fill_num < 0) {
                $money = round(floatval($money),$max_length);
            }
            return $money;
        }
        return $money . '.'.str_repeat('0', $max_length);
    }


    private function getrate($rate){
        $data = $this->db->getone('oa_option', 'num = "'.$rate.'"');
        if($data) return $data['name'];
        return '';
    }

    private function getcostname($costmodel){
        $data = $this->db->getone('oa_option', 'num = "'.$costmodel.'"');
        if($data) return $data['name'];
        return '';
    }

    private function getinvoicename($no){
        $data = $this->db->getone('oa_option', ' pid = 351 and num = "'.$no.'"');
        if($data) return $data['name'];
        return '';
    }

    private function getfeename($num){
        $data = $this->db->getone('oa_option', ' num = "'.$num.'"');
        if($data) return $data['name'];
        return '';
    }
}
