<?php

/**
 *
 * @method 接口管理
 * @author zhengweihua 
 * @copyright 2022-08-26
 */
class MainApiUser extends MainBase
{
    /**
     * @param  Array $dataArr
     * @method 保存配置
     * @author zhengweihua 
     * @copyright 2022-08-26
     */
    public function apiUserSet($dataArr = [])
    {
        if (empty($dataArr['au_uid'])) return LibFc::ReturnData(false, '提供的参数不正确');

        $this->MainDb->Begin();
        $data  = [
            'au_status' => (int)$dataArr['au_status'],
            'au_uid' => (int)$dataArr['au_uid'],
            'au_comname' => $dataArr['au_comname'],
            'au_btime' => (int)$dataArr['au_btime'],
            'au_etime' => (int)$dataArr['au_etime'],
            'au_remark' => $dataArr['au_remark'],
            'au_ado_id' => (int)$dataArr['ado_id'],
        ];
        $data['au_appkey'] = $dataArr['au_appkey']?:$dataArr['au_uid'];
        $data['au_appsecret'] = $dataArr['au_appsecret']?:md5('PJD' . $data['au_uid']);
        if (strlen($data['au_appsecret']) > 32) return LibFc::ReturnData(false, 'appsecret长度不正确');
        
        $where = sprintf(" and au_uid=%d", $data['au_uid']);
        $res = $this->get('api_user', ['au_uid'], $where, true);
        if ($res['data']) {
            unset($data['au_uid']);
            $res = $this->set('api_user', $data, $where);
        } else {
            $res = $this->add('api_user', $data);
        }
        //允许的接口
        if (!empty($dataArr['u_act'])) {
            $where = sprintf(" and aua_uid = %d", $dataArr['au_uid']);
            $oldres = $this->get('api_user_act', ['*'], $where);
            $oldArr = $oldres['data'] ? array_column($oldres['data'], 'aua_aa_id') : [];
            //新增的
            $addArr = array_diff($dataArr['u_act'], $oldArr);
            if (!empty($addArr)) {
                $addData = [];
                foreach ($addArr as $item) {
                    $addData = [
                        'aua_uid' => $dataArr['au_uid'],
                        'aua_aa_id' => $item
                    ];
                    $this->add('api_user_act', $addData);   
                }
            }
            //删除的
            $delArr = array_diff($oldArr, $dataArr['u_act']);

            if (!empty($delArr)) {
                $delWhere = sprintf(" and aua_uid = %d and aua_aa_id in (%s)", $dataArr['au_uid'], implode(',', $delArr));
                $this->del('api_user_act', $delWhere);
            }
        }
        $this->MainDb->End();
        return LibFc::ReturnData(true);
    }

    /**
     * @method 删除接口管理
     * @author zhengweihua 
     * @copyright 2022-08-26
     */
    public function delApiSet($au_uid)
    {
        if (empty($au_uid)) return LibFc::ReturnData(false, '提供的参数不正确');

        $this->MainDb->Begin();

        //删除接口配置
        $where = sprintf(" and au_uid=%d", $au_uid);
        $res =  $this->get('api_user', ['au_uid'], $where, true);
        if ($res['data']) $this->del('api_user', $where);

        //删除支持的接口
        $where = sprintf(" and aua_uid=%d", $au_uid);
        $actres =  $this->get('api_user_act', ['*'], $where);
        if ($actres['data']) $this->del('api_user_act', $where);
        $this->MainDb->End();
        return LibFc::ReturnData(true);
    }
}
