ConfigForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\libs\logging\operate\AdminOperate;
  6. use common\models\Config;
  7. use yii\db\Exception;
  8. use yii\helpers\Json;
  9. /**
  10. * Login form
  11. */
  12. class ConfigForm extends Model
  13. {
  14. const BONUS_TO_STRING_FIELD = [
  15. //共享奖
  16. 'shareRecIncomeFrom',
  17. 'shareIncomeFrom',
  18. //管理奖
  19. 'parentOddIncomeFrom_0',
  20. 'parentOddIncomeFrom_1',
  21. 'parentOddIncomeFrom_2',
  22. 'parentOddIncomeFrom_3',
  23. //共享奖-复消
  24. 'fxShareRecIncomeFrom',
  25. 'fxShareIncomeFrom',
  26. //管理奖
  27. 'fxParentOddIncomeFrom_0',
  28. 'fxParentOddIncomeFrom_1',
  29. 'fxParentOddIncomeFrom_2',
  30. 'fxParentOddIncomeFrom_3',
  31. //服务奖
  32. 'decRoleBonusFrom',
  33. ];
  34. const BONUS_JSON_FIELD = [
  35. 'jxStandard'
  36. ];
  37. public function init() {
  38. parent::init();
  39. $this->adminOperateLogger = new AdminOperate([
  40. 'fetchClass' => Config::class,
  41. ]);
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function rules()
  47. {
  48. return [
  49. //[['postData'], 'required'],
  50. ];
  51. }
  52. public function attributeLabels()
  53. {
  54. return [
  55. //'postData' => '传入数据',
  56. ];
  57. }
  58. /**
  59. * 更新配置
  60. * @return bool|null
  61. * @throws Exception
  62. */
  63. public function updateBase(){
  64. if(!$this->validate()){
  65. return null;
  66. }
  67. $configs = Config::find()->where("TYPE='base'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  68. $configNames = [];
  69. foreach ($configs as $value){
  70. $configNames[] = $value['CONFIG_NAME'];
  71. }
  72. $beforeData = Config::getConfigByType('base');
  73. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  74. $postData = \Yii::$app->request->post();
  75. $transaction = \Yii::$app->db->beginTransaction();
  76. try{
  77. foreach ($configs as $key => $value) {
  78. if (array_key_exists($key, $postData)) {
  79. if (!$postData[$key]) $postData[$key] = 0;
  80. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  81. }
  82. }
  83. $transaction->commit();
  84. } catch (Exception $e){
  85. $transaction->rollBack();
  86. $this->addError('updateBase', $e->getMessage());
  87. return null;
  88. }
  89. $afterData = Config::getConfigByType('base');
  90. $this->adminOperateLogger->saveAfterContent=$afterData;
  91. unset($beforeData,$afterData);
  92. $this->adminOperateLogger->clean()->save([
  93. 'optType' => '更新站点设置',
  94. ]);
  95. return true;
  96. }
  97. /**
  98. * 更新奖金配置
  99. * @return bool|null
  100. * @throws Exception
  101. */
  102. public function updateBonus(){
  103. if(!$this->validate()){
  104. return null;
  105. }
  106. $configs = Config::find()->where("TYPE='bonus'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  107. $configNames = [];
  108. foreach ($configs as $value){
  109. $configNames[] = $value['CONFIG_NAME'];
  110. }
  111. $beforeData = Config::getConfigByType('bonus');
  112. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  113. $postData = \Yii::$app->request->post();
  114. $transaction = \Yii::$app->db->beginTransaction();
  115. try{
  116. foreach ($configs as $key => $value) {
  117. if (array_key_exists($key, $postData)) {
  118. if (!$postData[$key]) $postData[$key] = 0;
  119. if( in_array($key, self::BONUS_TO_STRING_FIELD) ) $postData[$key] = implode(",", $postData[$key]);
  120. if( in_array($key, self::BONUS_JSON_FIELD) ) $postData[$key] = JSON::encode($postData[$key]);
  121. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  122. }
  123. }
  124. $transaction->commit();
  125. } catch (Exception $e){
  126. $transaction->rollBack();
  127. $this->addError('updateBase', $e->getMessage());
  128. return null;
  129. }
  130. $afterData = Config::getConfigByType('bonus');
  131. $this->adminOperateLogger->saveAfterContent=$afterData;
  132. unset($beforeData,$afterData);
  133. $this->adminOperateLogger->clean()->save([
  134. 'optType' => '更新奖金配置',
  135. ]);
  136. return true;
  137. }
  138. /**
  139. * 更新短信配置
  140. * @return bool|null
  141. * @throws Exception
  142. */
  143. public function updateSms(){
  144. if(!$this->validate()){
  145. return null;
  146. }
  147. $configs = Config::find()->where("TYPE='sms'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  148. $configNames = [];
  149. foreach ($configs as $value){
  150. $configNames[] = $value['CONFIG_NAME'];
  151. }
  152. $beforeData = Config::getConfigByType('sms');
  153. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  154. $postData = \Yii::$app->request->post();
  155. $transaction = \Yii::$app->db->beginTransaction();
  156. try{
  157. foreach ($configs as $key => $value) {
  158. if (array_key_exists($key, $postData)) {
  159. if (!$postData[$key]) $postData[$key] = 0;
  160. if(is_array($postData[$key])) $postData[$key] = implode(",", $postData[$key]);
  161. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  162. }
  163. }
  164. $transaction->commit();
  165. } catch (Exception $e){
  166. $transaction->rollBack();
  167. $this->addError('updateSms', $e->getMessage());
  168. return null;
  169. }
  170. $afterData = Config::getConfigByType('sms');
  171. $this->adminOperateLogger->saveAfterContent=$afterData;
  172. unset($beforeData,$afterData);
  173. $this->adminOperateLogger->clean()->save([
  174. 'optType' => '更新短信配置',
  175. ]);
  176. return true;
  177. }
  178. /**
  179. * 更新转账配置
  180. * @return bool|null
  181. * @throws Exception
  182. */
  183. public function updateTransfer(){
  184. if(!$this->validate()){
  185. return null;
  186. }
  187. $configs = Config::find()->where("TYPE='transfer'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  188. $configNames = [];
  189. foreach ($configs as $value){
  190. $configNames[] = $value['CONFIG_NAME'];
  191. }
  192. $beforeData = Config::getConfigByType('transfer');
  193. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  194. $postData = \Yii::$app->request->post();
  195. $transaction = \Yii::$app->db->beginTransaction();
  196. try{
  197. foreach ($configs as $key => $value) {
  198. if (array_key_exists($key, $postData)) {
  199. if (!$postData[$key]) $postData[$key] = 0;
  200. if(is_array($postData[$key])) $postData[$key] = Json::encode($postData[$key]);
  201. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  202. }
  203. }
  204. $transaction->commit();
  205. } catch (Exception $e){
  206. $transaction->rollBack();
  207. $this->addError('updateTransfer', $e->getMessage());
  208. return null;
  209. }
  210. $afterData = Config::getConfigByType('transfer');
  211. $this->adminOperateLogger->saveAfterContent=$afterData;
  212. unset($beforeData,$afterData);
  213. $this->adminOperateLogger->clean()->save([
  214. 'optType' => '更新转账配置',
  215. ]);
  216. return true;
  217. }
  218. /**
  219. * 更新分数配置
  220. * @return bool|null
  221. * @throws Exception
  222. */
  223. public function updateScore(){
  224. if(!$this->validate()){
  225. return null;
  226. }
  227. $configs = Config::find()->where("TYPE='score'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  228. $configNames = [];
  229. foreach ($configs as $value){
  230. $configNames[] = $value['CONFIG_NAME'];
  231. }
  232. $beforeData = Config::getConfigByType('score');
  233. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  234. $postData = \Yii::$app->request->post();
  235. $transaction = \Yii::$app->db->beginTransaction();
  236. try{
  237. foreach ($configs as $key => $value) {
  238. if (array_key_exists($key, $postData)) {
  239. if (!$postData[$key]) $postData[$key] = 0;
  240. if(is_array($postData[$key])) $postData[$key] = Json::encode($postData[$key]);
  241. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  242. }
  243. }
  244. $transaction->commit();
  245. } catch (Exception $e){
  246. $transaction->rollBack();
  247. $this->addError('updateScore', $e->getMessage());
  248. return null;
  249. }
  250. $afterData = Config::getConfigByType('score');
  251. $this->adminOperateLogger->saveAfterContent=$afterData;
  252. unset($beforeData,$afterData);
  253. $this->adminOperateLogger->clean()->save([
  254. 'optType' => '更新分数配置',
  255. ]);
  256. return true;
  257. }
  258. /**
  259. * 更新其他配置
  260. * @return bool|null
  261. * @throws Exception
  262. */
  263. public function updateOther(){
  264. if(!$this->validate()){
  265. return null;
  266. }
  267. $configs = Config::find()->where("TYPE='other'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  268. $configNames = [];
  269. foreach ($configs as $value){
  270. $configNames[] = $value['CONFIG_NAME'];
  271. }
  272. $beforeData = Config::getConfigByType('other');
  273. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  274. $postData = \Yii::$app->request->post();
  275. $transaction = \Yii::$app->db->beginTransaction();
  276. try{
  277. foreach ($configs as $key => $value) {
  278. if (array_key_exists($key, $postData)) {
  279. if (!$postData[$key]) $postData[$key] = 0;
  280. if(is_array($postData[$key])) $postData[$key] = implode(",", $postData[$key]);
  281. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  282. }
  283. }
  284. $transaction->commit();
  285. } catch (Exception $e){
  286. $transaction->rollBack();
  287. $this->addError('updateOther', $e->getMessage());
  288. return null;
  289. }
  290. $afterData = Config::getConfigByType('other');
  291. $this->adminOperateLogger->saveAfterContent=$afterData;
  292. unset($beforeData,$afterData);
  293. $this->adminOperateLogger->clean()->save([
  294. 'optType' => '更新其他配置',
  295. ]);
  296. return true;
  297. }
  298. }