package com.ruoyi.applet.contoller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.model.domain.*;
import com.ruoyi.model.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "伴宠师-伴宠师考核")
@RestController
@RequestMapping("/applet/examination")
public class ApiAppletExaminationController {

    //题目信息
    @Autowired
    private IAppletQuestionService appletQuestionService;

    //题目答案信息
    @Autowired
    private IAppletAnswerService appletAnswerService;

    //宠物类型
    @Autowired
    private IAppletPetTypeService appletPetTypeService;

    //专业执照
    @Autowired
    private IAppletLicenseService appletLicenseService;

    //用户基本考核答案
    @Autowired
    private IAppletAnswerBaseService appletAnswerBaseService;

    //用户培训考核答案
    @Autowired
    private IAppletAnswerTrainService appletAnswerTrainService;

    @ApiOperation("伴宠师工作台-题目列表数据查询")
    @GetMapping("/questionList")
    public AjaxResult questionList(AppletQuestion question){
        List<AppletQuestion> questionList = appletQuestionService.selectAppletQuestionList(question);
        for (AppletQuestion appletQuestion:questionList){
            AppletAnswer appletAnswer = new AppletAnswer();
            appletAnswer.setQuestionId(appletQuestion.getId());
            List<AppletAnswer> answerList = appletAnswerService.selectAppletAnswerList(appletAnswer);
            appletQuestion.setAnswerList(answerList);
        }
        return AjaxResult.success("题目列表数据",questionList);
    }

    @ApiOperation("伴宠师工作台-根据题目id查询题目答案")
    @GetMapping("/insertUser")
    public AjaxResult answerListByQuestionId(long questionId){
        AppletAnswer appletAnswer = new AppletAnswer();
        appletAnswer.setQuestionId(questionId);
        List<AppletAnswer> answerList = appletAnswerService.selectAppletAnswerList(appletAnswer);
        return AjaxResult.success("答案列表数据",answerList);
    }

    @ApiOperation("伴宠师工作台-查询宠物类型列表")
    @GetMapping("/petTypeList")
    public AjaxResult petTypeList(AppletPetType appletPetType){
        List<AppletPetType> petTypeList = appletPetTypeService.selectAppletPetTypeList(appletPetType);
        return AjaxResult.success("宠物类型列表数据",petTypeList);
    }

    @ApiOperation("伴宠师工作台-查询专业执照列表")
    @GetMapping("/licenseList")
    public AjaxResult licenseList(AppletLicense appletLicense){
        List<AppletLicense> licenseList = appletLicenseService.selectAppletLicenseList(appletLicense);
        return AjaxResult.success("专业执照列表数据",licenseList);
    }

//    @ApiOperation("伴宠师工作台-查询用户基本考核答案列表")
//    @PostMapping("/answerBaseList")
//    public AjaxResult answerBaseList(@RequestBody AppletAnswerBase answerBase){
//        List<AppletAnswerBase> answerBaseList = appletAnswerBaseService.selectAppletAnswerBaseList(answerBase);
//        return AjaxResult.success("用户基本考核答案列表数据",answerBaseList);
//    }

    @ApiOperation("伴宠师工作台-根据题目id查询基本考核答案")
    @GetMapping("/answeBaseByQuestionId")
    public AjaxResult answeBaseByQuestionId(Long questionId, Long userId){
        AppletAnswerBase answerBase = new AppletAnswerBase();
        answerBase.setQuestionId(questionId);
        answerBase.setUserId(userId);
        List<AppletAnswerBase> answerBaseList = appletAnswerBaseService.selectAppletAnswerBaseList(answerBase);

        for (AppletAnswerBase appletAnswerBase : answerBaseList) {
            AppletAnswer answer = appletAnswerService.selectAppletAnswerById(appletAnswerBase.getAnswerId());
            appletAnswerBase.setAnswer(answer);
        }

        return AjaxResult.success("用户基本考核答案列表数据",answerBaseList);
    }

    @ApiOperation("伴宠师工作台-添加用户基本考核答案")
    @PostMapping("/addBaseAnswer")
    public AjaxResult addBaseAnswer(@RequestBody AppletAnswerBase answerBase){
        if(answerBase.getUserId() == null){
            return AjaxResult.error("userId用户信息标识不能为空");
        }
        return toAjax(appletAnswerBaseService.insertAppletAnswerBase(answerBase));
    }

//    @ApiOperation("伴宠师工作台-查询用户培训考核答案列表")
//    @PostMapping("/answeTrainList")
//    public AjaxResult answeTrainList(@RequestBody AppletAnswerTrain answerTrain){
//        List<AppletAnswerTrain> answerTrainList = appletAnswerTrainService.selectAppletAnswerTrainList(answerTrain);
//
//        for (AppletAnswerTrain appletAnswerTrain : answerTrainList) {
//            AppletQuestion question = appletQuestionService.selectAppletQuestionById(appletAnswerTrain.getQuestionId());
//            appletAnswerTrain.setQuestion(question);
//        }
//
//        return AjaxResult.success("用户培训考核答案列表数据",answerTrainList);
//    }

    @ApiOperation("伴宠师工作台-根据题目id查询培训考核答案")
    @GetMapping("/answeTrainByQuestionId")
    public AjaxResult answeTrainByQuestionId(Long questionId, Long userId){
        AppletAnswerTrain answerTrain = new AppletAnswerTrain();
        answerTrain.setQuestionId(questionId);
        answerTrain.setUserId(userId);
        List<AppletAnswerTrain> answerTrainList = appletAnswerTrainService.selectAppletAnswerTrainList(answerTrain);

        return AjaxResult.success("用户培训考核答案列表数据",answerTrainList);
    }

    @ApiOperation("伴宠师工作台-添加用户培训考核答案")
    @PostMapping("/addTrainAnswer")
    public AjaxResult addTrainAnswer(@RequestBody AppletAnswerTrain answerTrain){
        if(answerTrain.getUserId() == null){
            return AjaxResult.error("userId用户信息标识不能为空");
        }
        return toAjax(appletAnswerTrainService.insertAppletAnswerTrain(answerTrain));
    }

    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

}
