MerchantReduceProfitService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace addons\Store\common\services;
  3. use addons\Store\common\enums\StoreEnum;
  4. use addons\Store\common\helper\Helper;
  5. use addons\Store\common\models\Store;
  6. use addons\Store\common\models\StoreMerchantReduceProfit;
  7. use common\enums\StatusEnum;
  8. use PHPUnit\Exception;
  9. class MerchantReduceProfitService
  10. {
  11. public function page(int $mall_id, array $params)
  12. {
  13. $where = [
  14. ['mall_id' => $mall_id],
  15. ['status' => StatusEnum::ENABLED],
  16. ['source' => StoreEnum::ADDONS_NAME]
  17. ];
  18. if (!empty($params['store_keyword'])) {
  19. if (is_numeric($params['store_keyword'])) {
  20. $s_ids = $params['store_keyword'];
  21. } else {
  22. $s_ids = Store::find()->where(['mall_id' => $mall_id])
  23. ->andWhere(['status' => StatusEnum::ENABLED])
  24. ->andWhere(['like', 'name', $params['store_keyword']])
  25. ->select('id')->column();
  26. if (empty($s_ids)) return [];
  27. }
  28. $where[] = ['source_id' => $s_ids];
  29. }
  30. if (!empty($params['rate'])) {
  31. $params[] = ['rate' => $params['rate']];
  32. }
  33. if (!empty($params['status'])) {
  34. $params[] = ['audit_status' => $params['status']];
  35. }
  36. if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
  37. if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
  38. $ret = StoreMerchantReduceProfit::listPage([
  39. 'where' => $where,
  40. 'order' => 'id desc',
  41. 'limit' => $params['limit'] ?? 10,
  42. 'with' => ['store', 'user']
  43. ]);
  44. if (!empty(StoreMerchantReduceProfit::getStaticError())) return StoreMerchantReduceProfit::getStaticError();
  45. return $ret;
  46. }
  47. /**
  48. * 设置
  49. * @param int $mall_id
  50. * @param int $user_id
  51. * @param int $store_id
  52. * @param float $rate
  53. * @param int $id
  54. * @return StoreMerchantReduceProfit|mixed|string|null
  55. */
  56. public function save(int $mall_id, int $user_id, int $store_id, float $rate, int $id = 0)
  57. {
  58. // 有可能是修改
  59. if ($id > 0) {
  60. // 修改
  61. $model = StoreMerchantReduceProfit::getOne([
  62. ['mall_id' => $mall_id],
  63. ['status' => StatusEnum::ENABLED],
  64. ['audit_status' => StatusEnum::ENABLED],
  65. ['source' => StoreEnum::ADDONS_NAME],
  66. ['user_id' => $user_id],
  67. ['id' => $id]
  68. ]);
  69. if (!empty($model)) {
  70. $model->rate = $rate;
  71. if (!$model->save()) return $model->getErrorMessage();
  72. return false;
  73. }
  74. }
  75. if (empty($user_id)) {
  76. $user_id = Store::find()->where(['mall_id' => $mall_id])
  77. ->andWhere(['id' => $store_id])->andWhere(['status' => StatusEnum::ENABLED])->select('user_id')->scalar();
  78. if (empty($user_id)) return '用户不存在';
  79. }
  80. $model = StoreMerchantReduceProfit::getOne([
  81. ['mall_id' => $mall_id],
  82. ['status' => StatusEnum::ENABLED],
  83. ['audit_status' => StatusEnum::ENABLED],
  84. ['source' => StoreEnum::ADDONS_NAME],
  85. ['user_id' => $user_id],
  86. ]);
  87. if (!empty($model)) return '还有待审核记录,请勿多次提交';
  88. return StoreMerchantReduceProfit::setData($mall_id, $user_id, StoreEnum::ADDONS_NAME, $store_id, $rate);
  89. }
  90. /**
  91. * 获取指定门店分佣
  92. * @param int $mall_id
  93. * @param int $user_id
  94. * @param int $store_id
  95. * @return false
  96. */
  97. public function getProfit(int $mall_id, int $user_id, int $store_id)
  98. {
  99. $where = [
  100. ['mall_id' => $mall_id],
  101. ['source_id' => $store_id],
  102. ['source' => StoreEnum::ADDONS_NAME],
  103. ['status' => StatusEnum::ENABLED],
  104. ['audit_status' => 100],
  105. ];
  106. if (!empty($user_id)) {
  107. $where[] = ['user_id' => $user_id];
  108. }
  109. // 当前用户设置的让利比例
  110. $reduceProfit = StoreMerchantReduceProfit::getOne($where, false, [], 'id desc');
  111. if (empty($reduceProfit)) return false;
  112. return $reduceProfit->rate;
  113. }
  114. /**
  115. * 审核
  116. * @param int $mall_id
  117. * @param int $id
  118. * @param int $admin_id
  119. * @param int $status
  120. * @param string $remark
  121. * @return string|true
  122. * @throws \yii\db\Exception
  123. * @throws \Exception
  124. */
  125. public function audit(int $mall_id, int $id, int $admin_id, int $status, string $remark)
  126. {
  127. // 审核
  128. $reduceProfit = StoreMerchantReduceProfit::getOne([
  129. ['mall_id' => $mall_id],
  130. ['id' => $id],
  131. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  132. ['source' => StoreEnum::ADDONS_NAME],
  133. ['audit_status' => StatusEnum::ENABLED],
  134. ]);
  135. if (empty($reduceProfit)) return '记录不存在';
  136. $t = \Yii::$app->db->beginTransaction();
  137. try {
  138. // 只有审核通过的情况下才需要将旧的数据设置为失效状态
  139. if ($status == 100) {
  140. StoreMerchantReduceProfit::updateAll(['status' => StatusEnum::DISABLED],
  141. ['and', ['mall_id' => $mall_id], ['user_id' => $reduceProfit->user_id], ['status' => StatusEnum::ENABLED], ['audit_status' => 100]]);
  142. }
  143. $reduceProfit->audit_status = $status;
  144. $reduceProfit->audit_remark = $remark;
  145. $reduceProfit->audit_at = time();
  146. $reduceProfit->admins_id = $admin_id;
  147. $reduceProfit->status = $status == 100 ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  148. if (!$reduceProfit->save()) throw new \Exception($reduceProfit->getErrorMessage());
  149. $t->commit();
  150. } catch (Exception $e) {
  151. $t->rollBack();
  152. return $e->getMessage();
  153. }
  154. return true;
  155. }
  156. /**
  157. * @param int $mall_id
  158. * @param int $user_id
  159. * @param int $store_id
  160. * @return array
  161. */
  162. public function profit(int $mall_id, int $user_id, int $store_id)
  163. {
  164. $config = Helper::hiGoConfig($mall_id);
  165. $rate = $this->getProfit($mall_id, $user_id, $store_id);
  166. return [
  167. 'rate' => $rate === false ? ($config['merchant_profit_multiple'] ?? 0) : $rate,
  168. 'is_enable' => $config['merchant_reduce_profit_enable'] ?? StatusEnum::DISABLED,
  169. 'max' => $config['merchant_reduce_profit_max'] ?? StatusEnum::DISABLED,
  170. 'min' => $config['merchant_reduce_profit_min'] ?? StatusEnum::DISABLED,
  171. 'new_rate' => ''
  172. ];
  173. }
  174. /**
  175. * @param int $mall_id
  176. * @param int $user_id
  177. * @param array $post
  178. * @return \Closure|string|null
  179. */
  180. public function storeApplyCreate(int $mall_id, int $user_id, array $post)
  181. {
  182. $reduce_profit_config = $this->profit($mall_id, $user_id, 0);
  183. $callback = null;
  184. if (!empty($reduce_profit_config['is_enable'])) { // 有开启
  185. if (empty($post['new_rate'])) return '让利比例不能为空';
  186. // 校验
  187. if ($post['new_rate'] < $reduce_profit_config['min'] || $post['new_rate'] > $reduce_profit_config['max']) {
  188. return "让利百分比必须在 {$reduce_profit_config['min']} - {$reduce_profit_config['max']} 之间";
  189. }
  190. $profit_id = 0;
  191. if (!empty($post['id'])) { // 编辑的
  192. $profit_id = StoreMerchantReduceProfit::find()
  193. ->where(['mall_id' => $mall_id])
  194. ->andWhere(['user_id' => $user_id])
  195. ->andWhere(['source' => StoreEnum::ADDONS_NAME])
  196. ->andWhere(['audit_status' => StatusEnum::ENABLED])
  197. ->andWhere(['status' => StatusEnum::ENABLED])
  198. ->select('id')->scalar();
  199. }
  200. $profitService = $this;
  201. $callback = function ($store_id) use($profitService, $post, $mall_id, $user_id, $profit_id) {
  202. return $profitService->save($mall_id, $user_id, $store_id, $post['new_rate'], $profit_id);
  203. };
  204. }
  205. return $callback;
  206. }
  207. }