NoticeModel.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace addons\QiJuhe\common\models;
  3. use Yii;
  4. use yii\db\ActiveRecord;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "qimall_addon_juhe_shop_notice".
  8. *
  9. * @property int $id
  10. * @property int|null $mall_id
  11. * @property string|null $notice_id 通知id
  12. * @property string|null $type 类型
  13. * @property string|null $push_time 推送时间
  14. * @property string|null $data 数据
  15. * @property int|null $created_at
  16. * @property int|null $updated_at
  17. * @property string|null $response 响应
  18. * @property int|null $response_time 响应时间
  19. */
  20. class NoticeModel extends ActiveRecord
  21. {
  22. const GOODS_UNDERCARRIAGE = 'GOODS_UNDERCARRIAGE';
  23. const GOODS_ON_SALE = 'GOODS_ON_SALE';
  24. const GOODS_ALTER= 'GOODS_ALTER';
  25. const GOODS_PRICE_ALTER = 'GOODS_PRICE_ALTER';
  26. const GOODS_STORAGE_ADD = 'GOODS_STORAGE_ADD';
  27. const GOODS_STORAGE_DELETE = 'GOODS_STORAGE_DELETE';
  28. const GOODS_STORAGE_REMOVE = 'GOODS_STORAGE_REMOVE';
  29. const ORDER_SUCCESS = 'ORDER_SUCCESS';
  30. const ORDER_DELIVERED = 'ORDER_DELIVERED';
  31. const ORDER_DELIVERY = 'ORDER_DELIVERY';
  32. const ORDER_CANCEL = 'ORDER_CANCEL';
  33. const AFTERSALE_AGREE = 'AFTERSALE_AGREE';
  34. const AFTERSALE_REFUSE = 'AFTERSALE_REFUSE';
  35. const AFTERSALE_SUCCESS = 'AFTERSALE_SUCCESS';
  36. const HANDLER_ORDER = 'order';
  37. const HANDLER_GOODS = 'goods';
  38. const HANDLER_AFTERSALE = 'aftersale';
  39. public static $type_text = [
  40. 'goods.undercarriage' => '商品下架',
  41. 'goods.on.sale' => '商品上架',
  42. 'goods.alter' => '商品修改',
  43. 'goods.price.alter' => '商品价格变更',
  44. 'goods.storage.add' => '商品库添加',
  45. 'goods.storage.delete' => '商品库删除',
  46. 'goods.storage.remove' => '移除商品库商品',
  47. 'order.success' => '订单交易成功',
  48. 'order.delivered' => '商品已收货',
  49. 'order.delivery' => '商家已发货',
  50. 'order.cancel' => '订单取消',
  51. 'aftersale.agree' => '售后同意',
  52. 'aftersale.refuse' => '拒绝',
  53. 'aftersale.success' => '退款到账',
  54. ];
  55. public static $event_name = [
  56. self::GOODS_UNDERCARRIAGE => '商品下架',
  57. self::GOODS_ON_SALE => '商品上架',
  58. self::GOODS_ALTER=> '商品修改',
  59. self::GOODS_PRICE_ALTER => '商品价格变更',
  60. self::GOODS_STORAGE_ADD => '商品库添加',
  61. self::GOODS_STORAGE_DELETE => '商品库删除',
  62. self::GOODS_STORAGE_REMOVE => '移除商品库商品',
  63. self::ORDER_SUCCESS => '订单交易成功',
  64. self::ORDER_DELIVERED => '商品已收货',
  65. self::ORDER_DELIVERY => '商家已发货',
  66. self::ORDER_CANCEL => '订单取消',
  67. self::AFTERSALE_AGREE => '售后',
  68. self::AFTERSALE_REFUSE => '拒绝',
  69. self::AFTERSALE_SUCCESS => '退款到账',
  70. ];
  71. public static $handler_type = [
  72. self::HANDLER_GOODS => [
  73. self::GOODS_UNDERCARRIAGE,
  74. self::GOODS_ON_SALE,
  75. self::GOODS_ALTER,
  76. self::GOODS_PRICE_ALTER ,
  77. self::GOODS_STORAGE_ADD ,
  78. self::GOODS_STORAGE_DELETE ,
  79. self::GOODS_STORAGE_REMOVE ,
  80. ],
  81. self::HANDLER_ORDER => [
  82. self::ORDER_SUCCESS,
  83. self::ORDER_DELIVERED,
  84. self::ORDER_DELIVERY,
  85. self::ORDER_CANCEL,
  86. ],
  87. self::HANDLER_AFTERSALE => [
  88. self::AFTERSALE_AGREE,
  89. self::AFTERSALE_REFUSE,
  90. self::AFTERSALE_SUCCESS,
  91. ]
  92. ];
  93. /**
  94. * @var array
  95. */
  96. public static $handler_class = [
  97. self::HANDLER_GOODS => [
  98. 'key' => 'juhe_goods_id',
  99. 'field' => 'goodsIds',
  100. 'class' => GoodsSkuModel::class,
  101. ],
  102. self::HANDLER_ORDER => [
  103. 'key' => 'order_no',
  104. 'field' => 'orderSn',
  105. 'class' => MallOrderModel::class,
  106. ],
  107. self::HANDLER_AFTERSALE => [
  108. 'key' => 'order_no',
  109. 'field' => 'orderSn',
  110. 'class' => MallOrderModel::class,
  111. ],
  112. ];
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public static function tableName()
  117. {
  118. return '{{%addons_qijuhe_notice}}';
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function rules()
  124. {
  125. return [
  126. [['mall_id', 'created_at', 'updated_at', 'response_time', 'supply_id'], 'integer'],
  127. [['data', 'response'], 'string'],
  128. [['type', 'push_time'], 'string', 'max' => 64],
  129. [['notice_id'], 'string', 'max' => 225],
  130. ];
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function attributeLabels()
  136. {
  137. return [
  138. 'id' => 'ID',
  139. 'mall_id' => 'Mall ID',
  140. 'supply_id' => 'Supply id',
  141. 'notice_id' => 'Notice ID',
  142. 'type' => 'Type',
  143. 'push_time' => 'Push Time',
  144. 'data' => 'Data',
  145. 'created_at' => 'Created At',
  146. 'updated_at' => 'Updated At',
  147. 'response' => 'Response',
  148. 'response_time' => 'Response Time',
  149. ];
  150. }
  151. /**
  152. * 获取Handler
  153. *
  154. * @param string $type
  155. * @return string
  156. */
  157. public function getHandler($type)
  158. {
  159. foreach (self::$handler_type as $handler => $handler_type) {
  160. if (in_array($type, $handler_type)) {
  161. return $handler;
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * 获取推送相关的mall_ids
  168. *
  169. * @param string $handler
  170. * @param array $data
  171. * @return array
  172. */
  173. public function getMallIds($handler, $data)
  174. {
  175. $handler = $this->getHandler($handler);
  176. $class = self::$handler_class[$handler];
  177. switch ($handler) {
  178. // 订单
  179. case self::HANDLER_GOODS:
  180. $ids = GoodsSkuModel::find()
  181. ->alias('sku')
  182. ->leftJoin(['goods' => GoodsModel::tableName()], 'goods.id = sku.juhe_goods_id')
  183. ->andWhere(['goods.juhe_goods_id' => $data[$class['field']]])
  184. ->select('sku.mall_id')
  185. ->groupBy('sku.mall_id')
  186. ->all();
  187. break;
  188. default:
  189. $ids = $class['class']::find()
  190. ->select('mall_id')
  191. ->where([$class['key'] => $data[$class['field']]])
  192. ->groupBy('mall_id')
  193. ->select('mall_id')
  194. ->all();
  195. break;
  196. }
  197. $mall_ids = [];
  198. foreach ($ids as $v) {
  199. $mall_ids[] = $v['mall_id'];
  200. }
  201. return $mall_ids;
  202. }
  203. /**
  204. * 添加日志
  205. *
  206. * @param array $data
  207. * @return integer
  208. */
  209. public static function addLog($type,$data)
  210. {
  211. $model = new self();
  212. // $data['response'] = Json::encode($data['response']);
  213. $log = $data;
  214. unset($log['message_id'],$log['notice_id']);
  215. $data['data'] = Json::encode($log);
  216. $data['type'] = $type;
  217. $data['created_at'] = $data['updated_at'] = time();
  218. $model->load($data, '');
  219. $model->save(false);
  220. return $model->id;
  221. }
  222. }