<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\sale;


use app\common\model\sale\SaleRules;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * SaleRules逻辑
 * Class SaleRulesLogic
 * @package app\adminapi\logic
 */
class SaleRulesLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/04 17:21
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            SaleRules::create([
                'name' => $params['name'],
                'type' => $params['type'],
                'rating_start' => $params['rating_start'],
                'rating_end' => $params['rating_end'],
                'rate' => $params['rate']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/04 17:21
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            SaleRules::where('id', $params['id'])->update([
                'name' => $params['name'],
                'type' => $params['type'],
                'rating_start' => $params['rating_start'],
                'rating_end' => $params['rating_end'],
                'rate' => $params['rate']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/03/04 17:21
     */
    public static function delete(array $params): bool
    {
        return SaleRules::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2025/03/04 17:21
     */
    public static function detail($params): array
    {
        return SaleRules::findOrEmpty($params['id'])->toArray();
    }

    /**
     * 根据档位获取销售绩效金额
     *
     * @param float $rating 档位
     * @return float|null 对应的金额，若未找到匹配的档位则返回0
     */
    public static function getSaleAmountByRating(float $rating,int $type): ?float
    {
        $rule = SaleRules::where('type', $type)
            ->where('rating_start', '<=',$rating)
            ->where('rating_end', '>',$rating)
            ->findOrEmpty();
        if (!$rule->isEmpty()){
            return $rule['rate']??0;
        }
        return 0;
    }







}