<?php

namespace app\api\logic;

use app\common\logic\BaseLogic;
use app\common\model\beforehand_order\BeforehandOrder;
use app\common\model\beforehand_order_cart_info\BeforehandOrderCartInfo;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_group_price\StoreProductGroupPrice;
use app\common\model\warehouse_product\WarehouseProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use think\facade\Db;

class DemoLogic extends BaseLogic
{
    public static function test()
    {
        $arr = Db::name('ceshi_two')->select();
        foreach ($arr as $k => $v) {
            //门店供货、商户、零售
            $res = Db::name('store_product')->where('id', $v['product_id'])->update(['purchase' => $v['price'], 'cost' => bcadd($v['price1'], 0, 2), 'vip_price' => bcadd($v['price10'], 0, 2), 'price' => bcadd($v['price6'], 0, 2)]);
            if ($res) {
                Db::name('store_branch_product')->where('product_id', $v['product_id'])->update(['purchase' => $v['price'], 'cost' => bcadd($v['price1'], 0, 2), 'vip_price' => bcadd($v['price10'], 0, 2), 'price' => bcadd($v['price6'], 0, 2)]);
                Db::name('ceshi_two')->where('product_id', $v['product_id'])->update(['status' => 1]);
            }
            //种养殖
            $find = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 5)->find();
            if ($find) {
                $find->save(['price' => bcadd($v['price8'], 0, 2)]);
            } else {
                StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 5, 'price' => bcadd($v['price8'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv8'], 100), 100, 2)]);
            }
            //食堂
            $find7 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 7)->find();
            if ($find7) {
                $find7->save(['price' => bcadd($v['price3'], 0, 2)]);
            } else {
                StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 7, 'price' => bcadd($v['price3'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv3'], 100), 100, 2)]);
            }
            //酒店
            $find6 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 6)->find();
            if ($find6) {
                $find6->save(['price' => bcadd($v['price3'], 0, 2)]);
            } else {
                StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 6, 'price' => bcadd($v['price3'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv3'], 100), 100, 2)]);
            }
            //一条龙
            $find3 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 18)->find();
            if ($find3) {
                $find3->save(['price' => bcadd($v['price5'], 0, 2)]);
            } else {
                StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 18, 'price' => bcadd($v['price5'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv5'], 100), 100, 2)]);
            }
            //厨师
            $find4 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 1)->find();
            if ($find4) {
                $find4->save(['price' => bcadd($v['price7'], 0, 2)]);
            } else {
                StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 1, 'price' => bcadd($v['price7'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv7'], 100), 100, 2)]);
            }
            //商户会员
            $find4 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 19)->find();
            if ($find4) {
                $find4->save(['price' => bcadd($v['price2'], 0, 2)]);
            } else {
                StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 19, 'price' => bcadd($v['price2'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv2'], 100), 100, 2)]);
            }
            //食堂会员
            $find20 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 20)->find();
            if ($find20) {
                $find20->save(['price' => bcadd($v['price4'], 0, 2)]);
            } else {
                StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 20, 'price' => bcadd($v['price4'], 0, 2), 'price_type' => 3, 'base_rate' => bcadd(bcmul($v['lv4'], 100), 100, 2)]);
            }
            //活动价
            $find42 = StoreProductGroupPrice::where('product_id', $v['product_id'])->where('group_id', 42)->find();
            if ($find42) {
                if($v['price9']>0){
                    $find42->save(['price' => bcadd($v['price9'], 0, 2)]);
                }
            } else {
                if($v['price9']>0){
                    StoreProductGroupPrice::insert(['product_id' => $v['product_id'], 'group_id' => 42, 'price' => bcadd($v['price9'], 0, 2), 'price_type' => 3, 'base_rate' =>0]);
                }
            }
        }
    }

    public static function test2($store_id, $srr)
    {
        foreach ($srr as $k => $v) {
            $find = StoreBranchProduct::where('store_id', $store_id)->where('product_id', $v['product_id'])->find();
            $find->stock = bcsub($find->stock, $v['cart_num'], 2);
            $find->save();
            SqlChannelLog('StoreBranchProduct', $find['id'], $v['cart_num'], -1, Request()->url(), 1);
        }
    }

    /**
     * 库存回滚
     */
    public static function test3($id,$warehouse_id=1)
    {
        $arr=WarehouseProduct::where('oid', $id)->select();
        foreach ($arr as $k => $v) {
            $find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id',$warehouse_id)->find();
            $nums = bcadd($find->nums, $v['nums'], 2);
            $res=WarehouseProductStorege::where('id', $find['id'])->update(['nums' => $nums]);
            if($res){
                SqlChannelLog('WarehouseProductStorege', $v['id'], $v['nums'], 1, Request()->url(), 1);
                $v->save(['delete_time'=>time()]);
            }
        }
    }

    public static function syncPrice($params,$is_vip=0)
    {
        $outbound_id=BeforehandOrder::where('id', $params['id'])->value('outbound_id');

        $cartInfo = BeforehandOrderCartInfo::where('bhoid', $params['id'])->select()->toArray();
        $productIds = array_column($cartInfo, 'product_id');
        $products = StoreProduct::whereIn('id', $productIds)->select()->toArray();
        $products = reset_index($products, 'id');
        $update = [];
        foreach ($cartInfo as $v) {
            $product = $products[$v['product_id']];
            if (empty($product)) {
                continue;
            }
            if ($is_vip == 1) {
                $price = $product['vip_price'];
            } else {
                $price = $product['price'];
            }
            $update[] = [
                'id' => $v['id'],
                'price' => $price,
                'total_price' => $price * $v['cart_num'],
                'pay_price' => $price * $v['cart_num'],
            ];
        }
        (new BeforehandOrderCartInfo())->saveAll($update);
        $totalPrice = array_sum(array_column($update, 'total_price'));
        BeforehandOrder::where('id', $params['id'])->update(['total_price' => $totalPrice,'pay_price' => $totalPrice]);
        if($outbound_id){
            WarehouseProduct::where('oid', $outbound_id)->select()->each(function ($item) use($is_vip)  {
                if($is_vip==0){
                    $find = StoreProduct::where('id', $item['product_id'])->withTrashed()->field('purchase,vip_price,price')->find();
                    $item->save(['price' => $find['price'], 'purchase' => $find['purchase'], 'vip_price' => $find['vip_price'],'total_price' => $find['price'] * $item['nums']]);
                }else{
                    $find = StoreProduct::where('id', $item['product_id'])->withTrashed()->field('purchase,vip_price,price')->find();
                    $item->save(['price' => $find['vip_price'], 'purchase' => $find['purchase'], 'vip_price' => $find['vip_price'],'total_price' => $find['vip_price'] * $item['nums']]);
                }
            });
            $total_price=WarehouseProduct::where('oid', $outbound_id)->sum('total_price');
            BeforehandOrder::where('id', $outbound_id)->update(['total_price' => $total_price]);
        }

    }
}