<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */
use think\facade\Db;

//读取文章分类列表
function get_article_cate()
{
    $cate = \think\facade\Db::name('ArticleCate')->order('create_time asc')->select()->toArray();
    return $cate;
}

//写入日志
function add_project_log($uid,$module,$param,$old)
{
	$log_data = [];
	$key_array = ['id', 'create_time', 'update_time', 'delete_time', 'over_time', 'md_content'];
	foreach ($param as $key => $value) {
		if (!in_array($key, $key_array)) {
			$log_data[] = array(
				'module' => $module,
				'field' => $key,
				$module . '_id' => $param['id'],
				'admin_id' => $uid,
				'old_content' => $old[$key],
				'new_content' => $value,
				'create_time' => time(),
			);
		}
	}
	Db::name('ProjectLog')->strict(false)->field(true)->insertAll($log_data);
}

function isAuthProject($uid)
{
	if($uid == 1){
		return 1;
	}
	$map = [];
	$map[] = ['name', '=', 'project_admin'];
	$map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',uids)")];
    $count = Db::name('DataAuth')->where($map)->count();
    return $count;
}

//读取会议室
function oa_meeting_cate_column()
{
	$list = Db::name('MeetingCate')->where(['status' => 1])->column('title', 'id');
	return $list;
}

//读取部门列表
function get_department_column()
{
    $department = Db::name('Department')->order('sort desc,id asc')->where(['status' => 1])->column('title', 'id');
    return $department;
}

//读取印章类型
function oa_seal_cate_column()
{
    $list = Db::name('SealCate')->where(['status' => 1])->column('title', 'id');
    return $list;
}

//读取车辆类型
function oa_car_cate_column()
{
    $list = Db::name('CarCate')->where(['status' => 1])->column('title,name', 'id');
	$car = [];
	foreach($list as $k=>$v) {
		$car[$k] = $v['title'] . '[' . $v['name']. ']';
	}
    return $car;
}

//读取费用类型
function oa_cost_cate_column()
{
    $list = Db::name('CostCate')->where(['status' => 1])->column('title', 'id');
    return $list;
}

//读取职位
function get_position_column()
{
    $position = Db::name('Position')->where(['status' => 1])->column('title', 'id');
    return $position;
}

//是否是报销打款管理员,count>1即有权限
function isAuthExpense($uid)
{
	if($uid == 1){
		return 1;
	}
	$map = [];
	$map[] = ['name', '=', 'finance_admin'];
	$map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',uids)")];
    $count = Db::name('DataAuth')->where($map)->count();
    return $count;
}

//是否是发票管理员,count>1即有权限
function isAuthInvoice($uid)
{
	if($uid == 1){
		return 1;
	}
	$map = [];
	$map[] = ['name', '=', 'finance_admin'];
	$map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',conf_1)")];
    $count = Db::name('DataAuth')->where($map)->count();
    return $count;
}

//是否是到账管理员,count>1即有权限
function isAuthIncome($uid)
{
	if($uid == 1){
		return 1;
	}
	$map = [];
	$map[] = ['name', '=', 'finance_admin'];
	$map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',conf_2)")];
    $count = Db::name('DataAuth')->where($map)->count();
    return $count;
}