<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\export;


use app\common\model\export\Export;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * Export逻辑
 * Class ExportLogic
 * @package app\adminapi\logic
 */
class ExportLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/11/18 13:31
     */
    public static function add(array $params)
    {
        Db::startTrans();
        try {
            $export = Export::create([
                'download_type' => $params['download_type'],
                'download_fun' => isset($params['download_fun'])?$params['download_fun']:'',
                'params' => isset($params['params'])?$params['params']:[],
                'name' => $params['name'],
                'down_num' => isset($params['down_num'])?$params['down_num']:0,
                'status' => isset($params['status'])?$params['status']:0,
                'expiration_time' => isset($params['expiration_time'])?$params['expiration_time']:0,
                'file_url' => isset($params['file_url'])?$params['file_url']:'',
                'generate_status' => isset($params['generate_status'])?$params['generate_status']:0,
                'clear_status' => isset($params['clear_status'])?$params['clear_status']:0,
                'admin_id' => isset($params['admin_id'])?$params['admin_id']:0,
                'createtime' => isset($params['createtime'])?$params['createtime']:time(),
                'updatetime' => isset($params['updatetime'])?$params['updatetime']:time(),
            ]);
            $exportId = $export->id??0;
            Db::commit();
            return $exportId;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/11/18 13:31
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            Export::where('id', $params['id'])->update([
                'download_type' => $params['download_type'],
                'download_fun' => $params['download_fun'],
                'params' => $params['params'],
                'name' => $params['name'],
                'down_num' => $params['down_num'],
                'status' => $params['status'],
                'expiration_time' => $params['expiration_time'],
                'file_url' => $params['file_url'],
                'generate_status' => $params['generate_status'],
                'clear_status' => $params['clear_status'],
                'admin_id' => $params['admin_id'],
                'createtime' => $params['createtime'],
                'updatetime' => $params['updatetime'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/11/18 13:31
     */
    public static function delete(array $params): bool
    {
        return Export::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/11/18 13:31
     */
    public static function detail($params): array
    {
        return Export::findOrEmpty($params['id'])->toArray();
    }
}