package com.liquidnet.service.sweet.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.commons.lang.util.StringUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.base.SqlMapping;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.sweet.dto.param.SweetYbEnrollParam;
import com.liquidnet.service.sweet.dto.vo.SweetYbActivityVo;
import com.liquidnet.service.sweet.entity.SweetYbEnroll;
import com.liquidnet.service.sweet.mapper.SweetYbEnrollMapper;
import com.liquidnet.service.sweet.service.ISweetYbEnrollService;
import com.liquidnet.service.sweet.utils.ObjectUtil;
import com.liquidnet.service.sweet.utils.QueueUtils;
import com.liquidnet.service.sweet.utils.RedisDataUtils;
import org.apache.logging.log4j.util.PropertySource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import static com.liquidnet.commons.lang.util.DateUtil.DTF_YMD_HMS;

/**
 * <p>
 * YB报名表 服务实现类
 * </p>
 *
 * @author jiangxiulong
 * @since 2022-07-29
 */
@Service
public class SweetYbEnrollServiceImpl implements ISweetYbEnrollService {

    @Autowired
    QueueUtils queueUtils;
    @Autowired
    RedisDataUtils redisDataUtils;

    @Override
    public ResponseDto<Boolean> create(SweetYbEnrollParam param) {
        if (param.getActivityId() == null || param.getActivityId().equals("")) {
            param.setActivityId("0");
        }
        if (param.getAge() == null ) {
            param.setAge(0);
        }
        LinkedList<String> sqls = CollectionUtil.linkedListString();
        LinkedList<Object[]> sqlsDataA = CollectionUtil.linkedListObjectArr();
        sqls.add(SqlMapping.get("sweet_yb_enroll.insert"));
        sqlsDataA.add(new Object[]{
                IDGenerator.nextSnowId(), StringUtil.isEmpty(param.getContactTell()) ? "" : param.getContactTell(),
                StringUtil.isEmpty(param.getContactWechat()) ? "" : param.getContactWechat(), param.getNickname(), param.getStyle(),
                param.getImgUrl(), param.getAudioUrl(), StringUtil.isEmpty(param.getUniversities()) ? "" : param.getUniversities(),
                StringUtil.isEmpty(param.getCityName()) ? "" : param.getCityName(), param.getActivityId(),param.getAge()
        });
        queueUtils.sendMsgByRedis(MQConst.SweetQueue.SWEET_ANSWER_INSERT.getKey(),
                SqlMapping.gets(sqls, sqlsDataA));

        return ResponseDto.success(true);
    }

    @Override
    public ResponseDto<List<SweetYbActivityVo>> list() {
        List<String> list = redisDataUtils.getSweetYbActivityList();
        List<SweetYbActivityVo> voList = ObjectUtil.sweetYbActivityVoList();
        LocalDateTime now = LocalDateTime.now();
        for (String id : list) {
            SweetYbActivityVo vo = redisDataUtils.getSweetYbActivityVo(id);
            LocalDateTime st = LocalDateTime.parse(vo.getStartTime(), DTF_YMD_HMS);
            LocalDateTime et = LocalDateTime.parse(vo.getEndTime(), DTF_YMD_HMS);
            if (now.isAfter(et)) {
                vo.setStatus(9);
            } else if (now.isBefore(st)) {
                vo.setStatus(0);
            } else {
                vo.setStatus(6);
            }
            voList.add(vo);
        }
        voList = voList.stream().sorted(Comparator.comparing(SweetYbActivityVo::getStatus, Comparator.naturalOrder()).thenComparing(SweetYbActivityVo::getStartTime,Comparator.reverseOrder())).collect(Collectors.toList());
        return ResponseDto.success(voList);
    }

}
