<?php
namespace app\common\model;

use think\Db;

class WasteScale extends Base
{
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    public $table = 'waste_scale';
    protected $validateName = 'WasteScale';
    public function updates(){
        $data = request()->post();
        $data['org_id'] =cur_org_id();
        $result = validate($this->validateName)->check($data,[],'');
        if(true !== $result){
            $this->error = validate($this->validateName)->getError();
            return false;
        }
        $id = $data['id'];
        unset($data['id']);

        if($data['scale_sn']){
            $deviceLog = Db::name('device_log')->where('sn',$data['scale_sn'])->where('type',1)->where('del',0)->find();

            $txt = '添加';
            if($id > 0){
                $txt = '修改';
            }
            if(!$deviceLog){
                $this->error = '该设备无法'.$txt.',请联系管理员';
                return false;
            }
        }
        if($data['printer_sn']){
            $deviceLog = Db::name('device_log')->where('sn',$data['printer_sn'])->where('type',6)->where('del',0)->find();

            $txt = '添加';
            if($id > 0){
                $txt = '修改';
            }
            if(!$deviceLog){
                $this->error = '该设备无法'.$txt.',请联系管理员';
                return false;
            }
        }


        if($id > 0){
            $check = Db::name('waste_scale')
                ->where('del',0)
                ->where('user_id',$data['user_id'])
                ->where('enable',1)
                ->where('id','<>',$id)
                ->where('org_id',$data['org_id'])
                ->find();
            if($check){
                $this->error = '当前人员已绑定过';
                return false;
            }
            $data['update_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data,['id'=>$id]);
        }else{
            $check = Db::name('waste_scale')
                ->where('del',0)
                ->where('user_id',$data['user_id'])
                ->where('enable',1)
                ->where('org_id',$data['org_id'])
                ->find();
            if($check){
                $this->error = '当前人员已绑定过';
                return false;
            }
            $data['create_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data);
        }
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }

    public function getList(){
        $list =$this
            ->where('org_id',cur_org_id())
            ->where('del',0)
            ->where('enable',1)
            ->select()
            ->toArray();
        return $list;
    }
    //获取绑定人员打印机
    public function list($orgId,$userId){
        $list =$this
            ->where('org_id',$orgId)
            ->where('user_id',$userId)
            ->where('del',0)
            ->where('enable',1)
            ->select()
            ->toArray();
        return $list;
    }

}