package com.ruoyi.web.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mall.domain.Bo.CollectSchoolBO;
import com.ruoyi.mall.domain.Bo.TargetSchoolBO;
import com.ruoyi.mall.domain.MallMemberCollectSchool;
import com.ruoyi.mall.domain.MallMemberTargetSchool;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.Vo.ContrastRecordVo;
import com.ruoyi.mall.domain.Vo.MallMemberCollectSchoolVo;
import com.ruoyi.mall.domain.Vo.MallMemberTargetSchoolVo;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.service.IMallMemberCollectSchoolService;
import com.ruoyi.mall.service.IMallMemberTargetSchoolService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 收藏接口控制层
 * @author 萌
 */
@RestController
@RequestMapping("/api/collect")
public class ApiCollectSchoolController {

    @Resource
    private IMallMemberCollectSchoolService mallMemberCollectSchoolService;

    @Resource
    private IMallMemberTargetSchoolService mallMemberTargetSchoolService;

    @Resource
    private MallMemberUserMapper mallMemberUserMapper;
    /**
     * 收藏/取消收藏学校
     * @param collectSchoolBO 收藏信息
     * @param bindingResult 校验结果
     * @return 结果
     */
    @PostMapping("/collectSchool")
    public AjaxResult collectSchool(@RequestBody @Validated CollectSchoolBO collectSchoolBO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        try {
            return mallMemberCollectSchoolService.collectSchool(collectSchoolBO);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }

    /**
     * 学校收藏状态
     * @param id 用户id
     * @param schoolId 学校id
     * @param batchId 批次id
     * @return 结果
     */
    @GetMapping("/getCollectStatus")
    public AjaxResult getCollectStatus(@RequestParam Long id,
                                       @RequestParam Long schoolId,
                                       @RequestParam Long batchId){

        if (ObjectUtils.isEmpty(id)){
            return AjaxResult.error("缺少id");
        }else if (ObjectUtils.isEmpty(schoolId)){
            return AjaxResult.error("缺少schoolId");
        }else if (ObjectUtils.isEmpty(batchId)) {
            return AjaxResult.error("缺少batchId");
        }
        try{
            return mallMemberCollectSchoolService.getCollectStatus(id, schoolId, batchId);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }

    /**
     * 设定/取消目标
     * @param targetSchoolBO 学校信息
     * @param bindingResult 校验结果
     * @return 结果
     */
    @PostMapping("/setTargetSchool")
    public AjaxResult setTargetSchool(@RequestBody @Validated TargetSchoolBO targetSchoolBO, BindingResult bindingResult){
        // 校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        try{
            return mallMemberTargetSchoolService.setTargetSchool(targetSchoolBO);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }

    /**
     * 查询学校是否设为目标
     * @param id 用户id
     * @param schoolId 学校id
     * @param batchId 批次id
     * @return 结果
     */
    @GetMapping("/getTargetStatus")
    public AjaxResult getTargetStatus(@RequestParam Long id,
                                      @RequestParam Long schoolId,
                                      @RequestParam Long batchId){
        if (ObjectUtils.isEmpty(id)){
            return AjaxResult.error("缺少id");
        }else if (ObjectUtils.isEmpty(schoolId)){
            return AjaxResult.error("缺少schoolId");
        }else if (ObjectUtils.isEmpty(batchId)) {
            return AjaxResult.error("缺少batchId");
        }
        try{
            return mallMemberTargetSchoolService.getTargetStatus(id, schoolId, batchId);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }

    /**我的-收藏学校
     *  用户id 批次查询 返回学校列表和录取率
     * @param userId 用户id
     * @param batchId 批次id
     * @param pageNum 页数
     * @param pageSize 页大小
     * @return
     */
    @GetMapping("/getCollectSchoolList")
    public AjaxResult getCollectSchoolList(@RequestParam Long userId,
                                           @RequestParam Long batchId,
                                           @RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize){
        if (ObjectUtils.isEmpty(userId)){
            return AjaxResult.error("缺少用户编号");
        }else if (ObjectUtils.isEmpty(batchId)) {
            return AjaxResult.error("缺少批次编号");
        }
        try{
            PageHelper.startPage(pageNum, pageSize);
            // 查询用户成绩
            MallMemberUser mallMemberUser = mallMemberUserMapper.selectMallMemberUserById(userId);
            if (ObjectUtils.isEmpty(mallMemberUser.getMajorScore()) || ObjectUtils.isEmpty(mallMemberUser.getCultureScore())){
                return AjaxResult.error("请先录入成绩");
            }
            Integer majorScore = mallMemberUser.getMajorScore();
            Integer cultureScore = mallMemberUser.getCultureScore();
            String subjectType = mallMemberUser.getSubjectType();

            MallMemberCollectSchool mallMemberCollectSchool = new MallMemberCollectSchool();
            mallMemberCollectSchool.setBatchId(batchId);
            mallMemberCollectSchool.setId(userId);
            mallMemberCollectSchool.setMajorScore(majorScore);
            mallMemberCollectSchool.setCultureScore(cultureScore);
            mallMemberCollectSchool.setSubjectType(subjectType);
            //查询
            List<MallMemberCollectSchoolVo> result = mallMemberCollectSchoolService.selectVoForMineList(mallMemberCollectSchool);
            if(null != result && result.size()>0){
                PageInfo<MallMemberCollectSchoolVo> pageInfo = new PageInfo<>(result);
                return AjaxResult.success().put("data", pageInfo);
            }else{
                PageInfo<MallMemberCollectSchoolVo> pageInfo = new PageInfo<>(result);
                return AjaxResult.success().put("data", pageInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }

    /**我的-目标管理
     *
     * @param userId 用户编号
     * @param batchId 批次编号
     * @param schoolName 学校名
     * @param pageNum 分页页数
     * @param pageSize 分页大小
     * @return
     */
    @GetMapping("/getTargetSchoolList")
    public AjaxResult getTargetSchoolList(@RequestParam Long userId,
                                           @RequestParam Long batchId,
                                          @RequestParam String schoolName,
                                           @RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize){
        if (ObjectUtils.isEmpty(userId)){
            return AjaxResult.error("缺少用户编号");
        }else if (ObjectUtils.isEmpty(batchId)) {
            return AjaxResult.error("缺少批次编号");
        }
        try{
            PageHelper.startPage(pageNum, pageSize);
            MallMemberTargetSchool mallMemberTargetSchool = new MallMemberTargetSchool();
            mallMemberTargetSchool.setBatchId(batchId);
            mallMemberTargetSchool.setId(userId);
            if(StringUtils.isNotBlank(schoolName)){
                mallMemberTargetSchool.setSchoolName(schoolName);
            }
            //查询
            List<MallMemberTargetSchoolVo> result = mallMemberTargetSchoolService.selectVoForMineList(mallMemberTargetSchool);
            if(null != result && result.size()>0){
                PageInfo<MallMemberTargetSchoolVo> pageInfo = new PageInfo<>(result);
                return AjaxResult.success().put("data", pageInfo);
            }else{
                PageInfo<MallMemberTargetSchoolVo> pageInfo = new PageInfo<>(result);
                return AjaxResult.success().put("data", pageInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }
}
