123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- declare (strict_types=1);
- namespace app\common\model\ms;
- use app\common\model\ms\MsActivityGood as MsActivityGoodModel;
- use app\common\model\ms\MsActivityImage as MsActivityImageModel;
- use app\common\model\Goods;
- use app\common\model\GoodsSku;
- use app\common\model\CommonHis;
- use app\common\service\store\User as StoreUserService;
- use app\common\model\BaseModel;
- use think\facade\Db;
- use app\common\model\UploadFile;
- use app\common\library\helper;
- /**
- * 活动中心 ▸ 秒杀活动
- * Class Ad
- * @package app\common\model
- */
- class MsActivity extends BaseModel
- {
-
- protected $name = 'ms_activity';
- protected $append = ['status','image'];
- public function getImageAttr(){
- $a = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_up_id)->ali_url??'';
- $b = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_down_id)->ali_url??'';
- return [$a,$b];
- }
- public function imageshare(){
- return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_share_id');
- }
- public function imageup(){
- return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_up_id');
- }
- public function imagedown(){
- return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_down_id');
- }
- public static function getCurrent(){
- $now = Date("Y-m-d H:i:s",time());
- $filter['audit_status_zg'] = 10;
- $filter['is_up'] = 1;
- $one = MsActivity::where('start_time','<',$now)->where('end_time','>',$now)->where($filter)->find();
- if($one){
- return $one;
- }else{
- $one = MsActivity::find();
- return $one;
- }
- return false;
- }
- /**
- * 一对多关联商品表
- */
- public function goods(){
- return $this->hasMany('MsActivityGood', 'ms_activity_id')->order('is_end','asc')->order('start_time', 'asc');
- }
- /**
- * 一对多关联商品表
- */
- public function imagelist(){
- return $this->hasMany('MsActivityImage', 'ms_activity_id')->order('id', 'desc');
- }
- /**
- * 作废
- * @param array $data
- * @return bool
- */
- public function down(array $data): bool {
- $this->transaction(function () use ($data) {
- $id = (int)$this['id'];
- $this->save($data);
- });
- return true;
- }
-
- //获取秒杀价
- public function getMsPrice($goods_id,$goods_price_min){
- $now = Date("Y-m-d H:i:s",time());
- $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')];
- $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')];
- $filter[] = ['activity.is_up','=',1];
- $filter[] = ['activity.audit_status','=',1];
- $filter[] = ['ms_activity_goods.goods_id','=',$goods_id];
- $filter[] = ['ms_activity_goods.end_time','>',$now];
- $goods = $this->field('ms_activity_goods.*')->alias('activity')->leftjoin('ms_activity_goods','ms_activity_goods.ms_activity_id=activity.id')->where($filter)->find();
- $type = 0;
- if($goods){
- $now = date("Y-m-d H:i:s",time());
- if ($now < $goods->start_time) {
- $d = Date("d",time());
- // 2022-01-01 15:00:00;
- if($d==substr($goods->start_time,8,2)){
- $type = 1;
- $text = substr($goods->start_time,11,5).'开始秒杀';
- }else{
- $type = 2;
- $text = Date('Y-m-d H:i',strtotime($goods->start_time)).'开始秒杀';
- }
- }
- if ($now > $goods->end_time) {
- $type = 3;
- $text = '秒杀结束';
- }
- if($now>$goods->start_time&&$now<$goods->end_time){
- $type = 4;
- $text = '限时秒杀';
- }
- $data['ms_price'] = $goods->ms_price; //秒杀价
- $data['type'] = $type; //类型 1 当天开始秒杀,2 某天某时开始秒杀 4秒杀中
- $data['text'] = $text; //文字
- $data['dif_end_time'] = strtotime($goods->end_time) - time();
- $data['limit_mount'] = $goods->limit_mount;
-
- $data['stock_num'] = $goods->stock_num;
- $data['ms_id'] = $goods->id;
- $data['start_time'] = substr($goods->start_time,0,16);
- $data['zj_price'] = helper::bcsub($goods_price_min - $goods->ms_price,0,2);
- $data['ms_activity_id'] = $goods->ms_activity_id;
- return $data;
- }
- return null ;
- }
- /**
- * 获取列表
- * @param array $param
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList($param = [])
- {
- // 检索查询砍价列表
- $filter = $this->getFilter($param);
- // 排序条件
- // $sort = $this->setQuerySort($param);
- $sort = ['activity.id'=>'desc'];
- // 查询列表数据
- $param['status'] = $param['status']??-1;
- //
- $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) {
- $item['good_cnt'] = count($item['goods']);
- $sale_cnt = 0;
- foreach($item['goods'] as $k){
- $sale_cnt = $sale_cnt + $k['sale_num'];
- }
- $item['sale_cnt'] = $sale_cnt;
- });
-
- return $list ;
- }
- /**
- * 检索查询条件
- * @param array $param
- * @return array
- */
- private function getFilter($param = [])
- {
- // 默认查询条件
- $params = $this->setQueryDefaultValue($param, ['goods_name'=>'','audit_status'=>-1,'status'=>-1]);
- // 检索查询条件
- $filter = [];
-
- if(isset($params['audit_status']) && $params['audit_status']>-1){
- $filter[] = ['audit_status', '=', $params['audit_status']];
- }
- if(isset($params['goods_name']) && !empty($params['goods_name'])){
- $filter[] = ['ms_activity_goods.goods_name', 'like','%'.$params['goods_name'].'%'];
- }
- if (!empty($params['betweenTime'])) {
- $times = between_time_format($params['betweenTime']);
- $filter[] = ['activity.start_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
- $filter[] = ['activity.end_time', '<', Date('Y-m-d H:i:s',$times['end_time'])];
- }
- //活动状态
- if(isset($params['status'])&&$params['status']>-1){
- switch ($params['status']){
- case 1://进行中
- $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')];
- $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')];
- // $filter[] = ['audit_status_zg','=',10];
- // $filter[] = ['is_up','=',1];
- // $filter[] = ['audit_status_zg','=',10];
- break;
- case 2://未开始
- $filter[] = ['activity.start_time','>',date('Y-m-d H:i:s')];
- // $filter[] = ['is_up','=',1];
- // $filter[] = ['audit_status_zg','=',10];
- break;
- case 3://已结束
- // $filter[] = ['end_time','<',date('Y-m-d H:i:s')];
- // $filter[] = ['is_up','in',[0,1]];
- $filter[] = ['activity.end_time','<',date('Y-m-d H:i:s')];
- break;
- }
- }
- return $filter;
- }
- //活动状态 即将开始或已结束
- public function getStatusAttr() {
- $now = date("Y-m-d H:i:s",time());
- if($this->is_up==0){
- return 4;
- }
- if ($now < $this->start_time) {
- return 2;
- }
- if ($now > $this->end_time) {
- return 3;
- }
- if($now>$this->start_time&&$now<$this->end_time){
- return 1;
- }
- return 0;
- }
- /**
- * 审核
- * @param array $data
- * @return bool
- */
- public function audit(array $data): bool
- {
- $this->transaction(function () use ($data) {
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- $this->save($data);
- $id = (int)$this['id'];
- if($data['audit_status']==1){
- (new CommonHis())->add(3, $id, $real_name,'同意并发布活动');
- }
- if($data['audit_status']==2){
- $remark = '不同意'." ".$data['audit_reason'];
- (new CommonHis())->add(3, $id, $real_name,$remark);
- }
-
- });
- return true;
- }
- /**
- * 详情
- * @param int $id
- * @return null|static
- */
- public static function detail(int $id,array $with=[])
- {
- return self::get($id,$with);
- }
- /**
- * 添加
- * @param $data
- * @return false|int
- */
- public function addOne($data)
- {
- $times = between_time_format($data['between_time']);
- $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']);
- $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']);
- isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
- isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
- $this->transaction(function () use ($data) {
- $this->save($data);
- if(isset($data['goods'])){
- $goods = new MsActivityGoodModel;
- $flag = $goods->add($this->id,$data['goods']);
- if(!$flag){
- $this->error = $goods->error;
- Db::rollback();
- return false;
- }
- }
- if(isset($data['images'])){
- $images = new MsActivityImageModel;
- $flag = $images->add($this->id,$data['images']);
- if(!$flag){
- $this->error = $images->error;
- Db::rollback();
- return false;
- }
- }
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- (new CommonHis())->add(3, $id, $real_name,'提交审核');
- });
- return true;
-
- }
- /**
- * 编辑
- * @param $data
- * @return bool|int
- */
- public function edit($data)
- {
- $times = between_time_format($data['between_time']);
- $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']);
- $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']);
- isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
- isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
- $this->transaction(function () use ($data) {
- $this->save($data);
- if(isset($data['goods'])){
- $goods = new MsActivityGoodModel;
- $flag = $goods->add($this->id,$data['goods']);
- if(!$flag){
- $this->error = $goods->error;
- Db::rollback();
- return false;
- }
- }
- if(isset($data['images'])){
- $images = new MsActivityImageModel;
- $flag = $images->add($this->id,$data['images']);
- if(!$flag){
- $this->error = $images->error;
- Db::rollback();
- return false;
- }
- }
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- (new CommonHis())->add(3, $id, $real_name,'再次编辑');
- });
- return true;
- }
- /**
- * 关联记录
- * @return \think\model\relation\BelongsTo
- */
- public function his()
- {
- return $this->hasMany('app\common\model\CommonHis','target_id','id')->where('target_type',3)->order('id', 'desc');
- }
- // /**
- // * 删除
- // * @param $id
- // * @return bool|int
- // */
- // public function remove()
- // {
- // return $this->delete();
- // }
- }
|