<?php

/**
 * 控制器:后台系统-通知公告
 * date:2019-08-31
 */

namespace app\modules\manage\v1\controllers;

use app\modules\common\Helper;
use app\modules\logic\model\NoticeModel;
use Yii;
use yii\web\Controller;

class NoticeController extends MiddleController {
	/**
	 * 返回
	 * @param $code
	 * @param string $info
	 * @param string $controller
	 * @param string $total
	 */
	protected function response($code, $info = '', $total = '', $controller = 'notice') {
		return parent::response($code, $info, $total, $controller);
	}
	/**
     * [actionList 通知公告列表]
     * @apiDoc
     * @api     v1/notice/list
     * @group   notice
     * @name    通知/公告列表
     * @desc    管理后台-通知公告列表
     * @method  POST
     * @param currentPage string 当前页 noreq 1
     * @param pageSize string 每页条数 noreq 15
     * @author JOHN.W
     * @version [1.0]
     * @return  json
        {
        "code": "000000",
        "msg": "操作成功",
        "total": "1",
        "info": [
        {
            "id": "1",
            "prov_id": "0",
            "city_id": "0",
            "area_id": "0",
            "type": "1",
            "content": "通知消息来拉",
            "operator_id": "0",
            "created": "2019-09-09 11:09:40",
            "type_show": "门店",
            "operator_id_show": "--"
        }
        ]
        }
     */
    public function actionList(){
        $_where = [];
        $_where['order'] = ['id' => 'desc'];
        /*分页处理*/
        $currentPage = 1;
        $pageSize = 15;
        if (isset($this->para['currentPage']) && !empty($this->para['currentPage']) && is_numeric($this->para['currentPage'])) {
            $currentPage = $this->para['currentPage'];
        }
        if (isset($this->para['pageSize']) && !empty($this->para['pageSize']) && is_numeric($this->para['pageSize'])) {
            $pageSize = $this->para['pageSize'];
        }
        $Notice = new NoticeModel();
        $total = $Notice->getWidgetTotal($_where);
        $list = $Notice->getWidgetPages($_where, $pageSize, $pageSize * ($currentPage - 1));
        if (!empty($list)) {
            foreach ($list as &$value) {
                $value['type_show'] = Helper::getNoticeObj($value['type']);
                $value['operator_id_show'] = Helper::getOperatorName($value['operator_id']);
                $value['created'] = date("Y-m-d H:i:s", $value['created']);
            }
        }
        return $this->response('search_succ', $list, $total);
    }
    /**
     * [actionAdd 发布通知/公告]
     * @apiDoc
     * @api     v1/notice/add
     * @group   notice
     * @name    发布通知/公告
     * @desc    管理后台-通知公告列表-发布
     * @method  POST
     * @param prov_id int 省ID noreq 0
     * @param city_id int 市ID noreq 0
     * @param area_id int 区ID noreq 0
     * @param content string 通知内容 req
     * @param type string 接收对象【0全部，1门店，2技师】 req
     * @param created string 发布时间 noreq now_time
     * @author JOHN.W
     * @version [1.0]
     * @return  json
     {
        "code": "000000",
        "msg": "操作成功",
        "info": "发布成功"
        }
     */
    public function actionAdd(){
        if (!isset($this->para['content']) || empty($this->para['content'])) {
            return $this->response('para_miss', '内容不能为空');
        }
        if (!isset($this->para['type']) || !in_array($this->para['type'], ['0','1','2'])) {
            return $this->response('para_miss', '接收对象不能为空');
        }
        $rec_role = $this->rec_role;
        $operator_id = isset($rec_role['operator_id'])?$rec_role['operator_id']:0;
        $param = [
            'prov_id'=>isset($this->para['prov_id'])?$this->para['prov_id']:0,
            'city_id'=>isset($this->para['city_id'])?$this->para['city_id']:0,
            'area_id'=>isset($this->para['area_id'])?$this->para['area_id']:0,
            'content'=>$this->para['content'],
            'type'=>$this->para['type'],
            'created'=>isset($this->para['created'])?strtotime($this->para['created']):time(),
        ];
        $Notice = new NoticeModel();
        $res = $Notice->saveUs($param);
        if($res){
            return $this->response('deal_succ', '发布成功');
        }else{
            return $this->response('deal_false', '发布失败');
        }   
    }
}