package com.miaxis.pc.controller.question;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.question.domain.QuestionThreeWrong;
import com.miaxis.question.dto.QuestionThreeWrongListDTO;
import com.miaxis.question.dto.QuestionThreeWrongYunDTO;
import com.miaxis.question.dto.QuestionWgYunDTO;
import com.miaxis.question.dto.QuestionWrongListDTO;
import com.miaxis.question.service.IQuestionThreeWrongService;
import com.miaxis.question.vo.QuestionWrongIdDateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【wrong】Controller
 *
 * @author miaxis
 * @date 2021-08-19
 */
@RestController
@RequestMapping("/question/threeWrong")
@Api(tags={"【PC-三力错题】"})
public class PcQuestionThreeWrongController extends BaseController {

    @Autowired
    private IQuestionThreeWrongService questionThreeWrongService;




    @PostMapping("wrongs")
    @ApiOperation("合并本机和云端错题")
    public Response<Integer> wrongs(@RequestBody QuestionThreeWrongYunDTO wrong) {
        //查询该用户已收藏的题目列表
        QuestionThreeWrongListDTO dto = new QuestionThreeWrongListDTO();
        dto.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        List<QuestionThreeWrong> dblist = questionThreeWrongService.selectQuestionThreeWrongList(dto);
        List<String> questionIdsStr = dblist.stream().map(o -> o.getQuestionId()+"").collect(Collectors.toList());

        List<QuestionThreeWrong> qlist = new ArrayList<QuestionThreeWrong>();
        for (QuestionWgYunDTO wgYunDTO : wrong.getWrongs()) {
            if (questionIdsStr.contains(wgYunDTO.getId())) {
                continue;
            }
            QuestionThreeWrong questionThreeWrong = new QuestionThreeWrong();
            questionThreeWrong.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
            questionThreeWrong.setQuestionId(wgYunDTO.getId());
            questionThreeWrong.setDeviceType(2);
            Date crDate = new Date(wgYunDTO.getTimestamp());
            questionThreeWrong.setCreateTime(crDate);
            qlist.add(questionThreeWrong);
        }
        if (qlist.isEmpty()) {
            Response response = new Response(200, "选中的错题都已添加！");
            return response;
        }
        return toResponse(questionThreeWrongService.saveBatch(qlist) ? 1 : 0);
    }


    @PostMapping("bakWrongs")
    @ApiOperation("备份本机错题到云端")
    public Response bakWrongs(@RequestBody QuestionThreeWrongYunDTO wrong) {
        //首先删除云端数据
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        QueryWrapper<QuestionThreeWrong> queryWrapper = new QueryWrapper<QuestionThreeWrong>();
        queryWrapper.eq("user_id", userId);
        questionThreeWrongService.remove(queryWrapper);

        //保存上传的错题
        List<QuestionThreeWrong> qlist = new ArrayList<QuestionThreeWrong>();
        for (QuestionWgYunDTO wgYunDTO : wrong.getWrongs()) {
            QuestionThreeWrong questionThreeWrong = new QuestionThreeWrong();
            questionThreeWrong.setUserId(userId);
            questionThreeWrong.setQuestionId(wgYunDTO.getId());
            questionThreeWrong.setDeviceType(2);
            Date crDate = new Date(wgYunDTO.getTimestamp());
            questionThreeWrong.setCreateTime(crDate);
            qlist.add(questionThreeWrong);
        }
        if (qlist.isEmpty()) {
            Response response = new Response(200, "该错已加入错题集(无须提示给用户)");
            return response;
        }
        return toResponse(questionThreeWrongService.saveBatch(qlist) ? 1 : 0);
    }


    @DeleteMapping("/cancel/{questionId}")
    @ApiOperation("删除错题")
    public Response<Integer> remove(
            @ApiParam(name = "questionId", value = "问题id", required = true)
            @PathVariable Long questionId
    ) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        QueryWrapper<QuestionThreeWrong> queryWrapper = new QueryWrapper<QuestionThreeWrong>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("question_id", questionId);
        queryWrapper.eq("device_type",2);
        questionThreeWrongService.remove(queryWrapper);
        return Response.success();

    }

    @DeleteMapping("/cancelAll")
    @ApiOperation("清空错题")
    public Response<Integer> removeAll() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        QueryWrapper<QuestionThreeWrong> queryWrapper = new QueryWrapper<QuestionThreeWrong>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("device_type",2);
        questionThreeWrongService.remove(queryWrapper);
        return Response.success();
    }


    @GetMapping("/wrongByUser")
    @ApiOperation("恢复云端错题到本机（根据用户获取错题）")
    public Response<List<QuestionWrongIdDateVo>> appWrongByUser(@ModelAttribute QuestionThreeWrongListDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        dto.setDeviceType(2);
        List<QuestionWrongIdDateVo> list = questionThreeWrongService.selectThreeWrongIdByUserId(dto);
        return Response.success(list);
    }


    @GetMapping("/wrongCountByUser")
    @ApiOperation("根据用户获取错题数")
    public Response<Integer> wrongCountByUser(@ModelAttribute QuestionWrongListDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        dto.setDeviceType(2);
        Integer count = questionThreeWrongService.selectThreeWrongCountByUserId(dto);
        return Response.success(count);
    }


}
