Active.php 3.4 KB

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