ScratchSettingLogic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace addons\Scratch\common\logic;
  3. use addons\Coupon\common\models\AddonsCoupon;
  4. use addons\Scratch\common\enums\ScratchEnum;
  5. use addons\Scratch\common\models\ScratchCouponModel;
  6. use addons\Scratch\common\models\ScratchLogModel;
  7. use addons\Scratch\common\models\ScratchModel;
  8. use addons\Scratch\common\models\ScratchSettingModel;
  9. use common\models\base\User;
  10. use common\models\goods\Goods;
  11. use common\models\goods\GoodsAttr;
  12. use common\models\mall\MallAddons;
  13. use phpDocumentor\Reflection\Types\True_;
  14. /**
  15. * Class ScratchSettingLogic
  16. * @package addons\Scratch\common\logic
  17. */
  18. class ScratchSettingLogic
  19. {
  20. /**
  21. * @param $id
  22. * @return array|false|mixed|void
  23. * @throws \ErrorException
  24. */
  25. public static function getData($mallId)
  26. {
  27. try {
  28. $model = ScratchSettingModel::find()
  29. ->where([
  30. 'mall_id' => $mallId,
  31. ])
  32. ->asArray()
  33. ->one();
  34. if (empty($model)) {
  35. return [];
  36. }
  37. $model['payment_type'] = json_decode($model['payment_type'], true);
  38. $model['send_type'] = json_decode($model['send_type'], true);
  39. !empty($model['background_imgs']) ?
  40. $model['background_imgs'] = json_decode($model['background_imgs'], true) :
  41. '';
  42. $model['start_at'] = date('Y-m-d H:i:s', $model['start_at']);
  43. $model['end_at'] = date('Y-m-d H:i:s', $model['end_at']);
  44. return $model;
  45. } catch (\ErrorException $errorException) {
  46. throw new \ErrorException($errorException->getMessage());
  47. } catch (\Exception $exception) {
  48. throw new \Exception($exception->getMessage());
  49. }
  50. }
  51. /**
  52. * @param $mallId
  53. * @param $params
  54. * @return array
  55. * @throws \ErrorException
  56. */
  57. public static function saveData($mallId, $params)
  58. {
  59. try {
  60. $model = ScratchSettingModel::findOne([
  61. 'mall_id' => $mallId,
  62. ]);
  63. if (!$model) {
  64. $model = new ScratchSettingModel();
  65. $model->mall_id = $mallId;
  66. }
  67. $params['payment_type'] = json_encode($params['payment_type'], JSON_UNESCAPED_UNICODE);
  68. $params['send_type'] = json_encode($params['send_type'], JSON_UNESCAPED_UNICODE);
  69. $backgroundImgs = ScratchSettingModel::getBackgroundImgs();
  70. if(!PublicLogic::isHttp($params['background_imgs']['image'])){
  71. $params['background_imgs']['image'] = $backgroundImgs['image'];
  72. }
  73. $model->title = $params['title'] ?? '';
  74. $model->type = $params['type'];
  75. $model->probability = $params['probability'];
  76. $model->oppty = $params['oppty'];
  77. $model->deplete_integral_num = $params['deplete_integral_num'] ?? 0;
  78. $model->rule = $params['rule'];
  79. $model->payment_type = $params['payment_type'];
  80. $model->send_type = $params['send_type'];
  81. $model->is_sms = $params['is_sms'];
  82. $model->is_wechat = $params['is_wechat'];
  83. $model->is_mail = $params['is_mail'];
  84. $model->is_print = $params['is_print'];
  85. $model->start_at = strtotime($params['start_at']);
  86. $model->end_at = strtotime($params['end_at']);
  87. $model->background_imgs = json_encode($params['background_imgs'], JSON_UNESCAPED_UNICODE);
  88. if (!$model->save()) {
  89. throw new \ErrorException($model->getErrorMessage());
  90. }
  91. return true;
  92. } catch (\ErrorException $errorException) {
  93. throw new \ErrorException($errorException->getMessage());
  94. } catch (\Exception $exception) {
  95. throw new \Exception($exception->getMessage());
  96. }
  97. }
  98. /**
  99. * @param $param
  100. * @return array
  101. * @throws \Exception
  102. */
  103. public static function saveBackground($mallId, $param){
  104. try {
  105. $model = ScratchSettingModel::findOne(['mall_id' => $mallId]);
  106. if(!$model){
  107. $model = new ScratchSettingModel();
  108. $model->mall_id = $mallId;
  109. }
  110. $backgroundImgs = ScratchSettingModel::getBackgroundImgs();
  111. if(!PublicLogic::isHttp($param['image'])){
  112. $param['image'] = $backgroundImgs['image'];
  113. }
  114. $model->background_imgs = json_encode($param,JSON_UNESCAPED_UNICODE);
  115. if (!$model->save()) {
  116. throw new \Exception($model->getErrorMessage());
  117. }
  118. return true;
  119. } catch (\ErrorException $errorException) {
  120. throw new \ErrorException($errorException->getMessage());
  121. } catch (\Exception $exception) {
  122. throw new \Exception($exception->getMessage());
  123. }
  124. }
  125. }