<?php
/**
 * 系统模版
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/11/29
 * Time: 15:22
 */

namespace JinDouYun\Model\System;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Dao\System\DSystemTemplate;
use Mall\Framework\Core\StatusCode;

class MSystemTemplate
{
    private $objDSystemTemplate;

    private $onlineUserId;

    private $onlineEnterpriseId;

    public function __construct($onlineUserId, $enterpriseId)
    {
        $this->onlineUserId = $onlineUserId;
        $this->onlineEnterpriseId = $enterpriseId;

        $this->objDSystemTemplate = new DSystemTemplate('default');
    }

    /**
     * 添加系统模版
     * @param $params
     * @return ResultWrapper
     */
    public function add($params)
    {
        $dbResult = $this->objDSystemTemplate->insert($params);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 系统模版启用/停用
     * @param $params
     * @return ResultWrapper
     */
    public function updateEnableStatus($params)
    {
        $dbResult = $this->objDSystemTemplate->update(['enableStatus' => $params['enableStatus']], $params['id']);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 获取模版列表
     * @param $selectParams
     * @return array|bool|ResultWrapper
     */
    public function getAll($selectParams)
    {
        $limit = $selectParams['limit'];
        unset($selectParams['limit']);
        $offset = $selectParams['offset'];
        unset($selectParams['offset']);
        $selectParams['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDSystemTemplate->select($selectParams, '*', 'createTime desc', $limit, $offset);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
        }
        $total = $this->objDSystemTemplate->count($selectParams);

        $return = [
            'data'  => $dbResult,
            'total' => ($total) ? intval($total) : 0,
        ];
        return ResultWrapper::success($return);
    }

    /**
     * 编辑系统模版
     * @param $params
     * @return ResultWrapper
     */
    public function edit($params)
    {
        $id = $params['id'];
        unset($params['id']);
        $dbResult = $this->objDSystemTemplate->update($params, $id);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 删除系统模版
     * @param $ids
     * @return ResultWrapper
     */
    public function del($ids)
    {
        $dbResult = $this->objDSystemTemplate->update(['deleteStatus' => StatusCode::$delete],$ids);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }
}