<?php


namespace wstmart\app\controller;


use think\Db;
use think\Exception;

class Note extends Base
{
    public function index()
    {
        $userId = (int)session('WST_USER.userId');
        $model = Db::name('note')->field(true)
            ->where(["user_id" => $userId])->order("update_time", "desc")
            ->select();
        return WSTReturn("OK", 1, $model);
    }

    public function detail()
    {
        $userId = (int)session('WST_USER.userId');
        if (($id = (int)input('id', 0)) > 0) {
            $detail = Db::name('note')
                ->where(["user_id" => $userId, "id" => $id])->find();
            if (!$detail) return WSTReturn("该条内容已被删除", 0);
            $detail['content'] = htmlspecialchars_decode($detail['content']);
            return WSTReturn("OK", 1, $detail);
        }
        return WSTReturn("异常请求", 0);
    }

    public function save()
    {
        $userId = (int)session('WST_USER.userId');
        $id = (int)input('post.id', 0);
        $title = input("post.title");
        $content = request()->post("content");
        $content = htmlspecialchars_decode($content);
        if (empty($title)) return WSTReturn("请填写标题", 0);
        if (empty($content)) return WSTReturn("请填写内容", 0);
        if ($id > 0) {
            $detail = Db::name('note')->field(true)
                ->where(["user_id" => $userId, "id" => $id])->select();
            if (!$detail) return WSTReturn("该条内容已被删除", 0);
            Db::name('note')->where(["user_id" => $userId, "id" => $id])
                ->update([
                    "title" => $title,
                    "content" => $content,
                    "update_time" => date("Y-m-d H:i:s"),
                ]);
            return WSTReturn("成功", 1);
        } elseif ($id == 0) {
            Db::name('note')->insert([
                "title" => $title,
                "content" => $content,
                "user_id" => $userId,
                "create_time" => date("Y-m-d H:i:s"),
                "update_time" => date("Y-m-d H:i:s"),
            ]);
            return WSTReturn("成功", 1);
        }
        return WSTReturn("异常请求", 0);
    }

    public function delete() {
        $userId = (int)session('WST_USER.userId');
        $id = (int)input('post.id', -1);
        Db::name('note')
            ->where(["user_id" => $userId, "id" => $id])->delete();
        return WSTReturn("OK", 1);
    }

    public function creditIndex()
    {
        $userId = (int)session('WST_USER.userId');
        $model = Db::name('note_credit')->field(true)
            ->where(["user_id" => $userId])->order("update_time", "desc")
            ->select();
        return WSTReturn("OK", 1, $model);
    }

    public function creditDetail()
    {
        $userId = (int)session('WST_USER.userId');
        if (($id = (int)input('id', 0)) > 0) {
            $detail = Db::name('note_credit')
                ->where(["user_id" => $userId, "id" => $id])->find();
            if (!$detail) return WSTReturn("该条内容已被删除", 0);
            $detail['content'] = htmlspecialchars_decode($detail['content']);
            return WSTReturn("OK", 1, $detail);
        }
        return WSTReturn("异常请求", 0);
    }

    public function creditDetailInfo()
    {
        $userId = (int)session('WST_USER.userId');
        if (($id = (int)input('id', 0)) > 0) {
            $detail = Db::name('note_credit_detail')
                ->order('create_time', 'desc')
                ->where(["user_id" => $userId, "credit_id" => $id])->select();
            foreach ($detail as &$item) {
                $item['create_date'] = date('Y-m-d', strtotime($item['create_time']));
            }
            return WSTReturn("OK", 1, $detail);
        }
        return WSTReturn("异常请求", 0);
    }

