MallService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace addons\QiShangTong\common\service;
  3. use addons\QiShangTong\common\models\QiShangTongBalance;
  4. use common\enums\StatusEnum;
  5. use common\models\common\MallSetting;
  6. use common\models\mall\Mall;
  7. class MallService
  8. {
  9. public function page(int $mall_id, array $params)
  10. {
  11. $this->batchInsert();
  12. $where = [
  13. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  14. ];
  15. if (SettingService::isMasterMall($mall_id)) {
  16. if (!empty($params['keyword'])) {
  17. $where[] = ['or', ['like', 'id', $params['keyword']], ['like', 'id', $params['keyword']]];
  18. }
  19. if (!empty($params['min_balance'])) {
  20. $ids = QiShangTongBalance::find()->where(['<=', 'balance', bcmul($params['min_balance'], 100)])->select('mall_id')->column();
  21. if (empty($ids)) return [];
  22. $where[] = ['id' => $ids];
  23. }
  24. $master = SettingService::getMasterMall();
  25. if (!empty($master)) $where[] = ['!=', 'id', $master];
  26. } else {
  27. $where[] = ['id' => $mall_id];
  28. }
  29. $ret = Mall::listPage([
  30. 'where' => $where,
  31. 'order' => 'id desc',
  32. 'limit' => $params['limit'] ?? 10,
  33. ]);
  34. if (!empty(Mall::getStaticError())) return Mall::getStaticError();
  35. if (!empty($ret['list'])) {
  36. $ids = array_column($ret['list'], 'id');
  37. $balances = QiShangTongBalance::lists([
  38. 'where' => [
  39. ['mall_id' => $ids]
  40. ],
  41. 'index' => 'mall_id',
  42. 'select'=> 'mall_id, balance, rate'
  43. ]);
  44. foreach ($ret['list'] as &$item) {
  45. $item['balance'] = $balances[$item['id']]['balance'] ?? 0;
  46. $item['rate'] = $balances[$item['id']]['rate'] ?? 0;
  47. $item['logo'] = MallService::getLogo($item['id']);
  48. }
  49. }
  50. return $ret;
  51. }
  52. protected function batchInsert()
  53. {
  54. $t_mids = Mall::getAllMallIds();
  55. $mids = QiShangTongBalance::find()->where(['mall_id' => $t_mids])->select(['mall_id'])->column();
  56. $mids = array_diff($t_mids, $mids);
  57. if (!empty($mids)) {
  58. $inserts = [];
  59. foreach ($mids as $mid) {
  60. $inserts[] = [
  61. 'mall_id' => $mid,
  62. 'created_at'=> time(),
  63. 'updated_at'=> time(),
  64. ];
  65. }
  66. if (!empty($inserts)) {
  67. QiShangTongBalance::find()->createCommand()->batchInsert(QiShangTongBalance::tableName(), array_keys($inserts[0]), $inserts)->execute();
  68. }
  69. }
  70. }
  71. public static function getLogo(int $mall_id)
  72. {
  73. return MallSetting::find()->where(['mall_id' => $mall_id])
  74. ->andWhere(['name' => 'logo'])->select('value')->scalar();
  75. }
  76. /**
  77. * 加价幅度
  78. * @param int $mall_id
  79. * @return string
  80. */
  81. public static function rate(int $mall_id)
  82. {
  83. // 如果是主商城,那么就是1:1
  84. if (SettingService::isMasterMall($mall_id)) return 1;
  85. $rate = QiShangTongBalance::find()->where(['mall_id' => $mall_id])
  86. ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
  87. ->select('rate')->scalar();
  88. return bcmul(!empty($rate) ? $rate : 100, 0.0001, 2);
  89. }
  90. }