ZaActivity.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\model\za;
  4. use app\common\model\za\ZaActivity as ZaActivityModel;
  5. use app\common\model\Goods;
  6. use app\common\model\GoodsSku;
  7. use app\common\model\CommonHis;
  8. use app\common\service\store\User as StoreUserService;
  9. use app\common\model\BaseModel;
  10. use think\facade\Db;
  11. use app\common\model\UploadFile;
  12. use app\common\library\helper;
  13. /**
  14. * 活动中心 ▸ 买一赠一活动
  15. * Class Ad
  16. * @package app\common\model
  17. */
  18. class ZaActivity extends ZaActivityModel
  19. {
  20. public function getZaGood($goodsId){
  21. $list = $this->validActivity($goodsId);
  22. if($list){
  23. return ['preview_url'=>$list->preview_url??'',
  24. 'end_time'=>$list->end_time,
  25. 'activity_id'=>$list->id,
  26. 'ts_end'=>strtotime($list->end_time)-time(),
  27. 'share_title'=>$list['share_title'],
  28. 'name'=>$list['name'],
  29. 'za_goods_id'=>$list['za_goods_id'],
  30. 'expire_day'=>$list->expire_day,
  31. 'goods_name'=>$list->goods_name,
  32. 'goods_id'=>$list->goods_id??0
  33. ];
  34. }
  35. return null;
  36. }
  37. /**
  38. * 获取列表
  39. * @param $goodsId
  40. * @return mixed
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public static function validActivity($goodsId)
  46. {
  47. // 检索查询条件
  48. $now = date('Y-m-d H:i:s');
  49. $filter[] = ['ac.start_time','<',$now];
  50. $filter[] = ['ac.end_time','>',$now];
  51. $filter[] = ['ac.audit_status','=',1];
  52. $filter[] = ['ac.is_up','=',1];
  53. $filter[] = ['g.goods_id','=',$goodsId];
  54. // 执行查询
  55. $one = self::field('ac.*,g.goods_name,g.preview_url,g.id as za_goods_id,g.goods_id as goods_id')
  56. ->with([])
  57. ->alias('ac')
  58. ->leftJoin('za_activity_goods g','g.za_activity_id = ac.id')
  59. ->where($filter)
  60. ->find();
  61. return $one;
  62. }
  63. // protected $name = 'za_activity';
  64. // protected $append = ['status','image'];
  65. // public function getImageAttr(){
  66. // $a = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_up_id)->ali_url??'';
  67. // $b = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_down_id)->ali_url??'';
  68. // return [$a,$b];
  69. // }
  70. // public function imageshare(){
  71. // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_share_id');
  72. // }
  73. // public function imageup(){
  74. // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_up_id');
  75. // }
  76. // public function imagedown(){
  77. // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_down_id');
  78. // }
  79. // public static function getCurrent(){
  80. // $now = Date("Y-m-d H:i:s",time());
  81. // // $filter['audit_status_zg'] = 10;
  82. // $filter['is_up'] = 1;
  83. // $one = ZaActivity::where('start_time','<',$now)->where('end_time','>',$now)->where($filter)->find();
  84. // if($one){
  85. // return $one;
  86. // }else{
  87. // $one = ZaActivity::find();
  88. // return $one;
  89. // }
  90. // return false;
  91. // }
  92. // /**
  93. // * 一对多关联商品表
  94. // */
  95. // public function goods(){
  96. // return $this->hasMany('MsActivityGood', 'ms_activity_id')->order('is_end','asc')->order('start_time', 'asc');
  97. // }
  98. // /**
  99. // * 一对多关联商品表
  100. // */
  101. // public function imagelist(){
  102. // return $this->hasMany('MsActivityImage', 'ms_activity_id')->order('id', 'desc');
  103. // }
  104. // /**
  105. // * 作废
  106. // * @param array $data
  107. // * @return bool
  108. // */
  109. // public function down(array $data): bool {
  110. // $this->transaction(function () use ($data) {
  111. // $id = (int)$this['id'];
  112. // $this->save($data);
  113. // });
  114. // return true;
  115. // }
  116. // //获取秒杀价
  117. // public function getMsPrice($goods_id,$goods_price_min){
  118. // $now = Date("Y-m-d H:i:s",time());
  119. // $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')];
  120. // $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')];
  121. // $filter[] = ['activity.is_up','=',1];
  122. // $filter[] = ['activity.audit_status','=',1];
  123. // $filter[] = ['ms_activity_goods.goods_id','=',$goods_id];
  124. // $filter[] = ['ms_activity_goods.end_time','>',$now];
  125. // $goods = $this->field('ms_activity_goods.*')->alias('activity')->leftjoin('ms_activity_goods','ms_activity_goods.ms_activity_id=activity.id')->where($filter)->find();
  126. // $type = 0;
  127. // if($goods){
  128. // $now = date("Y-m-d H:i:s",time());
  129. // if ($now < $goods->start_time) {
  130. // $d = Date("d",time());
  131. // // 2022-01-01 15:00:00;
  132. // if($d==substr($goods->start_time,8,2)){
  133. // $type = 1;
  134. // $text = substr($goods->start_time,11,5).'开始秒杀';
  135. // }else{
  136. // $type = 2;
  137. // $text = Date('Y-m-d H:i',strtotime($goods->start_time)).'开始秒杀';
  138. // }
  139. // }
  140. // if ($now > $goods->end_time) {
  141. // $type = 3;
  142. // $text = '秒杀结束';
  143. // }
  144. // if($now>$goods->start_time&&$now<$goods->end_time){
  145. // $type = 4;
  146. // $text = '限时秒杀';
  147. // }
  148. // $data['ms_price'] = $goods->ms_price; //秒杀价
  149. // $data['type'] = $type; //类型 1 当天开始秒杀,2 某天某时开始秒杀 4秒杀中
  150. // $data['text'] = $text; //文字
  151. // $data['dif_end_time'] = strtotime($goods->end_time) - time();
  152. // $data['limit_mount'] = $goods->limit_mount;
  153. // $data['stock_num'] = $goods->stock_num;
  154. // $data['ms_id'] = $goods->id;
  155. // $data['start_time'] = substr($goods->start_time,0,16);
  156. // $data['zj_price'] = helper::bcsub($goods_price_min - $goods->ms_price,0,2);
  157. // $data['ms_activity_id'] = $goods->ms_activity_id;
  158. // return $data;
  159. // }
  160. // return null ;
  161. // }
  162. // /**
  163. // * 获取列表
  164. // * @param array $param
  165. // * @return \think\Paginator
  166. // * @throws \think\db\exception\DbException
  167. // */
  168. // public function getList($param = [])
  169. // {
  170. // // 检索查询砍价列表
  171. // $filter = $this->getFilter($param);
  172. // // 排序条件
  173. // // $sort = $this->setQuerySort($param);
  174. // $sort = ['activity.id'=>'desc'];
  175. // // 查询列表数据
  176. // $param['status'] = $param['status']??-1;
  177. // //
  178. // $list = $this->field('activity.*')->alias('activity')->leftjoin('ms_activity_goods','ms_activity_goods.ms_activity_id=activity.id')->where($filter)->with(['goods'])->group('activity.id')->order($sort)->paginate(15)->each(function($item) {
  179. // $item['good_cnt'] = count($item['goods']);
  180. // $sale_cnt = 0;
  181. // foreach($item['goods'] as $k){
  182. // $sale_cnt = $sale_cnt + $k['sale_num'];
  183. // }
  184. // $item['sale_cnt'] = $sale_cnt;
  185. // });
  186. // return $list ;
  187. // }
  188. // /**
  189. // * 检索查询条件
  190. // * @param array $param
  191. // * @return array
  192. // */
  193. // private function getFilter($param = [])
  194. // {
  195. // // 默认查询条件
  196. // $params = $this->setQueryDefaultValue($param, ['goods_name'=>'','audit_status'=>-1,'status'=>-1]);
  197. // // 检索查询条件
  198. // $filter = [];
  199. // if(isset($params['audit_status']) && $params['audit_status']>-1){
  200. // $filter[] = ['audit_status', '=', $params['audit_status']];
  201. // }
  202. // if(isset($params['goods_name']) && !empty($params['goods_name'])){
  203. // $filter[] = ['ms_activity_goods.goods_name', 'like','%'.$params['goods_name'].'%'];
  204. // }
  205. // if (!empty($params['betweenTime'])) {
  206. // $times = between_time_format($params['betweenTime']);
  207. // $filter[] = ['activity.start_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
  208. // $filter[] = ['activity.end_time', '<', Date('Y-m-d H:i:s',$times['end_time'])];
  209. // }
  210. // //活动状态
  211. // if(isset($params['status'])&&$params['status']>-1){
  212. // switch ($params['status']){
  213. // case 1://进行中
  214. // $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')];
  215. // $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')];
  216. // // $filter[] = ['audit_status_zg','=',10];
  217. // // $filter[] = ['is_up','=',1];
  218. // // $filter[] = ['audit_status_zg','=',10];
  219. // break;
  220. // case 2://未开始
  221. // $filter[] = ['activity.start_time','>',date('Y-m-d H:i:s')];
  222. // // $filter[] = ['is_up','=',1];
  223. // // $filter[] = ['audit_status_zg','=',10];
  224. // break;
  225. // case 3://已结束
  226. // // $filter[] = ['end_time','<',date('Y-m-d H:i:s')];
  227. // // $filter[] = ['is_up','in',[0,1]];
  228. // $filter[] = ['activity.end_time','<',date('Y-m-d H:i:s')];
  229. // break;
  230. // }
  231. // }
  232. // return $filter;
  233. // }
  234. // //活动状态 即将开始或已结束
  235. // public function getStatusAttr() {
  236. // $now = date("Y-m-d H:i:s",time());
  237. // if($this->is_up==0){
  238. // return 4;
  239. // }
  240. // if ($now < $this->start_time) {
  241. // return 2;
  242. // }
  243. // if ($now > $this->end_time) {
  244. // return 3;
  245. // }
  246. // if($now>$this->start_time&&$now<$this->end_time){
  247. // return 1;
  248. // }
  249. // return 0;
  250. // }
  251. // /**
  252. // * 审核
  253. // * @param array $data
  254. // * @return bool
  255. // */
  256. // public function audit(array $data): bool
  257. // {
  258. // $this->transaction(function () use ($data) {
  259. // $user = StoreUserService::getLoginInfo();
  260. // $real_name = $user['user']['real_name']??'';
  261. // $id = (int)$this['id'];
  262. // $this->save($data);
  263. // $id = (int)$this['id'];
  264. // if($data['audit_status']==1){
  265. // (new CommonHis())->add(3, $id, $real_name,'同意并发布活动');
  266. // }
  267. // if($data['audit_status']==2){
  268. // $remark = '不同意'." ".$data['audit_reason'];
  269. // (new CommonHis())->add(3, $id, $real_name,$remark);
  270. // }
  271. // });
  272. // return true;
  273. // }
  274. // /**
  275. // * 详情
  276. // * @param int $id
  277. // * @return null|static
  278. // */
  279. // public static function detail(int $id,array $with=[])
  280. // {
  281. // return self::get($id,$with);
  282. // }
  283. // /**
  284. // * 添加
  285. // * @param $data
  286. // * @return false|int
  287. // */
  288. // public function addOne($data)
  289. // {
  290. // $times = between_time_format($data['between_time']);
  291. // $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']);
  292. // $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']);
  293. // isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
  294. // isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
  295. // $this->transaction(function () use ($data) {
  296. // $this->save($data);
  297. // if(isset($data['goods'])){
  298. // $goods = new MsActivityGoodModel;
  299. // $flag = $goods->add($this->id,$data['goods']);
  300. // if(!$flag){
  301. // $this->error = $goods->error;
  302. // Db::rollback();
  303. // return false;
  304. // }
  305. // }
  306. // if(isset($data['images'])){
  307. // $images = new MsActivityImageModel;
  308. // $flag = $images->add($this->id,$data['images']);
  309. // if(!$flag){
  310. // $this->error = $images->error;
  311. // Db::rollback();
  312. // return false;
  313. // }
  314. // }
  315. // $user = StoreUserService::getLoginInfo();
  316. // $real_name = $user['user']['real_name']??'';
  317. // $id = (int)$this['id'];
  318. // (new CommonHis())->add(3, $id, $real_name,'提交审核');
  319. // });
  320. // return true;
  321. // }
  322. // /**
  323. // * 编辑
  324. // * @param $data
  325. // * @return bool|int
  326. // */
  327. // public function edit($data)
  328. // {
  329. // $times = between_time_format($data['between_time']);
  330. // $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']);
  331. // $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']);
  332. // isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
  333. // isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
  334. // $this->transaction(function () use ($data) {
  335. // $this->save($data);
  336. // if(isset($data['goods'])){
  337. // $goods = new MsActivityGoodModel;
  338. // $flag = $goods->add($this->id,$data['goods']);
  339. // if(!$flag){
  340. // $this->error = $goods->error;
  341. // Db::rollback();
  342. // return false;
  343. // }
  344. // }
  345. // if(isset($data['images'])){
  346. // $images = new MsActivityImageModel;
  347. // $flag = $images->add($this->id,$data['images']);
  348. // if(!$flag){
  349. // $this->error = $images->error;
  350. // Db::rollback();
  351. // return false;
  352. // }
  353. // }
  354. // $user = StoreUserService::getLoginInfo();
  355. // $real_name = $user['user']['real_name']??'';
  356. // $id = (int)$this['id'];
  357. // (new CommonHis())->add(3, $id, $real_name,'再次编辑');
  358. // });
  359. // return true;
  360. // }
  361. // /**
  362. // * 关联记录
  363. // * @return \think\model\relation\BelongsTo
  364. // */
  365. // public function his()
  366. // {
  367. // return $this->hasMany('app\common\model\CommonHis','target_id','id')->where('target_type',3)->order('id', 'desc');
  368. // }
  369. // /**
  370. // * 删除
  371. // * @param $id
  372. // * @return bool|int
  373. // */
  374. // public function remove()
  375. // {
  376. // return $this->delete();
  377. // }
  378. }