<?php

namespace App\Http\Controllers;

use App\Models\DwbsUser;
use App\Models\ShopCar;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Goods;
use App\Models\Order;
use App\Models\Store;
use App\Models\Scan;
use App\Models\Comment;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;

class StoreController extends Controller
{
    //获取店铺信息
    public function getStoreIndex(Request $request){
        $store_id=$request->input('store_id');
        $goods_id=$request->input('goods_id');
        $store=Store::where('id',$store_id)
            ->select('id','img','name','phone','user_id','status','qr_code','sub_mchid','is_apply','star')
            ->first();
        if(empty($store)){
            return $this->error('450011', '店铺信息不存在，请联系商户扫码进入店铺');
        }
        $user=DwbsUser::where('id',$store->user_id)->whereIn('cert_status',[3,6])->where('in_group',1)->whereNull('deleted_at')->first();
        if(!$user){
            return $this->error('450011', '商户信息不存在，请联系商户扫码进入店铺');
        }
        $arr_test=Store::getTest();
        if(in_array($store->id,$arr_test)){
            $store->is_test=1;
        }else{
            $store->is_test=0;
        }
        if(!empty($goods_id)){
            $goods_ids=explode(',',$goods_id);
            $goods=Goods::where('is_delete',0)->where('is_shelves',1)->whereIn('id',$goods_ids)->get();
        }else{
            $goods=Goods::where('is_delete',0)->where('is_shelves',1)->get();
        }
        $user_id=Auth::user()->id;
        $user=User::where('id',$user_id)->first();

        if($store_id && $goods_id) {
            $scan = Scan::where('user_id', $user_id)->where('store_id', $store_id)->first();
            if (!$scan) {
                Scan::create([
                    'user_id' => $user_id,
                    'store_id' => $store_id,
                    'goods_id' =>$goods_id,
                    'order_no' => null,
                    'is_store' => 1
                ]);
            }
        }
        if($store_id && !$goods_id){
            $scan = Scan::where('user_id', $user_id)->where('store_id', $store_id)->first();
            if (!$scan) {
                Scan::create([
                    'user_id'=>$user_id,
                    'store_id'=>$store_id,
                    'goods_id'=>null,
                    'order_no'=>null,
                    'is_store'=>2
                ]);
            }
        }
        $dwbsUser = DwbsUser::where('mobile',$user->phone)
            ->where(function($query){
                $query->where('cert_status',3)
                    ->orWhere('cert_status',6);
            })
            ->where('service_status',0)
            ->whereNull('deleted_at')->first();
        if($dwbsUser){
            $data['is_agent'] = 1;
        }else{
            $data['is_agent'] = 0;
        }
        $data['store']=$store;

        /* 判断关闭 */
        $time=Carbon::now()->toDateTimeString();
        $data['goods']=$goods;
        return $this->success($data);
    }
    //获取购物车商品数量
    public function getShopCarNum(){
        $user_id=Auth::user()->id;
        $car_num=ShopCar::where('user_id',$user_id)->sum('num');
        return $this->success($car_num);
    }
    //发布店铺评论
    public function addStoreComment(Request $request)
    {
        $rules=[
            'content'=>'max:255',
        ];
        $message=[
            'content.max'=>'评价内容不能大于255个字符.'
        ];
        $validator = Validator::make($request->input(),$rules,$message);
        if($validator->fails()){
            return $this->error('400013',$validator->errors()->first());
        }
        try{
            DB::transaction(function()use($request){
                $user=Auth::user();
                $star=$request->input('star') ?? 0;
                $user_id=$user->id;
                $content=$request->input('content');
                $order_no=$request->input('order_no');
                $order=Order::where('order_no',$order_no)->first();
                if($order){
                    $order->is_comment=1;
                    $order->save();
                    $order_id=$order->id;
                }else{
                    $order_id=null;
                }
                Comment::create([
                    'user_id'=>$user_id,
                    'store_id'=>$order->store_id,
                    'content'=>$content,
                    'order_id'=>$order_id,
                    'comment_id'=>0,
                    'star'=>$star,
                    'is_reply'=>1,
                    'is_see'=>0
                ]);
                $star=Comment::where('store_id',$order->store_id)
                    ->where('is_reply',1)->avg('star');
                Store::lockForUpdate()->where('id',$order->store_id)->update(['star'=>floor($star)]);
            },5);

            return $this->success([]);
        }catch(\Exception $e){
            return $this->error('450001',$e->getMessage());
        }
    }
}
