123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- declare (strict_types=1);
- namespace app\common\model;
- use app\common\library\helper;
- use app\common\model\ActivityDiscountGoods as ActivityDiscountGoodsModel;
- use app\common\model\Goods;
- use app\common\model\GoodsSku;
- use app\common\model\CommonHis as CommonHisModel;
- use app\common\service\store\User as StoreUserService;
- /**
- * 活动中心 ▸ N件X折
- * Class Ad
- * @package app\common\model
- */
- class ActivityDiscount extends BaseModel
- {
-
-
- protected $name = 'activity_discount';
- protected $append = ['status'];
- /**
- * 一对多关联商品表
- */
- public function goods(){
- return $this->hasMany('ActivityDiscountGoods', 'activity_id')->order('id', 'desc');
- }
- /**
- * 获取器:阶梯属性
- * @param $value
- * @return array
- */
- public function getDiscountPropsAttr($value)
- {
- return helper::jsonDecode($value);
- }
- /**
- * 设置器:阶梯属性
- * @param $value
- * @return string
- */
- public function setDiscountPropsAttr($value)
- {
- return $value ? helper::jsonEncode($value) : '';
- }
-
- /**
- * 获取列表
- * @param array $param
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList($param = [])
- {
- // 检索查询调价你
- $filter = $this->getFilter($param);
- // 排序条件
- // $sort = $this->setQuerySort($param);
- $sort = ['id'=>'desc'];
- // 查询列表数据
- return $this->where($filter)->with([])->order($sort)->paginate(15);
- }
- //当前的进行中的活动id
- public function getActivityGoodIdArray($activity_id =0){
- $filter[] = ['start_time','<=',date('Y-m-d H:i:s')];
- $filter[] = ['end_time','>=',date('Y-m-d H:i:s')];
-
- // $filter[] = ['audit_status_zg','=',10];
- // $filter[] = ['is_up','=',1];
- $id_array = $this->where($filter)->column("id");
- if($activity_id>0){
- $id_array = array_diff($id_array,[$activity_id]);
- }
- $goods_id_array = ActivityDiscountGoodsModel::where("activity_id","in",$id_array)->column("goods_id");
- return $goods_id_array;
- }
- /**
- * 检索查询条件
- * @param array $param
- * @return array
- */
- private function getFilter($param = [])
- {
- // 默认查询条件
- $params = $this->setQueryDefaultValue($param, ['name' => '','audit_status_zg'=>-1,'status'=>-1]);
- // 检索查询条件
- $filter = [];
- !empty($params['name']) && $filter[] = ['name', 'like', "%{$params['name']}%"];
- if(isset($params['audit_status_zg']) && $params['audit_status_zg']>-1){
- $filter[] = ['audit_status_zg', '=', $params['audit_status_zg']];
- }
- if (!empty($params['betweenTime'])) {
- $times = between_time($params['betweenTime']);
- $filter[] = ['start_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
- $filter[] = ['end_time', '<', Date('Y-m-d H:i:s',$times['end_time'] + 86400)];
- }
- //活动状态
- if(isset($params['status'])&&$params['status']>-1){
- switch ($params['status']){
- case 1://进行中
- $filter[] = ['start_time','<=',date('Y-m-d H:i:s')];
- $filter[] = ['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[] = ['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','=',0];
- $filter[] = ['audit_status_zg','=',10];
- break;
- }
- }
- return $filter;
- }
- //活动状态 即将开始或已结束
- public function getStatusAttr() {
- if($this->audit_status_zg!=10){
- return 0;
- }
- if($this->is_up==0){
- return 3;
- }
- $now = date("Y-m-d H:i:s",time());
- if ($now < $this->start_time) {
- return 2;
- }
- if ($now > $this->end_time) {
- return 3;
- }
- if($this->is_up==1){
- return 1;
- }else{
- return 2;
- }
-
- }
- /**
- * 审核
- * @param array $data
- * @return bool
- */
- public function audit(array $data): bool
- {
-
- $this->transaction(function () use ($data) {
- $id = (int)$this['id'];
- $this->save($data);
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- if($data['audit_status_zg']==10){
- (new CommonHisModel())->add(1, $id, $real_name,'同意并发布活动');
- }
- if($data['audit_status_zg']==20){
- $remark = '不同意'." ".$data['refuse_desc_zg'];
- (new CommonHisModel())->add(1, $id, $real_name,$remark);
- }
-
- });
- return true;
- }
- /**
- * 作废
- * @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;
- }
- /**
- * 详情
- * @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)
- {
- 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 ActivityDiscountGoodsModel;
- $goods->add($this->id,$data['goods']);
- }
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- (new CommonHisModel())->add(1, $id, $real_name,'提交审核');
- });
- return true;
-
- }
- /**
- * 编辑
- * @param $data
- * @return bool|int
- */
- public function edit($data)
- {
- 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 ActivityDiscountGoodsModel;
- $goods->add($this->id,$data['goods']);
- }
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- (new CommonHisModel())->add(1, $id, $real_name,'提交审核');
- });
- return true;
- }
- /**
- * 关联记录
- * @return \think\model\relation\BelongsTo
- */
- public function his()
- {
- return $this->hasMany('CommonHis','target_id','id')->where('target_type',1)->order('id', 'desc');
- }
- // /**
- // * 删除
- // * @param $id
- // * @return bool|int
- // */
- // public function remove()
- // {
- // return $this->delete();
- // }
- }
|