LotteryWinRecord.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. /**
  5. * 中奖记录模型
  6. */
  7. class LotteryWinRecord extends Model
  8. {
  9. // 表名
  10. protected $name = 'shop_lottery_win_record';
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = false;
  17. // 追加属性
  18. protected $append = [
  19. 'deliver_status_text',
  20. 'prize_value_data',
  21. 'deliver_info_data'
  22. ];
  23. // 发放状态常量
  24. const DELIVER_STATUS_PENDING = 0; // 待发放
  25. const DELIVER_STATUS_SUCCESS = 1; // 已发放
  26. const DELIVER_STATUS_FAILED = 2; // 发放失败
  27. const DELIVER_STATUS_CANCELLED = 3; // 已取消
  28. /**
  29. * 关联抽奖记录
  30. */
  31. public function drawRecord()
  32. {
  33. return $this->belongsTo('LotteryDrawRecord', 'draw_record_id');
  34. }
  35. /**
  36. * 关联活动
  37. */
  38. public function activity()
  39. {
  40. return $this->belongsTo('LotteryActivity', 'activity_id');
  41. }
  42. /**
  43. * 关联用户
  44. */
  45. public function user()
  46. {
  47. return $this->belongsTo('app\common\model\User', 'user_id');
  48. }
  49. /**
  50. * 关联奖品
  51. */
  52. public function prize()
  53. {
  54. return $this->belongsTo('LotteryPrize', 'prize_id');
  55. }
  56. /**
  57. * 获取发放状态文本
  58. */
  59. public function getDeliverStatusTextAttr($value, $data)
  60. {
  61. $status = [
  62. self::DELIVER_STATUS_PENDING => '待发放',
  63. self::DELIVER_STATUS_SUCCESS => '已发放',
  64. self::DELIVER_STATUS_FAILED => '发放失败',
  65. self::DELIVER_STATUS_CANCELLED => '已取消'
  66. ];
  67. return isset($status[$data['deliver_status']]) ? $status[$data['deliver_status']] : '未知';
  68. }
  69. /**
  70. * 获取奖品信息数据
  71. */
  72. public function getPrizeValueDataAttr($value, $data)
  73. {
  74. return !empty($data['prize_value']) ? json_decode($data['prize_value'], true) : [];
  75. }
  76. /**
  77. * 设置奖品信息
  78. */
  79. public function setPrizeValueAttr($value)
  80. {
  81. return is_array($value) ? json_encode($value) : $value;
  82. }
  83. /**
  84. * 获取发放信息数据
  85. */
  86. public function getDeliverInfoDataAttr($value, $data)
  87. {
  88. return !empty($data['deliver_info']) ? json_decode($data['deliver_info'], true) : [];
  89. }
  90. /**
  91. * 设置发放信息
  92. */
  93. public function setDeliverInfoAttr($value)
  94. {
  95. return is_array($value) ? json_encode($value) : $value;
  96. }
  97. /**
  98. * 创建中奖记录
  99. */
  100. public static function createWinRecord($drawRecordId, $activityId, $userId, $prizeId, $prizeName, $prizeType, $prizeValue = [])
  101. {
  102. $data = [
  103. 'draw_record_id' => $drawRecordId,
  104. 'activity_id' => $activityId,
  105. 'user_id' => $userId,
  106. 'prize_id' => $prizeId,
  107. 'prize_name' => $prizeName,
  108. 'prize_type' => $prizeType,
  109. 'prize_value' => is_array($prizeValue) ? json_encode($prizeValue) : $prizeValue,
  110. 'deliver_status' => self::DELIVER_STATUS_PENDING
  111. ];
  112. return static::create($data);
  113. }
  114. /**
  115. * 标记为发放成功
  116. */
  117. public function markAsDelivered($deliverInfo = [])
  118. {
  119. $this->deliver_status = self::DELIVER_STATUS_SUCCESS;
  120. $this->deliver_time = time();
  121. if ($deliverInfo) {
  122. $this->deliver_info = json_encode($deliverInfo);
  123. }
  124. return $this->save();
  125. }
  126. /**
  127. * 标记为发放失败
  128. */
  129. public function markAsFailed($reason = '')
  130. {
  131. $this->deliver_status = self::DELIVER_STATUS_FAILED;
  132. $this->fail_reason = $reason;
  133. return $this->save();
  134. }
  135. /**
  136. * 设置收货地址
  137. */
  138. public function setDeliveryAddress($name, $mobile, $address)
  139. {
  140. $this->receiver_name = $name;
  141. $this->receiver_mobile = $mobile;
  142. $this->receiver_address = $address;
  143. return $this->save();
  144. }
  145. /**
  146. * 设置快递信息
  147. */
  148. public function setExpressInfo($company, $number)
  149. {
  150. $this->express_company = $company;
  151. $this->express_number = $number;
  152. return $this->save();
  153. }
  154. /**
  155. * 设置兑换码
  156. */
  157. public function setExchangeCode($code)
  158. {
  159. $this->exchange_code = $code;
  160. return $this->save();
  161. }
  162. /**
  163. * 标记兑换码已使用
  164. */
  165. public function markCodeUsed()
  166. {
  167. $this->code_used_time = time();
  168. return $this->save();
  169. }
  170. /**
  171. * 获取待发放的记录
  172. */
  173. public static function getPendingRecords($limit = 100)
  174. {
  175. return static::where('deliver_status', self::DELIVER_STATUS_PENDING)
  176. ->order('createtime', 'asc')
  177. ->limit($limit)
  178. ->select();
  179. }
  180. /**
  181. * 获取用户中奖记录
  182. */
  183. public static function getUserWinRecords($userId, $page = 1, $limit = 20)
  184. {
  185. return static::where('user_id', $userId)
  186. ->with(['activity', 'prize'])
  187. ->order('createtime', 'desc')
  188. ->page($page, $limit)
  189. ->select();
  190. }
  191. /**
  192. * 获取活动中奖统计
  193. */
  194. public static function getActivityWinStats($activityId)
  195. {
  196. return [
  197. 'total_win' => static::where('activity_id', $activityId)->count(),
  198. 'delivered' => static::where('activity_id', $activityId)
  199. ->where('deliver_status', self::DELIVER_STATUS_SUCCESS)
  200. ->count(),
  201. 'pending' => static::where('activity_id', $activityId)
  202. ->where('deliver_status', self::DELIVER_STATUS_PENDING)
  203. ->count(),
  204. 'failed' => static::where('activity_id', $activityId)
  205. ->where('deliver_status', self::DELIVER_STATUS_FAILED)
  206. ->count()
  207. ];
  208. }
  209. }