123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace app\store\model\sharp;
- use app\common\model\sharp\ActiveTime as ActiveTimeModel;
- use app\store\model\sharp\ActiveGoods as ActiveGoodsModel;
- /**
- * 整点秒杀-活动会场场次模型
- * Class ActiveTime
- * @package app\store\model\sharp
- */
- class ActiveTime extends ActiveTimeModel
- {
- /**
- * 获取活动会场场次列表
- * @param $activeId
- * @return \think\Paginator
- * @throws \think\exception\DbException
- */
- public function getList($activeId)
- {
- $list = $this->with(['active'])
- ->withCount(['goods'])
- ->where('active_id', '=', $activeId)
- ->order(['active_time' => 'asc'])
- ->paginate(15, false, [
- 'query' => \request()->request()
- ]);
- return $list;
- }
- /**
- * 修改商品状态
- * @param $state
- * @return false|int
- */
- public function setStatus($state)
- {
- return $this->allowField(true)->save(['status' => (int)$state]) !== false;
- }
- /**
- * 获取指定会场的所有场次时间
- * @param $activeId
- * @return array
- */
- public function getActiveTimeData($activeId)
- {
- return $this->where('active_id', '=', $activeId)->column('active_time');
- }
- /**
- * 根据活动场次ID获取商品列表 (格式化后用于编辑页)
- * @param $activeTimeId
- * @return array
- */
- public function getGoodsListByActiveTimeId($activeTimeId)
- {
- $data = [];
- foreach (ActiveGoodsModel::getGoodsListByActiveTimeId($activeTimeId) as $item) {
- $data[] = [
- 'goods_id' => $item['sharp_goods_id'],
- 'goods_name' => $item['goods']['goods_name'],
- 'goods_image' => $item['goods']['goods_image'],
- ];
- }
- return $data;
- }
- /**
- * 新增记录
- * @param $activeId
- * @param $data
- * @return bool|mixed
- */
- public function add($activeId, $data)
- {
- // 表单验证
- if (!$this->onValidate($data)) return false;
- // 事务处理
- return $this->transaction(function () use ($activeId, $data) {
- // 新增活动场次
- $this->onBatchAdd(
- $activeId,
- $data['active_times'],
- $data['sharp_goods'],
- $data['status']
- );
- return true;
- });
- }
- /**
- * 更新记录
- * @param $data
- * @return bool|mixed
- */
- public function edit($data)
- {
- // 验证是否选择商品
- if (!$this->onValidateSharpGoods($data)) {
- return false;
- }
- // 事务处理
- return $this->transaction(function () use ($data) {
- // 更新活动场次
- $this->allowField(true)->save($data);
- // 更新场次的商品关联记录
- $this->onUpdateActiveGoodsRec($data['sharp_goods']);
- return true;
- });
- }
- /**
- * 更新当前场次的商品关联记录
- * @param $sharpGoodsIds
- * @return array|false
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- private function onUpdateActiveGoodsRec($sharpGoodsIds)
- {
- $saveData = [];
- foreach ($sharpGoodsIds as $goodsId) {
- $saveData[] = [
- 'active_id' => $this['active_id'],
- 'active_time_id' => $this['active_time_id'],
- 'sharp_goods_id' => $goodsId,
- 'wxapp_id' => static::$wxapp_id,
- ];
- }
- $this->goods()->delete();
- return (new ActiveGoodsModel)->isUpdate(false)->saveAll($saveData);
- }
- /**
- * 表单验证
- * @param $data
- * @return bool
- */
- private function onValidate($data)
- {
- // 验证是否选择活动场次
- if (!isset($data['active_times']) || empty($data['active_times'])) {
- $this->error = '您还没有选择活动场次';
- return false;
- }
- // 验证是否选择商品
- if (!$this->onValidateSharpGoods($data)) {
- return false;
- }
- return true;
- }
- /**
- * 验证是否选择商品
- * @param $data
- * @return bool
- */
- private function onValidateSharpGoods($data)
- {
- // 验证是否选择商品
- if (!isset($data['sharp_goods']) || empty($data['sharp_goods'])) {
- $this->error = '您还没有选择秒杀商品';
- return false;
- }
- return true;
- }
- /**
- * 批量新增活动场次
- * @param $activeId
- * @param array $times
- * @param array $sharpGoodsIds
- * @param int $status
- * @return bool
- * @throws \Exception
- */
- public function onBatchAdd($activeId, $times, $sharpGoodsIds, $status = 1)
- {
- $saveData = [];
- foreach ($times as $time) {
- $saveData[] = [
- 'active_id' => $activeId,
- 'active_time' => (int)$time,
- 'status' => (int)$status,
- 'wxapp_id' => static::$wxapp_id,
- ];
- }
- // 批量更新
- $activeTimes = $this->isUpdate(false)->saveAll($saveData);
- // 新增活动场次与商品关联关系记录
- if (!empty($sharpGoodsIds)) {
- $this->onBatchAddActiveGoodsRec($activeTimes, $sharpGoodsIds);
- }
- return true;
- }
- /**
- * 新增活动场次与商品关联记录
- * @param $activeTimes
- * @param $sharpGoodsIds
- * @return array|false
- * @throws \Exception
- */
- private function onBatchAddActiveGoodsRec($activeTimes, $sharpGoodsIds)
- {
- $saveData = [];
- foreach ($activeTimes as $item) {
- foreach ($sharpGoodsIds as $goodsId) {
- $saveData[] = [
- 'active_id' => $item['active_id'],
- 'active_time_id' => $item['active_time_id'],
- 'sharp_goods_id' => $goodsId,
- 'wxapp_id' => static::$wxapp_id,
- ];
- }
- }
- return (new ActiveGoodsModel)->isUpdate(false)->saveAll($saveData);
- }
- /**
- * 根据活动ID删除全部场次和商品关系
- * @param $activeId
- * @return bool
- */
- public function onDeleteByActiveId($activeId)
- {
- $this->where('active_id', '=', $activeId)->delete();
- (new ActiveGoodsModel)->where('active_id', '=', $activeId)->delete();
- return true;
- }
- /**
- * 删除当前场次
- * @return bool
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function onDelete()
- {
- $this->delete();
- $this->goods()->delete();
- return true;
- }
- }
|