Gift.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
  4. */
  5. /**
  6. * Created by PhpStorm.
  7. * User: Administrator
  8. * Date: 2018/1/23
  9. * Time: 16:22
  10. */
  11. namespace app\common\model;
  12. use think\Model;
  13. //use traits\model\SoftDelete;
  14. class Gift extends Model
  15. {
  16. //use SoftDelete;
  17. protected $pk = 'id';
  18. protected $table = 'cy_libaoinfo';
  19. protected $updateTime = false;
  20. protected $autoWriteTimestamp = true;
  21. protected $dateFormat = true;
  22. //protected $deleteTime = 'isdelete';
  23. public function giftCode()
  24. {
  25. return $this->hasMany('Giftcode', 'infoid');
  26. }
  27. /**
  28. * 获取列表内容
  29. * @param array $condition
  30. * @return \think\Paginator
  31. * @throws \think\exception\DbException
  32. */
  33. public function getList($condition = [], $order = 'cy_libaoinfo.id desc')
  34. {
  35. $gameid = $condition['gameid'];
  36. unset($condition['gameid']);
  37. if (isset($condition['title']) && !empty($condition['title'])) {
  38. $condition['cy_libaoinfo.title'] = ['like', "%{$condition['title']}%"];
  39. unset($condition['title']);
  40. }
  41. $tmpGameList = model('Common/Game')->getAllByCondition('id,name');
  42. $gameList = array();
  43. foreach ($tmpGameList as $game) {
  44. $gameList[$game['id']] = $game;
  45. }
  46. return self::view('cy_libaoinfo', 'id,title,gameid,starttime,endtime,consume,is_top,update_time,create_time as createtime,total,used')
  47. ->where($condition)
  48. ->where(function($query) use ($gameid){
  49. if (isset($gameid) && !empty($gameid)) {
  50. $query->whereRaw(sprintf('FIND_IN_SET(%s,gameid)', $gameid));
  51. }
  52. })
  53. ->order($order)->paginate()->each(function ($item, $key) use ($gameList) {
  54. $game_ids = explode(',', $item['gameid']);
  55. $tmp = [];
  56. foreach ($game_ids as $k => $v) {
  57. $tmp[] = $gameList[$v]['name'];
  58. }
  59. $item['name'] = implode('<br>', $tmp);
  60. return $item;
  61. });
  62. }
  63. /**
  64. * 新增礼包数据和礼包序列号数据
  65. * @mark 优化为批量插入并且增加事务
  66. * @param $data
  67. * @return bool
  68. * @throws \think\exception\PDOException
  69. */
  70. public function add($data)
  71. {
  72. if (empty($data)) {
  73. return false;
  74. }
  75. self::startTrans();
  76. try {
  77. $this->allowField(true)->save($data);
  78. $codes = [];
  79. foreach ($data['code'] as $v) {
  80. $codes[] = ['code' => $v];
  81. }
  82. $this->find($this->getLastInsID())->giftCode()->saveAll($codes);
  83. self::commit();
  84. } catch (\Exception $e) {
  85. self::rollback();
  86. $this->error = $e->getMessage();
  87. return false;
  88. }
  89. return true;
  90. }
  91. protected function getStarttimeAttr($value)
  92. {
  93. return date('Y-m-d', $value);
  94. }
  95. protected function getEndtimeAttr($value)
  96. {
  97. return date('Y-m-d', $value);
  98. }
  99. }