BookService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/11/21
  6. * Time: 下午3:21
  7. */
  8. namespace app\common\service;
  9. use app\common\library\Redis;
  10. use app\common\model\Book;
  11. use app\common\model\ManageTitle;
  12. class BookService
  13. {
  14. /**
  15. * @var BookService
  16. */
  17. private static $self;
  18. /**
  19. * @return BookService
  20. */
  21. public static function instance()
  22. {
  23. if(self::$self == NULL){
  24. self::$self = new self();
  25. }
  26. return self::$self;
  27. }
  28. /**
  29. * @return Book
  30. */
  31. public function getBookModel()
  32. {
  33. return model('Book');
  34. }
  35. /**
  36. * @return ManageTitle
  37. */
  38. public function getManageTitleModel()
  39. {
  40. return model('ManageTitle');
  41. }
  42. /**
  43. * 批量更新书籍信息
  44. * @param $aUpdate
  45. * @param $bookIds
  46. * @return array
  47. */
  48. public function updateManyBookInfo($aUpdate, $bookIds)
  49. {
  50. try{
  51. $data = [
  52. 'success' => 0,
  53. 'invalid' => [],
  54. ];
  55. $ids = $this->getBookModel()
  56. ->whereIn('id', $bookIds)
  57. ->column('id');
  58. $data['invalid'] = array_diff($bookIds, $ids);
  59. if ($ids) {
  60. $list = $this->getBookModel()
  61. ->whereIn('id', $bookIds)->select();
  62. foreach ($list as $item) {
  63. $item->save($aUpdate);
  64. $redisKey = 'B:'. $item['id'];
  65. Redis::instance()->del($redisKey);
  66. }
  67. $data['success']++;
  68. }
  69. $msg = '';
  70. if ($data['invalid']) {
  71. $msg = '。无效书籍ID::' . implode(',', $data['invalid']);
  72. }
  73. if ($data['success'] > 0) {
  74. return [
  75. 'code' => 0,
  76. 'msg' => '更新成功' . $msg,
  77. ];
  78. } else {
  79. return [
  80. 'code' => 1,
  81. 'msg' => '更新失败' . $msg,
  82. ];
  83. }
  84. } catch (\Exception $e) {
  85. return [
  86. 'code' => $e->getCode(),
  87. 'msg' => $e->getMessage(),
  88. ];
  89. }
  90. }
  91. /**
  92. * 批量添加标题
  93. * @param $titles
  94. * @param $status
  95. * @param $sex
  96. * @return array
  97. */
  98. public function batchAddTitle($titles, $status, $sex)
  99. {
  100. try {
  101. $inserts = [];
  102. foreach ($titles as $title) {
  103. $inserts[] = [
  104. 'status' => $status,
  105. 'title' => $title,
  106. 'sex' => $sex,
  107. ];
  108. }
  109. $data = $this->getManageTitleModel()->saveAll($inserts);
  110. if ($data) {
  111. return [
  112. 'code' => 0,
  113. 'msg' => '添加成功',
  114. ];
  115. } else {
  116. return [
  117. 'code' => 1,
  118. 'msg' => '添加失败',
  119. ];
  120. }
  121. } catch (\Exception $e) {
  122. return [
  123. 'code' => $e->getCode(),
  124. 'msg' => $e->getMessage(),
  125. ];
  126. }
  127. }
  128. }