<?php

namespace app\api\model;

use think\Model;
use think\Db;

class Folder extends Model
{
    public function index()
    {
        $user = Db::name('folder')->where('state', '=', -1)->find();
        return $user;
//        $sql = Db::name('folder')->getLastSql();
////        var_dump($sql);
    }

    //获取文件夹名称
    public function folderList($parentid)
    {
        if (!$parentid) {
            $where = ['parentid' => '', 'state' => 0];

        } else {
            $where = ['parentid' => $parentid, 'state' => 0];
        }
        $folderlist = Db::name('folder')->where($where)->order('')->select();

//        $sql = Db::name('folder')->getLastSql();
//        var_dump($sql);
        return $folderlist;

    }

    public function addFolder($name, $parentid)
    {

        $folderid = randomkeys(18);
//         $folderid='bpy0p4617b7fun9lxqa1';
        if (!$parentid) {
            $parentid = '';
        }
        $foldername = Db::name('folder')->where(['folderid' => $folderid])->count();

        $rs = new Folder();
        if ($foldername == 0) {
//            $data = [
//                'folderid' => $folderid,
//                'name' => $name,
//                'updatetime' => date("Y-m-d H:i:s", time()),
//                'parentid' => $parentid,
//                'state' => 0
//            ];
//            $childfolder_nums = Db::name('folder')->where(['parentid' => $parentid])->count();
//
//            $rs->isUpdate(false)->save($data);
//            if ($rs) {
//                $items = $this->where(['folderid' => $parentid])->setField('items', $childfolder_nums + 1);
//            }
//            $return = $rs->folderid;
//            return $return;
            // 启动事务
            Db::startTrans();
            try {
                $data = [
                    'folderid' => $folderid,
                    'name' => $name,
                    'updatetime' => date("Y-m-d H:i:s", time()),
                    'parentid' => $parentid,
                    'state' => 0
                ];
                $childfolder_nums = Db::name('folder')->where(['parentid' => $parentid])->count();
                $res = $rs->isUpdate(false)->save($data);
                if ($res !== false) {
                    if ($parentid) {
                        $items = $this->where(['folderid' => $parentid])->setField('items', $childfolder_nums + 1);
                        if ($items) {
                            $return = $rs->folderid;
                        } else {
                            throw new \Exception('添加失败');
                        }
                    } else {

                        $return = $rs->folderid;
                    }
                }
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $return = ['code' => -200, 'msg' => $e->getMessage()];
            }

        } else {
            $return = ['code' => -10002, 'msg' => '文件名重复'];
        }
        return $return;

    }

    public function editFolder($name, $parentid, $folderid)
    {
        $rs = new Folder();


        $where = '';
        $data = [
            'name' => $name,
        ];
        if ($folderid) {
            $where = ['folderid' => $folderid];

        }
        if ($parentid) {
            $where = ['parentid' => $parentid];
        }

        $oldname = Db::name('folder')->where($where)->value('name');
        if ($name != $oldname) {
            $rs = $this->where($where)->update($data);
            if ($rs === false) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }


//        var_dump($rs);die();
//                $sql = Db::name('folder')->getLastSql();
//       var_dump($sql);

    }

    public function delFolder($folderid)
    {
        $rs = new Folder();
        $where = ['folderid' => $folderid];
        $data = [
            'state' => -1,
        ];

        $rs = $this->where($where)->update($data);
        if ($rs === false) {
            return false;
        } else {
            return true;
        }


//        var_dump($rs);die();
//                $sql = Db::name('folder')->getLastSql();
//       var_dump($sql);

    }
}
