<?php

/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/12/13
 */

namespace app\models\store;


use crmeb\basic\BaseModel;
use think\facade\Db;
use crmeb\traits\ModelTrait;

/**
 * TODO  产品属性Model
 * Class StoreProductAttr
 * @package app\models\store
 */
class SystemStockBill extends BaseModel
{


    use ModelTrait;
    public static function getAddTimeAttr($value)
    {
        return date("Y-m-d H:i:s",$value);
    }
    public static function getAuditTimeAttr($value)
    {
        return $value?date("Y-m-d H:i:s",$value):'-';
    }
    public static function getStatusAttr($value)
    {
        switch ($value)
        {
            case 0:
                return '待审核';
                break;
            case 1:
                return '已通过';
                break;
            case -1:
                return '已拒绝';
                break;
        }
    }

    /**
     *创建记录
     */
    public static function order_create($data)
    {
        if ($data['product_id'] > 0 && $data['in_stock'] > 0) {
            $data['add_time'] = time();
            self::create($data);
            return true;
        }
        return false;
    }

    /**
     *获取列表
     */
    public static function lst($where)
    {
        $model = new self;
        $model = $model->alias("a")->join("store_product b","a.product_id=b.id","right");
        if(isset($where['status']) && $where['status']>-2)  $model = $model->where('a.status',$where['status']);
        if(isset($where['key']) && $where['key']) $model = $model->wherelike('b.store_name',"%".$where['key']."%");
        if(isset($where['product_id']) && $where['product_id']) $model = $model->wherelike('a.product_id',$where['product_id']);
        $model = $model->where('a.id','>',0);
        $model = $model->field('a.*,b.image,b.is_show,b.is_del,b.store_name');
        $count = $model->count();
        $data = $model->page($where['page'],$where['limit'])->order("id desc")->select()->toarray();
        return compact('count','data');
    }

}