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.MallUserMajorIntention;
import com.ruoyi.mall.service.IMallMajorClassService;
import com.ruoyi.mall.service.IMallMajorRankingService;
import com.ruoyi.mall.service.IMallUserMajorIntentionService;
import org.apache.velocity.runtime.directive.contrib.For;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName 志愿填报接口
 * @Description 志愿填报接口
 * @Author LCL
 * @Date 2023/10/18
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/volunteerFilling")
public class ApiVolunteerFillingController {
    @Resource
    private IMallMajorRankingService mallMajorRankingService;

    @Resource
    private IMallUserMajorIntentionService mallUserMajorIntentionService;

    @Resource
    private IMallMajorClassService mallMajorClassService;


    /**
     * 志愿填报-意向专业列表
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/getUserMajorIntentionList")
    public AjaxResult getUserMajorIntentionList(@RequestParam Long userId) {
        try {
            List<Map<String, Object>> mapList = mallUserMajorIntentionService.selectMallUserMajorIntentionListByUserId(userId);
            return AjaxResult.success().put("data", mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("志愿填报-意向专业列表异常");
        }
    }

    /**
     * 志愿填报-顺序调整
     *
     * @param mallUserMajorIntention 用户意向专业对象
     * @return
     */
    @PostMapping("/updateIntentionReorder")
    public AjaxResult updateIntentionReorder(@RequestBody MallUserMajorIntention mallUserMajorIntention) {
        try {
            mallUserMajorIntentionService.deleteMallUserMajorIntentionByUserId(mallUserMajorIntention.getUserId());
            Long[] majorIdList = mallUserMajorIntention.getMajorIdList();
            if (majorIdList.length > 0) {
                for (int i = 0; i < majorIdList.length; i++) {
                    MallUserMajorIntention majorIntention = new MallUserMajorIntention();
                    majorIntention.setUserId(mallUserMajorIntention.getUserId());
                    majorIntention.setMajorId(majorIdList[i]);
                    majorIntention.setSort((long) (i +1));
                    mallUserMajorIntentionService.insertMallUserMajorIntention(majorIntention);
                }
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("志愿填报-顺序调整异常");
        }
    }

    /**
     * 志愿填报-选择专业
     *
     * @param mallUserMajorIntention 用户意向专业对象
     * @return
     */
    @PostMapping("/updateChooseMajor")
    public AjaxResult updateChooseMajor(@RequestBody MallUserMajorIntention mallUserMajorIntention) {
        Long[] majorIdList = mallUserMajorIntention.getMajorIdList();
        if (majorIdList.length < 1) {
            return AjaxResult.error("请选择专业");
        }
        if (majorIdList.length > 6) {
            return AjaxResult.error("最多选择6个专业");
        }
        try {
            mallUserMajorIntentionService.deleteMallUserMajorIntentionByUserId(mallUserMajorIntention.getUserId());
            for (int i = 0; i < majorIdList.length; i++) {
                MallUserMajorIntention majorIntention = new MallUserMajorIntention();
                majorIntention.setUserId(mallUserMajorIntention.getUserId());
                majorIntention.setMajorId(majorIdList[i]);
                majorIntention.setSort((long) (i +1));
                mallUserMajorIntentionService.insertMallUserMajorIntention(majorIntention);
            }

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("志愿填报-选择专业异常");
        }
    }

    /**
     * 志愿填报-首页
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/getVolunteerFillingIndex")
    public AjaxResult getVolunteerFillingIndex(@RequestParam Long userId) {
        try {
            Map<String, Object> map = mallUserMajorIntentionService.selectVolunteerFillingIndexByUserId(userId);
            return AjaxResult.success().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("志愿填报-首页异常");
        }
    }

    /**
     * 志愿填报-专业列表
     *
     * @return
     */
    @GetMapping("/getMajorList")
    public AjaxResult getMajorList() {
        try {
            Map<Object, List<Map<String, Object>>> mapList = mallMajorClassService.selectAllTwoMallMajorClassList();
            return AjaxResult.success().put("data", mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("志愿填报-专业列表异常");
        }
    }

}
