package com.ruoyi.web.controller.back;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.syzy.domain.BBusiWishUniversitySubmitMarjors;
import com.ruoyi.syzy.domain.BBusiWishUniversitySubmitRecruitPlan;
import com.ruoyi.syzy.domain.BindEnrollMajorPlan;
import com.ruoyi.syzy.domain.BindPlanEnrollMajor;
import com.ruoyi.syzy.service.IBBusiWishUniversitySubmitMarjorsService;
import com.ruoyi.syzy.service.IBBusiWishUniversitySubmitRecruitPlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 院校专业分数线Controller
 * 
 * @author voluntary
 * @date 2023-01-08
 */
@RestController
@RequestMapping("/back/busiWishUniversitySubmitMarjors")
public class BackBusiWishUniversitySubmitMarjorsController extends BaseController
{
    @Autowired
    private IBBusiWishUniversitySubmitMarjorsService bBusiWishUniversitySubmitMarjorsService;
    @Autowired
    private IBBusiWishUniversitySubmitRecruitPlanService planService;
    /**
     * 查询院校专业分数线列表
     */
    @PreAuthorize("@ss.hasPermi('back:busiWishUniversitySubmitMarjors:list')")
    @GetMapping("/list")
    public TableDataInfo list(BBusiWishUniversitySubmitMarjors bBusiWishUniversitySubmitMarjors)
    {
        startPage();
        List<BBusiWishUniversitySubmitMarjors> list = bBusiWishUniversitySubmitMarjorsService.selectBBusiWishUniversitySubmitMarjorsList(bBusiWishUniversitySubmitMarjors);
        for (BBusiWishUniversitySubmitMarjors pp : list) {
            if(StringUtils.isEmpty(pp.getMarjorNameText())){
                continue;
            }
            BBusiWishUniversitySubmitRecruitPlan plan = new BBusiWishUniversitySubmitRecruitPlan();
            plan.setUniversityId(pp.getUniversityId());
            plan.setLocation(pp.getLocation());
            plan.setLiberalScience(pp.getLiberalScience());
            plan.setMarjorNameText(pp.getMarjorNameText());
            List<BBusiWishUniversitySubmitRecruitPlan> plans = planService.selectBBusiWishUniversitySubmitRecruitPlanList(plan);
            pp.setBindPlanList(plans);
        }
        return getDataTable(list);
    }

    /**
     * 导出院校专业分数线列表
     */
    @PreAuthorize("@ss.hasPermi('back:busiWishUniversitySubmitMarjors:export')")
    @Log(title = "院校专业分数线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BBusiWishUniversitySubmitMarjors bBusiWishUniversitySubmitMarjors)
    {
        List<BBusiWishUniversitySubmitMarjors> list = bBusiWishUniversitySubmitMarjorsService.selectBBusiWishUniversitySubmitMarjorsList(bBusiWishUniversitySubmitMarjors);
        ExcelUtil<BBusiWishUniversitySubmitMarjors> util = new ExcelUtil<BBusiWishUniversitySubmitMarjors>(BBusiWishUniversitySubmitMarjors.class);
        util.exportExcel(response, list, "院校专业分数线数据");
    }

    /**
     * 获取院校专业分数线详细信息
     */
    @PreAuthorize("@ss.hasPermi('back:busiWishUniversitySubmitMarjors:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bBusiWishUniversitySubmitMarjorsService.selectBBusiWishUniversitySubmitMarjorsById(id));
    }

    /**
     * 新增院校专业分数线
     */
    @PreAuthorize("@ss.hasPermi('back:busiWishUniversitySubmitMarjors:add')")
    @Log(title = "院校专业分数线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BBusiWishUniversitySubmitMarjors bBusiWishUniversitySubmitMarjors)
    {
        return toAjax(bBusiWishUniversitySubmitMarjorsService.insertBBusiWishUniversitySubmitMarjors(bBusiWishUniversitySubmitMarjors));
    }

    /**
     * 修改院校专业分数线
     */
    @PreAuthorize("@ss.hasPermi('back:busiWishUniversitySubmitMarjors:edit')")
    @Log(title = "院校专业分数线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BBusiWishUniversitySubmitMarjors bBusiWishUniversitySubmitMarjors)
    {
        return toAjax(bBusiWishUniversitySubmitMarjorsService.updateBBusiWishUniversitySubmitMarjors(bBusiWishUniversitySubmitMarjors));
    }

    /**
     * 删除院校专业分数线
     */
    @PreAuthorize("@ss.hasPermi('back:busiWishUniversitySubmitMarjors:remove')")
    @Log(title = "院校专业分数线", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bBusiWishUniversitySubmitMarjorsService.deleteBBusiWishUniversitySubmitMarjorsByIds(ids));
    }

    @Log(title = "绑定招生计划与院校线", businessType = BusinessType.UPDATE)
    @PostMapping("/bindEnrollPlanMajor")
    public AjaxResult bindEnrollPlanMajor(@RequestBody BindEnrollMajorPlan majorPlan)
    {
        BBusiWishUniversitySubmitRecruitPlan plan = planService.selectBBusiWishUniversitySubmitRecruitPlanById(majorPlan.getPlanId());
        if(StringUtils.isEmpty(plan.getMarjorNameText())){
            return AjaxResult.error("绑定失败，计划的【专业名称备注marjorNameText】为空");
        }
        BBusiWishUniversitySubmitMarjors marjor = bBusiWishUniversitySubmitMarjorsService.selectBBusiWishUniversitySubmitMarjorsById(majorPlan.getEnrollId());
        if(!plan.getMarjorName().equalsIgnoreCase(marjor.getMarjorName())){
            return AjaxResult.error("绑定失败，计划与录取的专业名称不一致");
        }

        marjor.setMarjorNameText(plan.getMarjorNameText());
        bBusiWishUniversitySubmitMarjorsService.updateBBusiWishUniversitySubmitMarjors(marjor);

        return AjaxResult.success("更新专业的marjorNameText成功");
    }
}
