package com.ytpm.lemonios.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.ytpm.agent.enums.UserStatusEnum;
import com.ytpm.app.model.YtDyzAnswerRecord;
import com.ytpm.app.model.YtDyzPowerRecord;
import com.ytpm.app.model.YtDyzUser;
import com.ytpm.app.param.AnswerRecordParam;
import com.ytpm.app.view.IosAnswerQuestionView;
import com.ytpm.app.view.QuestionListView;
import com.ytpm.general.RepMessage;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.general.StatusCode;
import com.ytpm.lemonios.dao.AppUserMapper;
import com.ytpm.lemonios.dao.QuestionMapper;
import com.ytpm.lemonios.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private AppUserMapper appUserMapper;
    @Value("${spring.application.name:}")
    private String applicationName;

    /**
     * 获取题库
     */
    @Override
    public ResultTable<QuestionListView> questionList() {
        return ResultTable.resultTableOk(new PageInfo<QuestionListView>(questionMapper.questionList("AP_202505210009")));
    }
    /**
     * 回答问题
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<IosAnswerQuestionView> answerQuestion(AnswerRecordParam param) {
        YtDyzUser user = appUserMapper.selectPrimaryKey(param.getUserId());
        if(Objects.isNull(user)){
            return Result.resultOk(RepMessage.TOKEN_EXPIRE);
        }
        if(!UserStatusEnum.NORMAL.getCode().equals(user.getUserStatus())){
            return new Result<>(StatusCode.ACCESS_ERR,"当前用户处于风控中");
        }
        // lemonios gollumios 需要校验体力
        if (StrUtil.containsAny(applicationName, "lemonios-service","gollumios-service")) {
            if (user.getPower() == null || user.getPower() <= 0) {
                return new Result<>(StatusCode.ACCESS_ERR, "用户体力不足，无法答题");
            }
        }
        YtDyzAnswerRecord record = new YtDyzAnswerRecord();
        BeanUtil.copyProperties(param,record);
        record.setRecordId(IdUtil.fastSimpleUUID());
        record.setAnswerTime(new Date());
        questionMapper.saveAnswerRecord(record);
        appUserMapper.subOnePower(param.getUserId());
        YtDyzPowerRecord powerRecord = new YtDyzPowerRecord();
        powerRecord.setUserId(param.getUserId());
        powerRecord.setRecordId(IdUtil.fastSimpleUUID());
        powerRecord.setAddTime(new Date());
        powerRecord.setType(1);
        powerRecord.setRemark("减少体力");
        appUserMapper.addPowerRecord(powerRecord);
        IosAnswerQuestionView view = new IosAnswerQuestionView();
        view.setTodayAnswerCount(questionMapper.getAnswerCount(user.getUserId(),1));
        view.setHistoryAnswerCount(questionMapper.getAnswerCount(user.getUserId(),2));
        List<String> viewList  = new ArrayList<>();
        List<YtDyzAnswerRecord> recordList = questionMapper.getAnswerRecords(user.getUserId());
        int count = recordList.size();
        for (YtDyzAnswerRecord ytDyzAnswerRecord : recordList) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String timeStr = count + ":" + sdf.format(ytDyzAnswerRecord.getAnswerTime());
            count--;
            viewList.add(timeStr);
        }
        view.setAnswerRecordList(viewList);
        return Result.resultOk(RepMessage.SAVE_SUCCESS, view);
    }
}
