GiftModel.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * GiftModel.php UTF-8
  4. * 礼包类
  5. *
  6. * @date : 2017/11/18 16:54
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\game;
  13. use huo\controller\gift\GiftCache;
  14. use huo\model\common\CommonModel;
  15. class GiftModel extends CommonModel {
  16. protected $name = 'gift';
  17. //设置只读字段
  18. protected $readonly = ['app_id', 'create_time'];
  19. // 开启自动写入时间戳字段
  20. protected $autoWriteTimestamp = true;
  21. //类型转换
  22. protected $type
  23. = [
  24. 'app_id' => 'integer',
  25. ];
  26. /**
  27. * 基础查询
  28. *
  29. * @param $query
  30. */
  31. protected function base($query) {
  32. $query->where('is_delete', 2);
  33. }
  34. /**
  35. * 关联game表
  36. *
  37. * @return mixed
  38. */
  39. public function game() {
  40. return $this->belongsTo('huo\model\game\GameModel', 'app_id')->field('id,name game_name,icon game_icon');
  41. }
  42. /**
  43. * @return mixed
  44. */
  45. public function code() {
  46. return $this->hasMany('huo\model\game\GiftcodeModel', 'gift_id');
  47. }
  48. /**
  49. * start_time 自动转化
  50. *
  51. * @param $value
  52. *
  53. * @return int
  54. */
  55. public function setStartTimeAttr($value) {
  56. if (is_numeric($value)) {
  57. return $value;
  58. }
  59. return strtotime($value);
  60. }
  61. /**
  62. * end_time 自动转化
  63. *
  64. * @param $value
  65. *
  66. * @return int
  67. */
  68. public function setEndTimeAttr($value) {
  69. if (is_numeric($value)) {
  70. return $value;
  71. }
  72. return strtotime($value);
  73. }
  74. /**
  75. * dead_time 自动转化
  76. *
  77. * @param $value
  78. *
  79. * @return int
  80. */
  81. public function setDeadTimeAttr($value) {
  82. if (is_numeric($value)) {
  83. return $value;
  84. }
  85. return strtotime($value);
  86. }
  87. /**
  88. * 更新礼包
  89. *
  90. * @param array $gift_data
  91. * @param string $gift_id
  92. *
  93. * @return bool
  94. * @throws \think\Exception
  95. */
  96. public function updateGift($gift_data, $gift_id) {
  97. $_map['id'] = $gift_id;
  98. $_data = $gift_data;
  99. if (isset($_data['code_more']) && !empty($_data['code_more'])) {
  100. $_code_arr = explode("\r\n", $_data['code_more']);
  101. $_code_arr = array_unique($_code_arr); //数组去重
  102. $_code_arr = array_filter($_code_arr); //数组去空字符
  103. // 添加数量
  104. $_code_num = count($_code_arr);
  105. }
  106. $_rs = self::update($_data, $_map, true);
  107. if (false == $_rs) {
  108. return false;
  109. } else {
  110. if (isset($_code_arr) && !empty($_code_arr)) {
  111. $_create_time = time();
  112. foreach ($_code_arr as $_k => $_v) {
  113. $_gc_data[$_k]['code'] = trim($_v);
  114. $_gc_data[$_k]['gift_id'] = $gift_id;
  115. $_gc_data[$_k]['create_time'] = $_create_time;
  116. }
  117. }
  118. if (isset($_gc_data) && !empty($_gc_data)) {
  119. $_gc_res = (new GiftcodeModel())->insertAll($_gc_data);
  120. if ($_gc_res) {
  121. self::where($_map)->setInc('total_cnt', $_code_num);
  122. self::where($_map)->setInc('remain_cnt', $_code_num);
  123. }
  124. }
  125. return true;
  126. }
  127. }
  128. /**
  129. * 后台获取礼包列表
  130. *
  131. * @param array $_param
  132. * @param int $_offset
  133. *
  134. * @return \think\Paginator
  135. * @throws \think\exception\DbException
  136. */
  137. public function getGiftList($_param = [], $_offset = 10) {
  138. $_offset = isset($_param['list_rows']) ? $_param['list_rows'] : $_offset;
  139. $_map = ['is_delete' => 2];
  140. if (isset($_param['app_id']) && !empty($_param['app_id'])) {
  141. $_map['app_id'] = $_param['app_id'];
  142. }
  143. if (isset($_param['qq_id']) && !empty($_param['qq_id'])) {
  144. $_map['qq_id'] = $_param['qq_id'];
  145. }
  146. if (isset($_param['title']) && !empty($_param['title'])) {
  147. $_title = $_param['title'];
  148. $_map['title'] = array('like', "%$_title%");
  149. }
  150. $_field = 'id, title, total_cnt, remain_cnt, app_id';
  151. $_field .= ', start_time , end_time, condition, qq_id, need_vip, is_hot, is_luxury, is_rmd, create_time';
  152. $_data = $this
  153. ->field($_field)
  154. ->where($_map)
  155. ->order('id desc')
  156. ->paginate($_offset);
  157. return $_data;
  158. }
  159. /**
  160. * 添加礼包
  161. *
  162. * @param array $_param
  163. *
  164. * @return bool|int|string
  165. */
  166. public function addNewGift($_param = []) {
  167. if (isset($_param['title']) && !empty($_param['title'])) {
  168. $_g_data['title'] = $_param['title'];
  169. } else {
  170. return false;
  171. }
  172. $_g_data['app_id'] = $_param['app_id'];
  173. $_g_data['content'] = $_param['content'];
  174. $_g_data['func'] = $_param['func'];
  175. $_g_data['scope'] = get_val($_param, 'scope', '');
  176. $_g_data['start_time'] = strtotime($_param['start_time']);
  177. $_g_data['end_time'] = strtotime($_param['end_time']);
  178. $_g_data['condition'] = get_val($_param, 'condition', '0');
  179. $_g_data['qq_id'] = get_val($_param, 'qq_id', '0');
  180. //$_g_data['need_vip'] = $_param['need_vip'];
  181. $_g_data['is_hot'] = get_val($_param, 'is_hot', '1');
  182. $_g_data['is_luxury'] = get_val($_param, 'is_luxury', '1');
  183. $_g_data['is_rmd'] = get_val($_param, 'is_rmd', '1');
  184. $_g_data['hits_cnt'] = get_val($_param, 'hits_cnt', '');
  185. $_g_data['create_time'] = time();
  186. if (isset($_param['code']) && !empty($_param['code'])) {
  187. $_code_arr = explode("\r\n", $_param['code']);
  188. $_code_arr = array_unique($_code_arr); //数组去重
  189. $_code_arr = array_filter($_code_arr); //数组去空字符
  190. // 添加总数
  191. $_code_num = count($_code_arr);
  192. $_g_data['total_cnt'] = $_code_num;
  193. $_g_data['remain_cnt'] = $_code_num;
  194. }
  195. $_g_id = $this->insertGetId($_g_data);
  196. if ($_g_id) {
  197. (new GameextModel())->where('app_id', $_g_data['app_id'])->setInc('gift_cnt');
  198. (new GameextModel())->where('app_id', $_g_data['app_id'])->setInc('real_gift_cnt');
  199. }
  200. if ($_g_id && isset($_code_arr) && !empty($_code_arr)) {
  201. $_create_time = time();
  202. foreach ($_code_arr as $_k => $_v) {
  203. $_gc_data[$_k]['code'] = trim($_v);
  204. $_gc_data[$_k]['gift_id'] = $_g_id;
  205. $_gc_data[$_k]['create_time'] = $_create_time;
  206. }
  207. }
  208. if (isset($_gc_data) && !empty($_gc_data)) {
  209. $_gc_res = (new GiftcodeModel())->insertAll($_gc_data);
  210. return $_g_id && $_gc_res;
  211. }
  212. return $_g_id;
  213. }
  214. /**
  215. * 修改礼包的状态
  216. *
  217. * @param array $_param
  218. *
  219. * @return bool|int
  220. */
  221. public function editStatus($_param = []) {
  222. $_map = [];
  223. if (isset($_param['ids']) && !empty($_param['ids'])) {
  224. $_map['id'] = array('in', $_param['ids']);
  225. } else {
  226. $_map['id'] = $_param['id'];
  227. }
  228. if (isset($_param['need_vip']) && !empty($_param['need_vip'])) {
  229. $_name = 'need_vip';
  230. }
  231. if (isset($_param['is_hot']) && !empty($_param['is_hot'])) {
  232. $_name = 'is_hot';
  233. }
  234. if (isset($_param['is_luxury']) && !empty($_param['is_luxury'])) {
  235. $_name = 'is_luxury';
  236. }
  237. if (isset($_param['is_rmd']) && !empty($_param['is_rmd'])) {
  238. $_name = 'is_rmd';
  239. }
  240. switch ($_name) {
  241. case 'need_vip' :
  242. $_data['need_vip'] = $_param['need_vip'];
  243. $_data['update_time'] = time();
  244. break;
  245. case 'is_hot' :
  246. $_data['is_hot'] = $_param['is_hot'];
  247. $_data['update_time'] = time();
  248. break;
  249. case 'is_luxury' :
  250. $_data['is_luxury'] = $_param['is_luxury'];
  251. $_data['update_time'] = time();
  252. break;
  253. case 'is_rmd' :
  254. $_data['is_rmd'] = $_param['is_rmd'];
  255. $_data['update_time'] = time();
  256. break;
  257. default :
  258. break;
  259. }
  260. //$_res = $this->where($_map)->setField($_data);
  261. foreach ($_param['ids'] as $v) {
  262. $_res = GiftCache::ins()->updateGift($v, $_data);
  263. }
  264. return $_res;
  265. }
  266. }