| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace addons\QiShangTong\common\service;
- use addons\QiShangTong\common\models\QiShangTongBalance;
- use common\enums\StatusEnum;
- use common\models\common\MallSetting;
- use common\models\mall\Mall;
- class MallService
- {
- public function page(int $mall_id, array $params)
- {
- $this->batchInsert();
- $where = [
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ];
- if (SettingService::isMasterMall($mall_id)) {
- if (!empty($params['keyword'])) {
- $where[] = ['or', ['like', 'id', $params['keyword']], ['like', 'id', $params['keyword']]];
- }
- if (!empty($params['min_balance'])) {
- $ids = QiShangTongBalance::find()->where(['<=', 'balance', bcmul($params['min_balance'], 100)])->select('mall_id')->column();
- if (empty($ids)) return [];
- $where[] = ['id' => $ids];
- }
- $master = SettingService::getMasterMall();
- if (!empty($master)) $where[] = ['!=', 'id', $master];
- } else {
- $where[] = ['id' => $mall_id];
- }
- $ret = Mall::listPage([
- 'where' => $where,
- 'order' => 'id desc',
- 'limit' => $params['limit'] ?? 10,
- ]);
- if (!empty(Mall::getStaticError())) return Mall::getStaticError();
- if (!empty($ret['list'])) {
- $ids = array_column($ret['list'], 'id');
- $balances = QiShangTongBalance::lists([
- 'where' => [
- ['mall_id' => $ids]
- ],
- 'index' => 'mall_id',
- 'select'=> 'mall_id, balance, rate'
- ]);
- foreach ($ret['list'] as &$item) {
- $item['balance'] = $balances[$item['id']]['balance'] ?? 0;
- $item['rate'] = $balances[$item['id']]['rate'] ?? 0;
- $item['logo'] = MallService::getLogo($item['id']);
- }
- }
- return $ret;
- }
- protected function batchInsert()
- {
- $t_mids = Mall::getAllMallIds();
- $mids = QiShangTongBalance::find()->where(['mall_id' => $t_mids])->select(['mall_id'])->column();
- $mids = array_diff($t_mids, $mids);
- if (!empty($mids)) {
- $inserts = [];
- foreach ($mids as $mid) {
- $inserts[] = [
- 'mall_id' => $mid,
- 'created_at'=> time(),
- 'updated_at'=> time(),
- ];
- }
- if (!empty($inserts)) {
- QiShangTongBalance::find()->createCommand()->batchInsert(QiShangTongBalance::tableName(), array_keys($inserts[0]), $inserts)->execute();
- }
- }
- }
- public static function getLogo(int $mall_id)
- {
- return MallSetting::find()->where(['mall_id' => $mall_id])
- ->andWhere(['name' => 'logo'])->select('value')->scalar();
- }
- /**
- * 加价幅度
- * @param int $mall_id
- * @return string
- */
- public static function rate(int $mall_id)
- {
- // 如果是主商城,那么就是1:1
- if (SettingService::isMasterMall($mall_id)) return 1;
- $rate = QiShangTongBalance::find()->where(['mall_id' => $mall_id])
- ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
- ->select('rate')->scalar();
- return bcmul(!empty($rate) ? $rate : 100, 0.0001, 2);
- }
- }
|