1000], [['agent_extra'], 'string', 'max' => 255], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => '主键', 'mall_id' => 'mall_id', 'store_id' => 'store_id', 'order_detail_id' => '订单详情id', 'give_score' => '赠送积分', 'give_balance' => '赠送余额', 'give_currency' => '赠送货币(json字符串保存)', 'deduct_score' => '减扣积分', 'deduct_currency' => '减扣货币(json字符串保存)', 'pay_money' => '商品实际付款金额,单位:元', 'full_reduce_money' => '满减金额,单位:元', 'coupon_discount_money' => '优惠券优惠金额,单位:元', 'member_price_deduct' => '会员价优惠', 'user_coupon_id' => '用户优惠券id map', 'user_card_id' => '用户会员卡id map', 'agent_deduct' => 'Agent Deduct', 'agent_extra' => '经销商内购商品抵扣额外信息', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } public static function setData($deduct_data, $order_detail_id, $store_id, $mall_id) { $deduct_data['user_coupon_id'] = !empty($deduct_data['user_coupon_id'])? json_encode($deduct_data['user_coupon_id']):json_encode([]); $order_marketing = new StoreOrderMarketing(); $order_marketing->loadDefaultValues(); $order_marketing->load($deduct_data,''); $order_marketing->order_detail_id = $order_detail_id; $order_marketing->store_id = $store_id; $order_marketing->mall_id = $mall_id; $order_marketing->save(); return $order_marketing; } public static function giveMarketing($mallId, $user_id, $source_table_id) { try { // 查询订单详情 $detail_ids = StoreOrderDetail::find()->select('id')->where(['order_id' => $source_table_id])->asArray()->column(); if (empty($detail_ids)) throw new \Exception('订单详情id查询为空'); // 获取订单的营销数据 $order_marketing_list = self::find()->andWhere(['in', 'order_detail_id', $detail_ids])->all(); if (empty($order_marketing_list)) return true; $give_type = [UserWalletService::WALLET_TYPE_SCORE]; $give_money = []; // 循环计算赠送数量 foreach ($order_marketing_list as $marketing) { if (!isset($give_money[UserWalletService::WALLET_TYPE_SCORE])) $give_money[UserWalletService::WALLET_TYPE_SCORE] = 0; $give_money[UserWalletService::WALLET_TYPE_SCORE] += $marketing->give_score; } // 插入资金 foreach ($give_type as $type) { if ($give_money[$type] <= 0) continue; $insert_wallet[] = [ 'mall_id' => $mallId, 'user_id' => $user_id, 'is_frozen' => false, 'wallet_type' => $type, 'money' => $give_money[$type], 'desc' => 'o2o订单支付赠送', 'source_table' => StoreOrder::tableName(), 'source_table_id' => $source_table_id, 'from_type' => UserWalletEnum::CONSUME, 'capital_type' => UserWalletEnum::CONSUME_ORDER_GIVE, ]; $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new \Exception(UserWalletService::getStaticError()); } return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } public static function deductBack($store_order,$order_detail_id_all, $order_detail_ids, $mall_id, $user_id, $desc, $from_type = UserWalletEnum::CONSUME, $capital_type = UserWalletEnum::CONSUME_ORDER_CLOSE) { try { $order_id = $store_order['id']; $order_marketing_list = StoreOrderMarketing::lists([ 'where' => [['order_detail_id' => $order_detail_ids], ['status' => StatusEnum::ENABLED]], 'select' => 'deduct_score,user_coupon_id,order_detail_id' ]); $refund_user_coupon_id_arr = []; $user_coupon_arr = $order_detail_arr = $user_coupon_order_detail_id_arr = []; $order_detail_list = StoreOrderDetail::lists(['where'=>[['order_id'=>$order_id]]]); foreach($order_detail_list as $order_detail) { $order_detail_arr[$order_detail['id']] = ['goods_id' => $order_detail['goods_id'], 'num' => $order_detail['num']]; } $deduct_score = 0; if ($from_type == UserWalletEnum::REFUND) { $capital_type = 'consume_store_order_refund'; //退款 $order_detail_id_diff = array_diff($order_detail_id_all, $order_detail_ids); if (empty($order_detail_id_diff)) $order_detail_id_diff = $order_detail_id_all; $order_marketing_list_diff = StoreOrderMarketing::lists([ 'where' => [['order_detail_id' => $order_detail_id_diff], ['status' => StatusEnum::ENABLED]], 'select' => 'user_coupon_id' ]); $user_coupon_id_arr_diff = []; foreach ($order_marketing_list_diff as $value) { $user_coupon_id = json_decode($value['user_coupon_id'], true); if (!empty($user_coupon_id)) $user_coupon_id_arr_diff = array_merge($user_coupon_id_arr_diff, array_keys($user_coupon_id)); } foreach ($order_marketing_list as $item) { $deduct_score+=$item['deduct_score']; $user_coupon_id_arr = json_decode($item['user_coupon_id'], true); if (!empty($user_coupon_id_arr)) { foreach ($user_coupon_id_arr as $user_coupon_id => $money_) { if (in_array($user_coupon_id, $user_coupon_id_arr_diff)){ $refund_user_coupon_id_arr[] = $user_coupon_id; if(isset($user_coupon_arr[$user_coupon_id])){ $user_coupon_arr[$user_coupon_id] += $money_; }else{ $user_coupon_arr[$user_coupon_id] = $money_; } } $user_coupon_order_detail_id_arr[$user_coupon_id][] = $item['order_detail_id']; } } } } else { //订单关闭 $deduct_score = 0; foreach ($order_marketing_list as $item) { $deduct_score+=$item['deduct_score']; $user_coupon_id = json_decode($item['user_coupon_id'], true); if (!empty($user_coupon_id)) $refund_user_coupon_id_arr = array_merge($refund_user_coupon_id_arr, array_keys($user_coupon_id)); foreach ($user_coupon_id as $key=>$value){ if(isset($user_coupon_arr[$key])){ $user_coupon_arr[$key] += $value; }else{ $user_coupon_arr[$key] = $value; } $user_coupon_order_detail_id_arr[$key][] = $item['order_detail_id']; } } $capital_type = 'consume_store_order_close'; } if($deduct_score>0){ $insert_wallet[] = [ 'mall_id' => $mall_id, 'user_id' => $user_id, 'is_frozen' => false, 'wallet_type' => 'score', 'money' => $deduct_score, 'desc' => $desc, 'source_table' => StoreOrder::tableName(), 'source_table_id' => $order_id, 'from_type' => UserWalletEnum::CONSUME, 'capital_type' =>$capital_type, ]; $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new \Exception(UserWalletService::getStaticError()); } if (!empty($refund_user_coupon_id_arr)) { StoreCouponUserRelate::updateAll(['is_use' => 0], ['id' => $refund_user_coupon_id_arr]); StoreCouponUseLog::updateAll(['status' => StatusEnum::DELETE], ['order_id' => $order_id, 'user_coupon_id' => $refund_user_coupon_id_arr]); $coupon_user_relation_list = StoreCouponUserRelate::lists(['where'=>[['id'=>$refund_user_coupon_id_arr]]]); foreach ($coupon_user_relation_list as $coupon_user_relation){ //优惠券-商品统计 $set_goods_data = [ 'mall_id'=>$mall_id, 'coupon_id'=>$coupon_user_relation['coupon_id'], 'payed_number'=>0, 'buyed_user_number'=>-1, ]; if(!empty($user_coupon_order_detail_id_arr[$coupon_user_relation['id']])){ $order_detail_ids = $user_coupon_order_detail_id_arr[$coupon_user_relation['id']]; foreach ($order_detail_ids as $order_detail_id){ if(!empty($order_detail_arr[$order_detail_id])){ $set_goods_data['good_id'] = !empty($order_detail_arr[$order_detail_id]['goods_id'])?$order_detail_arr[$order_detail_id]['goods_id']:0; $set_goods_data['payed_number'] = !empty($order_detail_arr[$order_detail_id]['num'])?-$order_detail_arr[$order_detail_id]['num']:0; if($coupon_user_relation['from_type']==1){ if(MallAddons::isInstall($coupon_user_relation['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){ AddonsCouponGoodStatistics::setData($set_goods_data); } }else{ StoreCouponGoodStatistics::setData($set_goods_data); } } } } //优惠券-统计 $set_data = [ 'mall_id'=>$mall_id, 'coupon_id'=>$coupon_user_relation['coupon_id'], 'used_number'=>-1, 'use_user_number'=>-1, 'use_order_number'=>-1, 'total_payed_amount'=>-($store_order['original_goods_price']+$store_order['shipping_money']), 'total_use_coupon_payed_amount'=>-($store_order['goods_price']+$store_order['shipping_money']), 'total_discount_amount'=>!empty($user_coupon_arr[$coupon_user_relation['id']])?-$user_coupon_arr[$coupon_user_relation['id']]:0, ]; if($coupon_user_relation['from_type']==1){ if(MallAddons::isInstall($coupon_user_relation['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){ AddonsCouponStatistics::setData($set_data); } }else{ $set_data['store_id'] = $coupon_user_relation['store_id']; StoreCouponStatistics::setData($set_data); } } } return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } }