| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- namespace common\models\forms;
- use common\components\Model;
- use common\helpers\Date;
- use common\libs\logging\operate\AdminOperate;
- use common\models\Config;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * Login form
- */
- class ConfigForm extends Model
- {
- const BONUS_TO_STRING_FIELD = [
- //共享奖
- 'shareRecIncomeFrom',
- 'shareIncomeFrom',
- //管理奖
- 'parentOddIncomeFrom_0',
- 'parentOddIncomeFrom_1',
- 'parentOddIncomeFrom_2',
- 'parentOddIncomeFrom_3',
- //共享奖-复消
- 'fxShareRecIncomeFrom',
- 'fxShareIncomeFrom',
- //管理奖
- 'fxParentOddIncomeFrom_0',
- 'fxParentOddIncomeFrom_1',
- 'fxParentOddIncomeFrom_2',
- 'fxParentOddIncomeFrom_3',
- //服务奖
- 'decRoleBonusFrom',
- ];
- const BONUS_JSON_FIELD = [
- 'jxStandard'
- ];
- public function init() {
- parent::init();
- $this->adminOperateLogger = new AdminOperate([
- 'fetchClass' => Config::class,
- ]);
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- //[['postData'], 'required'],
- ];
- }
- public function attributeLabels()
- {
- return [
- //'postData' => '传入数据',
- ];
- }
- /**
- * 更新配置
- * @return bool|null
- * @throws Exception
- */
- public function updateBase(){
- if(!$this->validate()){
- return null;
- }
- $configs = Config::find()->where("TYPE='base'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
- $configNames = [];
- foreach ($configs as $value){
- $configNames[] = $value['CONFIG_NAME'];
- }
- $beforeData = Config::getConfigByType('base');
- $this->adminOperateLogger->saveBeforeContent=$beforeData;
- $postData = \Yii::$app->request->post();
- $transaction = \Yii::$app->db->beginTransaction();
- try{
- foreach ($configs as $key => $value) {
- if (array_key_exists($key, $postData)) {
- if (!$postData[$key]) $postData[$key] = 0;
- Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
- }
- }
- $transaction->commit();
- } catch (Exception $e){
- $transaction->rollBack();
- $this->addError('updateBase', $e->getMessage());
- return null;
- }
- $afterData = Config::getConfigByType('base');
- $this->adminOperateLogger->saveAfterContent=$afterData;
- unset($beforeData,$afterData);
- $this->adminOperateLogger->clean()->save([
- 'optType' => '更新站点设置',
- ]);
- return true;
- }
- /**
- * 更新奖金配置
- * @return bool|null
- * @throws Exception
- */
- public function updateBonus(){
- if(!$this->validate()){
- return null;
- }
- $configs = Config::find()->where("TYPE='bonus'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
- $configNames = [];
- foreach ($configs as $value){
- $configNames[] = $value['CONFIG_NAME'];
- }
- $beforeData = Config::getConfigByType('bonus');
- $this->adminOperateLogger->saveBeforeContent=$beforeData;
- $postData = \Yii::$app->request->post();
- $transaction = \Yii::$app->db->beginTransaction();
- try{
- foreach ($configs as $key => $value) {
- if (array_key_exists($key, $postData)) {
- if (!$postData[$key]) $postData[$key] = 0;
- if( in_array($key, self::BONUS_TO_STRING_FIELD) ) $postData[$key] = implode(",", $postData[$key]);
- if( in_array($key, self::BONUS_JSON_FIELD) ) $postData[$key] = JSON::encode($postData[$key]);
- Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
- }
- }
- $transaction->commit();
- } catch (Exception $e){
- $transaction->rollBack();
- $this->addError('updateBase', $e->getMessage());
- return null;
- }
- $afterData = Config::getConfigByType('bonus');
- $this->adminOperateLogger->saveAfterContent=$afterData;
- unset($beforeData,$afterData);
- $this->adminOperateLogger->clean()->save([
- 'optType' => '更新奖金配置',
- ]);
- return true;
- }
- /**
- * 更新短信配置
- * @return bool|null
- * @throws Exception
- */
- public function updateSms(){
- if(!$this->validate()){
- return null;
- }
- $configs = Config::find()->where("TYPE='sms'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
- $configNames = [];
- foreach ($configs as $value){
- $configNames[] = $value['CONFIG_NAME'];
- }
- $beforeData = Config::getConfigByType('sms');
- $this->adminOperateLogger->saveBeforeContent=$beforeData;
- $postData = \Yii::$app->request->post();
- $transaction = \Yii::$app->db->beginTransaction();
- try{
- foreach ($configs as $key => $value) {
- if (array_key_exists($key, $postData)) {
- if (!$postData[$key]) $postData[$key] = 0;
- if(is_array($postData[$key])) $postData[$key] = implode(",", $postData[$key]);
- Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
- }
- }
- $transaction->commit();
- } catch (Exception $e){
- $transaction->rollBack();
- $this->addError('updateSms', $e->getMessage());
- return null;
- }
- $afterData = Config::getConfigByType('sms');
- $this->adminOperateLogger->saveAfterContent=$afterData;
- unset($beforeData,$afterData);
- $this->adminOperateLogger->clean()->save([
- 'optType' => '更新短信配置',
- ]);
- return true;
- }
- /**
- * 更新转账配置
- * @return bool|null
- * @throws Exception
- */
- public function updateTransfer(){
- if(!$this->validate()){
- return null;
- }
- $configs = Config::find()->where("TYPE='transfer'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
- $configNames = [];
- foreach ($configs as $value){
- $configNames[] = $value['CONFIG_NAME'];
- }
- $beforeData = Config::getConfigByType('transfer');
- $this->adminOperateLogger->saveBeforeContent=$beforeData;
- $postData = \Yii::$app->request->post();
- $transaction = \Yii::$app->db->beginTransaction();
- try{
- foreach ($configs as $key => $value) {
- if (array_key_exists($key, $postData)) {
- if (!$postData[$key]) $postData[$key] = 0;
- if(is_array($postData[$key])) $postData[$key] = Json::encode($postData[$key]);
- Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
- }
- }
- $transaction->commit();
- } catch (Exception $e){
- $transaction->rollBack();
- $this->addError('updateTransfer', $e->getMessage());
- return null;
- }
- $afterData = Config::getConfigByType('transfer');
- $this->adminOperateLogger->saveAfterContent=$afterData;
- unset($beforeData,$afterData);
- $this->adminOperateLogger->clean()->save([
- 'optType' => '更新转账配置',
- ]);
- return true;
- }
- /**
- * 更新分数配置
- * @return bool|null
- * @throws Exception
- */
- public function updateScore(){
- if(!$this->validate()){
- return null;
- }
- $configs = Config::find()->where("TYPE='score'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
- $configNames = [];
- foreach ($configs as $value){
- $configNames[] = $value['CONFIG_NAME'];
- }
- $beforeData = Config::getConfigByType('score');
- $this->adminOperateLogger->saveBeforeContent=$beforeData;
- $postData = \Yii::$app->request->post();
- $transaction = \Yii::$app->db->beginTransaction();
- try{
- foreach ($configs as $key => $value) {
- if (array_key_exists($key, $postData)) {
- if (!$postData[$key]) $postData[$key] = 0;
- if(is_array($postData[$key])) $postData[$key] = Json::encode($postData[$key]);
- Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
- }
- }
- $transaction->commit();
- } catch (Exception $e){
- $transaction->rollBack();
- $this->addError('updateScore', $e->getMessage());
- return null;
- }
- $afterData = Config::getConfigByType('score');
- $this->adminOperateLogger->saveAfterContent=$afterData;
- unset($beforeData,$afterData);
- $this->adminOperateLogger->clean()->save([
- 'optType' => '更新分数配置',
- ]);
- return true;
- }
- /**
- * 更新其他配置
- * @return bool|null
- * @throws Exception
- */
- public function updateOther(){
- if(!$this->validate()){
- return null;
- }
- $configs = Config::find()->where("TYPE='other'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
- $configNames = [];
- foreach ($configs as $value){
- $configNames[] = $value['CONFIG_NAME'];
- }
- $beforeData = Config::getConfigByType('other');
- $this->adminOperateLogger->saveBeforeContent=$beforeData;
- $postData = \Yii::$app->request->post();
- $transaction = \Yii::$app->db->beginTransaction();
- try{
- foreach ($configs as $key => $value) {
- if (array_key_exists($key, $postData)) {
- if (!$postData[$key]) $postData[$key] = 0;
- if(is_array($postData[$key])) $postData[$key] = implode(",", $postData[$key]);
- Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
- }
- }
- $transaction->commit();
- } catch (Exception $e){
- $transaction->rollBack();
- $this->addError('updateOther', $e->getMessage());
- return null;
- }
- $afterData = Config::getConfigByType('other');
- $this->adminOperateLogger->saveAfterContent=$afterData;
- unset($beforeData,$afterData);
- $this->adminOperateLogger->clean()->save([
- 'optType' => '更新其他配置',
- ]);
- return true;
- }
- /**
- * 更新汇率配置
- * @return bool|null
- */
- public function updateExchangeRate()
- {
- if(!$this->validate()){
- return null;
- }
- $beforeData = Config::getConfigByType('exchangeRate');
- $this->adminOperateLogger->saveBeforeContent = $beforeData;
- $postData = \Yii::$app->request->post();
- $transaction = \Yii::$app->db->beginTransaction();
- try{
- Config::updateAll(['VALUE' => $postData['VALUE'], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => 'exchangeRate']);
- // 异步处理添加任务
- $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle('config/update-exchange-rate', \Yii::$app->request->post());
- if($taskKey === false){
- throw new Exception('请求异步服务器失败');
- }
- $transaction->commit();
- } catch (Exception $e){
- $transaction->rollBack();
- $this->addError('updateExchangeRate', $e->getMessage());
- return null;
- }
- $afterData = Config::getConfigByType('exchangeRate');
- $this->adminOperateLogger->saveAfterContent = $afterData;
- unset($beforeData,$afterData);
- $this->adminOperateLogger->clean()->save([
- 'optType' => '更新汇率配置',
- ]);
- return true;
- }
- }
|