* @version : HUOSDK 8.0 */ namespace huo\model\game; use huo\controller\gift\GiftCache; use huo\model\common\CommonModel; class GiftModel extends CommonModel { protected $name = 'gift'; //设置只读字段 protected $readonly = ['app_id', 'create_time']; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; //类型转换 protected $type = [ 'app_id' => 'integer', ]; /** * 基础查询 * * @param $query */ protected function base($query) { $query->where('is_delete', 2); } /** * 关联game表 * * @return mixed */ public function game() { return $this->belongsTo('huo\model\game\GameModel', 'app_id')->field('id,name game_name,icon game_icon'); } /** * @return mixed */ public function code() { return $this->hasMany('huo\model\game\GiftcodeModel', 'gift_id'); } /** * start_time 自动转化 * * @param $value * * @return int */ public function setStartTimeAttr($value) { if (is_numeric($value)) { return $value; } return strtotime($value); } /** * end_time 自动转化 * * @param $value * * @return int */ public function setEndTimeAttr($value) { if (is_numeric($value)) { return $value; } return strtotime($value); } /** * dead_time 自动转化 * * @param $value * * @return int */ public function setDeadTimeAttr($value) { if (is_numeric($value)) { return $value; } return strtotime($value); } /** * 更新礼包 * * @param array $gift_data * @param string $gift_id * * @return bool * @throws \think\Exception */ public function updateGift($gift_data, $gift_id) { $_map['id'] = $gift_id; $_data = $gift_data; if (isset($_data['code_more']) && !empty($_data['code_more'])) { $_code_arr = explode("\r\n", $_data['code_more']); $_code_arr = array_unique($_code_arr); //数组去重 $_code_arr = array_filter($_code_arr); //数组去空字符 // 添加数量 $_code_num = count($_code_arr); } $_rs = self::update($_data, $_map, true); if (false == $_rs) { return false; } else { if (isset($_code_arr) && !empty($_code_arr)) { $_create_time = time(); foreach ($_code_arr as $_k => $_v) { $_gc_data[$_k]['code'] = trim($_v); $_gc_data[$_k]['gift_id'] = $gift_id; $_gc_data[$_k]['create_time'] = $_create_time; } } if (isset($_gc_data) && !empty($_gc_data)) { $_gc_res = (new GiftcodeModel())->insertAll($_gc_data); if ($_gc_res) { self::where($_map)->setInc('total_cnt', $_code_num); self::where($_map)->setInc('remain_cnt', $_code_num); } } return true; } } /** * 后台获取礼包列表 * * @param array $_param * @param int $_offset * * @return \think\Paginator * @throws \think\exception\DbException */ public function getGiftList($_param = [], $_offset = 10) { $_offset = isset($_param['list_rows']) ? $_param['list_rows'] : $_offset; $_map = ['is_delete' => 2]; if (isset($_param['app_id']) && !empty($_param['app_id'])) { $_map['app_id'] = $_param['app_id']; } if (isset($_param['qq_id']) && !empty($_param['qq_id'])) { $_map['qq_id'] = $_param['qq_id']; } if (isset($_param['title']) && !empty($_param['title'])) { $_title = $_param['title']; $_map['title'] = array('like', "%$_title%"); } $_field = 'id, title, total_cnt, remain_cnt, app_id'; $_field .= ', start_time , end_time, condition, qq_id, need_vip, is_hot, is_luxury, is_rmd, create_time'; $_data = $this ->field($_field) ->where($_map) ->order('id desc') ->paginate($_offset); return $_data; } /** * 添加礼包 * * @param array $_param * * @return bool|int|string */ public function addNewGift($_param = []) { if (isset($_param['title']) && !empty($_param['title'])) { $_g_data['title'] = $_param['title']; } else { return false; } $_g_data['app_id'] = $_param['app_id']; $_g_data['content'] = $_param['content']; $_g_data['func'] = $_param['func']; $_g_data['scope'] = get_val($_param, 'scope', ''); $_g_data['start_time'] = strtotime($_param['start_time']); $_g_data['end_time'] = strtotime($_param['end_time']); $_g_data['condition'] = get_val($_param, 'condition', '0'); $_g_data['qq_id'] = get_val($_param, 'qq_id', '0'); //$_g_data['need_vip'] = $_param['need_vip']; $_g_data['is_hot'] = get_val($_param, 'is_hot', '1'); $_g_data['is_luxury'] = get_val($_param, 'is_luxury', '1'); $_g_data['is_rmd'] = get_val($_param, 'is_rmd', '1'); $_g_data['hits_cnt'] = get_val($_param, 'hits_cnt', ''); $_g_data['create_time'] = time(); if (isset($_param['code']) && !empty($_param['code'])) { $_code_arr = explode("\r\n", $_param['code']); $_code_arr = array_unique($_code_arr); //数组去重 $_code_arr = array_filter($_code_arr); //数组去空字符 // 添加总数 $_code_num = count($_code_arr); $_g_data['total_cnt'] = $_code_num; $_g_data['remain_cnt'] = $_code_num; } $_g_id = $this->insertGetId($_g_data); if ($_g_id) { (new GameextModel())->where('app_id', $_g_data['app_id'])->setInc('gift_cnt'); (new GameextModel())->where('app_id', $_g_data['app_id'])->setInc('real_gift_cnt'); } if ($_g_id && isset($_code_arr) && !empty($_code_arr)) { $_create_time = time(); foreach ($_code_arr as $_k => $_v) { $_gc_data[$_k]['code'] = trim($_v); $_gc_data[$_k]['gift_id'] = $_g_id; $_gc_data[$_k]['create_time'] = $_create_time; } } if (isset($_gc_data) && !empty($_gc_data)) { $_gc_res = (new GiftcodeModel())->insertAll($_gc_data); return $_g_id && $_gc_res; } return $_g_id; } /** * 修改礼包的状态 * * @param array $_param * * @return bool|int */ public function editStatus($_param = []) { $_map = []; if (isset($_param['ids']) && !empty($_param['ids'])) { $_map['id'] = array('in', $_param['ids']); } else { $_map['id'] = $_param['id']; } if (isset($_param['need_vip']) && !empty($_param['need_vip'])) { $_name = 'need_vip'; } if (isset($_param['is_hot']) && !empty($_param['is_hot'])) { $_name = 'is_hot'; } if (isset($_param['is_luxury']) && !empty($_param['is_luxury'])) { $_name = 'is_luxury'; } if (isset($_param['is_rmd']) && !empty($_param['is_rmd'])) { $_name = 'is_rmd'; } switch ($_name) { case 'need_vip' : $_data['need_vip'] = $_param['need_vip']; $_data['update_time'] = time(); break; case 'is_hot' : $_data['is_hot'] = $_param['is_hot']; $_data['update_time'] = time(); break; case 'is_luxury' : $_data['is_luxury'] = $_param['is_luxury']; $_data['update_time'] = time(); break; case 'is_rmd' : $_data['is_rmd'] = $_param['is_rmd']; $_data['update_time'] = time(); break; default : break; } //$_res = $this->where($_map)->setField($_data); foreach ($_param['ids'] as $v) { $_res = GiftCache::ins()->updateGift($v, $_data); } return $_res; } }