<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\common\model;

use cores\BaseModel;
use think\model\relation\HasOne;
use app\common\model\store\Shop;
/**
 * 活动栏目模型类
 * Class User
 * @package app\common\model
 */
class PriceSet extends BaseModel
{
    // 定义表名
    protected $name = 'price_set';

    // 定义主键
    protected $pk = 'id';

    /**
     * 会员价
     * [membershipPrice description]
     * @param  [type] $market_price [description]
     * @param  [type] $cost_price   [description]
     * @param  [type] $category_ids [description]
     * @return [type]               [description]
     */
    public static function membershipPrice($market_price, $cost_price, $category_ids){
        $addPriceRate = self::getAddPriceRate(0, $category_ids, $cost_price);
        //没有加价率返回市场价
        if (!$addPriceRate) {
            return sprintf("%.2f", $market_price);
        }
        $membershipPrice = $cost_price * (1 + $addPriceRate * 0.01);
        //当加价率生效后,会员价高于市场价
        if ($membershipPrice > $market_price) {
            $membershipPrice = ($market_price - $cost_price) * $addPriceRate * 0.01 + $cost_price;
            
        }
        return sprintf("%.2f", $membershipPrice);
        
    }
    /**
     * 分销价
     * [distributionPrice description]
     * @param  [type] $market_price [description]
     * @param  [type] $cost_price   [description]
     * @param  [type] $type         [description]
     * @param  [type] $category_ids [description]
     * @return [type]               [description]
     */
    public static function distributionPrice($market_price, $cost_price, $category_ids){
        $arr = [];
        //会员价
        $membershipPrice = self::membershipPrice($market_price, $cost_price, $category_ids);
        $arr['membershipPrice'] = $membershipPrice;
        //分销价
        $addPriceRate = self::getAddPriceRate(1, $category_ids, $cost_price);
        
        //没有加价率返回市场价
        if (!$addPriceRate) {
            $arr['distributionPrice'] = sprintf("%.2f", $market_price);
            return $arr;
        }
        $distributionPrice = $cost_price * (1 + $addPriceRate * 0.01);
        $price = $distributionPrice;
        //当加价率生效后,分销价高于市场价
        if ($distributionPrice > $market_price) {
            $price = ($market_price - $cost_price) * $addPriceRate * 0.01 + $cost_price;
        }
        //当加价率生效后,分销价高于会员价
        if ($distributionPrice > $membershipPrice) {
            $price = ($membershipPrice - $cost_price) * $addPriceRate * 0.01 + $cost_price;
        }
        $arr['distributionPrice'] = sprintf("%.2f", $price);
        return $arr;
    }


    /**
     * 获取商品加价率
     * [getAddPriceRate description]
     * @param  [type] $store_id     [description]
     * @param  [type] $type         [description]
     * @param  [type] $category_ids [description]
     * @param  [type] $goods_price  [description]
     * @return [type]               [description]
     */
    public static function getAddPriceRate($type, $category_ids, $cost_price){
        $info = static::where('type',$type)
            ->whereIn('code', $category_ids)
            ->where('min', '<=', $cost_price)
            ->where('max', '>', $cost_price)
            ->order('id desc')
            ->column('add_price_rate');
        if (!$info) {
            return 0;
        }
        
        return $info[0];
    }
}