<?php

namespace app\common\model;
use app\hander\HelpHander;
use think\Db;
use think\Exception;

class CleanPlan extends Base {
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    public $table = 'clean_plan';
    protected $validateName = 'CleanPlan';

    public function updates() {
        $data = request()->post();
        $data['forms'] = !empty($data['forms'])?implode(',',$data['forms']):'';
        $data['addrs'] = !empty($data['addrs'])?implode(',',$data['addrs']):'';
        $data['org_id'] = cur_org_id();
        $result = validate($this->validateName)->check($data, [], 'add');
        if (true !== $result) {
            $this->error = validate($this->validateName)->getError();
            return false;
        }
        $id = $data['id'];
        unset($data['id']);
        $this->startTrans();
        try {
            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) {
                throw new Exception('任务保存失败');
            }

            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }

    public function mupdates() {
        $data = request()->post();
        $data['forms'] = !empty($data['forms'])?implode(',',$data['forms']):'';
        $data['addrs'] = !empty($data['addrs'])?implode(',',$data['addrs']):'';
        $data['org_id'] = cur_org_id();
        $result = validate($this->validateName)->check($data, [], 'madd');
        if (true !== $result) {
            $this->error = validate($this->validateName)->getError();
            return false;
        }
        $id = $data['id'];
        unset($data['id']);
        $this->startTrans();
        try {
            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) {
                throw new Exception('任务保存失败');
            }

            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }

}