| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace common\models\mall;
- use common\enums\StatusEnum;
- use common\helpers\CurlHelper;
- use common\models\backend\Menu;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%optimize}}".
- *
- *
- * @property int $mall_id 商城id
- * @property string $category 工单类型
- * @property int|null $mobile 联系方式
- * @property string|null $backend_path 后台地址路径json字符串
- * @property string|null $backend_path_str 后台地址中文
- * @property string|null $title 标题
- * @property int $examine_status 审核状态[1=客户已回复,2=中台已回复,3=工单结束]
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int $send_status 发送状态
- * @property int $created_at
- * @property int $updated_at
- */
- class Optimize extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%optimize}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'mobile', 'examine_status', 'status', 'send_status', 'created_at', 'updated_at'], 'integer'],
- [['category', 'backend_path', 'backend_path_str', 'title'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'category' => '类型[optimize=功能优化,bug=BUG修复,others=其他建议]',
- 'backend_path' => '后台地址',
- 'backend_path_str' => '后台地址路径',
- 'mobile' => '联系方式',
- 'title' => '标题',
- 'examine_status' => '工单状态[1=客户已回复,2=中台已回复,3=工单结束]',
- 'status' => '状态【1启用 0禁用 -1删除】',
- 'send_status' => '发送状态',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 获取关联的详情第一条
- * @return \yii\db\ActiveQuery
- */
- public function getDetail()
- {
- return $this->hasOne(OptimizeDetail::class, ['optimize_id' => 'id'])->select('optimize_id,member_name');
- }
- /**
- * 新增数据
- * @Author lun
- * @DateTime 2021-09-16 14:16:24
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param array $params
- * @param array $mall
- * @return object|boolean
- */
- public static function setData(array $params, $mall)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $model = new self();
- $model->loadDefaultValues();
- if (!empty($params['backend_path'])) {
- $backend_path = json_decode($params['backend_path'], true);
- $backend_path_str = '';
- foreach ($backend_path as $menuids) {
- $menus = Menu::find()->select('title')->where(['id' => $menuids])->orderBy(["FIELD(`id`,".implode(',',$menuids).")" => true])->asArray()->column();
- $backend_path_str .= '['.implode('/', $menus).']';
- }
- $params['backend_path_str'] = $backend_path_str;
- }
- if (!$model->load($params, '') || !$model->save()) {
- throw new \Exception($model->getErrorMessage().'==');
- }
- // 沟通记录插入
- $detail = OptimizeDetail::setData(array(
- 'mall_id' => $params['mall_id'],
- 'optimize_id' => $model->id,
- 'ip' => $params['ip'],
- 'member_id' => $params['member_id'],
- 'member_name' => $params['member_name'],
- 'optimize' => $params['optimize'],
- ));
- if ($detail === false) throw new \Exception(OptimizeDetail::getStaticError().'---');
- $trans->commit();
- // 新增数据成功,推送数据到中台
- if (!(self::pushOptimize($model, $mall) && self::pushDetail($detail))) {
- // 推送失败
- $model->setSendFail();
- }
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getFile().','.$e->getLine().'行,'.$e->getMessage();
- Yii::error($e->getMessage());
- $trans->rollBack();
- return false;
- }
- }
- /**
- * 获取服务器ip
- * @return mixed|string
- */
- public static function getServiceIp()
- {
- exec('dig +short myip.opendns.com @resolver1.opendns.com', $output);
- $ip = $output[0] ?? '127.0.0.1';
- return $ip;
- }
- /**
- * 数据传输到中台
- * @param Optimize $optimize
- * @param array $mall
- * @return bool
- */
- public static function pushOptimize(Optimize $optimize, $mall)
- {
- try {
- // 获取基础配置
- $config = Yii::$app->services->setting->get('system');
- $params = [
- 'mall_id' => $optimize->mall_id,
- 'ip' => self::getServiceIp(),
- 'domain' => $config['backend_url'],
- 'mall_name' => $mall['name'],
- 'optimize_id' => $optimize->id,
- 'mobile' => $optimize->mobile,
- 'category' => $optimize->category,
- 'backend_path_str' => $optimize->backend_path_str,
- 'title' => $optimize->title,
- 'notify_url' => $config['backend_url'] . '/index.php?r=notify',
- ];
- $url = 'middle-api.qijianshigw.com/qimall-statistics/optimize-list/add';
- // $url = '127.0.0.1:9502/qimall-statistics/optimize-list/add';
- $response = CurlHelper::postJson($url, $params);
- if ($response == false) throw new \Exception('工单推送失败,ID='.$optimize->id.',原因:'.$response['message']);
- return true;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- Yii::error($e->getMessage());
- return false;
- }
- }
- /**
- * 沟通记录推送
- * @param OptimizeDetail $optimize
- * @return bool
- */
- public static function pushDetail(OptimizeDetail $optimize_detail)
- {
- try {
- // 获取基础配置
- $config = Yii::$app->services->setting->get('system');
- $params = [
- 'mall_id' => $optimize_detail->mall_id,
- 'ip' => self::getServiceIp(),
- 'domain' => $config['backend_url'],
- 'optimize_id' => $optimize_detail->optimize_id,
- 'optimize' => $optimize_detail->optimize,
- ];
- $url = 'middle-api.qijianshigw.com/qimall-statistics/optimize-list/detail';
- // $url = '127.0.0.1:9502/qimall-statistics/optimize-list/detail';
- $data = CurlHelper::postJson($url, $params);
- if ($data['success'] != true) throw new \Exception('工单沟通推送失败,ID='.$optimize_detail->id.',原因:'.$data['message']);
- return true;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- Yii::error($e->getMessage());
- return false;
- }
- }
- /**
- * 结束对话
- *
- * @param array $params [mall_id => '', optimize_id => '']
- * @return boolean
- */
- public static function finish(array $params)
- {
- $optimize = static::find()->where(['id' => $params['optimize_id'], 'mall_id' => $params['mall_id']])->one();
- if (empty($optimize)) return false;
- $optimize->examine_status = 3;
- return $optimize->save();
- }
- /**
- * 设置发送失败
- *
- * @return boolean
- */
- public function setSendFail()
- {
- $this->send_status = 0;
- return $this->save();
- }
- /**
- * 设置发送成功
- *
- * @return boolean
- */
- public function setSendSuccess()
- {
- $this->send_status = 1;
- return $this->save();
- }
- /**
- * 重新推送
- *
- * @param integer $id
- * @param array $mall
- * @return boolean
- */
- public static function reSend(int $id, $mall)
- {
- $info = static::find()->where(['id' => $id, 'mall_id' => $mall['id'], 'status' => 1])->one();
- if (empty($info)) {
- self::$static_error = '推送不存在';
- return false;
- }
- $detail = OptimizeDetail::find()->where(['optimize_id' => $id])->one();
- if (empty($detail)) {
- self::$static_error = '推送不存在';
- return false;
- }
- if (self::pushOptimize($info, $mall) && self::pushDetail($detail)) {
- $info->setSendSuccess();
- return true;
- }
- return false;
- }
- }
|