Optimize.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace common\models\mall;
  3. use common\enums\StatusEnum;
  4. use common\helpers\CurlHelper;
  5. use common\models\backend\Menu;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%optimize}}".
  10. *
  11. *
  12. * @property int $mall_id 商城id
  13. * @property string $category 工单类型
  14. * @property int|null $mobile 联系方式
  15. * @property string|null $backend_path 后台地址路径json字符串
  16. * @property string|null $backend_path_str 后台地址中文
  17. * @property string|null $title 标题
  18. * @property int $examine_status 审核状态[1=客户已回复,2=中台已回复,3=工单结束]
  19. * @property int $status 状态【1启用 0禁用 -1删除】
  20. * @property int $send_status 发送状态
  21. * @property int $created_at
  22. * @property int $updated_at
  23. */
  24. class Optimize extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%optimize}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id'], 'required'],
  40. [['mall_id', 'mobile', 'examine_status', 'status', 'send_status', 'created_at', 'updated_at'], 'integer'],
  41. [['category', 'backend_path', 'backend_path_str', 'title'], 'string'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城id',
  52. 'category' => '类型[optimize=功能优化,bug=BUG修复,others=其他建议]',
  53. 'backend_path' => '后台地址',
  54. 'backend_path_str' => '后台地址路径',
  55. 'mobile' => '联系方式',
  56. 'title' => '标题',
  57. 'examine_status' => '工单状态[1=客户已回复,2=中台已回复,3=工单结束]',
  58. 'status' => '状态【1启用 0禁用 -1删除】',
  59. 'send_status' => '发送状态',
  60. 'created_at' => 'Created At',
  61. 'updated_at' => 'Updated At',
  62. ];
  63. }
  64. /**
  65. * 获取关联的详情第一条
  66. * @return \yii\db\ActiveQuery
  67. */
  68. public function getDetail()
  69. {
  70. return $this->hasOne(OptimizeDetail::class, ['optimize_id' => 'id'])->select('optimize_id,member_name');
  71. }
  72. /**
  73. * 新增数据
  74. * @Author lun
  75. * @DateTime 2021-09-16 14:16:24
  76. * @copyright: Copyright (c) 2020 广东七件事集团
  77. * @param array $params
  78. * @param array $mall
  79. * @return object|boolean
  80. */
  81. public static function setData(array $params, $mall)
  82. {
  83. $trans = Yii::$app->db->beginTransaction();
  84. try {
  85. $model = new self();
  86. $model->loadDefaultValues();
  87. if (!empty($params['backend_path'])) {
  88. $backend_path = json_decode($params['backend_path'], true);
  89. $backend_path_str = '';
  90. foreach ($backend_path as $menuids) {
  91. $menus = Menu::find()->select('title')->where(['id' => $menuids])->orderBy(["FIELD(`id`,".implode(',',$menuids).")" => true])->asArray()->column();
  92. $backend_path_str .= '['.implode('/', $menus).']';
  93. }
  94. $params['backend_path_str'] = $backend_path_str;
  95. }
  96. if (!$model->load($params, '') || !$model->save()) {
  97. throw new \Exception($model->getErrorMessage().'==');
  98. }
  99. // 沟通记录插入
  100. $detail = OptimizeDetail::setData(array(
  101. 'mall_id' => $params['mall_id'],
  102. 'optimize_id' => $model->id,
  103. 'ip' => $params['ip'],
  104. 'member_id' => $params['member_id'],
  105. 'member_name' => $params['member_name'],
  106. 'optimize' => $params['optimize'],
  107. ));
  108. if ($detail === false) throw new \Exception(OptimizeDetail::getStaticError().'---');
  109. $trans->commit();
  110. // 新增数据成功,推送数据到中台
  111. if (!(self::pushOptimize($model, $mall) && self::pushDetail($detail))) {
  112. // 推送失败
  113. $model->setSendFail();
  114. }
  115. return $model;
  116. } catch (\Exception $e) {
  117. self::$static_error = $e->getFile().','.$e->getLine().'行,'.$e->getMessage();
  118. Yii::error($e->getMessage());
  119. $trans->rollBack();
  120. return false;
  121. }
  122. }
  123. /**
  124. * 获取服务器ip
  125. * @return mixed|string
  126. */
  127. public static function getServiceIp()
  128. {
  129. exec('dig +short myip.opendns.com @resolver1.opendns.com', $output);
  130. $ip = $output[0] ?? '127.0.0.1';
  131. return $ip;
  132. }
  133. /**
  134. * 数据传输到中台
  135. * @param Optimize $optimize
  136. * @param array $mall
  137. * @return bool
  138. */
  139. public static function pushOptimize(Optimize $optimize, $mall)
  140. {
  141. try {
  142. // 获取基础配置
  143. $config = Yii::$app->services->setting->get('system');
  144. $params = [
  145. 'mall_id' => $optimize->mall_id,
  146. 'ip' => self::getServiceIp(),
  147. 'domain' => $config['backend_url'],
  148. 'mall_name' => $mall['name'],
  149. 'optimize_id' => $optimize->id,
  150. 'mobile' => $optimize->mobile,
  151. 'category' => $optimize->category,
  152. 'backend_path_str' => $optimize->backend_path_str,
  153. 'title' => $optimize->title,
  154. 'notify_url' => $config['backend_url'] . '/index.php?r=notify',
  155. ];
  156. $url = 'middle-api.qijianshigw.com/qimall-statistics/optimize-list/add';
  157. // $url = '127.0.0.1:9502/qimall-statistics/optimize-list/add';
  158. $response = CurlHelper::postJson($url, $params);
  159. if ($response == false) throw new \Exception('工单推送失败,ID='.$optimize->id.',原因:'.$response['message']);
  160. return true;
  161. } catch (\Exception $e) {
  162. self::$static_error = $e->getMessage();
  163. Yii::error($e->getMessage());
  164. return false;
  165. }
  166. }
  167. /**
  168. * 沟通记录推送
  169. * @param OptimizeDetail $optimize
  170. * @return bool
  171. */
  172. public static function pushDetail(OptimizeDetail $optimize_detail)
  173. {
  174. try {
  175. // 获取基础配置
  176. $config = Yii::$app->services->setting->get('system');
  177. $params = [
  178. 'mall_id' => $optimize_detail->mall_id,
  179. 'ip' => self::getServiceIp(),
  180. 'domain' => $config['backend_url'],
  181. 'optimize_id' => $optimize_detail->optimize_id,
  182. 'optimize' => $optimize_detail->optimize,
  183. ];
  184. $url = 'middle-api.qijianshigw.com/qimall-statistics/optimize-list/detail';
  185. // $url = '127.0.0.1:9502/qimall-statistics/optimize-list/detail';
  186. $data = CurlHelper::postJson($url, $params);
  187. if ($data['success'] != true) throw new \Exception('工单沟通推送失败,ID='.$optimize_detail->id.',原因:'.$data['message']);
  188. return true;
  189. } catch (\Exception $e) {
  190. self::$static_error = $e->getMessage();
  191. Yii::error($e->getMessage());
  192. return false;
  193. }
  194. }
  195. /**
  196. * 结束对话
  197. *
  198. * @param array $params [mall_id => '', optimize_id => '']
  199. * @return boolean
  200. */
  201. public static function finish(array $params)
  202. {
  203. $optimize = static::find()->where(['id' => $params['optimize_id'], 'mall_id' => $params['mall_id']])->one();
  204. if (empty($optimize)) return false;
  205. $optimize->examine_status = 3;
  206. return $optimize->save();
  207. }
  208. /**
  209. * 设置发送失败
  210. *
  211. * @return boolean
  212. */
  213. public function setSendFail()
  214. {
  215. $this->send_status = 0;
  216. return $this->save();
  217. }
  218. /**
  219. * 设置发送成功
  220. *
  221. * @return boolean
  222. */
  223. public function setSendSuccess()
  224. {
  225. $this->send_status = 1;
  226. return $this->save();
  227. }
  228. /**
  229. * 重新推送
  230. *
  231. * @param integer $id
  232. * @param array $mall
  233. * @return boolean
  234. */
  235. public static function reSend(int $id, $mall)
  236. {
  237. $info = static::find()->where(['id' => $id, 'mall_id' => $mall['id'], 'status' => 1])->one();
  238. if (empty($info)) {
  239. self::$static_error = '推送不存在';
  240. return false;
  241. }
  242. $detail = OptimizeDetail::find()->where(['optimize_id' => $id])->one();
  243. if (empty($detail)) {
  244. self::$static_error = '推送不存在';
  245. return false;
  246. }
  247. if (self::pushOptimize($info, $mall) && self::pushDetail($detail)) {
  248. $info->setSendSuccess();
  249. return true;
  250. }
  251. return false;
  252. }
  253. }