| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace addons\Store\common\services;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\helper\Helper;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreMerchantReduceProfit;
- use common\enums\StatusEnum;
- use PHPUnit\Exception;
- class MerchantReduceProfitService
- {
- public function page(int $mall_id, array $params)
- {
- $where = [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['source' => StoreEnum::ADDONS_NAME]
- ];
- if (!empty($params['store_keyword'])) {
- if (is_numeric($params['store_keyword'])) {
- $s_ids = $params['store_keyword'];
- } else {
- $s_ids = Store::find()->where(['mall_id' => $mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['like', 'name', $params['store_keyword']])
- ->select('id')->column();
- if (empty($s_ids)) return [];
- }
- $where[] = ['source_id' => $s_ids];
- }
- if (!empty($params['rate'])) {
- $params[] = ['rate' => $params['rate']];
- }
- if (!empty($params['status'])) {
- $params[] = ['audit_status' => $params['status']];
- }
- if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
- if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
- $ret = StoreMerchantReduceProfit::listPage([
- 'where' => $where,
- 'order' => 'id desc',
- 'limit' => $params['limit'] ?? 10,
- 'with' => ['store', 'user']
- ]);
- if (!empty(StoreMerchantReduceProfit::getStaticError())) return StoreMerchantReduceProfit::getStaticError();
- return $ret;
- }
- /**
- * 设置
- * @param int $mall_id
- * @param int $user_id
- * @param int $store_id
- * @param float $rate
- * @param int $id
- * @return StoreMerchantReduceProfit|mixed|string|null
- */
- public function save(int $mall_id, int $user_id, int $store_id, float $rate, int $id = 0)
- {
- // 有可能是修改
- if ($id > 0) {
- // 修改
- $model = StoreMerchantReduceProfit::getOne([
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['audit_status' => StatusEnum::ENABLED],
- ['source' => StoreEnum::ADDONS_NAME],
- ['user_id' => $user_id],
- ['id' => $id]
- ]);
- if (!empty($model)) {
- $model->rate = $rate;
- if (!$model->save()) return $model->getErrorMessage();
- return false;
- }
- }
- if (empty($user_id)) {
- $user_id = Store::find()->where(['mall_id' => $mall_id])
- ->andWhere(['id' => $store_id])->andWhere(['status' => StatusEnum::ENABLED])->select('user_id')->scalar();
- if (empty($user_id)) return '用户不存在';
- }
- $model = StoreMerchantReduceProfit::getOne([
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['audit_status' => StatusEnum::ENABLED],
- ['source' => StoreEnum::ADDONS_NAME],
- ['user_id' => $user_id],
- ]);
- if (!empty($model)) return '还有待审核记录,请勿多次提交';
- return StoreMerchantReduceProfit::setData($mall_id, $user_id, StoreEnum::ADDONS_NAME, $store_id, $rate);
- }
- /**
- * 获取指定门店分佣
- * @param int $mall_id
- * @param int $user_id
- * @param int $store_id
- * @return false
- */
- public function getProfit(int $mall_id, int $user_id, int $store_id)
- {
- $where = [
- ['mall_id' => $mall_id],
- ['source_id' => $store_id],
- ['source' => StoreEnum::ADDONS_NAME],
- ['status' => StatusEnum::ENABLED],
- ['audit_status' => 100],
- ];
- if (!empty($user_id)) {
- $where[] = ['user_id' => $user_id];
- }
- // 当前用户设置的让利比例
- $reduceProfit = StoreMerchantReduceProfit::getOne($where, false, [], 'id desc');
- if (empty($reduceProfit)) return false;
- return $reduceProfit->rate;
- }
- /**
- * 审核
- * @param int $mall_id
- * @param int $id
- * @param int $admin_id
- * @param int $status
- * @param string $remark
- * @return string|true
- * @throws \yii\db\Exception
- * @throws \Exception
- */
- public function audit(int $mall_id, int $id, int $admin_id, int $status, string $remark)
- {
- // 审核
- $reduceProfit = StoreMerchantReduceProfit::getOne([
- ['mall_id' => $mall_id],
- ['id' => $id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['source' => StoreEnum::ADDONS_NAME],
- ['audit_status' => StatusEnum::ENABLED],
- ]);
- if (empty($reduceProfit)) return '记录不存在';
- $t = \Yii::$app->db->beginTransaction();
- try {
- // 只有审核通过的情况下才需要将旧的数据设置为失效状态
- if ($status == 100) {
- StoreMerchantReduceProfit::updateAll(['status' => StatusEnum::DISABLED],
- ['and', ['mall_id' => $mall_id], ['user_id' => $reduceProfit->user_id], ['status' => StatusEnum::ENABLED], ['audit_status' => 100]]);
- }
- $reduceProfit->audit_status = $status;
- $reduceProfit->audit_remark = $remark;
- $reduceProfit->audit_at = time();
- $reduceProfit->admins_id = $admin_id;
- $reduceProfit->status = $status == 100 ? StatusEnum::ENABLED : StatusEnum::DISABLED;
- if (!$reduceProfit->save()) throw new \Exception($reduceProfit->getErrorMessage());
- $t->commit();
- } catch (Exception $e) {
- $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * @param int $mall_id
- * @param int $user_id
- * @param int $store_id
- * @return array
- */
- public function profit(int $mall_id, int $user_id, int $store_id)
- {
- $config = Helper::hiGoConfig($mall_id);
- $rate = $this->getProfit($mall_id, $user_id, $store_id);
- return [
- 'rate' => $rate === false ? ($config['merchant_profit_multiple'] ?? 0) : $rate,
- 'is_enable' => $config['merchant_reduce_profit_enable'] ?? StatusEnum::DISABLED,
- 'max' => $config['merchant_reduce_profit_max'] ?? StatusEnum::DISABLED,
- 'min' => $config['merchant_reduce_profit_min'] ?? StatusEnum::DISABLED,
- 'new_rate' => ''
- ];
- }
- /**
- * @param int $mall_id
- * @param int $user_id
- * @param array $post
- * @return \Closure|string|null
- */
- public function storeApplyCreate(int $mall_id, int $user_id, array $post)
- {
- $reduce_profit_config = $this->profit($mall_id, $user_id, 0);
- $callback = null;
- if (!empty($reduce_profit_config['is_enable'])) { // 有开启
- if (empty($post['new_rate'])) return '让利比例不能为空';
- // 校验
- if ($post['new_rate'] < $reduce_profit_config['min'] || $post['new_rate'] > $reduce_profit_config['max']) {
- return "让利百分比必须在 {$reduce_profit_config['min']} - {$reduce_profit_config['max']} 之间";
- }
- $profit_id = 0;
- if (!empty($post['id'])) { // 编辑的
- $profit_id = StoreMerchantReduceProfit::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['source' => StoreEnum::ADDONS_NAME])
- ->andWhere(['audit_status' => StatusEnum::ENABLED])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->select('id')->scalar();
- }
- $profitService = $this;
- $callback = function ($store_id) use($profitService, $post, $mall_id, $user_id, $profit_id) {
- return $profitService->save($mall_id, $user_id, $store_id, $post['new_rate'], $profit_id);
- };
- }
- return $callback;
- }
- }
|