<?php
/**
 * @Statement: [ An Ordinary Person ]
 * @Author: 闻子 < QQ：270988107 >
 * @Copyright: 闻子 2016-2019 All rights reserved.
 *
 *    .--,       .--,
 *   ( (  \.---./  ) )
 *    '.__/o   o\__.'
 *       {=  ^  =}
 *        >  -  <
 *       /       \
 *      //       \
 *     //|   .   |\
 *     "'\       /'"_.-~^`'-.
 *        \  _  /--'         `
 *      ___)( )(___
 *     (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之.
 *
 */

namespace app\common\logic;

use think\Db;

class Scode extends Basedata
{
    /**
     * 定义模型
     * @Author 闻子 <270988107@qq.com>
     */
    public static $ScodeModel = null;

    /**
     * 实例化模型
     * @Author 闻子 <270988107@qq.com>
     */
    public function __construct()
    {
        parent::__construct();
        self::$ScodeModel = model($this->name);
    }

    /**
     * 获取源码数据逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getScodeInfo($where = [], $field = true)
    {
        return self::$ScodeModel->getInfo($where, $field);
    }

    /**
     * 获取源码列表逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getScodeList($where = [], $data = '')
    {
        return self::$ScodeModel->getWhereOrder($where, $data);
    }

    /**
     * 源码统计数据逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getScodeCount($where = [])
    {
        return self::$ScodeModel->stat($where);
    }

    /**
     * 源码数据提交逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function formdata($data)
    {
        if (empty($data['catid'])) {
            return [RESULT_ERROR, '请添加分类再提交'];
        }
        $userinfo = Db::name('user')->where('id',session('admin.id'))->find();
        if ($userinfo['manager'] == 1) {
            $data['examine'] = '1';
        }
        $data['catid'] = implode(",",$data['catid']);
    	$data['create_time'] = date('Y-m-d H:i:s');
        return self::$ScodeModel->setInfo($data) ? [RESULT_SUCCESS, '提交成功'] : [RESULT_ERROR, self::$ScodeModel->getError()];
    }

    /**
     * 源码编辑逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getedit($data)
    {
        return self::$ScodeModel->setInfo($data) ? ['code' => 1, 'msg' => '提交成功', 'data' => ''] : ['code' => 0, 'msg' => '提交失败', 'data' => ''];
    }

    /**
     * 源码删除逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getScodeDel($id)
    {
        if (empty($id)) {
            return ["ERROR", '参数异常'];
        }
        $data['id'] = $id;
        return self::$ScodeModel->deleteyi($data) ? ['code' => 1, 'msg' => '删除成功', 'data' => ''] : ['code' => 0, 'msg' => '删除失败', 'data' => ''];
    }

    /**
     * 批量删除源码逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getScodealldel($data)
    {
        $str = rtrim($data, ","); 
        $row = self::$ScodeModel->deleteyi($str);
        return $row ? ['code' => 1, 'msg' => '删除成功', 'data' => ''] : ['code' => 1, 'msg' => '删除失败', 'data' => ''];
    }

}