Active.php 3.6 KB

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