<?php

namespace app\index\model;

use think\Model;

class BoxModel extends Model {

    // 确定链接表名
    protected $name = 'box';

    public function Order()
    {
        return $this->hasOne('OrderModel', 'order_sn', 'order_sn')->joinType('LEFT');
    }

    /**
     * 根据柜号箱号获取箱子信息
     * @cabinet_number $param
     * @box_number $param
     */
    public function getOneCabinetBox($cabinet_number,$box_number)
    {
        return $this->where(['cabinet_number' => $cabinet_number, 'box_number'=>$box_number])->find();
    }

    /**
     * 根据既定条件修改信息
     * @where $param
     * @update $param
     */
    public function editData($where,$update)
    {
        try{
            $result = $this->save($update, $where);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, '', 'success');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 根据搜索条件获取箱子列表信息
     * @param $offset
     * @param $limit
     */
    public function getBoxsByWhere($where, $offset, $limit)
    {
        return $this->Order()->haswhere($where)->order('sc_box.box_number asc')->field('sc_box.*,OrderModel.enter_time')->limit($offset, $limit)->select();
    }

    /**
     * 根据搜索条件获取所有的箱子数量
     */
    public function getAllCounts()
    {
        return $this->count();
    }

    /**
     * 根据搜索条件获取箱子列表信息
     * @param $offset
     * @param $limit
     */
    public function getBoxNumbers($where)
    {
        return $this->where($where)->order('box_number asc')->field('box_number')->select();
    }

    public function CheckCode($code){
        return $this->where('fetch_code' , $code)->find();
    }
}