<?php

/**
 * 基础方法
 */
defined('IN_WEB') or die('Include Error!');
class Base
{
    public function __construct()
    {

    }

    /**
     * 是否查询所有包
     * @param $sidList
     * @return bool
     */
    public function isAllSid($sidList)
    {
        if(in_array(999, $sidList)) {
            return true;
        }
        return false;
    }

    /**
     * 统一输出返回json内容
     *
     * @access public
     * @date   2016-08-16
     * @param int    $ret  状态码,0成功
     * @param string $msg  错误信息
     * @param array  $data 返回数据内容
     * @return json;
     */
    public function response($ret = 0, $msg = '', array $data = array())
    {
        $result = [
            'ret'  => $ret,
            'msg'  => $msg,
            'data' => $data,
        ];

        die(json_encode($result));
    }

    /**
     *	计算天数差
     * $format = 1 Y-m-d else Ymd
     */
    protected function getDays($day1, $day2, $format = 1)
    {
        $second1 = $day1;
        $second2 = $day2;

        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        $days = ($second1 - $second2) / 86400;
        $dayList = array();
        for ($i=0; $i <= $days ; $i++) {
            if($format == 1) {
                $dayList[] = date('Y-m-d', $second2 + 86400*$i);
            }else {
                $dayList[] = date('Ymd', $second2 + 86400*$i);
            }
        }
        return $dayList;
    }


    /**
     * 多维数组按多字段排序
     * @return mixed|null
     * @throws Exception
     * 调用方式 $arr = sortArrByManyField($array1,'id',SORT_ASC,'name',SORT_ASC,'age',SORT_DESC);
     */
    public  function sortArrByManyField(){
        $args = func_get_args();
        if(empty($args)){
            return null;
        }
        $arr = array_shift($args);
        if(!is_array($arr)){
            throw new Exception("第一个参数不为数组");
        }
        foreach($args as $key => $field){
            if(is_string($field)){
                $temp = array();
                foreach($arr as $index=> $val){
                    $temp[$index] = $val[$field];
                }
                $args[$key] = $temp;
            }
        }
        $args[] = &$arr;//引用值
        call_user_func_array('array_multisort',$args);
        return array_pop($args);
    }
}