OptimizeDetail.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace common\models\mall;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%optimize}}".
  8. *
  9. *
  10. * @property int $id id
  11. * @property int $mall_id 商城id
  12. * @property int $optimize_id qimall_optimize.id
  13. * @property string $ip IP
  14. * @property int $member_id 操作人id
  15. * @property string|null $member_name 操作人
  16. * @property string|null $optimize 优化建议
  17. * @property int $status 状态【1启用 0禁用 -1删除】
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class OptimizeDetail extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%optimize_detail}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'member_id', 'member_name'], 'required'],
  37. [['mall_id', 'optimize_id', 'member_id', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['ip', 'member_name', 'optimize'], 'string'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => '商城id',
  49. 'optimize_id' => 'qimall_optimize.id',
  50. 'ip' => 'ip',
  51. 'member_id' => '操作人id',
  52. 'member_name' => '操作人',
  53. 'optimize' => '优化建议',
  54. 'status' => '状态【1启用 0禁用 -1删除】',
  55. 'created_at' => 'Created At',
  56. 'updated_at' => 'Updated At',
  57. ];
  58. }
  59. /**
  60. * 新增数据
  61. * @Author lun
  62. * @DateTime 2021-09-16 14:16:24
  63. * @copyright: Copyright (c) 2020 广东七件事集团
  64. * @param array $params
  65. * @return OptimizeDetail|boolean
  66. */
  67. public static function setData(array $params)
  68. {
  69. try {
  70. $model = new self();
  71. $model->loadDefaultValues();
  72. if (!$model->load($params, '') || !$model->save()) {
  73. throw new \Exception($model->getErrorMessage());
  74. }
  75. return $model;
  76. } catch (\Exception $e) {
  77. self::$static_error = $e->getMessage();
  78. return false;
  79. }
  80. }
  81. /**
  82. * 沟通回复
  83. * @param array $params
  84. * @return bool
  85. */
  86. public static function resubmit(array $params)
  87. {
  88. try {
  89. $model = Optimize::findOne(['id' => $params['optimize_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  90. if (!$model) {
  91. throw new \Exception('查无此优化项');
  92. }
  93. $detail = self::setData($params);
  94. if ($detail === false) throw new \Exception(self::getStaticError());
  95. $model->examine_status = 1;
  96. $model->save();
  97. // 推送到中台
  98. Optimize::pushDetail($detail);
  99. return true;
  100. } catch (\Exception $e) {
  101. self::$static_error = $e->getMessage();
  102. return false;
  103. }
  104. }
  105. /**
  106. * 接收回调
  107. * @param $params
  108. * @return bool
  109. */
  110. public static function receiveNotify($params)
  111. {
  112. try {
  113. $model = Optimize::findOne(['id' => $params['optimize_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  114. if (!$model) {
  115. throw new \Exception('查无此优化项');
  116. }
  117. // 生成沟通记录
  118. $res = self::setData($params);
  119. if ($res == false) throw new \Exception(self::getStaticError());
  120. $model->examine_status = 2;
  121. $model->save();
  122. return true;
  123. } catch (\Exception $e) {
  124. self::$static_error = $e->getMessage();
  125. return false;
  126. }
  127. }
  128. /**
  129. * 添加回复
  130. *
  131. * @param array $data
  132. * @return boolean
  133. */
  134. public static function addReply(array $data)
  135. {
  136. try {
  137. $model = Optimize::findOne(['id' => $data['optimize_id'], 'mall_id' => $data['mall_id']]);
  138. if (!$model) {
  139. throw new \Exception('查无此优化项');
  140. }
  141. // 生成沟通记录
  142. $data = array_merge($data, [
  143. 'member_id' => 0,
  144. 'member_name' => '中台回复',
  145. ]);
  146. $res = self::setData($data);
  147. if ($res == false) throw new \Exception(self::getStaticError());
  148. $model->examine_status = 2;
  149. $model->save();
  150. return true;
  151. } catch (\Exception $e) {
  152. self::$static_error = $e->getMessage();
  153. return false;
  154. }
  155. }
  156. }