<?php
namespace app\common\model;

use think\Db;

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

    public $status = [
        '待确认',
        '已确认',
        '已取消',
    ];
    public function saveData($data){
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['user_id'] = is_login();
        $data['sn'] = get_unique_id();

        $ret = $this->allowField(true)->save($data);
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }
        return $this->getLastInsID();
    }

    public function getInfo($id){
        $info = $this
            ->where('id',$id)
            ->find()
            ->toArray();
        if(!$info){
            return false;
        }
        $info['userName'] = Db::name('user')
            ->where('id',$info['user_id'])
            ->value('real_name');
        $info['goods'] = Db::name('company_dispatch_goods')
            ->alias('a')
            ->join('mate_goods b','a.goods_id=b.id')
            ->where('a.dispatch_id',$info['id'])
            ->field('a.*,b.title,b.unit,b.brand,b.spec')
            ->select();
        return $info;
    }
}