<?php

declare (strict_types = 1);
namespace library\utils;
// +----------------------------------------------------------------------
// | [ 广告位  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-29 20:41
// +----------------------------------------------------------------------

use app\model\api\SiteProduct;
use app\model\system\Advert;
use \app\model\system\Advert as AdvertModel;
use app\model\system\ExpCost;
use app\model\system\MemberLevel;
use app\model\system\Product;

class MakeSite
{

    private $sassid;

    /**
     * 生成站点数据
     * MakeSite constructor.
     * @param int $sassid
     */
    public function __construct($sassid = 0)
    {
            $this->sassid = $sassid;
    }


    /**
     * 生成默认数据
     */
    public function make(){
        $this->makeAdvert();
    }


    /**
     * 生成广告位
     */
    public function makeAdvert(){
        $adver = (new Advert)->where('is_show',1)->select();
        foreach ($adver as $v) {
            unset($v['id']);
            $v['sassid'] = $this->sassid;
            (new Advert)->insert($v);
        }
    }

    /**
     * 生成产品库
     */
    public function makeProduct(){
        $sitePro = new SiteProduct;
        $product = (new Product)->where('status',1)->select();
        foreach ($product as $v) {
            $pro = $sitePro
                        ->where('sassid',$this->sassid)
                        ->where('p_id',$v['id'])
                        ->find();
            if(empty($pro)) {
                $sitePro->insert([
                   'p_id'   => $v['id'],
                   'sassid' => $this->sassid,
                    'price' => $v['commission'],
                    'sales' => 0,
                    'ver_bug_count' => 0,
                    'status' => 1,
                    'is_host' => $v['is_host'],
                    'is_new' => $v['is_new'],
                    'cate_id' => $v['cate_id']
                ]);
            } else {
                $price = $v['commission'] > $pro['price'] ? $v['commission']  : $pro['price'];
                $sitePro
                    ->where('sassid',$this->sassid)
                    ->where('p_id',$v['id'])
                    ->save(['price'=>$price]);
            }
        }
    }

    /**
     * 更新站邮费配置
     */
    public function makeExpcos($warehouse_id = 0) {
        $expCos = new ExpCost;
        $expData = $expCos->where('sassid',0)
            ->when(!empty($warehouse_id),function ($query) use($warehouse_id){
                $query->where('warehouse_id',$warehouse_id);
            })
            ->select();
        $level = (new MemberLevel)->select()->toArray();
        $level = array_merge([['id'=>-1,'name'=>'平台底价','code' => '']],$level);
        foreach ($expData as $v) {
            foreach ($level as $v2) {
                $d = $expCos
                    ->where('warehouse_id',$v['warehouse_id'])
                    ->where('express_id',$v['express_id'])
                    ->where('level_id',$v2['id'])
                    ->where('sassid',$this->sassid)
                    ->find();
                //解决
                $d1['warehouse_id'] = $v['warehouse_id'];
                $d1['express_id'] = $v['express_id'];
                $d1['money'] = $v['money'];
                $d1['level_id'] = $v2['id'];
                $d1['sassid'] = $this->sassid;
                $d1['status'] = $v['status'];
                if(empty($d)) {
                    $money = $expCos
                            ->where('level_id',$v2['id'])
                            ->where('sassid',$this->sassid)
                            ->value('money');
                    if(!empty($money) && $money > $d1['money']) {
                        $d1['money'] = $money;
                    }
                    $expCos->insert($d1);
                } else {
                    if($v['money'] > $d['money']) {
                        $d1['money'] = $v['money'];
                    } else {
                        unset($d1['money']);
                    }
                    $expCos->where('id',$d['id'])->save($d1);
                }
            }
        }
    }





}