<?php
/**----------------------------------------------------------------------
 * OpenCenter V3
 * Copyright 2014-2018 http://www.ocenter.cn All rights reserved.
 * ----------------------------------------------------------------------
 * Author: wdx(wdx@ourstu.com)
 * Date: 2018/10/17
 * Time: 18:33
 * ----------------------------------------------------------------------
 */

namespace app\admin\controller;

use app\admin\validate\ScoreRule;
use app\admin\validate\ScoreType;
use app\admin\model\AdminLog;

/**
 * Class Score
 * 积分控制器
 * @package app\admin\controller
 */
class Score extends Base
{
    protected $scoreType;
    protected $scoreLog;
    protected $scoreRule;
    protected $user;
    protected $userRule;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->scoreLog = model('admin/ScoreLog');
        $this->scoreType = model('admin/ScoreType');
        $this->scoreRule = model('admin/ScoreRule');
        $this->user = model('admin/User');
        $this->userRule = model('admin/UserRule');
    }

    /**
     * 积分类型
     * @author:wdx(wdx@ourstu.com)
     */
    public function typeList()
    {
        if ($this->request->isAjax()) {
            $page = input('get.page/d', 1);
            $limit = input('get.limit/d', 20);
            $map[] = ['status', '>=', 0];
            //积分类型
            $scoreList = $this->scoreType->getList($map, $page, $limit);
            foreach ($scoreList as &$val) {
                to_status($val);
            }
            $count = $this->scoreType->where($map)->count();
            $data = [
                'code' => 0,
                'msg' => '数据返回成功',
                'count' => $count,
                'data' => $scoreList
            ];
            AdminLog::setTitle('获取积分类型');
            return json($data);
        }
        AdminLog::setTitle('积分类型');
        return $this->fetch();
    }

    /**
     * 类型表单
     * @return mixed
     * @author:wdx(wdx@ourstu.com)
     */
    public function typeForm()
    {
        if ($this->request->isPost()) {
            $id = input('post.id/d', 0);
            $title = $id ? '编辑' : '新增';
            $data['title'] = input('post.title/s', '');
            $data['unit'] = input('post.unit/s', '');
            $data['intro'] = input('post.intro/s', '');
            $status = input('post.status/s', '');
            $data['status'] = ($status) ? 1 : 0;
            $validate = new ScoreType();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            } else {
                if ($id) {
                    $data['id'] = $id;
                    $rs = $this->scoreType->update($data);
                } else {
                    $rs = $this->scoreType->insert($data);
                }
                if ($rs) {
                    AdminLog::setTitle($title . '积分类型成功');
                    $this->success($title . '积分类型成功');
                } else {
                    AdminLog::setTitle($title . '积分类型失败');
                    $this->error($title . '积分类型失败');
                }
            }
        } else {
            $aid = input('get.aid/d', 0);
            $type = $this->scoreType->find($aid);
            $this->assign('type', $type);
            return $this->fetch();
        }
    }

    /**
     * 删除类型
     * @author:wdx(wdx@ourstu.com)
     */
    public function delType()
    {
        $ids = array_unique(input('post.id/a', []));
        $rs = $this->scoreType->whereIn('id', $ids)->setField('status', '-1');
        if (!$rs) {
            AdminLog::setTitle('删除积分类型');
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }

    /**
     * 规则列表
     * @return mixed|\think\response\Json
     * @author:wdx(wdx@ourstu.com)
     */
    public function ruleList()
    {
        if ($this->request->isAjax()) {
            $page = input('get.page/d', 1);
            $limit = input('get.limit/d', 20);
            $map[] = ['status', '>=', 0];
            //积分类型
            $scoreList = $this->scoreRule->getList($map, $page, $limit);
            $ruleList = $this->userRule->getRuleList();
            $timeUnit = config('app.time_unit');
            $chageType = [1 => '加', 2 => '减'];
            $scoreType = $this->scoreType->getTypeList();
            foreach ($scoreList as &$val) {
                to_status($val);
                $val['frequency'] = $val['frequency'] . '次/' . $timeUnit[$val['time_unit']];
                $val['rule_name'] = $ruleList[$val['rule_id']];
                $val['change'] = $chageType[$val['change_type']] . $val['change_num'] .$scoreType[$val['score_type']];
            }
            $count = $this->scoreType->where($map)->count();
            $data = [
                'code' => 0,
                'msg' => '数据返回成功',
                'count' => $count,
                'data' => $scoreList
            ];
            AdminLog::setTitle('获取积分规则规则列表');
            return json($data);
        }
        AdminLog::setTitle('积分规则列表');
        return $this->fetch();
    }

    /**
     * 规则表单
     * @return mixed
     * @author:wdx(wdx@ourstu.com)
     */
    public function ruleForm()
    {
        if ($this->request->isPost()) {
            $data = input('post.data/a', []);
            $title = $data['id'] ? '编辑' : '新增';
            $data['status'] = isset($data['status']) ? 1 : 0;
            $validate = new ScoreRule();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            } else {
                $data['update_time'] = time();
                if ($data['id']) {
                    $rs = $this->scoreRule->update($data);
                } else {
                    unset($data['id']);
                    $data['create_time'] = time();
                    $rs = $this->scoreRule->insert($data);
                }
                if ($rs) {
                    AdminLog::setTitle($title . '积分规则成功');
                    $this->success($title . '积分规则成功');
                } else {
                    AdminLog::setTitle($title . '积分规则失败');
                    $this->error($title . '积分规则失败');
                }
            }
        } else {
            $aid = input('get.aid/d', 0);
            $rule = $this->scoreRule->find($aid);
            //积分类型
            $scoreType = $this->scoreType->getTypeList();
            $timeUnit = config('app.time_unit');
            $ruleList = $this->userRule->getTree('id, pid, title');
            $this->assign('rule', $rule);
            $this->assign('scoreType', $scoreType);
            $this->assign('timeUnit', $timeUnit);
            $this->assign('ruleList', $ruleList);
            return $this->fetch();
        }
    }

    /**
     * 删除规则
     * @return mixed
     * @author:wdx(wdx@ourstu.com)
     */
    public function delRule()
    {
        $ids = array_unique(input('post.id/a', []));
        $rs = $this->scoreRule->whereIn('id', $ids)->setField('status', '-1');
        if ($rs) {
            AdminLog::setTitle('删除积分规则');
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 积分日志
     * @return mixed|\think\response\Json
     * @author:wdx(wdx@ourstu.com)
     */
    public function scoreLog()
    {
        if ($this->request->isAjax()) {
            $page = input('get.page/d', 1);
            $limit = input('get.limit/d', 20);
            $map = [];
            $data = $this->scoreLog->getList($map, $page, $limit);
            $scoreType = $this->scoreType->getTypeList();
            foreach ($data as &$val) {
                to_status($val);
                $val['score_type'] = $scoreType[$val['score_type']];
                to_ip($val, 'ip');
            }
            $count = $this->scoreType->where($map)->count();
            $data = [
                'code' => 0,
                'msg' => '数据返回成功',
                'count' => $count,
                'data' => $data
            ];
            AdminLog::setTitle('积分日志');
            return json($data);
        }
        return $this->fetch();
    }
}