Message.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "{{%addons_star_chain_message}}".
  6. *
  7. * @property int $id
  8. * @property int|null $mall_id
  9. * @property int|null $msg_id 消息ID
  10. * @property string|null $tenant_id
  11. * @property int|null $enum_num 消息类型
  12. * @property string|null $content 消息内容
  13. * @property int|null $status 0 未处理 1 已处理
  14. * @property string|null $fail_msg 处理失败消息
  15. * @property int|null $create_time 消息时间
  16. * @property int|null $handled_at 处理时间
  17. */
  18. class Message extends BaseModel
  19. {
  20. /**
  21. * 处理成功
  22. */
  23. const SUCCESS = 1;
  24. /**
  25. * 处理失败
  26. */
  27. const FAIL = 2;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_star_chain_message}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'msg_id', 'enum_num', 'status', 'create_time', 'handled_at'], 'integer'],
  42. [['content'], 'string'],
  43. [['tenant_id'], 'string', 'max' => 64],
  44. [['fail_msg'], 'string', 'max' => 255],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => 'Mall ID',
  55. 'msg_id' => 'Msg ID',
  56. 'tenant_id' => 'Tenant ID',
  57. 'enum_num' => 'Enum Num',
  58. 'content' => 'Content',
  59. 'status' => 'Status',
  60. 'fail_msg' => 'Fail Msg',
  61. 'create_time' => 'Create Time',
  62. 'handled_at' => 'Handled At',
  63. ];
  64. }
  65. /**
  66. * 新增消息
  67. *
  68. * @param array $list
  69. * @param int $mall_id
  70. * @return int
  71. */
  72. public static function addMsg($mall_id, $list)
  73. {
  74. $list = array_map(function ($item) use ($mall_id) {
  75. return [
  76. 'msg_id' => $item['id'],
  77. 'mall_id' => $mall_id,
  78. 'tenant_id' => $item['tenantId'],
  79. 'enum_num' => $item['enumNum'],
  80. 'content' => $item['content'],
  81. 'create_time' => strtotime($item['createTime']),
  82. ];
  83. }, $list);
  84. return self::batchInsert($list, false);
  85. }
  86. /**
  87. * 设置消息为已处理
  88. *
  89. * @param array $msg_ids
  90. * @return int
  91. */
  92. public static function setHandled($msg_ids)
  93. {
  94. return self::updateAll(
  95. [
  96. 'handled_at' => time(),
  97. 'status' => static::SUCCESS,
  98. ],
  99. [
  100. 'msg_id' => $msg_ids,
  101. ]
  102. );
  103. }
  104. /**
  105. * 设置消息为处理失败
  106. *
  107. * @return int
  108. */
  109. public static function setHandledFail($msg_ids, string $fail_msg)
  110. {
  111. return static::updateAll(
  112. [
  113. 'handled_at' => time(),
  114. 'status' => static::FAIL,
  115. 'fail_msg' => $fail_msg,
  116. ],
  117. [
  118. 'msg_id' => $msg_ids,
  119. ]
  120. );
  121. }
  122. }