package com.ruoyi.web.controller.api;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.mall.domain.Bo.ContrastRecordBo;
import com.ruoyi.mall.domain.BusinessBulletin;
import com.ruoyi.mall.domain.ContrastRecord;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.Vo.BusinessArticleVo;
import com.ruoyi.mall.domain.Vo.BusinessBulletinResponse;
import com.ruoyi.mall.domain.Vo.ContrastRecordVo;
import com.ruoyi.mall.service.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.spi.LoginModule;
import javax.validation.constraints.NotNull;
import java.util.List;

/** 大学对比 */
@RestController
@RequestMapping("/api/ContrastSchool")

public class ApiContrastSchoolController {
    @Resource
    private ContrastSchoolService contrastSchoolService;
    @Resource
    private IMallSchoolService mallSchoolService;
    @Resource
    private IMallMemberUserService mallMemberUserService;
    @Resource
    private IContrastRecordService contrastRecordService;

    /**验证成绩完善度 */
    @GetMapping("/checkScore")
    public AjaxResult checkScore(@RequestParam@NotNull(message = "contrastSchool.checkScore.memberId.isnull") Long memberId){
        try{//文化成绩 联考成绩
            MallMemberUser mallMemberUser = mallMemberUserService.selectMallMemberUserById(memberId);
            if(null == mallMemberUser || null == mallMemberUser.getMajorScore() || null == mallMemberUser.getCultureScore() ){
                return AjaxResult.error("请先完善 我的-成绩管理 !");
            }
            return AjaxResult.success("验证成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }
    /**
     * 大学对比结果列表
     */
    @GetMapping("/getContrastSchoolList")
    public AjaxResult getContrastSchoolList(@NotNull(message = "api.ContrastSchool.getContrastSchoolList.schoolIds.isnull") Long[] schoolIds,
                                      @RequestParam(required = false) Long batchId,
                                            @NotNull(message = "api.contrastSchool.getContrastSchoolList.memberId.isnull") Long memberId) {
        try{
            return AjaxResult.success().put("data",contrastSchoolService.getSchoolContrastList(schoolIds,batchId,memberId));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("大学对比列表查询异常");
        }
    }
    /**
     * 院校列表
     * @param schoolName
     * @param batchId
     * @param configLevelId
     * @param configTypeId
     * @param provinceId
     * @param cityId
     * @param id 用户id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectMatchSchoolList")
    public AjaxResult selectMatchSchoolList(@RequestParam(required = false) String schoolName,
                                            @RequestParam(required = false) Long batchId,
                                            @RequestParam(required = false) Long configLevelId,
                                            @RequestParam(required = false) Long configTypeId,
                                            @RequestParam(required = false) Long provinceId,
                                            @RequestParam(required = false) Long cityId,
                                            @RequestParam Long id,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "10") Integer pageSize){
        if (ObjectUtils.isEmpty(id)){
            return AjaxResult.error("缺少id");
        }
        try {
            return mallSchoolService.selectMatchSchoolList(schoolName, batchId, configLevelId, configTypeId, provinceId, cityId, id, pageNum, pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }
    /**添加对比*/
    @PostMapping("/addContrastSchool")
    public AjaxResult addContrastSchool(@RequestBody ContrastRecordBo bo){
        try {
            ContrastRecord contrastRecord = BeanUtil.toBean(bo, ContrastRecord.class);
            int i = contrastRecordService.insertContrastRecord(contrastRecord);
            if(i>0){
                return AjaxResult.success("对比添加成功");
            }else{
                return AjaxResult.error("对比添加失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("对比添加异常");
        }
    }
    /**删除已添加的对比学校*/
    @PostMapping("/delContrastSchool")
    public AjaxResult delContrastSchool(@RequestBody ContrastRecord contrastRecord){
        try {
            int i = contrastRecordService.deleteContrastRecordByRecordId(contrastRecord.getRecordId());
            if(i>0){
                return AjaxResult.success("删除成功");
            }else{
                return AjaxResult.error("删除失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除异常");
        }
    }
    /**筛选 用户添加列表*/
    @GetMapping("getAddList")
    public AjaxResult getAddList(@NotNull(message = "用户编号为空")Long memberId,
                                 @RequestParam(required = false) Long batchId,
                                 @RequestParam(required = false) String schoolName,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize){
        try{
            PageHelper.startPage(pageNum, pageSize);
            ContrastRecordVo contrastRecordVo = new ContrastRecordVo();
            contrastRecordVo.setUserId(memberId);
            contrastRecordVo.setBatchId(batchId);
            if(StringUtils.isNotBlank(schoolName)){
                contrastRecordVo.setSchoolName(schoolName);
            }
            List<ContrastRecordVo> contrastRecords = contrastRecordService.selectContrastRecordVoList(contrastRecordVo);
            if(null != contrastRecords && contrastRecords.size()>0){
                PageInfo<ContrastRecordVo> pageInfo = new PageInfo<>(contrastRecords);
                return AjaxResult.success().put("data", pageInfo);
            }else{
                PageInfo<ContrastRecordVo> pageInfo = new PageInfo<>(contrastRecords);
                return AjaxResult.success().put("data",pageInfo);
            }
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询已添加对比列表异常");
        }

    }
}
