<?php
namespace app\common\logic;
use app\common\logic\LogLogic;
use think\Db;
class BonusLogic
{
protected $config = [];
protected $user = [];
protected $isBonus = [];
protected $useDayAmountToBonus = [];
protected $dyamounyid = false;
protected $bcv = '';
protected $blv = '';
public function user($user,$dyamounyid = false)
{
// 获得所有配置
$this->user = $user;
self::getSupperCof($this->user['supper_id']);
self::isBonus();
self::useDayAmountToBonus(); /
self::getBonusLevel();
self::getBonusCof();
$res = self::bonus($user);
if (true !== $res)
{
return $res;
}
else
{
return true;
}
}
/**
* 对多位员工进行发放奖金
* @param [type] $user [description]
* @param boolean $dyamounyid 日
* @return [type] [description]
*/
public function users($user,$dyamounyid = false)
{
$res = '';
$this->dyamounyid = $dyamounyid;
$len = count($user);
for($i = 0; $i < $len; $i++)
{
$res = self::user($user[$i],$dyamounyid);
}
if (true !== $res)
{
return $res;
}
return true;
}
public function bonus($user)
{
$bonus = '';
$uUp = '';
$update = '';
//判断是否开启奖金
if ($this->isBonus == 1)
{
// 百分比
if ($this->blv > 0 && $this->blv <=1)
{
//使用日营业额进行发放
if ($this->useDayAmountToBonus == 1 || $this->dyamounyid !== false)
{
$dayamount = Db::name('supperDayAmount')->where(['id' => $this->dyamounyid])->find();
$bonus = number_format($this->blv * $dayamount['day_amount'], 2);
}
else
{
$bonus = number_format($this->blv * $this->bcv, 2);
}
}
else
{
$bonus = $this->blv;
}
$update = ['id' => $this->user['id'], 'bonus' => $this->user['bonus'] + $bonus];
$uUp = Db::name('user')->update($update);
if($uUp)
{
$log = new LogLogic;
//添加用户日志
$log->addUserLog('bonus','获得奖金:¥'.$bonus, $this->user['id']);
//添加店铺日志
$log->addSupperLog('bonus', '向 “'.$this->user['name'].'” 发放奖金:¥'.$bonus, $this->user['supper_id']);
return true;
}
else
{
return ['code' => 0, 'msg' => '奖金发放失败'];
}
}
else
{
return ['code' => 0, 'msg' => '奖金未开启'];
}
}
/**
* 初始化店铺配置
* @param [type] $supper_id [description]
* @return [type] [description]
*/
private function getSupperCof($supper_id)
{
$this->config = Db::name('supperConfig')->where(['supper_id' => $supper_id])->select();
return $this->config;
}
/**
* 获得奖金开关配置
* @return data
*/
private function isBonus()
{
$bonus = self::getCofVal('isBonus');
return $this->isBonus = $bonus;
}
/**
* 获得奖金的类型,是否使用日营业额进行分配
* @return [type] [description]
*/
private function useDayAmountToBonus()
{
return $this->useDayAmountCof = self::getCofVal('useDayAmountToBonus');
}
/**
* 获取对应的奖金等级
* @return [type] [description]
*/
private function getBonusLevel()
{
$cofN = '';
$level = $this->user['level'];
switch ($level) {
case 1:
$cofN = 'bonus1';
break;
case 2:
$cofN = 'bonus2';
break;
case 3:
$cofN = 'bonus3';
break;
case 4:
$cofN = 'bonus4';
break;
case 5:
$cofN = 'bonus5';
break;
case 6:
$cofN = 'bonus6';
break;
case 7:
$cofN = 'bonus7';
break;
default:
$cofN = '';
break;
}
return $this->blv = self::getCofVal($cofN);
}
public function getBonusCof()
{
return $this->bcv = self::getCofVal('bonus');
}
/**
* 获取其中一条configName的配置
* @param [type] $cofName [description]
* @return [type] [description]
*/
private function getOneCof($cofName)
{
$cof = [];
//print_r($this->config);die;
$len = count($this->config);
for ($i = 0; $i < $len; $i++)
{
if ($this->config[$i]['config_name'] == $cofName)
{
$cof = $this->config[$i];
}
}
return $cof;
}
/**
* 获取相对配置名称的值
* @return [type] [description]
*/
private function getCofVal($cofName)
{
$cof = [];
$len = count($this->config);
for ($i = 0; $i < $len; $i++)
{
if ($this->config[$i]['config_name'] == $cofName)
{
$cof = $this->config[$i]['config_value'];
}
}
return $cof;
}
private function getLastCof($data)
{
$len = count($data);
return $data[$len];
}
}