Active.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\store\model\sharp;
  3. use app\common\library\helper;
  4. use app\common\model\sharp\Active as ActiveModel;
  5. use app\store\model\sharp\ActiveTime as ActiveTimeModel;
  6. /**
  7. * 整点秒杀-活动会场模型
  8. * Class Active
  9. * @package app\store\model\sharp
  10. */
  11. class Active extends ActiveModel
  12. {
  13. /**
  14. * 获取器:活动日期
  15. * @param $value
  16. * @return false|string
  17. */
  18. public function getActiveDateAttr($value)
  19. {
  20. return date('Y-m-d', $value);
  21. }
  22. /**
  23. * 获取活动会场列表
  24. * @return \think\Paginator
  25. * @throws \think\exception\DbException
  26. */
  27. public function getList()
  28. {
  29. $list = $this->with(['active_time'])
  30. ->where('is_delete', '=', 0)
  31. ->order(['active_date' => 'desc'])
  32. ->paginate(15, false, [
  33. 'query' => \request()->request()
  34. ]);
  35. return $this->getActiveTimeCount($list);
  36. }
  37. private function getActiveTimeCount($list)
  38. {
  39. foreach ($list as &$item) {
  40. $activeTimeArr = helper::getArrayColumn($item['active_time'], 'active_time');
  41. $item['active_time_count'] = count($activeTimeArr);
  42. }
  43. return $list;
  44. }
  45. /**
  46. * 新增记录
  47. * @param $data
  48. * @return bool
  49. * @throws \Exception
  50. */
  51. public function add($data)
  52. {
  53. // 表单验证
  54. if (!$this->onValidate($data)) return false;
  55. // 新增活动
  56. $data['wxapp_id'] = static::$wxapp_id;
  57. $data['active_date'] = strtotime($data['active_date']);
  58. !isset($data['sharp_goods']) && $data['sharp_goods'] = [];
  59. return $this->transaction(function () use ($data) {
  60. // 新增活动
  61. $this->allowField(true)->save($data);
  62. // 新增活动场次
  63. (new ActiveTimeModel)->onBatchAdd(
  64. $this['active_id'],
  65. $data['active_times'],
  66. $data['sharp_goods']
  67. );
  68. return true;
  69. });
  70. }
  71. /**
  72. * 表单验证
  73. * @param $data
  74. * @return bool
  75. */
  76. private function onValidate($data)
  77. {
  78. // 活动日期是否已存在
  79. if ($this->isExistByActiveDate($data['active_date'])) {
  80. $this->error = '该活动日期已存在';
  81. return false;
  82. }
  83. // // 验证是否选择商品
  84. // if (!isset($data['sharp_goods']) || empty($data['sharp_goods'])) {
  85. // $this->error = '您还没有选择秒杀商品';
  86. // return false;
  87. // }
  88. return true;
  89. }
  90. /**
  91. * 活动日期是否已存在
  92. * @param $date
  93. * @return bool
  94. */
  95. private function isExistByActiveDate($date)
  96. {
  97. return !!(new static)->where('active_date', '=', strtotime($date))
  98. ->where('is_delete', '=', 0)
  99. ->value('active_id');
  100. }
  101. /**
  102. * 修改商品状态
  103. * @param $state
  104. * @return false|int
  105. */
  106. public function setStatus($state)
  107. {
  108. return $this->allowField(true)->save(['status' => (int)$state]) !== false;
  109. }
  110. /**
  111. * 软删除
  112. * @return false|int
  113. */
  114. public function setDelete()
  115. {
  116. // 同步删除场次和商品关联
  117. (new ActiveTimeModel)->onDeleteByActiveId($this['active_id']);
  118. // 将该活动设置为已删除
  119. return $this->allowField(true)->save(['is_delete' => 1]);
  120. }
  121. }