123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\common\repositories\system\config;
- use app\common\dao\system\config\SystemConfigValueDao;
- use app\common\repositories\BaseRepository;
- use app\common\repositories\store\product\ProductRepository;
- use app\common\repositories\system\groupData\GroupDataRepository;
- use app\common\repositories\system\groupData\GroupRepository;
- use crmeb\jobs\SyncProductTopJob;
- use crmeb\services\DownloadImageService;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\Queue;
- /**
- * Class ConfigValueRepository
- * @package app\common\repositories\system\config
- * @mixin SystemConfigValueDao
- */
- class ConfigValueRepository extends BaseRepository
- {
- /**
- * ConfigValueRepository constructor.
- * @param SystemConfigValueDao $dao
- */
- public function __construct(SystemConfigValueDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * @param array $keys
- * @param int $merId
- * @return array
- * @author xaboy
- * @day 2020-03-27
- */
- public function more(array $keys, int $merId): array
- {
- $config = $this->dao->fields($keys, $merId);
- foreach ($keys as $key) {
- if (!isset($config[$key])) $config[$key] = '';
- }
- return $config;
- }
- /**
- * @param string $key
- * @param int $merId
- * @return mixed|string|null
- * @author xaboy
- * @day 2020-05-08
- */
- public function get(string $key, int $merId)
- {
- $value = $this->dao->value($key, $merId);
- return $value ?? '';
- }
- /**
- * @param int|array $cid
- * @param array $formData
- * @param int $merId
- * @author xaboy
- * @day 2020-03-27
- */
- public function save($cid, array $formData, int $merId)
- {
- $keys = array_keys($formData);
- $keys = app()->make(ConfigRepository::class)->intersectionKey($cid, $keys);
- if (!count($keys)) return;
- foreach ($keys as $key => $info) {
- if (!isset($formData[$key]))
- unset($formData[$key]);
- else {
- if ($info['config_type'] == 'number') {
- if ($formData[$key] === '' || $formData[$key] < 0)
- throw new ValidateException($info['config_name'] . '不能小于0');
- $formData[$key] = floatval($formData[$key]);
- }
- $this->separate($key,$formData[$key],$merId);
- }
- }
- $this->setFormData($formData, $merId);
- }
- /**
- * TODO 需要做特殊处理的配置参数
- * @param $key
- * @author Qinii
- * @day 2022/11/17
- */
- public function separate($key,$value,$merId)
- {
- switch($key) {
- case 'mer_svip_status':
- //修改商户的会员状态
- app()->make(ProductRepository::class)->getSearch([])->where(['mer_id' => $merId,'product_type' => 0])->update([$key => $value]);
- break;
- // case 'site_ico':
- // //修改ico图标
- // $stie_ico = systemConfig('site_ico');
- // $ico = substr($value,-3);
- // if ($stie_ico != $value && $ico != 'ico') {
- // $path = app()->make(DownloadImageService::class)->downloadImage($value,'def','favicon.ico',1)['path'];
- // $value = public_path().$path;
- // if (!is_file($value)) throw new ValidateException('Ico图标文件不存在');
- // rename($value, public_path() . 'favicon.ico');
- // }
- // break;
- //热卖排行
- case 'hot_ranking_switch':
- if ($value) {
- Queue::push(SyncProductTopJob::class, []);
- }
- break;
- case 'svip_switch_status':
- if ($value == 1) {
- $groupDataRepository = app()->make(GroupDataRepository::class);
- $groupRepository = app()->make(GroupRepository::class);
- $group_id = $groupRepository->getSearch(['group_key' => 'svip_pay'])->value('group_id');
- $where['group_id'] = $group_id;
- $where['status'] = 1;
- $count = $groupDataRepository->getSearch($where)->field('group_data_id,value,sort,status')->count();
- if (!$count)
- throw new ValidateException('请先添加会员类型');
- }
- break;
- default:
- break;
- }
- return ;
- }
- public function setFormData(array $formData, int $merId)
- {
- Db::transaction(function () use ($merId, $formData) {
- foreach ($formData as $key => $value) {
- if ($this->dao->merExists($key, $merId))
- $this->dao->merUpdate($merId, $key, ['value' => $value]);
- else
- $this->dao->create([
- 'mer_id' => $merId,
- 'value' => $value,
- 'config_key' => $key
- ]);
- }
- });
- }
- }
|