<?php

namespace App\Services\Api\MobileWeb;

use App\Models\Inventory;
use App\Models\PjGoods;
use App\Services\CommonService;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\DB;
use BlueCity\Core\Inc\ErrorInc;

// 移动端，技师管理
class InventoryService
{
    // 列表查询
    public static function index($request, $show_count = 10)
    {
        $session = $request['session'];
//        $session['company_id'] = 14;
        if($session['user_type']==1){
            return ['ErrorCode' => 1, 'ErrorMessage'=>'暂无数据', 'data'=>[]];
        }
        //获取商品信息
//        CommonService::startmysql();
        $list_data = DB::table('pj_goods')
            ->leftJoin('inventory', function ($join)  use ($session) {
                $join->on('pj_goods.pg_id', '=', 'inventory.inv_goodid')
                    ->where('inventory.inv_serid', $session['admin_id'])//所属服务商
                    ->where('inventory.inv_company_id', $session['company_id']);//所属公司
            })
            ->where('company_id',$session['company_id']) //所属公司
            ->where('is_delect',1)
            ->where(function ($query) use ($request) {
                // 品牌id
                if (!empty($request['gs_bid'])) {
                    $query->where('gs_bid', $request['gs_bid']);
                }
            })
            ->select('pg_id', 'pg_goodsname',
                DB::raw('IFNULL(gone_inventory.inv_num, 0) as stock_num'))
            ->orderby('pg_id', 'desc')
            ->get();
//            ->paginate($show_count);
//        $data['sql'] = CommonService::showmysql();
//        $page['count'] = 0;
//        $page['page'] = 0;
//        $page['page_size'] = $show_count;
//        $page['allPage'] = 0;
//        $data['list'] = [];
//        $data['page'] = $page;
//        $list_items = $list_data->items();
//        if ($list_items) {
//            $page['count'] = $list_data->total(); //总数据个数
//            $page['page'] = $list_data->currentPage(); //当前页面页码
//            $page['page_size'] = $show_count; //一页显示多少个
//            $page['allPage'] = $list_data->lastPage(); //表示最后一页的页码
//            $data['list'] = $list_items;
//            $data['page'] = $page;
//        }

//        if ($data['list']) {
            return ['ErrorCode' => 1, 'ErrorMessage'=>'成功', 'data'=>$list_data];
//        } else {
//            return ['ErrorCode' => 1, 'ErrorMessage'=>'暂无数据', 'data'=>$data];
//        }
    }

    //获取品牌
    public static function getBrand ( $request ){
        $session = $request['session'];
//        $session['company_id'] = 14;
        $list_data = DB::table('goods_brand')
            ->where("company_id",$session['company_id'])
            ->where("del_flag",1)
            ->select('id','brand_name')
            ->get();
        return ['ErrorCode' => 1, 'ErrorMessage'=>'成功', 'data'=>$list_data];
    }
    //编辑库存
    public static function edit ( $request, $ip  ){
        $session = $request['session'];
//        $session['company_id'] = 14;
        $res = Inventory::updateOrCreate(
            [
                'inv_goodid'     => $request['goodid'],
                'inv_serid'      => $session['admin_id'],
                'inv_company_id' => $session['company_id'],
            ],
            [
                'inv_num'  => $request['goodnum'],
                'inv_brand'  => $request['brand'],
                'inv_time' => date("Y-m-d H:i:s"),
            ]
        );
        if ($res) {
            CommonService::inserting_log('编辑商品库存，', $request['goodid'], '编辑商品，' . $request['pg_goodsname'] . '库存为：'.$request['goodnum'], '商品库存web', $session, $ip);//日志
            return ['ErrorCode' => 1, 'ErrorMessage'=>'操作成功', 'data'=>$res];
        } else {
            return ['ErrorCode' => 201, 'ErrorMessage'=>'操作失败', 'data'=>$res];
        }
    }

}
