<?php
namespace app\common\model;

use think\Db;

class Dinner extends Base
{
    public function updates($cate=0){
        $data = request()->post();
        $data['org_id'] = cur_org_id();
        if(($cate == 0 && $data['free'] == 1) || $cate == 1){
            $data['discount'] = 0;
        }
        $result = validate('Dinner')->check($data,[],'cate'.$cate);
        if(true !== $result){
            $this->error = validate('Dinner')->getError();
            return false;
        }
        $id = $data['id'];
        $data['imgs'] = $data['imgs']?implode(',',$data['imgs']):'';
        unset($data['id']);
        if($id > 0){
            $data['update_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data,['id'=>$id]);
        }else{
            $data['create_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data);
        }
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }
        $name = '菜单管理';
        if($cate == 1){
            $name = '特殊人群菜单';
        }
        $content = $name.':新增';
        if($id > 0){
            $content = $name.':编辑';
            $data['id'] = $id;
        }else{
            $data['id'] = $this->id;
        }

        return true;
    }


}