123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/11/21
- * Time: 下午3:21
- */
- namespace app\common\service;
- use app\common\library\Redis;
- use app\common\model\Book;
- use app\common\model\ManageTitle;
- class BookService
- {
- /**
- * @var BookService
- */
- private static $self;
- /**
- * @return BookService
- */
- public static function instance()
- {
- if(self::$self == NULL){
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return Book
- */
- public function getBookModel()
- {
- return model('Book');
- }
- /**
- * @return ManageTitle
- */
- public function getManageTitleModel()
- {
- return model('ManageTitle');
- }
- /**
- * 批量更新书籍信息
- * @param $aUpdate
- * @param $bookIds
- * @return array
- */
- public function updateManyBookInfo($aUpdate, $bookIds)
- {
- try{
- $data = [
- 'success' => 0,
- 'invalid' => [],
- ];
- $ids = $this->getBookModel()
- ->whereIn('id', $bookIds)
- ->column('id');
- $data['invalid'] = array_diff($bookIds, $ids);
- if ($ids) {
- $list = $this->getBookModel()
- ->whereIn('id', $bookIds)->select();
- foreach ($list as $item) {
- $item->save($aUpdate);
- $redisKey = 'B:'. $item['id'];
- Redis::instance()->del($redisKey);
- }
- $data['success']++;
- }
- $msg = '';
- if ($data['invalid']) {
- $msg = '。无效书籍ID::' . implode(',', $data['invalid']);
- }
- if ($data['success'] > 0) {
- return [
- 'code' => 0,
- 'msg' => '更新成功' . $msg,
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '更新失败' . $msg,
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode(),
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 批量添加标题
- * @param $titles
- * @param $status
- * @param $sex
- * @return array
- */
- public function batchAddTitle($titles, $status, $sex)
- {
- try {
- $inserts = [];
- foreach ($titles as $title) {
- $inserts[] = [
- 'status' => $status,
- 'title' => $title,
- 'sex' => $sex,
- ];
- }
- $data = $this->getManageTitleModel()->saveAll($inserts);
- if ($data) {
- return [
- 'code' => 0,
- 'msg' => '添加成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '添加失败',
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode(),
- 'msg' => $e->getMessage(),
- ];
- }
- }
- }
|