package com.ruoyi.web.controller.ie;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ie.domain.Papers;
import com.ruoyi.ie.service.IPapersService;
import com.ruoyi.ie.service.IQuestionsService;
import com.ruoyi.mingxue.domain.CustomerFavorites;
import com.ruoyi.mingxue.service.ICustomerFavoritesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "前端V2 试卷")
@RestController
@RequestMapping("front/papers")
public class FrontPapersController extends BaseController {
    @Autowired
    private IPapersService papersService;
    @Autowired
    private ICustomerFavoritesService customerFavoritesService;

    @ApiOperation("试卷列表")
    @GetMapping(value = "list")
    public TableDataInfo list(String subjectName, String gradeName, @RequestParam String paperType, String area,@RequestParam Integer subjectId, String paperName) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Papers params = new Papers();
        params.setPaperName(paperName);
        params.setSubjectName(subjectName);
        params.setPaperType(paperType);
        params.setSubjectId(subjectId);
        params.setLocation(user.getProvinceName());
        params.setExamineeType(user.getExamType());
        startPage();
        List<Papers> list = papersService.selectPapersList(params);
        papersService.fillCollectInfo(String.valueOf(SecurityUtils.getLoginUser().getUserId()), list);
        return getDataTable(list);
    }

    @ApiOperation("收藏试卷")
    @PostMapping("collect")
    public AjaxResult collect(Long paperId) {
        String customercode = String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId());
        CustomerFavorites query = new CustomerFavorites();
        query.setRefid(paperId + "");
        query.setCustomercode(customercode);
        query.setType("paper");
        List<CustomerFavorites> cfList = customerFavoritesService.selectCustomerFavoritesList(query);
        if (CollectionUtils.isEmpty(cfList)) {
            Papers papers = papersService.selectPapersById(paperId);
            CustomerFavorites cf = new CustomerFavorites();
            cf.setCourse(String.valueOf(papers.getSubjectId()));
            cf.setCustomercode(customercode);
            cf.setType("paper");
            cf.setRefid(paperId + "");
            cf.setTime(new Date());
            cf.setStatus(1);
            customerFavoritesService.insertCustomerFavorites(cf);
        } else {
            CustomerFavorites cf = cfList.get(0);
            if (cf.getStatus() == 0) {
                cf.setStatus(1);
                customerFavoritesService.updateCustomerFavorites(cf);
            }
        }

        return AjaxResult.success();
    }

    @ApiOperation("取消收藏试卷")
    @PostMapping("cancelCollect")
    public AjaxResult cancelCollect(Long paperId) {
        String customercode = String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId());
        CustomerFavorites query = new CustomerFavorites();
        query.setRefid(paperId + "");
        query.setCustomercode(customercode);
        List<CustomerFavorites> cfList = customerFavoritesService.selectCustomerFavoritesList(query);
        if (CollectionUtils.isNotEmpty(cfList)) {
            CustomerFavorites cf = cfList.get(0);
            if (cf.getStatus() == 1) {
                cf.setStatus(0);
                customerFavoritesService.updateCustomerFavorites(cf);
            }
        }

        return AjaxResult.success();
    }

    @ApiOperation("试卷是否已经收藏了")
    @GetMapping("isCollected")
    public AjaxResult isCollected(Long paperId) {
        String customercode = String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId());
        CustomerFavorites customerFavorites = new CustomerFavorites();
        customerFavorites.setCustomercode(customercode);
        customerFavorites.setType("paper");
        customerFavorites.setRefid(paperId + "");
        customerFavorites.setStatus(1);
        List<CustomerFavorites> list = customerFavoritesService.selectCustomerFavoritesList(customerFavorites);
        return AjaxResult.success(CollectionUtils.isNotEmpty(list));
    }

    @ApiOperation("查询科目情况")
    @GetMapping("getObjectivePaperStatistic")
    public AjaxResult getObjectivePaperStatistic(Integer subjectId) {
        Map data = papersService.getObjectivePaperStatistic(subjectId);
        return AjaxResult.success(data);
    }
}