    public function creditSave()
    {
        $userId = (int)session('WST_USER.userId');
        $id = (int)input('post.id', -1);
        $name = input("post.name");
        $cur_cash = input('post.cur_cash');
        $credit_cash = input('post.credit_cash');
        $return_date = input('post.return_date', 1);
        if ($id == 0){
            Db::startTrans();
            try {
                $id = Db::name('note_credit')->insertGetId([
                    "user_id" => $userId, "id" => $id,
                    "cur_cash" => $cur_cash,
                    'name' => $name,
                    'credit_cash' => $credit_cash,
                    "return_date" => $return_date,
                ]);
                Db::name("note_credit_detail")->insert([
                    "credit_id" => $id,
                    "user_id"=>$userId,
                    "type" => 0,
                    "amount" => $cur_cash,
                    "create_time" => date("Y-m-d H:i:s"),
                ]);
                Db::commit();
                return WSTReturn("OK", 1);
            } catch (\Exception $e) {
                Db::rollback();
                errLog($e);
                return WSTReturn('操作失败', -1);
            }
        }
        $detail = Db::name('note_credit')
            ->where(["user_id" => $userId, "id" => $id])->find();
        if (!$detail) return WSTReturn("该条内容已被删除", 0);
        Db::startTrans();
        try {
            if ($detail['cur_cash'] != $cur_cash) {
                Db::name('note_credit')
                    ->where(["user_id" => $userId, "id" => $id])
                    ->update(compact('cur_cash'));
                Db::name("note_credit_detail")->insert([
                    "credit_id" => $id,
                    "user_id"=>$userId,
                    "type" => 9,
                    "amount" => $cur_cash,
                    "create_time" => date("Y-m-d H:i:s"),
                ]);
            }
            Db::name('note_credit')
                ->where(["user_id" => $userId, "id" => $id])
                ->update(compact('name', 'credit_cash', 'return_date'));
            Db::commit();
            return WSTReturn("OK", 1);
        } catch (Exception $e) {
            Db::rollback();
            errLog($e);
            return WSTReturn('操作失败', -1);
        }
    }

    public function creditAdd()
    {
        $userId = (int)session('WST_USER.userId');
        $id = (int)input('post.id', 0);
        $type = (int)input("post.type", 1);
        $amount = (float)input("post.amount");
        $credit = Db::name('note_credit')->where([
            "user_id" => $userId,
            "id" => $id,
        ])->field(true)->find();
        if (empty($credit)) {
            return WSTReturn("数据不存在", 0);
        }
        switch ($type) {
            case 1:
                //+
                $credit["cur_cash"] -= $amount;
                break;
            case 2:
                //-
                $credit["cur_cash"] += $amount;
                break;
        }
        Db::startTrans();
        try {
            Db::name('note_credit')->where([
                "user_id" => $userId,
                "id" => $id,
            ])->update($credit);
            Db::name("note_credit_detail")->insert([
                "credit_id" => $id,
                "user_id"=>$userId,
                "type" => $type,
                "amount" => $amount,
                "create_time" => date("Y-m-d H:i:s"),
            ]);
            Db::commit();
            return WSTReturn("OK", 1);
        } catch (\Exception $e) {
            Db::rollback();
            errLog($e);
            return WSTReturn('操作失败', -1);
        }
    }

    public function creditDelete() {
        $userId = (int)session('WST_USER.userId');
        $id = (int)input('post.id', -1);
        Db::name('note_credit')
            ->where(["user_id" => $userId, "id" => $id])->delete();
        return WSTReturn("OK", 1);
    }

    public function creditDeleteInfo () {
        $userId = (int)session('WST_USER.userId');
        $id = (int)input('post.id', 0);
        $cid = (int)input('post.credit_id', 0);
        $credit = Db::name('note_credit')->where([
            "user_id" => $userId,
            "id" => $cid,
        ])->field(true)->find();
        $detail = Db::name("note_credit_detail")->where([
            "credit_id" => $cid,
            "user_id"=>$userId,
            "id" => $id,
        ])->field(true)->find();
        if (empty($credit) || empty($detail)) {
            return WSTReturn("数据不存在", 0);
        }
        switch ((int)$detail['type']) {
            case 1:
                //+
                $credit["cur_cash"] += $detail['amount'];
                break;
            case 2:
                //-
                $credit["cur_cash"] -= $detail['amount'];
                break;
        }
        Db::startTrans();
        try {
            Db::name('note_credit')->where([
                "user_id" => $userId,
                "id" => $cid,
            ])->update($credit);
            Db::name("note_credit_detail")->where([
                "credit_id" => $cid,
                "user_id"=>$userId,
                "id" => $id,
            ])->delete();
            Db::commit();
            return WSTReturn("OK", 1);
        } catch (\Exception $e) {
            Db::rollback();
            errLog($e);
            return WSTReturn('操作失败', -1);
        }
    }
}