<?php

/**
 * 控制器:后台系统基础设置-单点设置控制器
 * 调度维护
 * 门店付款权限维护
 * 提现规则
 * 保险设置
 * 客服联系方式维护
 * 技师处罚维护
 * 门店取消订单规则
 * 技师星级维度权重
 * 评价维护
 * 错误代码：015
 * date:2019-08-31
 */

namespace app\modules\manage\v1\controllers;

use app\modules\common\Helper;
use Yii;
use yii\web\Controller;
use app\modules\logic\model\SysBaseSetModel;
use app\modules\logic\model\SysPaySetModel;

class SysotherController extends MiddleController {
	/**
	 * 返回
	 * @param $code
	 * @param string $info
	 * @param string $controller
	 * @param string $total
	 */
	protected function response($code, $info = '', $total = '', $controller = 'system') {
		return parent::response($code, $info, $total, $controller);
	}
	/**
	 * [actionModset 更新公用配置]
	 * @apiDoc
	 * @api     v1/sysother/modset
	 * @group   sysother
	 * @name    新增/修改配置
	 * @desc    基础配置- 、 调度维护、 门店付款权限维护、 提现规则、 保险设置、 客服联系方式维护、 技师处罚维护 、 门店取消订单规则、 技师星级维度权重 、 评价维护 接口
	 * @method  POST
	 * @param aliasname string 配置项别名[diy] req
	 * @param extra json 扩展配置 noreq
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
	 {
		"code": "000000",
		"msg": "操作成功",
		"info": "4"
		}
	 */
	public function actionModset(){
		if (!isset($this->para['aliasname']) || empty($this->para['aliasname'])) {
            return $this->response('para_miss', '请选择维护配置类别');
        }
        $SysBaseSet = new SysBaseSetModel();
        $flag =  $SysBaseSet->getWidgetRow(['aliasname'=>$this->para['aliasname']]);
        $data = Helper::elements(['aliasname','extra','name'], $this->para, '');
        $data['name'] = Helper::getNameByAliasname($data['aliasname']);
        $data['created'] = time();
        $res = $SysBaseSet->saveUs($data,isset($flag['id'])?isset($flag['id']):0);
        if($res){
			return $this->response('deal_succ', $res);
		}else{
			return $this->response('deal_false', $res);
		}
	}
	/**
	 * [actionDetailset 通过aliasname获取配置详情]
	 * @apiDoc
	 * @api     v1/sysother/detailset
	 * @group   sysother
	 * @name    获取配置详情
	 * @desc    基础配置- 、 调度维护、 门店付款权限维护、 提现规则、 保险设置、 客服联系方式维护、 技师处罚维护 、 门店取消订单规则、 技师星级维度权重 、 评价维护 接口
	 * @method  POST
	 * @param aliasname string 配置项别名[diy] req
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
	 	{
		"code": "000000",
		"msg": "操作成功",
		"info": []
		}
	 */
	public function actionDetailset(){
		if (!isset($this->para['aliasname']) || empty($this->para['aliasname'])) {
            return $this->response('para_miss', '请选择维护配置类别');
        }
        $SysBaseSet = new SysBaseSetModel();
        $res = $SysBaseSet->getWidgetRow(['aliasname'=>$this->para['aliasname']]);
        if(isset($res['extra'])){
        	$res = json_decode($res['extra'],true);
        }
        return $this->response('deal_succ', !empty($res)?$res:[]);
	}
	/**
	 * [actionAddpayset 添加/修改付款权限模板]
	 * @apiDoc
	 * @api     v1/sysother/addpayset
	 * @group   sysother
	 * @name    添加/修改付款权限
	 * @desc    基础设置-付款权限设置-新增/修改
	 * @method  POST
	 * @param name string 模板名称 req
	 * @param group_id int 岗位ID req
	 * @param amount int 金额 req
	 * @param day int 时间 req
	 * @param total init 笔数 req
	 * @param status string 状态【'allow':启用,'deny':禁用】 req
	 * @param id string 模板名称 req
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
	 {
		"code": "000000",
		"msg": "操作成功",
		"info": "4"
		}
	 */
	public function actionAddpayset(){
		if (!isset($this->para['name']) || empty($this->para['name'])) {
            return $this->response('para_miss', '请输入模板名称');
        }
		if (empty($this->para['group_id']) || !is_numeric($this->para['group_id'])) {
            return $this->response('para_miss', '请选择岗位ID');
        }
        if (!isset($this->para['amount']) || !is_numeric($this->para['amount'])) {
            return $this->response('para_miss', '请输入正确的金额');
        }
        if (!isset($this->para['day']) || !is_numeric($this->para['day'])) {
            return $this->response('para_miss', '请输入正确的时间');
        }
        if (!isset($this->para['total']) || !is_numeric($this->para['total'])) {
            return $this->response('para_miss', '请输入正确的笔数');
        }
        if (empty($this->para['status']) || !in_array($this->para['status'], ['allow','deny'])) {
            return $this->response('para_miss', '请选择正确的更新类型');
        }
        $id = isset($this->para['id'])?(int)$this->para['id']:0;
        $SysPaySet = new SysPaySetModel();
        $flag =  $SysPaySet->getWidgetRow(['scope'=>['neq'=>['id'=>$id]],'name'=>$this->para['name']]);
        if($id==0 && $flag){
        	return $this->response('para_miss', '模板名称已存在');
        }
        $data = Helper::elements(['name','group_id','amount','day','total','status'], $this->para, '');
        $data['created'] = time();
        $res = $SysPaySet->saveUs($data,$id);
        if($res){
			return $this->response('deal_succ', $res);
		}else{
			return $this->response('deal_false', $res);
		}
	}
	/**
	 * [actionListpayset 付款权限模板列表]
	 * @apiDoc
	 * @api     v1/sysother/listpayset
	 * @group   sysother
	 * @name    付款权限列表
	 * @desc    基础设置-付款权限设置-列表
	 * @method  POST
	 * @param  name string 名称 noreq
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"total": 1,
		"info": [
		{
		    "id": "1",
		    "name": "每单支付",
		    "group_id": "1",
		    "amount": "50000.00",
		    "day": "7",
		    "total": "5",
		    "status": "allow",
		    "operator_id": "0",
		    "created": "2019-09-03 18:41:35",
		    "status_name": "启用"
		}
		]
		}
	 */
	public function actionListpayset(){
		$_where = [];
		$_where['order'] = ['id' => 'desc'];
		/*名称搜索*/
		if (!empty($this->para['name'])) {
			$_where['name'] = $this->para['name'];
		}
		$SysPaySet = new SysPaySetModel();
		$list = $SysPaySet->getWidgetRows($_where);
		if (!empty($list)) {
			foreach ($list as &$value) {
				$value['status_name'] = Helper::getStatusByKey($value['status']);
				$value['created'] = date("Y-m-d H:i:s", $value['created']);
			}
		}
		return $this->response('search_succ', $list, count($list));
	}
	/**
	 * [actionUppayset 启用/禁用付款权限模板]
	 * @apiDoc
	 * @api     v1/sysother/uppayset
	 * @group   sysother
	 * @name    启用/关闭付款权限
	 * @desc    基础设置-付款权限设置-启用/关闭付款权限
	 * @method  POST
	 * @param id int ID req
	 * @param status string 操作类型【allow:启用;deny:禁用 】 req
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"info": true
		}
	 */
	public function actionUppayset(){
		if (empty($this->para['status']) || !in_array($this->para['status'], ['allow','deny'])) {
            return $this->response('para_miss', '请选择正确的更新类型');
        }
        if (empty($this->para['id']) || !is_numeric($this->para['id'])) {
            return $this->response('para_miss', '操作ID不能为空');
        }
        $SysPaySet = new SysPaySetModel();
        $res = $SysPaySet->saveUs(['status'=>$this->para['status'],'created'=>time()],(int)$this->para['id']);
        if($res){
			return $this->response('deal_succ', $res);
		}else{
			return $this->response('deal_false', $res);
		}
	}

}
