package com.ruoyi.web.controller.front;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.CommonUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.syzy.domain.BBusiWishLocationSubmits;
import com.ruoyi.syzy.service.IBBusiWishLocationSubmitsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "模拟志愿-批次控制线")
@RequestMapping("front/syzy/pckzx")
public class FrontSyzyPckzxController extends BaseController {
    @Autowired
    private IBBusiWishLocationSubmitsService locationSubmitsService;
    @GetMapping("locations")
    @ApiOperation("01.地域")
    public TableDataInfo locations() {
        List<String> list = locationSubmitsService.locations();
        CommonUtils.sortByChina(list);
        return getDataTable(list);
    }

    @GetMapping("years")
    @ApiOperation("02.录取年份")
    public TableDataInfo years(@ApiParam("年份") @RequestParam String location) {
        if(StringUtils.isBlank(location)){
            //默认查询学生所在省份
            location= SecurityUtils.getLoginUser().getUser().getProvinceName();
        }
        //不限制status
        List<String> list = locationSubmitsService.yearsNoStatus(location);
        return getDataTable(list);
    }

    @GetMapping("list")
    @ApiOperation("03.批次控制线列表")
    public TableDataInfo list(@ApiParam("地域") @RequestParam String location, @ApiParam("录取年份") @RequestParam String year,
        @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
        @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {
        BBusiWishLocationSubmits query = new BBusiWishLocationSubmits();
        query.setLocation(location).setYear(year);
        startPage();
        List<BBusiWishLocationSubmits> list = locationSubmitsService.selectBBusiWishLocationSubmitsList(query);
        return getDataTable(list);
    }

    @GetMapping("listAllByYear")
    @ApiOperation("04.批次控制线列表-app使用，按年份排列")
    public AjaxResult listAllByYear(@ApiParam("地域") @RequestParam String location, @ApiParam("录取年份") String year) {
        BBusiWishLocationSubmits query = new BBusiWishLocationSubmits();
        query.setLocation(location).setYear(year);
        List<BBusiWishLocationSubmits> list = locationSubmitsService.selectBBusiWishLocationSubmitsList(query);
        //按年份来分类
        Map<String,List<BBusiWishLocationSubmits>> submitMap = new LinkedHashMap<>();
        list.forEach(ll->{
            List<BBusiWishLocationSubmits> yearLocationSubmitList= new LinkedList<>();
            if(submitMap.containsKey(ll.getYear())){
                yearLocationSubmitList= submitMap.get(ll.getYear());
            }
            yearLocationSubmitList.add(ll);
            submitMap.put(ll.getYear(),yearLocationSubmitList);
        });

        List<ListAllByYear> result = new LinkedList<>();
        for(Map.Entry<String,List<BBusiWishLocationSubmits>> entry:submitMap.entrySet()){
            ListAllByYear resultMap = new ListAllByYear();
            resultMap.setYear(Integer.parseInt(entry.getKey()));
            resultMap.setTitle(entry.getKey()+ " "+location);
            resultMap.setDatas(entry.getValue());
            result.add(resultMap);
        }
        //year倒序
//        result.stream().sorted(Comparator.comparing(ListAllByYear::getYear).reversed());

        return AjaxResult.success(result);
    }

    @Data
    static class ListAllByYear{
        private Integer year;
        private String title;
        private List<BBusiWishLocationSubmits> datas;
    }

}
