| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- <?php
- namespace addons\Seed\common\models;
- use addons\Seed\common\enums\SeedEnum;
- use addons\Store\common\models\StoreCashierOrder;
- use addons\Store\common\models\StoreOrder;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\user\User;
- use services\common\UserWalletService;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_seed_order}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $store_id 门店id
- * @property int $user_id 用户ID
- * @property int $identity 身份[1:消费者,2:店长]
- * @property int $order_id 订单ID
- * @property int $event_pay 事件类型[1=普通订单,2=收银台订单]
- * @property float $credit_money 赊账金额,单位:元
- * @property int $is_credit 是否是赊账订单[0:否,1:是]
- * @property string $order_no 订单号
- * @property string $goods_name 第一个商品名称
- * @property int $seed_num 种子金数量
- * @property float $basic_unit_price 种子金基础单价,单位:元
- * @property float $unit_price 种子金现单价,单位:元
- * @property float $seed_cost 种子金总价值,单位:元
- * @property int $increase_num 增长次数
- * @property int $join_pool_money 存放到能量池的金额,单位:元
- * @property int $can_send_at 可发放能量时间
- * @property int $last_send_at 上次能量池释放时间
- * @property int|null $seed_status 状态[0:冻结中,1:增长中,2:已收取]
- * @property int $is_able_collect 是否允许收取[0:不允许,1:允许]
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class SeedOrder extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_seed_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'user_id', 'identity', 'order_id', 'event_pay', 'is_credit', 'seed_num', 'increase_num', 'can_send_at', 'last_send_at', 'seed_status', 'status', 'is_able_collect', 'created_at', 'updated_at'], 'integer'],
- [['credit_money','basic_unit_price', 'unit_price', 'seed_cost', 'join_pool_money'], 'number'],
- [['order_no'], 'string', 'max' => 60],
- [['goods_name'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'store_id' => '门店id',
- 'user_id' => '用户ID',
- 'identity' => '身份[1:消费者,2:店长]',
- 'order_id' => '订单ID',
- 'order_no' => '订单号',
- 'event_pay' => '事件类型[1=普通订单,2=收银台订单]',
- 'is_credit' => '是否是赊账订单[0:否,1:是]',
- 'credit_money' => '赊账金额,单位:元',
- 'goods_name' => '第一个商品名称',
- 'seed_num' => '种子金数量',
- 'basic_unit_price' => '种子金基础单价,单位:元',
- 'unit_price' => '种子金现单价,单位:元',
- 'seed_cost' => '种子金总价值,单位:元',
- 'increase_num' => '增长次数',
- 'join_pool_money' => '存放到能量池的金额,单位:元',
- 'can_send_at' => '可发放能量时间',
- 'last_send_at' => '上次能量池释放时间',
- 'seed_status' => '状态[0:冻结中,1:增长中,2:已收取]',
- 'is_able_collect' => '是否允许收取[0:不允许,1:允许]',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 关联用户表
- * @Author: lun
- * @DateTime: 2022/7/28 0028 10:07
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,username,avatar_url');
- }
- /**
- * 关联订单表
- * @Author: lun
- * @DateTime: 2022/8/26 0026 19:33
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getOrder()
- {
- return $this->hasOne(StoreOrder::class, ['id' => 'order_id'])->select('id,order_no,order_status');
- }
- /**
- * 消费生成数据
- * @Author: lun
- * @DateTime: 2022/7/27 0027 11:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return bool
- */
- public static function consumeOrder($params, $extra)
- {
- try {
- $model = self::findOne(['user_id' => $params['user_id'], 'order_no' => $params['order_no'], 'identity' => $params['identity'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- // 额度变化处理
- if (!empty($params['unit_price'])) {
- $unit_price = $params['unit_price'];// 变动的单价
- $model->unit_price += $params['unit_price'];
- !empty($params['seed_num']) && $model->seed_num = $params['seed_num'];
- $model->seed_cost = sprintf("%.2f",substr(sprintf("%.3f", $model->seed_num * $unit_price), 0, -2));// 不四舍五入保留两位小数
- unset($params['unit_price']);
- }
- if (!$model->load($params, '') || !$model->save()) throw new Exception('订单种子金:'.$model->getErrorMessage());
- // 增加用户种子金
- $res = SeedUser::setData($model->mall_id, $model->user_id, [
- 'total_seed_num' => $model->seed_num,
- 'total_seed_cost' => $model->seed_cost,
- ]);
- if ($res === false) throw new \Exception(SeedUser::getStaticError());
- // 增加种子金变动记录
- $res = SeedLog::setData([
- 'seed_order_id' => $model->id,
- 'mall_id' => $model->mall_id,
- 'store_id' => $model->store_id,
- 'user_id' => $model->user_id,
- 'order_id' => $model->order_id,
- 'event_pay' => $model->event_pay,
- 'seed_num' => $model->seed_num,
- 'seed_cost' => $model->seed_cost,
- 'change_unit_price' => $unit_price ?? 0,
- 'after_unit_price' => $model->unit_price,
- 'desc' => $extra['desc'] ?? '',
- 'capital_type' => $extra['capital_type'] ?? '',
- 'pool_id' => $extra['pool_id'] ?? 0,
- 'setting' => $extra['setting'] ?? '',
- ]);
- if ($res === false) throw new \Exception(SeedLog::getStaticError());
- return true;
- } catch (\Exception $e) {
- self::setStaticError('用户种子金保存:第'.$e->getLine().'行,原因:'.$e->getMessage());
- return false;
- }
- }
- /**
- * API接口收取种子金
- * @Author: lun
- * @DateTime: 2022/8/30 0030 18:34
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $user_id
- * @param $id
- * @param string $desc
- * @throws Exception
- */
- public static function collect($user_id, $id, $desc = '种子金收取')
- {
- try {
- // 种子金订单状态修改
- $seed_order = self::findOne(['id' => $id,'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($seed_order)) throw new \Exception('暂无此订单');
- self::collectError($seed_order->seed_status);
- // 检查是否是赊账订单,并且是否可收取
- if ($seed_order->is_credit == StatusEnum::ENABLED && $seed_order->is_able_collect == StatusEnum::DISABLED) {
- throw new Exception('该订单为赊账订单,未还款前暂不允许收取');
- }
- $res = self::handleCollect($seed_order, 'api', $desc);
- if ($res === false) throw new \Exception(self::getStaticError());
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 种子金收取处理
- * @Author: lun
- * @DateTime: 2022/8/1 0001 10:11
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $seed_order
- * @param $type
- * @param $desc
- */
- public static function handleCollect($seed_order, $type = 'api', $desc = '种子金收取')
- {
- $trans = \Yii::$app->db->beginTransaction();
- try {
- // 获取收取类型[1=支付完成后,2=订单完成后]
- $collect_type = \Yii::$app->services->setting->addons->get($seed_order->mall_id, SeedEnum::ADDONS_NAME, 'basic.collect_type');
- if ($collect_type == 2 && $seed_order->event_pay == 1) {
- // 获取订单状态
- $order_status = StoreOrder::find()->select('order_status')->where(['id' => $seed_order->order_id])->scalar();
- if ($order_status != OrderStatusEnum::ACCOMPLISH) {
- if ($type == 'api') {
- throw new Exception('订单未完成不可收取');
- } else {
- $trans->commit();
- return true;
- }
- }
- }
- $seed_order->seed_status = SeedEnum::SEED_COLLECT;
- $seed_order->is_able_collect = StatusEnum::ENABLED;
- if (!$seed_order->save()) throw new Exception($seed_order->getErrorMessage());
- // 增加用户种子金收取金额
- $res = SeedUser::setData($seed_order->mall_id, $seed_order->user_id, ['collect_seed_num' => $seed_order->seed_num, 'total_change_money' => $seed_order->seed_cost]);
- if ($res === false) throw new \Exception(SeedUser::getStaticError());
- // 增加种子金收取记录
- $res = SeedLog::setData([
- 'seed_order_id' => $seed_order->id,
- 'mall_id' => $seed_order->mall_id,
- 'store_id' => $seed_order->store_id,
- 'user_id' => $seed_order->user_id,
- 'order_id' => $seed_order->order_id,
- 'event_pay' => $seed_order->event_pay,
- 'seed_num' => $seed_order->seed_num,
- 'seed_cost' => $seed_order->seed_cost,
- 'change_unit_price' => 0,
- 'after_unit_price' => $seed_order->unit_price,
- 'desc' => $desc,
- 'capital_type' => SeedEnum::LOG_COLLECT,
- ]);
- if ($res === false) throw new \Exception(SeedLog::getStaticError());
- // 用户数据统计
- $res = SeedUserStatistics::setData([
- 'mall_id' => $seed_order->mall_id,
- 'store_id' => $seed_order->store_id,
- 'user_id' => $seed_order->user_id,
- ], ['collect_seed_num' => $seed_order->seed_num]);
- if ($res === false) throw new \Exception(SeedUserStatistics::getStaticError());
- // 资金统计
- SeedCapitalStatistics::saveStatistics($seed_order->mall_id, ['collect_seed_num' => $seed_order->seed_num]);
- // 插入资金
- $insert_wallet[] = [
- 'mall_id' => $seed_order->mall_id,
- 'user_id' => $seed_order->user_id,
- 'is_frozen' => false,
- 'wallet_type' => 'balance',
- 'money' => $seed_order->seed_cost,
- 'desc' => $desc,
- 'source_table' => 'seed_order',
- 'source_table_id' => $seed_order->id,
- 'from_type' => SeedEnum::ADDONS_NAME,
- 'capital_type' => SeedEnum::LOG_COLLECT,
- ];
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 收取状态提示
- * @Author: lun
- * @DateTime: 2022/8/30 0030 15:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $seed_status
- * @return bool
- * @throws \Exception
- */
- private static function collectError($seed_status)
- {
- switch ($seed_status) {
- case SeedEnum::SEED_COLLECT :
- throw new \Exception('该订单已被收取');
- break;
- case SeedEnum::SEED_REFUND :
- throw new \Exception('订单已退款,无法收取');
- break;
- case SeedEnum::SEED_BAD :
- throw new \Exception('订单被冻结,请尽快还款或等待订单完成');
- break;
- default :
- return true;
- break;
- }
- }
- /**
- * 批量收取
- * @Author: lun
- * @DateTime: 2022/8/31 0031 11:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $seed_order_ids
- * @param int $event_pay
- * @return array|bool
- */
- public static function batchCollect($mall_id, $seed_order_ids, $event_pay = 1)
- {
- $trans = \Yii::$app->db->beginTransaction();
- try {
- // 获取订单id
- $store_order_ids = self::find()->select('order_id')->where(['id' => $seed_order_ids])->column();
- $collect_order_ids = $store_order_ids = array_unique($store_order_ids);
- $handle_credit = [];// 赊账订单自动收取
- // 获取收取类型[1=支付完成后,2=订单完成后]
- $collect_type = \Yii::$app->services->setting->addons->get($mall_id, SeedEnum::ADDONS_NAME, 'basic.collect_type');
- if ($collect_type == 2 && $event_pay <= 2) {
- // 获取已完成的订单id
- if ($event_pay == 1) {
- $collect_order_ids = StoreOrder::find()->select('id')->where(['id' => $store_order_ids, 'order_status' => OrderStatusEnum::ACCOMPLISH])->column();
- } else {
- $collect_order_ids = StoreCashierOrder::find()->select('id')->where(['id' => $store_order_ids, 'pay_status' => StatusEnum::ENABLED])->column();
- }
- // 将未完成的订单并且是已达到倍数自动收取的订单改为停止增长 - 针对消费者订单停止增长
- $diff_order_ids = array_diff($store_order_ids, $collect_order_ids);
- if ($diff_order_ids) {
- self::updateAll(['seed_status' => SeedEnum::SEED_STOP, 'updated_at' => time()], 'id in('.implode(',', $seed_order_ids).') and order_id in('.implode(',', $diff_order_ids).") and event_pay={$event_pay}");
- }
- }
- // 查询条件
- $where = ['id' => $seed_order_ids, 'order_id' => $collect_order_ids, 'event_pay' => $event_pay, 'seed_status' => SeedEnum::SEED_ADD, 'status' => StatusEnum::ENABLED];
- // 获取可收取的种子金订单
- $list = self::find()->where($where)->with(['order'])->asArray()->all();
- if (empty($list)) {
- $trans->commit();
- return $handle_credit;
- }
- // 批量修改状态,改为已收取
- self::updateAll(['seed_status' => SeedEnum::SEED_COLLECT, 'updated_at' => time()], 'id in('.implode(',', $seed_order_ids).') and order_id in('.implode(',', $collect_order_ids).") and event_pay={$event_pay}");
- // 增加用户收取金额跟数量
- $seed_users = $statistics = [];
- $total_collect_num = 0;
- $change_money_arr = [];
- foreach ($list as $item) {
- $seed_users[$item['user_id']] = empty($seed_users[$item['user_id']]) ? $item['seed_num'] : $item['seed_num'] + $seed_users[$item['user_id']];
- $statistics[$item['store_id']][$item['user_id']] = empty($statistics[$item['store_id']][$item['user_id']]) ? $item['seed_num'] : $item['seed_num'] + $statistics[$item['store_id']][$item['user_id']];
- $change_money_arr[$item['user_id']] = empty($change_money_arr[$item['user_id']]) ? $item['seed_cost'] : $item['seed_cost'] + $change_money_arr[$item['user_id']];
- $total_collect_num += $item['seed_num'];
- // 插入资金列表
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $item['user_id'],
- 'is_frozen' => false,
- 'wallet_type' => 'balance',
- 'money' => $item['seed_cost'],
- 'desc' => '自动收取种子金',
- 'source_table' => 'seed_order',
- 'source_table_id' => $item['id'],
- 'from_type' => SeedEnum::ADDONS_NAME,
- 'capital_type' => SeedEnum::LOG_COLLECT,
- ];
- // 插入收取记录
- $res = SeedLog::setData([
- 'seed_order_id' => $item['id'],
- 'mall_id' => $item['mall_id'],
- 'store_id' => $item['store_id'],
- 'user_id' => $item['user_id'],
- 'identity' => $item['identity'],
- 'order_id' => $item['order_id'],
- 'event_pay' => $item['event_pay'],
- 'seed_num' => $item['seed_num'],
- 'seed_cost' => $item['seed_cost'],
- 'change_unit_price' => 0,
- 'after_unit_price' => $item['unit_price'],
- 'desc' => '自动收取种子金',
- 'capital_type' => SeedEnum::LOG_COLLECT,
- ]);
- if ($res === false) throw new \Exception(SeedLog::getStaticError());
- // 赊账订单处理 - 只处理消费者
- if ($item['is_credit'] == StatusEnum::ENABLED && $item['identity'] == StatusEnum::ENABLED) $handle_credit[$item['event_pay']][] = $item['order_id'];
- }
- // 增加用户种子金收取金额
- foreach ($seed_users as $user_id => $collect_seed_num) {
- $res = SeedUser::setData($mall_id, $user_id, ['collect_seed_num' => $collect_seed_num, 'total_change_money' => $change_money_arr[$user_id]]);
- if ($res === false) throw new \Exception(SeedUser::getStaticError());
- }
- // 用户数据统计
- foreach ($statistics as $store_id => $item) {
- foreach ($item as $user_id => $seed_num) {
- $res = SeedUserStatistics::setData([
- 'mall_id' => $mall_id,
- 'store_id' => $store_id,
- 'user_id' => $user_id,
- ], ['collect_seed_num' => $seed_num]);
- if ($res === false) throw new \Exception(SeedUserStatistics::getStaticError());
- }
- }
- // 资金统计
- SeedCapitalStatistics::saveStatistics($mall_id, ['collect_seed_num' => $total_collect_num]);
- // 插入资金
- if ($insert_wallet) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- }
- $trans->commit();
- return $handle_credit;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::setStaticError('种子金批量自动收取:第'.$e->getLine().'行,原因:'.$e->getMessage());
- return false;
- }
- }
- /**
- * 坏账订单处理
- * @Author: lun
- * @DateTime: 2022/8/5 0005 16:17
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $seed_order_ids
- * @return bool
- */
- public static function badOrderHandle($mall_id, $seed_order_ids)
- {
- try {
- // 订单列表
- $bad_seed_num = self::find()->where(['id' => $seed_order_ids, 'seed_status' => SeedEnum::SEED_ADD, 'status' => StatusEnum::ENABLED])->sum('seed_num');
- // 批量修改状态
- self::updateAll(['seed_status' => SeedEnum::SEED_BAD, 'updated_at' => time()], 'id in('.implode(',', $seed_order_ids).')');
- // 增加坏账订单金额
- if ($bad_seed_num) SeedCapitalStatistics::saveStatistics($mall_id, ['bad_seed_num' => $bad_seed_num]);
- return true;
- } catch (\Exception $e) {
- self::setStaticError('坏账订单处理:'.$e->getMessage());
- return false;
- }
- }
- }
|