Active.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\bargain;
  10. use app\common\model\bargain\Active as ActiveModel;
  11. use app\store\service\Goods as GoodsService;
  12. /**
  13. * 砍价活动模型
  14. * Class Active
  15. * @package app\store\model\bargain
  16. */
  17. class Active extends ActiveModel
  18. {
  19. /**
  20. * 获取列表数据
  21. * @param string $search
  22. * @return mixed|\think\Paginator
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. */
  27. public function getList($search = '')
  28. {
  29. $this->setBaseQuery($this->alias, [
  30. ['goods', 'goods_id'],
  31. ]);
  32. // 检索查询条件
  33. if (!empty($search)) {
  34. $this->where('goods.goods_name', 'like', "%{$search}%");
  35. }
  36. // 获取活动列表
  37. $list = $this->where("{$this->alias}.is_delete", '=', 0)
  38. ->order(["{$this->alias}.sort" => 'asc', "{$this->alias}.create_time" => 'desc'])
  39. ->paginate(15, false, [
  40. 'query' => \request()->request()
  41. ]);
  42. if (!$list->isEmpty()) {
  43. // 设置商品数据
  44. $list = GoodsService::setGoodsData($list);
  45. }
  46. return $list;
  47. }
  48. /**
  49. * 新增记录
  50. * @param $data
  51. * @return bool|int
  52. */
  53. public function add($data)
  54. {
  55. if (!$this->onValidate($data, 'add')) {
  56. return false;
  57. }
  58. $data = $this->createData($data);
  59. return $this->allowField(true)->save($data) !== false;
  60. }
  61. /**
  62. * 更新记录
  63. * @param $data
  64. * @return bool|int
  65. */
  66. public function edit($data)
  67. {
  68. if (!$this->onValidate($data, 'edit')) {
  69. return false;
  70. }
  71. $data = $this->createData($data);
  72. return $this->allowField(true)->save($data) !== false;
  73. }
  74. private function createData($data)
  75. {
  76. $data['wxapp_id'] = static::$wxapp_id;
  77. $data['start_time'] = strtotime($data['start_time']);
  78. $data['end_time'] = strtotime($data['end_time']);
  79. return $data;
  80. }
  81. /**
  82. * 表单验证
  83. * @param $data
  84. * @param string $scene
  85. * @return bool
  86. */
  87. private function onValidate($data, $scene = 'add')
  88. {
  89. if ($scene === 'add') {
  90. if (!isset($data['goods_id']) || empty($data['goods_id'])) {
  91. $this->error = '请选择商品';
  92. return false;
  93. }
  94. }
  95. // 验证活动时间
  96. if (empty($data['start_time']) || empty($data['end_time'])) {
  97. $this->error = '请选择活动的开始时间与截止时间';
  98. return false;
  99. }
  100. $data['start_time'] = strtotime($data['start_time']);
  101. $data['end_time'] = strtotime($data['end_time']);
  102. if ($data['end_time'] <= $data['start_time']) {
  103. $this->error = '活动结束时间必须大于开始时间';
  104. return false;
  105. }
  106. return true;
  107. }
  108. /**
  109. * 软删除
  110. * @return false|int
  111. */
  112. public function setDelete()
  113. {
  114. return $this->allowField(true)->save(['is_delete' => 1]);
  115. }
  116. /**
  117. * 商品ID是否存在
  118. * @param $goodsId
  119. * @return bool
  120. */
  121. public static function isExistGoodsId($goodsId)
  122. {
  123. return !!(new static)->where('goods_id', '=', $goodsId)
  124. ->where('is_delete', '=', 0)
  125. ->value('active_id');
  126. }
  127. }