<?php

namespace app\admin\service;

use app\admin\service\AdminBaseService;
use app\model\VoteModel;

class VoteService extends AdminBaseService
{

    public function index($param)
    {
        try {
            $vote = VoteModel::where('is_delete', VoteModel::IS_DELETE_NO)
                ->order('id', 'desc');
            $total = $vote->count();
            $list = $vote->page($param['page'], $param['limit'])->select();

            return $this->toData('0', 'SUCCESS', ['list' => $list, 'total' => $total]);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }

    public function add($param)
    {
        try {

            if (empty($param['code'])) {
                return $this->toData('1', 'code无效');
            }

            if (empty($param['title'])) {
                return $this->toData('1', '标题无效');
            }

            if (empty($param['icon'])) {
                return $this->toData('1', '图标无效');
            }

            if (empty($param['content'])) {
                return $this->toData('1', '内容无效');
            }

            $vote = new VoteModel();
            $vote->code = $param['code'];
            $vote->title = $param['title'];
            $vote->icon = $param['icon'];
            $vote->content = $param['content'];
            $vote->score_num = $param['score_num'] ?? 0;
            $vote->sort = $param['sort'] ?? 0;
            $vote->oneday_times = $param['oneday_times'] ?? 1;
            $vote->create_time = date('Y-m-d H:i:s');
            $vote->update_time = date('Y-m-d H:i:s');
            $vote->save();

            return $this->toData('0', 'SUCCESS');
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }


    public function edit($param)
    {
        try {
            if (empty($param['id']) || !is_numeric($param['id'])) {
                return $this->toData('1', '目标不存在');
            }

            $vote = VoteModel::where('id', $param['id'])
                ->where('is_delete', VoteModel::IS_DELETE_NO)
                ->find();
            if (empty($doc)) {
                return $this->toData('1', '目标不存在');
            }

            if (empty($param['code'])) {
                return $this->toData('1', 'code无效');
            }

            if (empty($param['title'])) {
                return $this->toData('1', '标题无效');
            }

            if (empty($param['icon'])) {
                return $this->toData('1', '图标无效');
            }

            if (empty($param['content'])) {
                return $this->toData('1', '内容无效');
            }

            $isHas = VoteModel::where('is_delete', VoteModel::IS_DELETE_NO)
                ->where('id', '<>', $param['id'])
                ->where('title', $param['title'])->find();
            if (!empty($isHas)) {
                return $this->toData('1', '标题在相同内容');
            }

            $vote->code = $param['code'];
            $vote->title = $param['title'];
            $vote->icon = $param['icon'];
            $vote->content = $param['content'];
            $vote->score_num = $param['score_num'] ?? 0;
            $vote->sort = $param['sort'] ?? 0;
            $vote->oneday_times = $param['oneday_times'] ?? 1;
            $vote->update_time = date('Y-m-d H:i:s');
            $vote->save();

            return $this->toData('0', 'SUCCESS');
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }

    public function del($param)
    {
        try {
            if (empty($param['id']) || !is_numeric($param['id'])) {
                return $this->toData('1', '目标不存在');
            }

            $vote = VoteModel::where('id', $param['id'])
                ->where('is_delete', VoteModel::IS_DELETE_NO)
                ->find();
            if (empty($vote)) {
                return $this->toData('1', '目标不存在');
            }

            $vote->is_delete = VoteModel::IS_DELETE_YES;
            $vote->save();

            return $this->toData('0', 'SUCCESS');
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }
}