<?php
namespace api\action\v1;

use admin\action\sample\interioAction;
use api\action\baseAction;
use League\Csv\Exception;

/**
 * Class proAction
 * @package api\action\v1
 */
class sampleAction extends baseAction
{
    public $sample;
    public function __construct ()
    {
        parent::__construct();
        $this->sample = makeOne(\model\sample\sample::class);
    }
 
    /**
     * 格式化 $rows;
     */
    public function formatRow($rows)
    {
        if(isset($rows[0])){
            foreach($rows as $k=>$row){
                $rows[$k] = $this->_formatRow($row);
            }
            return $rows;
        }else{
            return $this->_formatRow($rows);
        }
    }
    public function _formatRow($row)
    {
        $row = $this->sample->getRowInfo($row);
        //$row['tech'] = $this->sample->getTech($row['s_id']);
        return $row;
    }

    /**
     * 取样品
     * @param seq
     */
    public function getData()
    {
        try {
            $params = $this->getParaam();
            if(empty($params['params']) || !is_array($params['params'])){
                $this->returnJson(500, '参数 params 必须,格式 {"key":"value"}');
            }
            if(!is_numeric($params['page']) || $params['page']<0){
                $this->returnJson(500, '参数 page 必须,格式 大于等于 0 的数字');
            }
            $page = $params['page'];
            $pagesize = $this->cfg['pagesize'];
            $maxpage = $this->cfg['maxpage'];
            if(empty($pagesize)) $pagesize = 1000;
            if(empty($maxpage)) $maxpage = 1;
            
            //读取一行，得到所有子段
            $arr  = $this->sample->getSampleAndTechRowsByWhere(['LIMIT'=>1]);
            $allFields = [];
            if(isset($arr[0])){
                foreach($arr[0] as $k=>$v){
                    $allFields[] = $k;
                }
            }else{
                $this->returnJson(404, '没有数据');
            }
            
            $where = [];
            foreach($params['params'] as $field => $value){
                $field = trim($field);
                if(!empty($this->userParams) && !in_array(preg_replace('/\[.*\]/','',$field),$this->userParams)){
                    $this->returnJson(500, '参数 '.$field.' 无权限');
                }
                if($field!='' && in_array(preg_replace('/\[.*\]/','',$field),$allFields)){
                    $s = preg_replace('/\[.*\]/','',$field);
                    if($s=='create_time' || $s=='update_time'){
                        $field = preg_replace('/^([a-z0-1_]+)/i','s.$1',$field);
                    }
                    $where[$field] = $value;
                }else{
                    $this->returnJson(500, '参数 '.$field.' 不存在');
                }
            }
            //rules
            if(isset($this->cfg) && isset($this->cfg['rules']) && !empty($this->cfg['rules'])){
                foreach($this->cfg['rules'] as $field=>$value){
                    if(isset($where[$field])){
                        $arr = !is_array($where[$field]) ? [$where[$field]] : $where[$field];
                        foreach($arr as $v){
                            if(!in_array($v,$value)){
                                $this->returnJson(500, $field.':'.$v.' 无权限');
                            }
                        }
                    }
                    $where['AND #rule'.md5($field)] = [$field=>$value];
                }
            }
            if($page==0){
                $records = $this->sample->getSampleAndTechCountByWhere($where);
                $pages = ceil($records / $pagesize);
                $this->returnJson(200,'ok',['pages'=>$pages]);
            }
            if($page > $maxpage){
                $this->returnJson(500, '超出最大页码('.$maxpage.')限制');
            }
            $where['LIMIT'] = [($page-1) * $pagesize,$pagesize];
            if($rows = $this->sample->getSampleAndTechRowsByWhere($where)){
                $rows = $this->formatRow($rows);
                $this->returnJson(200,'ok',$rows);
            }else{
                $this->returnJson(500, '样品未找到');
            }
        }catch (Exception $e) {
            errorLog([__METHOD__,$param],'sample');
            $this->returnJson(500,$e->getMessage());
        }
    }
}