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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.zhengzai.goblin.service.IGoblinFrontSeckillService;
import com.liquidnet.common.cache.redis.util.RedisGoblinUtil;
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.GoblinFrontSeckill;
import com.liquidnet.service.goblin.mapper.GoblinFrontSeckillMapper;
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.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 秒杀表 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-12-29
 */
@Service
public class GoblinFrontSeckillServiceImpl extends ServiceImpl<GoblinFrontSeckillMapper, GoblinFrontSeckill> implements IGoblinFrontSeckillService {
    @Autowired
    GoblinFrontSeckillMapper goblinFrontSeckillMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisGoblinUtil redisGoblinUtil;

    public boolean create(GoblinFrontSeckill goblinFrontSeckill){
        goblinFrontSeckill.setCreateTime(LocalDateTime.now());
        //设置bannerid
        goblinFrontSeckill.setSeckilId(GoblinRedisConst.FRONT_SECKILL+ IDGenerator.nextTimeId());
        //增加 banner
        goblinFrontSeckillMapper.insert(goblinFrontSeckill);
        return true;
    }
    /***
     * @author zhangfuxin
     * @Description: 修改
     * @date 2021/12/27 下午4:03
     */
    public boolean update(GoblinFrontSeckill goblinFrontSeckill){
        goblinFrontSeckill.setUpdateTime(LocalDateTime.now());
        //数据库修改
        goblinFrontSeckillMapper.updateById(goblinFrontSeckill);
        //修改所有的展示形式
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("activityType", goblinFrontSeckill.getActivityType());
        updateWrapper.set("show_type", goblinFrontSeckill.getShowType());

        goblinFrontSeckillMapper.update(null,updateWrapper);
        return true;
    }
    public GoblinFrontSeckill getOne(Long id){
        return  goblinFrontSeckillMapper.selectById(id);
    }

    public boolean delte(Long id){
        GoblinFrontSeckill goblinFrontSeckill=goblinFrontSeckillMapper.selectById(id);
        goblinFrontSeckill.setDelTag(1);
        this.update(goblinFrontSeckill);
        return true;
    }
    /**
     * @author zhangfuxin
     * @Description:分页
     * @date 2021/12/27 下午5:19
     */
    public PageInfo<GoblinFrontSeckill> page(int pageSize, int pageNumber, GoblinFrontSeckill goblinFrontSeckill) {
        PageInfo<GoblinFrontSeckill> pageInfoTmp = null;
        try {
            PageHelper.startPage(pageNumber, pageSize);
            LambdaQueryWrapper<GoblinFrontSeckill> queryWrapper = Wrappers.lambdaQuery(GoblinFrontSeckill.class);
            queryWrapper.orderByDesc(GoblinFrontSeckill::getCreateTime);
            List<GoblinFrontSeckill> list=goblinFrontSeckillMapper.selectList(queryWrapper);
            pageInfoTmp = new PageInfo(list);
        } catch (Exception e) {
            return new PageInfo();
        }
        return pageInfoTmp;
    }
    /**
     * @author zhangfuxin
     * @Description:上线
     * @date 2021/12/29 下午4:44
     */
    public boolean online(){
        //查询
        LambdaQueryWrapper<GoblinFrontSeckill> queryWrapper = Wrappers.lambdaQuery(GoblinFrontSeckill.class);
        queryWrapper.ne(GoblinFrontSeckill::getDelTag,1);
        queryWrapper.orderByAsc(GoblinFrontSeckill::getIndexs);
        List<GoblinFrontSeckill> list=goblinFrontSeckillMapper.selectList(queryWrapper);
        //mongodb
        for(GoblinFrontSeckill goblinFrontSeckill: list){
            GoblinFrontSeckill goblinFrontNavigation1=mongoTemplate.findOne(Query.query(Criteria.where("seckilId").is(goblinFrontSeckill.getSeckilId())), GoblinFrontSeckill.class, GoblinFrontSeckill.class.getSimpleName());
            if(null==goblinFrontNavigation1){
                mongoTemplate.save(goblinFrontSeckill,GoblinFrontSeckill.class.getSimpleName());
            }else{
                BasicDBObject orderObject = new BasicDBObject("$set", JSON.parse(JsonUtils.toJson(goblinFrontSeckill)));
                mongoTemplate.getCollection(GoblinFrontSeckill.class.getSimpleName()).updateOne(
                        Query.query(Criteria.where("seckilId").is(goblinFrontSeckill.getSeckilId())).getQueryObject(),
                        orderObject
                );
            }
        }
        //redis  根据  秒杀配置， 然后 判断活动是否 有效 然后 拼接 活动
        for(GoblinFrontSeckill goblinFrontSeckill:list){
            //1、关闭模块2、保留模块
            int activityType=goblinFrontSeckill.getActivityType();
            //查看是 1、官方营销表2、商铺营销表
            int marketType =goblinFrontSeckill.getMarketType();
            //找到对应表里的信息
            if(marketType==1){
                //官方营销表

                //找到活动时间
                //找到商品
            }else if(marketType==2){
                //商铺营销表

            }
        }
        //redisGoblinUtil.set(GoblinRedisConst.FRONT_NAVIGATION,list);

        return true;
    }
}
