KjActivityJoin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\common\model\kjactivity;
  4. use app\common\model\Goods;
  5. use app\common\model\GoodsSku;
  6. use app\common\model\BaseModel;
  7. use app\common\model\User;
  8. /**
  9. * 活动中心 ▸ 砍价活动发起人
  10. * Class Ad
  11. * @package app\common\model
  12. */
  13. class KjActivityJoin extends BaseModel
  14. {
  15. protected $name = 'kj_activity_join';
  16. protected $append = ['fmt_end_time','user'];
  17. public function getFmtEndTimeAttr(){
  18. return strtotime($this->end_time)-time();
  19. }
  20. public function getUserAttr(){
  21. $user = User::detail($this->user_id,['avatar']);
  22. return ['nick_name'=>$user['nick_name'],'avatar_url'=>$user['avatar_url']];
  23. }
  24. /**
  25. * 获取列表
  26. * @param array $param
  27. * @return \think\Paginator
  28. * @throws \think\db\exception\DbException
  29. */
  30. public function getList($param = [])
  31. {
  32. // 检索查询调价你
  33. $filter = $this->getFilter($param);
  34. // 排序条件
  35. // $sort = $this->setQuerySort($param);
  36. $sort = ['id'=>'desc'];
  37. // 查询列表数据
  38. $list = $this->where($filter)
  39. ->with(['help'])
  40. ->order($sort)
  41. ->paginate(15)->each(function($item) {
  42. // $id = $item['id'];
  43. $item['help_cnt'] = count($item['help']);
  44. });
  45. return $list ;
  46. }
  47. /**
  48. * 检索查询条件
  49. * @param array $param
  50. * @return array
  51. */
  52. private function getFilter($param = [])
  53. {
  54. // 默认查询条件
  55. // $params = $this->setQueryDefaultValue($param, []);
  56. // 检索查询条件
  57. $filter = [];
  58. if(isset($param['activity_id']) && $param['activity_id']>-1){
  59. $filter[] = ['activity_id', '=', $param['activity_id']];
  60. }
  61. return $filter;
  62. }
  63. /**
  64. * 一对多关联商品表
  65. */
  66. public function activity(){
  67. return $this->belongsTo('kj', 'id','activity_id');
  68. }
  69. /**
  70. * 一对多关联砍价表
  71. */
  72. public function help(){
  73. return $this->hasMany('KjActivityHelp', 'join_id')->order('id', 'desc');
  74. }
  75. // public function add($activity_id, $data){
  76. // // 先删除全部
  77. // static::deleteAll(['activity_id' => $activity_id]);
  78. // if (!empty($data)) {
  79. // // 添加
  80. // $dataset = [];
  81. // foreach ($data as $item) {
  82. // $dataset[] = [
  83. // 'activity_id' => $activity_id,
  84. // 'goods_id' => $item['goods_id'],
  85. // 'goods_sku_id' => $item['goods_sku_id'],
  86. // 'low_price'=>$item['low_price'],
  87. // 'help_kj_up_cnt'=>$item['help_kj_up_cnt']
  88. // ];
  89. // }
  90. // (new static)->addAll($dataset);
  91. // }
  92. // }
  93. }