| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- <?php
- namespace addons\HiGo\common\logic;
- use addons\HiGo\common\enums\HiGoEnum;
- use addons\HiGo\common\models\HigoGoodsSetting;
- use addons\HiGo\common\models\HigoHibeiLog;
- use addons\HiGo\common\models\HigoMall;
- use addons\HiGo\common\models\HigoStoreGoodsMode;
- use addons\HiGo\common\models\HigoWallet;
- use addons\HiGo\common\models\HigoWalletFrozenLog;
- use addons\HiGo\common\models\HigoWalletLog;
- use addons\Store\common\models\Store;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\census\CensusMall;
- use common\models\common\CapitalLog;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\order\OrderMarketing;
- use common\traits\ErrorTrait;
- use Exception;
- use yii\helpers\Json;
- /**
- * Class HiValueLogic
- * 嗨呗逻辑类
- * @Author: mz
- * @DateTime: 2022/11/16 16:39
- * @Copyright: copyright (c) 2022 广东七件事集团
- */
- class HiBeiLogic
- {
- use ErrorTrait;
- /**
- * 获取嗨呗当前价值【1个嗨呗 = 多少元】
- * @param int $mall_id
- * @return
- */
- public static function getNowPrice($mall_id)
- {
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- if(!$setting['hi_bei_pricing_method']){
- //自定义
- $price = $setting['hi_bei_pricing'] ?? 0;
- } else{
- //自动按总营业的百分比/发行的总量
- $census_mall = CensusMall::findOne(['mall_id' => $mall_id]);
- $total_pay_money = $census_mall['total_pay_money'] ?? 0;
- $higo_mall = HigoMall::findOne(['mall_id' => $mall_id]);
- $hi_bei_pricing_radio = $setting['hi_bei_pricing_radio'] ?? 0;
- //价格是否 到小数点2位
- $price = bcmul($total_pay_money,$hi_bei_pricing_radio/100,2);
- if($higo_mall['hi_bei'] <= 0){
- $price = 0;
- }else{
- $price = bcdiv($price,$higo_mall['hi_bei'],2);
- }
- }
- $price = $price > 0 ? $price : 0;
- return $price;
- }
- /**
- * 嗨呗空投
- * @param $mall_id
- * @return bool
- * @throws Exception
- */
- public static function airdrop($mall_id)
- {
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- $total_num = $setting['hi_bei_total_issuance'];
- $day_num = 0;
- //每天空投数量
- if($setting['airdrop_method'] == 0){
- //每天
- $day_num = $setting['airdrop_everyday_number'] ?? 0;
- }else{
- //每年
- $is_percent = $setting['airdrop_years_type'] ?? 0;
- //计算今年的数量
- $airdrop_years_config = json_decode($setting['airdrop_years_config'],true);
- $airdrop_years_config = array_column($airdrop_years_config,null,'year');
- $year = date('Y');
- $now_year_config = $airdrop_years_config[$year] ?? [];
- $year_num = $is_percent == 1 ? bcmul($total_num,$now_year_config['value']/100,10) : $now_year_config['value'];
- $day_num = bcdiv($year_num,self::cal_days_in_year($year),10);
- }
- //判断 总发行 是否超出
- $higo_mall = HigoMall::findOne(['mall_id' => $mall_id]);
- if(bcadd($higo_mall->hi_bei,$day_num) > $total_num){
- //超出
- $day_num = bcsub($total_num,$higo_mall->hi_bei,10);
- $day_num = $day_num > 0 ? $day_num : 0;
- }
- if($day_num > 0){
- $where = [
- 'and',
- ['mall_id' => $mall_id],
- ['>', 'hi_value', 0],
- ['status' => StatusEnum::ENABLED],
- ];
- $total_num = 0;
- //发放嗨呗
- foreach (HigoWallet::find()->where($where)->each(500) as $user_wallet) {
- $send_num = bcdiv($user_wallet->hi_value,$higo_mall->hi_value,10);
- $send_num = bcmul($send_num,$day_num,10);
- $desc = '每天空投释放奖励';
- $wallet_log = [
- 'mall_id' => $user_wallet->mall_id,
- 'user_id' => $user_wallet->user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $send_num,
- 'desc' => $desc,
- 'source_table' => '',
- 'source_table_id' => 0,
- 'from_type' => HiGoEnum::FROM_TYPE_AIRDROP,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
- 'is_send' => true
- ];
- WalletLogic::handleInQueue($wallet_log);
- $total_num = bcadd($total_num,$send_num,10);
- }
- //添加嗨呗记录
- $desc = date('Y-m-d').'系统嗨呗空投';
- HigoHibeiLog::setData(['mall_id' => $mall_id,'change_type' => CapitalLog::CHANGE_TYPE_ADD,'change_num'=>$total_num,'from_type' => HigoHibeiLog::FROM_TYPE_AIRDROP,'desc' => $desc,'setting' => json_encode($setting)]);
- }
- return true;
- }
- /**
- * 计算今年天数
- * @param $year
- * @return int
- */
- public static function cal_days_in_year($year){
- $days=0;
- for($month=1;$month<=12;$month++){
- //$days = $days + cal_days_in_month(CAL_GREGORIAN,$month,$year);
- $days = $days + date("t",strtotime("$year-$month"));
- }
- return $days;
- }
- /**
- * 商品设置营销赠送嗨呗-商城订单、门店订单、多商户 发放嗨呗
- * @param $order
- * @throws
- * @return bool
- *
- */
- public static function sendGoodsSettingHiBei($order)
- {
- $mall_id = $order['mall_id'];
- $user_id = $order['user_id'];
- $source_table = Order::tableName();
- $from_type= HiGoEnum::FROM_TYPE_ORDER_GOODS;
- //嗨值自定义名称
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- $name = $setting['hi_bei_name'] ?? '嗨贝';
- //查询订单商品
- $order_goods = OrderDetail::find()->where(['order_id' => $order['id']])->asArray()->all();
- foreach ($order_goods as $info){
- $is_frozen = false;
- $source = $order['store_id'] > 0 ? 'Store' : 'Main';
- $goods_setting = HigoGoodsSetting::getGoodsSetting($info['goods_id'],HiGoEnum::WALLET_TYPE_HI_BEI,$source);
- if(!$goods_setting) continue;
- //检测该订单是否发放过该奖励
- if(HigoWalletLog::checkSend($mall_id,$user_id,$source_table,$order['id'],$from_type,HiGoEnum::WALLET_TYPE_HI_BEI)){
- continue;
- }
- //查询冻结表是否有记录
- if(HigoWalletFrozenLog::checkSend($mall_id,$user_id,$source_table,$order['id'],$from_type,HiGoEnum::WALLET_TYPE_HI_VALUE)){
- continue;
- }
- if($goods_setting['settlement_method'] == HiGoEnum::SETTLEMENT_METHOD_AFTER_FINISH){
- //订单完成时结算,先存入冻结记录表,待订单完成时去 结算该奖励
- $is_frozen = true;
- }
- $change_num = $goods_setting['give'];
- //计算赠送嗨呗
- if($goods_setting['is_percent'] == 1){
- //百分比
- $change_num = bcmul($info['goods_price'],$goods_setting['give']/100,10);
- }
- if($order['store_id'] > 0){
- if (MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_STORE)){
- //检测门店账号是否有嗨呗
- $store = Store::findOne(['id' => $order['store_id']]);
- $store_user_id = $store['user_id'] ?? 0;
- if(!$store_user_id) continue;
- $store_wallet = HigoWallet::findOne(['user_id' => $store_user_id,'mall_id' =>$mall_id ]);
- if(!$store_wallet || bccomp($change_num,$store_wallet->hi_bei,10) > 0 ){
- //门店嗨呗不足
- \Yii::error('发放失败,门店嗨呗不足,订单发放嗨呗,order_id='.$order['id'].' ,需发放嗨呗数量:'.$change_num);
- continue;
- }
- //先扣除门店的嗨呗
- $desc = '门店商品营销设置赠送'.$name.',订单ID('.$order['id'].')';
- $wallet_log = [
- 'mall_id' => $mall_id,
- 'user_id' => $store_user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
- 'change_num' => $change_num * -1,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => $from_type,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
- 'is_send' => 0,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'setting' => json_encode($goods_setting)
- ];
- $res = WalletLogic::handleInQueue($wallet_log,true);
- }
- }else{
- if(!self::checkAddHibei($mall_id,$change_num)){
- //系统嗨呗不足
- \Yii::error('发放失败,系统嗨呗不足,商城订单发放嗨呗,order_id='.$order['id'].' ,需发放嗨呗数量:'.$change_num);
- continue;
- }
- }
- $desc = '用户('.$user_id.')获得订单ID('.$order['id'].'),门店商品营销设置赠送'.$name;
- $wallet_log = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $change_num,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => $from_type,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
- 'is_send' => $order['store_id'] > 0 ? 0: 1,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'setting' => json_encode($goods_setting)
- ];
- if($is_frozen){
- $res = HigoWalletFrozenLog::setData($wallet_log);
- if(!$res) throw new Exception( HigoWalletFrozenLog::getStaticError());
- }else{
- $res = WalletLogic::handleInQueue($wallet_log,true);
- if(!$res) throw new Exception('sendMallOrderHiBei handleInQueue error');
- }
- }
- return true;
- }
- /**
- * 商户订单发放嗨呗
- * @param $order
- * @param int $settlement_method
- * @return bool|void
- */
- public static function sendMerchantOrderHiBei($order,$settlement_method = 0)
- {
- $mall_id = $order['mall_id'];
- $user_id = $order['user_id'];
- $source_table = Order::tableName();
- $from_type= HiGoEnum::FROM_TYPE_ORDER_GOODS;
- //嗨呗自定义名称
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- $name = $setting['hi_bei_name'] ?? '嗨贝';
- if (empty($setting['merchant_buy_goods'])) return ;
- if($settlement_method != $setting['payment_method'] ) return ;
- return true;
- }
- /**
- * 检测系统是否可以增加嗨呗
- * @param $mall_id
- * @param $add_num
- * @return bool
- */
- public static function checkAddHibei($mall_id,$add_num)
- {
- $result = true;
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- //判断 总发行 是否超出
- $higo_mall = HigoMall::findOne(['mall_id' => $mall_id]);
- if(bccomp(bcadd($higo_mall->hi_bei,$add_num),$setting['hi_bei_total_issuance'],10) > 0 ){
- $result = false;
- }
- return $result;
- }
- /**
- * 嗨贝,嗨值返回
- * @Author: lun
- * @DateTime: 2022/9/21 0021 9:54
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $order_detail_id
- * @param array $params
- * @return bool
- */
- public static function backGoodsMarketing($order_detail_id, $params = [])
- {
- //$trans = \Yii::$app->db->beginTransaction();
- try {
- $deduct = OrderMarketing::find()->select('deduct_currency')->where(['order_detail_id' => $order_detail_id, 'status' => StatusEnum::ENABLED])->scalar();
- if (empty($deduct)) {
- //$trans->commit();
- return true;
- }
- $deduct_arr = Json::decode($deduct, true);
- foreach ($deduct_arr as $key => $item) {
- // 判断里面是否存在嗨贝跟嗨值
- if (in_array($key, [HigoEnum::WALLET_TYPE_HI_BEI, HigoEnum::WALLET_TYPE_HI_VALUE]) && $item['money'] > 0) {
- $wallet_log = [
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'source_table' => $params['source_table'],
- 'source_table_id' => $params['source_table_id'],
- 'from_type' => $params['from_type'],
- 'desc' => $params['desc'] ?? '',
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $item['money'],
- 'wallet_type' => $key,
- ];
- $res = WalletLogic::handleInQueue($wallet_log);
- if ($res == false) throw new Exception(WalletLogic::getStaticError());
- }
- }
- //$trans->commit();
- return true;
- } catch (\Exception $e) {
- //$trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 门店,多商户订单 售后退回赠送的嗨呗
- * @param $order_id
- * @param $source_table
- * @param $source_table_id
- * @param $from_type
- * @return bool
- * @throws Exception
- */
- public static function backMchOrder($order_id,$source_table,$source_table_id)
- {
- $mch_hi_bei_logs = HigoWalletLog::findAll([
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
- 'change_type' => 'sub',
- 'source_table' => $source_table,
- 'source_table_id' => $source_table_id,
- 'status' => StatusEnum::ENABLED,
- //'from_type' => $from_type
- ]);
- foreach($mch_hi_bei_logs as $log){
- $back_wallet_log = [
- 'mall_id' => $log['mall_id'],
- 'user_id' => $log['user_id'],
- 'source_table' => $log['source_table'],
- 'source_table_id' => $log['source_table_id'],
- 'from_type' => HiGoEnum::FROM_TYPE_ORDER_REFUND_BACK,
- 'desc' => '订单ID:'.$order_id.'售后完成,营销赠送退回',
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $log['change_num'],
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
- 'is_send' => false
- ];
- $res = WalletLogic::handleInQueue($back_wallet_log,false);
- if(!$res){
- echo WalletLogic::getStaticError();
- }
- }
- return true;
- }
- }
|