package com.cirle.scientific.service.impl;

import com.cirle.scientific.constants.TaskConsts;
import com.cirle.scientific.dao.MemberDao;
import com.cirle.scientific.dao.QkjMemberTaskDao;
import com.cirle.scientific.dao.QkjMemberTaskDetailDao;
import com.cirle.scientific.dao.QkjMemberTaskInfoDao;
import com.cirle.scientific.po.QkjMemberTask;
import com.cirle.scientific.po.QkjMemberTaskDetail;
import com.cirle.scientific.po.QkjMemberTaskInfo;
import com.cirle.scientific.service.MemberTaskInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.*;


@Service
@Slf4j
@Transactional
public class MemberTaskInfoServiceImpl implements MemberTaskInfoService {

    @Autowired
    QkjMemberTaskDao qkjMemberTaskDao;
    @Autowired
    QkjMemberTaskInfoDao qkjMemberTaskInfoDao;
    @Autowired
    QkjMemberTaskDetailDao qkjMemberTaskDetailDao;
    @Autowired
    MemberDao memberDao;


    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public boolean increasePoints(String memCard, Integer taskId){
        log.info("increasePoints memCard:{} taskId:{}", memCard, taskId);
        //校验任务是否存在
        try {
            QkjMemberTask task = qkjMemberTaskDao.findById(taskId.longValue()).orElse(null);
            if (Objects.isNull(task)) {
                log.info("increasePoints 该任务不存在或者状态为0 taskId:{}", taskId);
                return false;
            }
            //先插入记录表，在修改总积分
            QkjMemberTaskInfo info = qkjMemberTaskInfoDao.findByMemCard(memCard);

            if (Objects.isNull(info)) {
                log.info("increasePoints 积分总表数据为空 memCard:{}", memCard);
                return false;
            }

            QkjMemberTaskDetail detail = new QkjMemberTaskDetail();
            detail.setPoints(task.getTaskPoints());
            detail.setCompleteTime(Calendar.getInstance());
            //是否领取积分 0未领取 1 已领取
            detail.setIsReceive(1);
            detail.setMemCard(memCard);
            detail.setTaskId(task.getId().intValue());
            //每日最多完成次数
            detail.setTaskTargetNum(task.getTaskTargetNum());
            //任务类型 1 每日任务 2 一次性任务
            detail.setTaskType(task.getTaskType());
            //积分记录类型 0 任务 1 积分兑换
            detail.setType(0);
            // 0 减  1加
            detail.setChangeType(1);
            qkjMemberTaskDetailDao.save(detail);

            info.setMemTotalPoints(info.getMemTotalPoints() + task.getTaskPoints());
            info.setMemResidualPoints(info.getMemResidualPoints() + task.getTaskPoints());
            qkjMemberTaskInfoDao.save(info);

            log.info("increasePoints 赠送积分成功 memCard:{} taskId:{}", memCard, taskId);

        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public boolean whetherFinishFirstLoginTask(String memCard) {
        log.info("join addFirstLoginTsk memCard:{}", memCard);
        //先校验是否赠送过首次登陆任务的积分
        boolean saved = false;
        List<QkjMemberTaskDetail> memberTaskDetails = qkjMemberTaskDetailDao.findMemberAndTaskId(memCard, TaskConsts.FIRST_LOGIN_TASK);
        if (CollectionUtils.isEmpty(memberTaskDetails)) {
            log.info("addFirstLoginTsk memberTaskDetails is null");
            return saved;
        }
        for (QkjMemberTaskDetail memberTaskDetail : memberTaskDetails) {
            if (Objects.nonNull(memberTaskDetail) && memberTaskDetail.getIsReceive() == 1) {
                saved = true;
            }
        }
        return saved;
    }

    @Override
    public boolean initMemberTaskInfo(String memCard) {
        boolean result = true;
        try {
            QkjMemberTaskInfo byMemCard = qkjMemberTaskInfoDao.findByMemCard(memCard);
            if (byMemCard == null) {
                QkjMemberTaskInfo qkjMemberTaskInfo = new QkjMemberTaskInfo();
                qkjMemberTaskInfo.setMemTotalPoints(0);
                qkjMemberTaskInfo.setMemCard(memCard);
                qkjMemberTaskInfo.setMemLevel(0);
                qkjMemberTaskInfo.setMemResidualPoints(0);
                qkjMemberTaskInfo.setMemStudyTimes(0);
                qkjMemberTaskInfo.setMemUnconpleteTaskNum(0);
                qkjMemberTaskInfoDao.save(qkjMemberTaskInfo);
            }
        } catch (Exception e) {
            log.error("initMemberTaskInfo 初始化积分表报错，请尽快处理 e:{}, e");
            result = false;
        }

        return result;
    }
}
