ActiveTime.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\store\model\sharp;
  3. use app\common\model\sharp\ActiveTime as ActiveTimeModel;
  4. use app\store\model\sharp\ActiveGoods as ActiveGoodsModel;
  5. /**
  6. * 整点秒杀-活动会场场次模型
  7. * Class ActiveTime
  8. * @package app\store\model\sharp
  9. */
  10. class ActiveTime extends ActiveTimeModel
  11. {
  12. /**
  13. * 获取活动会场场次列表
  14. * @param $activeId
  15. * @return \think\Paginator
  16. * @throws \think\exception\DbException
  17. */
  18. public function getList($activeId)
  19. {
  20. $list = $this->with(['active'])
  21. ->withCount(['goods'])
  22. ->where('active_id', '=', $activeId)
  23. ->order(['active_time' => 'asc'])
  24. ->paginate(15, false, [
  25. 'query' => \request()->request()
  26. ]);
  27. return $list;
  28. }
  29. /**
  30. * 修改商品状态
  31. * @param $state
  32. * @return false|int
  33. */
  34. public function setStatus($state)
  35. {
  36. return $this->allowField(true)->save(['status' => (int)$state]) !== false;
  37. }
  38. /**
  39. * 获取指定会场的所有场次时间
  40. * @param $activeId
  41. * @return array
  42. */
  43. public function getActiveTimeData($activeId)
  44. {
  45. return $this->where('active_id', '=', $activeId)->column('active_time');
  46. }
  47. /**
  48. * 根据活动场次ID获取商品列表 (格式化后用于编辑页)
  49. * @param $activeTimeId
  50. * @return array
  51. */
  52. public function getGoodsListByActiveTimeId($activeTimeId)
  53. {
  54. $data = [];
  55. foreach (ActiveGoodsModel::getGoodsListByActiveTimeId($activeTimeId) as $item) {
  56. $data[] = [
  57. 'goods_id' => $item['sharp_goods_id'],
  58. 'goods_name' => $item['goods']['goods_name'],
  59. 'goods_image' => $item['goods']['goods_image'],
  60. ];
  61. }
  62. return $data;
  63. }
  64. /**
  65. * 新增记录
  66. * @param $activeId
  67. * @param $data
  68. * @return bool|mixed
  69. */
  70. public function add($activeId, $data)
  71. {
  72. // 表单验证
  73. if (!$this->onValidate($data)) return false;
  74. // 事务处理
  75. return $this->transaction(function () use ($activeId, $data) {
  76. // 新增活动场次
  77. $this->onBatchAdd(
  78. $activeId,
  79. $data['active_times'],
  80. $data['sharp_goods'],
  81. $data['status']
  82. );
  83. return true;
  84. });
  85. }
  86. /**
  87. * 更新记录
  88. * @param $data
  89. * @return bool|mixed
  90. */
  91. public function edit($data)
  92. {
  93. // 验证是否选择商品
  94. if (!$this->onValidateSharpGoods($data)) {
  95. return false;
  96. }
  97. // 事务处理
  98. return $this->transaction(function () use ($data) {
  99. // 更新活动场次
  100. $this->allowField(true)->save($data);
  101. // 更新场次的商品关联记录
  102. $this->onUpdateActiveGoodsRec($data['sharp_goods']);
  103. return true;
  104. });
  105. }
  106. /**
  107. * 更新当前场次的商品关联记录
  108. * @param $sharpGoodsIds
  109. * @return array|false
  110. * @throws \think\Exception
  111. * @throws \think\exception\PDOException
  112. */
  113. private function onUpdateActiveGoodsRec($sharpGoodsIds)
  114. {
  115. $saveData = [];
  116. foreach ($sharpGoodsIds as $goodsId) {
  117. $saveData[] = [
  118. 'active_id' => $this['active_id'],
  119. 'active_time_id' => $this['active_time_id'],
  120. 'sharp_goods_id' => $goodsId,
  121. 'wxapp_id' => static::$wxapp_id,
  122. ];
  123. }
  124. $this->goods()->delete();
  125. return (new ActiveGoodsModel)->isUpdate(false)->saveAll($saveData);
  126. }
  127. /**
  128. * 表单验证
  129. * @param $data
  130. * @return bool
  131. */
  132. private function onValidate($data)
  133. {
  134. // 验证是否选择活动场次
  135. if (!isset($data['active_times']) || empty($data['active_times'])) {
  136. $this->error = '您还没有选择活动场次';
  137. return false;
  138. }
  139. // 验证是否选择商品
  140. if (!$this->onValidateSharpGoods($data)) {
  141. return false;
  142. }
  143. return true;
  144. }
  145. /**
  146. * 验证是否选择商品
  147. * @param $data
  148. * @return bool
  149. */
  150. private function onValidateSharpGoods($data)
  151. {
  152. // 验证是否选择商品
  153. if (!isset($data['sharp_goods']) || empty($data['sharp_goods'])) {
  154. $this->error = '您还没有选择秒杀商品';
  155. return false;
  156. }
  157. return true;
  158. }
  159. /**
  160. * 批量新增活动场次
  161. * @param $activeId
  162. * @param array $times
  163. * @param array $sharpGoodsIds
  164. * @param int $status
  165. * @return bool
  166. * @throws \Exception
  167. */
  168. public function onBatchAdd($activeId, $times, $sharpGoodsIds, $status = 1)
  169. {
  170. $saveData = [];
  171. foreach ($times as $time) {
  172. $saveData[] = [
  173. 'active_id' => $activeId,
  174. 'active_time' => (int)$time,
  175. 'status' => (int)$status,
  176. 'wxapp_id' => static::$wxapp_id,
  177. ];
  178. }
  179. // 批量更新
  180. $activeTimes = $this->isUpdate(false)->saveAll($saveData);
  181. // 新增活动场次与商品关联关系记录
  182. if (!empty($sharpGoodsIds)) {
  183. $this->onBatchAddActiveGoodsRec($activeTimes, $sharpGoodsIds);
  184. }
  185. return true;
  186. }
  187. /**
  188. * 新增活动场次与商品关联记录
  189. * @param $activeTimes
  190. * @param $sharpGoodsIds
  191. * @return array|false
  192. * @throws \Exception
  193. */
  194. private function onBatchAddActiveGoodsRec($activeTimes, $sharpGoodsIds)
  195. {
  196. $saveData = [];
  197. foreach ($activeTimes as $item) {
  198. foreach ($sharpGoodsIds as $goodsId) {
  199. $saveData[] = [
  200. 'active_id' => $item['active_id'],
  201. 'active_time_id' => $item['active_time_id'],
  202. 'sharp_goods_id' => $goodsId,
  203. 'wxapp_id' => static::$wxapp_id,
  204. ];
  205. }
  206. }
  207. return (new ActiveGoodsModel)->isUpdate(false)->saveAll($saveData);
  208. }
  209. /**
  210. * 根据活动ID删除全部场次和商品关系
  211. * @param $activeId
  212. * @return bool
  213. */
  214. public function onDeleteByActiveId($activeId)
  215. {
  216. $this->where('active_id', '=', $activeId)->delete();
  217. (new ActiveGoodsModel)->where('active_id', '=', $activeId)->delete();
  218. return true;
  219. }
  220. /**
  221. * 删除当前场次
  222. * @return bool
  223. * @throws \think\Exception
  224. * @throws \think\exception\PDOException
  225. */
  226. public function onDelete()
  227. {
  228. $this->delete();
  229. $this->goods()->delete();
  230. return true;
  231. }
  232. }