<?php
class depa_mansAction extends action
{
    function __construct(){
        parent::__construct();
		$this->mod = new depa_mansMod();
    }

    function __destruct(){
    }

	/**
	 * Method 列表数据
	 */
    function list(){
		$where = '';
		if($_POST['filters']){
			$groups = json_decode(htmlspecialchars_decode($_POST['filters']),true);
			$where = $this->groups2sql($groups);
		}
		$data = $this->mod->list($where, $_POST['page'], $_POST['rows'], $_POST['sidx'], $_POST['sord']);
		echo json_encode($data, JSON_UNESCAPED_UNICODE);
	}
	/**
	 * Method 通过关键字取一行数据
	 */
	function getRowByKey($key){
		$row = $this->mod->getRowByKey('*',$key);
		if($row){
			$this->exitJson(0,$row);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * Method 唯一值检测
	 */
	function onlyCheck(){
		$result = false;
		$dm_id = (empty($_REQUEST['dm_id'])) ? 0 : $_REQUEST['dm_id'];
		$dm_depa_text = $_REQUEST['dm_depa_text'];
		if($dm_depa_text!=''){
			$where = sprintf("dm_id<>'%s' AND dm_depa_text='%s'",$dm_id,$dm_depa_text);
			if($this->mod->getOne('1',$where)){
				$result = true;
			}
		}
		echo ($result) ? 'false' : 'true'; //存在不通过，不存在通过
	}
	/**
	 * Method 添加操作
	 */
	function add(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		$key = $this->mod->e['key'];
		$post = $this->mod->checkValueForm($params,[$key]);//添加时,主键可以为空
		if($post['error']){
			$this->exitJson(2001,$post['error']);
		}
		$data = $post['data'];
		$this->mod->Begin();
		$lastID = $this->mod->addRow($data);
		$dbOk = $this->mod->End();
		if($dbOk && $lastID>0){
			$this->exitJson(0,['key'=>$lastID]);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * Method 修改操作
	 */
	function edit(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		$post = $this->mod->checkValueForm($params);
		if($post['error']){
			$this->exitJson(2001,$post['error']);
		}
		$data = $post['data'];
		$key = $data[$this->mod->e['key']];
		$this->mod->Begin();
		$this->mod->setRowByKey($data,$key);
		$dbOk = $this->mod->End();
		if($dbOk){
			$this->exitJson(0,['key'=>$key]);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * Method 删除操作
	 */
	function delete(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		if(empty($params['value'])){
			$this->exitJson(2001);
		}
		$this->mod->Begin();
		$this->mod->deleteRows("dm_cycle='".trim($params['value'])."'");
		$dbOk = $this->mod->End();
		if($dbOk){
			$this->exitJson(0);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * 统计工资表 完整数据
	 */
	function report($cycle){
		if(empty($cycle))
			$this->exitJson(2001);
		$data = $this->mod->report($cycle);
		$result = [
			'page'=> 1,
			'total'=> 1,
			'records' => 2,
			"userdata"=> [],
			'rows' => $data
		];
		echo json_encode($result, JSON_UNESCAPED_UNICODE);
	}

	/**
	 * 导入
	 */
	function import(){
		$object = new objectMod();
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		$onamekeys = [];
		$cycle='';
		foreach($params as $i=>$row){
			$line = $i+1;
			$v = trim();
			foreach($row as $k=>$v){
				$v = trim($v);
				if($k=='dm_cycle'){
					preg_match('/(\d+)年(\d+)月/',$v,$m);
					if(count($m)!=3 || !is_numeric($m[1]) || !is_numeric($m[2])){
						$this->exitJson(2001,'第 '.$line.' 行，月份格式不正确, 格式例: 2020年5月');
					}
					if($m[2].''<'10') $m[2] = '0'.$m[2];
					$v = $m[1].$m[2];
					if(strlen($v)!=6){
						$this->exitJson(2001,'第 '.$line.' 行，月份格式不正确, 格式例: 2020年5月');
					}
					if($cycle==''){
						$cycle = $v;
					}
					if($cycle!=$v){
						$this->exitJson(2001,'第 '.$line.' 行，出现了新的月份，一次仅支持导入一个月份的数据');
					}
				}else if($k=='dm_depa_text'){
					$o_id = $object->getOidByName(1,$v);
					if(empty($o_id) || $v==''){
						$o_id = 0;
					}
					if(empty($o_id)){
						$this->exitJson(2001,'第 '.$line.' 行，部门 '.$v.'，没有在部门表中找到');
					}
					$row['dm_depa'] = $o_id;
				}else if($k=='dm_qty'){
					if($v=='')
						$v=1;
					else if(!is_numeric($v)){
						$this->exitJson(2001,'第 '.$line.' 行，数量 '.$v.'，不是一个数字');
					}
				}
				$row[$k]=$v;
			}
			$params[$i] = $row;
		}
		$this->mod->Begin();
		$this->mod->import($cycle,$params);
		$isOk = $this->mod->End();
		if($isOk)
			$this->exitJson(0,$params);
		else
			$this->exitJson(2000);
	}
}