<?php
/**
 * 系统公告
 * Created by PhpStorm.
 * User: 小威
 * Date: 2020/04/16
 * Time: 17:22
 */

namespace JinDouYun\Model\System;

use Exception;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Factory;

use JinDouYun\Dao\System\DAnnouncement;


class MAnnouncement
{

    private $objDAnnouncement;
    private $enterpriseId;

    public function __construct($enterpriseId)
    {
        $this->enterpriseId = $enterpriseId;
        $this->objDAnnouncement = new DAnnouncement('default');

    }

    /**
     * 公告添加
     * @param $params
     * @return ResultWrapper
     * @throws Exception
     */
    public function addAnnouncement($params)
    {
        $params['createTime'] = time();
        $params['updateTime'] = time();
        $params['enterpriseId'] = $this->enterpriseId;
        $dbResult = $this->objDAnnouncement->insert($params);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 公告删除
     * @param $params
     * @return ResultWrapper
     */
    public function delAnnouncement($params)
    {
        $dbResult = $this->objDAnnouncement->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['id' => $params]);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 公告弹框
     * @param $where
     * @return ResultWrapper
     */
    public function upAnnouncement($where)
    {
        $where['enterpriseId'] = $this->enterpriseId;
        $dbResult = $this->objDAnnouncement->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        $update = ['updateTime' => time()];
        if($dbResult['upStatus'] == StatusCode::$standard){
            $update['upStatus'] = StatusCode::$delete;
        }else{
            $update['upStatus'] = StatusCode::$standard;
            $dbResult = $this->objDAnnouncement->update(['upStatus' => StatusCode::$delete], ['type' => $dbResult['type'], 'enterpriseId' => $this->enterpriseId]);
            if ($dbResult === false) {
                return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
            }
        }

        $dbResult = $this->objDAnnouncement->update($update, $where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 公告修改
     * @param $params
     * @param $id
     * @return ResultWrapper
     */
    public function editAnnouncement($params, $id)
    {
        $params['updateTime'] = time();
        if($params['status'] == StatusCode::$standard){
            $endDbResult = $this->objDAnnouncement->update(['status' => StatusCode::$delete, 'updateTime' => time()], ['enterpriseId' => $this->enterpriseId, 'type' => $params['type']]);
            if($endDbResult === false){
                return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
            }
        }
        $dbResult = $this->objDAnnouncement->update($params, ['id' => $id, 'enterpriseId' => $this->enterpriseId]);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 公告启用/禁用
     * @param $id
     * @return ResultWrapper
     */
    public function onAnnouncement($id)
    {
        $dbResult = $this->objDAnnouncement->get(['id' => $id]);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        $update['status'] = StatusCode::$standard;
        $update['updateTime'] = time();
        if($dbResult['status'] == StatusCode::$standard){
            $update['status'] = StatusCode::$delete;
        }else{
            $endDbResult = $this->objDAnnouncement->update(['status' => StatusCode::$delete, 'updateTime' => time()], ['enterpriseId' => $this->enterpriseId, 'type' => $dbResult['type']]);
            if($endDbResult === false){
                return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
            }
        }
        $dbResult = $this->objDAnnouncement->update($update,['id' => $id]);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 获取启用公告
     */
    public function getAnnouncement($where = [])
    {
        $where['status'] = StatusCode::$standard;
        $where['enterpriseId'] = $this->enterpriseId;
        $dbResult = $this->objDAnnouncement->get($where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 公告列表
     * @param $selectParams
     * @return ResultWrapper
     */
    public function getAllAnnouncement($selectParams)
    {
        $limit = $selectParams['limit'];
        unset($selectParams['limit']);
        $offset = $selectParams['offset'];
        unset($selectParams['offset']);
        $where = ['deleteStatus' => StatusCode::$standard, 'enterpriseId' => $this->enterpriseId];

        if(isset($selectParams['type'])){
            $where['type'] = $selectParams['type'];
        }

        $dbResult = $this->objDAnnouncement->select($where, '*', 'createTime desc', $offset, $limit);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        $total = $this->objDAnnouncement->count($where);
        $return = [
            'data' => $dbResult,
            'total' => ($total) ? intval($total) : 0,
        ];
        return ResultWrapper::success($return);
    }

    /**
     * 公告详情
     * @param $where
     * @return ResultWrapper
     */
    public function getAnnouncementInfo($where = [])
    {
        $where['enterpriseId'] = $this->enterpriseId;
        $where['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDAnnouncement->get($where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDAnnouncement->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }
}