package com.liquidnet.service.goblin.service.impl;

import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.base.PagedResult;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.base.SqlMapping;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.goblin.dto.manage.GoblinGoodsAnticipateAddParam;
import com.liquidnet.service.goblin.dto.manage.GoblinGoodsAnticipateParam;
import com.liquidnet.service.goblin.dto.manage.GoblinGoodsAnticipateUpdateParam;
import com.liquidnet.service.goblin.dto.manage.vo.GoblinGoodsAnticipateValueVo;
import com.liquidnet.service.goblin.dto.vo.GoblinGoodsAnticipateVo;
import com.liquidnet.service.goblin.service.IGoblinGoodsAnticipateService;
import com.liquidnet.service.goblin.util.GoblinMongoUtils;
import com.liquidnet.service.goblin.util.GoblinRedisUtils;
import com.liquidnet.service.goblin.util.QueueUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 预约表 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2022-04-07
 */
@Service
public class GoblinGoodsAnticipateServiceImpl implements IGoblinGoodsAnticipateService {

    @Autowired
    GoblinMongoUtils goblinMongoUtils;

    @Autowired
    GoblinRedisUtils goblinRedisUtils;

    @Autowired
    QueueUtils queueUtils;

    @Override
    public ResponseDto<List<GoblinGoodsAnticipateVo>> list(GoblinGoodsAnticipateParam goodsAnticipateParam) {
        List<GoblinGoodsAnticipateVo> goblinGoodsAnticipateVos = goblinMongoUtils.getGoblinGoodsAnticipateVos(goodsAnticipateParam);

        return ResponseDto.success(goblinGoodsAnticipateVos);
    }

    @Override
    public ResponseDto<Object> add(GoblinGoodsAnticipateAddParam goodsAnticipateAddParam) {
        if (StringUtils.isNotBlank(goodsAnticipateAddParam.getName()) && StringUtils.isNotBlank(goodsAnticipateAddParam.getRule()) && StringUtils.isNotBlank(goodsAnticipateAddParam.getAboutStartDate().toString()) && StringUtils.isNotBlank(goodsAnticipateAddParam.getAboutEndDate().toString()) && goodsAnticipateAddParam.getGoodsAnticipateValues() == null) {
            List<GoblinGoodsAnticipateValueVo> goodsAnticipateValues = goodsAnticipateAddParam.getGoodsAnticipateValues();

            //生成的antId
            String antId = IDGenerator.nextTimeId2();
            GoblinGoodsAnticipateVo goodsAnticipateVo = new GoblinGoodsAnticipateVo();
            BeanUtils.copyProperties(goodsAnticipateAddParam, goodsAnticipateVo);
            goodsAnticipateVo.setAntId(antId);

            //redis消息预约消息表
            LinkedList<Object[]> sqlAnt = CollectionUtil.linkedListObjectArr();
            sqlAnt.add(new Object[]{
                    antId,
                    goodsAnticipateVo.getName(),
                    goodsAnticipateVo.getRule(),
                    goodsAnticipateVo.getAboutStartDate(),
                    goodsAnticipateVo.getAboutEndDate()
            });
            sendRedis(MQConst.GoblinQueue.GOBLIN_STORE_MARKET.getKey(), "goblin_goods_anticipate", sqlAnt);

            //redis消息
            goodsAnticipateValues.forEach(goodsAnticipateValueVo -> {
                LinkedList<Object[]> sqlValue = CollectionUtil.linkedListObjectArr();
                sqlValue.add(new Object[]{
                        antId,
                        goodsAnticipateValueVo.getSkuName(),
                        goodsAnticipateValueVo.getSkuId(),
                        goodsAnticipateValueVo.getSpuId(),
                        goodsAnticipateValueVo.getAboutStartDate(),
                        goodsAnticipateValueVo.getAboutEndDate(),
                });
                sendRedis(MQConst.GoblinQueue.GOBLIN_STORE_MARKET.getKey(), "goblin_goods_anticipate_value", sqlValue);
            });

            //更新mongodb
            goblinMongoUtils.setGoblinGoodsAnticipateVo(goodsAnticipateVo);
            return ResponseDto.success();
        }
        return ResponseDto.failure("参数有误");
    }

    public void sendRedis(String rediskey, String sqlKey, LinkedList<Object[]> sqlData) {
        LinkedList<String> sql = CollectionUtil.linkedListString();
        sql.add(SqlMapping.get(sqlKey));
        String sqlStr = SqlMapping.gets(sql, sqlData);
        queueUtils.sendMsgByRedis(
                rediskey,
                sqlStr);
    }

    @Override
    public ResponseDto<Object> updatePeople(Long people) {
        //修改Mongodb

        //修改mysql

        return null;
    }

    @Override
    public ResponseDto<Object> update(GoblinGoodsAnticipateUpdateParam goodsAnticipateUpdateParam) {

        return null;
    }


}
