package com.liquidnet.client.admin.zhengzai.goblin.service.imp;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.zhengzai.goblin.service.IGoblinFrontNavigationService;
import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.goblin.constant.GoblinRedisConst;
import com.liquidnet.service.goblin.entity.GoblinFrontBanner;
import com.liquidnet.service.goblin.entity.GoblinFrontNavigation;
import com.liquidnet.service.goblin.mapper.GoblinFrontBannerMapper;
import com.liquidnet.service.goblin.mapper.GoblinFrontNavigationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mongodb.BasicDBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 首页导航栏
 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-12-28
 */
@Service
public class GoblinFrontNavigationServiceImpl extends ServiceImpl<GoblinFrontNavigationMapper, GoblinFrontNavigation> implements IGoblinFrontNavigationService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private GoblinFrontNavigationMapper goblinFrontNavigationMapper;
    @Autowired
    private RedisDataSourceUtil redisDataSourceUtil;

    public boolean create(GoblinFrontNavigation goblinFrontNavigation){
        //设置金刚位id
        goblinFrontNavigation.setNavigationId(GoblinRedisConst.FRONT_NAVIGATION+ IDGenerator.nextTimeId());
        //mysql插入
        goblinFrontNavigationMapper.insert(goblinFrontNavigation);
        //mongo  db增加
        mongoTemplate.save(goblinFrontNavigation,GoblinFrontNavigation.class.getSimpleName());
        //redis增加
        if(redisDataSourceUtil.getRedisGoblinUtil().hasKey(GoblinRedisConst.FRONT_NAVIGATION)){
            //从redis里面取出
            ArrayList<GoblinFrontNavigation> list = (ArrayList<GoblinFrontNavigation>) redisDataSourceUtil.getRedisGoblinUtil().get(GoblinRedisConst.FRONT_BANNER);
            list.add(goblinFrontNavigation);
            redisDataSourceUtil.getRedisGoblinUtil().set(GoblinRedisConst.FRONT_NAVIGATION,list);
        }else{
            //创建一个list
            ArrayList<Object> list= CollectionUtil.arrayListObject();
            list.add(goblinFrontNavigation);
            //redis 没有这个键
            redisDataSourceUtil.getRedisGoblinUtil().set(GoblinRedisConst.FRONT_NAVIGATION,list);
        }
        return  true;
    }
    public boolean update(GoblinFrontNavigation goblinFrontNavigation){
        //数据库修改
        goblinFrontNavigationMapper.updateById(goblinFrontNavigation);
        //mongodb修改
        BasicDBObject orderObject = new BasicDBObject("$set", JSON.parse(JsonUtils.toJson(goblinFrontNavigation)));
        mongoTemplate.getCollection(GoblinFrontNavigation.class.getSimpleName()).updateOne(
                Query.query(Criteria.where("navigationId").is(goblinFrontNavigation.getNavigationId())).getQueryObject(),
                orderObject
        );
        //redis 修改
          //从数据库中查出来，从新塞进去
        LambdaQueryWrapper<GoblinFrontNavigation> queryWrapper = Wrappers.lambdaQuery(GoblinFrontNavigation.class);
        queryWrapper.ne(GoblinFrontNavigation::getDelTag,1);
        queryWrapper.orderByDesc(GoblinFrontNavigation::getIndexs);
        List<GoblinFrontNavigation> list=goblinFrontNavigationMapper.selectList(queryWrapper);
        redisDataSourceUtil.getRedisGoblinUtil().set(GoblinRedisConst.FRONT_NAVIGATION,list);
        return true;
    }
    public GoblinFrontNavigation getOne(Long id){
        return  goblinFrontNavigationMapper.selectById(id);
    }
    public boolean delete(Long id){
        GoblinFrontNavigation goblinFrontNavigation=goblinFrontNavigationMapper.selectById(id);
        goblinFrontNavigation.setDelTag(1);
        this.update(goblinFrontNavigation);
        return true;
    }
    public PageInfo<GoblinFrontNavigation> page(int pageSize, int pageNumber, GoblinFrontNavigation goblinFrontNavigation) {
        PageInfo<GoblinFrontNavigation> pageInfoTmp = null;
        try {
            PageHelper.startPage(pageNumber, pageSize);
            LambdaQueryWrapper<GoblinFrontNavigation> queryWrapper = Wrappers.lambdaQuery(GoblinFrontNavigation.class);
            queryWrapper.orderByDesc(GoblinFrontNavigation::getCreateTime);
            List<GoblinFrontNavigation> list=goblinFrontNavigationMapper.selectList(queryWrapper);
            pageInfoTmp = new PageInfo(list);
        } catch (Exception e) {
            return new PageInfo();
        }
        return pageInfoTmp;
    }

}
