package com.ruoyi.mall.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.Bo.*;
import com.ruoyi.mall.domain.MallSchoolAccept;
import com.ruoyi.mall.service.IMallLinkMajorSchoolService;
import com.ruoyi.mall.service.IMallSchoolAcceptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 学校专业录取数据Controller
 *
 * @author lsm
 * @date 2023-10-18
 */
@RestController
@RequestMapping("/mall/mallSchoolAccept")
public class MallSchoolAcceptController extends BaseController
{
    @Autowired
    private IMallSchoolAcceptService mallSchoolAcceptService;

    @Resource
    private IMallLinkMajorSchoolService linkMajorSchoolService;

    /**
     * 查询学校专业录取数据列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSchoolAccept:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallSchoolAccept mallSchoolAccept)
    {
        startPage();
        List<MallSchoolAccept> list = mallSchoolAcceptService.selectMallSchoolAcceptList(mallSchoolAccept);
        return getDataTable(list);
    }

    /**
     * 导出学校专业录取数据列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSchoolAccept:export')")
    @Log(title = "学校专业录取数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallSchoolAccept mallSchoolAccept)
    {
        List<MallSchoolAccept> list = mallSchoolAcceptService.selectMallSchoolAcceptList(mallSchoolAccept);
        ExcelUtil<MallSchoolAccept> util = new ExcelUtil<MallSchoolAccept>(MallSchoolAccept.class);
        util.exportExcel(response, list, "学校专业录取数据数据");

        //List<SchoolMajorLinkBO> list = linkMajorSchoolService.getAllSchoolAndMajorList();
        //ExcelUtil<SchoolMajorLinkBO> util = new ExcelUtil<SchoolMajorLinkBO>(SchoolMajorLinkBO.class);
        //util.exportExcel(response, list, "学校专业关联");
    }

    /**
     * 获取学校专业录取数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSchoolAccept:query')")
    @GetMapping(value = "/{schoolAcceptId}")
    public AjaxResult getInfo(@PathVariable("schoolAcceptId") Long schoolAcceptId)
    {
        return AjaxResult.success(mallSchoolAcceptService.selectMallSchoolAcceptBySchoolAcceptId(schoolAcceptId));
    }

    /**
     * 新增学校专业录取数据
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSchoolAccept:add')")
    @Log(title = "学校专业录取数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallSchoolAccept mallSchoolAccept)
    {
        mallSchoolAccept.setCreateAt(DateUtils.getTime());
        mallSchoolAccept.setDelFlag("0");
        return toAjax(mallSchoolAcceptService.insertMallSchoolAccept(mallSchoolAccept));
    }

    /**
     * 修改学校专业录取数据
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSchoolAccept:edit')")
    @Log(title = "学校专业录取数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallSchoolAccept mallSchoolAccept)
    {
        mallSchoolAccept.setUpdateAt(DateUtils.getTime());
        return toAjax(mallSchoolAcceptService.updateMallSchoolAccept(mallSchoolAccept));
    }

    /**
     * 删除学校专业录取数据
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSchoolAccept:remove')")
    @Log(title = "学校专业录取数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{schoolAcceptIds}")
    public AjaxResult remove(@PathVariable Long[] schoolAcceptIds)
    {
        return toAjax(mallSchoolAcceptService.deleteMallSchoolAcceptBySchoolAcceptIds(schoolAcceptIds));
    }

    /**
     * 导入录取数据列表
     */
    @Log(title = "录取数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, HttpServletResponse response) throws Exception
    {
        try {
            ExcelUtil<NewAcceptBO> util = new ExcelUtil<NewAcceptBO>(NewAcceptBO.class);
            List<NewAcceptBO> list = util.importExcel(file.getInputStream());
            if (!list.isEmpty()){
                String msg = mallSchoolAcceptService.importAcceptData(list);

                   return AjaxResult.success(msg);

            }else{
                return AjaxResult.error("无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 导入专科
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/zhuankeImportData")
    public AjaxResult zhuankeImportData(MultipartFile file, boolean updateSupport) throws Exception
    {
        try {
            ExcelUtil<ZhuankeBO> util = new ExcelUtil<ZhuankeBO>(ZhuankeBO.class);
            List<ZhuankeBO> list = util.importExcel(file.getInputStream());
            if (!list.isEmpty()){


                String message = mallSchoolAcceptService.importZhuankeData(list);

                return AjaxResult.success(message);

            }else{
                return AjaxResult.error("无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 专科模板导出
     * @param response
     * @throws IOException
     */
    @PostMapping("/zhuankeImportTemplate")
    public void zhuankeTemplate(HttpServletResponse response) throws IOException
    {
        //List<SchoolMajorLinkBO> list = linkMajorSchoolService.getAllSchoolAndMajorList();

        ExcelUtil<ZhuankeBO> util = new ExcelUtil<ZhuankeBO>(ZhuankeBO.class);
        util.importTemplateExcel(response ,"专科成绩");
    }

    /**
     * 导出学校专业关联
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportLink")
    public void exportLink(HttpServletResponse response) throws IOException
    {
        List<SchoolMajorLinkBO> list = linkMajorSchoolService.getAllSchoolAndMajorList();

        ExcelUtil<SchoolMajorLinkBO> util = new ExcelUtil<SchoolMajorLinkBO>(SchoolMajorLinkBO.class);
        util.exportExcel(response, list, "学校专业录关联数据");
    }


    //@PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<ImportSchoolAcceptBO> util = new ExcelUtil<ImportSchoolAcceptBO>(ImportSchoolAcceptBO.class);
        util.importTemplateExcel(response, "专业录取数据");
    }

    /**
     * 专业分类导出
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void linkTemplate(HttpServletResponse response) throws IOException
    {
        //List<SchoolMajorLinkBO> list = linkMajorSchoolService.getAllSchoolAndMajorList();

        ExcelUtil<NewAcceptBO> util = new ExcelUtil<NewAcceptBO>(NewAcceptBO.class);
        util.importTemplateExcel(response ,"sheet");
    }

    /**
     * 导入学校批次和专业
     */
    //@Log(title = "录取数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importMajorData")
    public AjaxResult importMajorData(MultipartFile file, boolean updateSupport) throws Exception
    {
        try {
            ExcelUtil<SchoolMajorLinkBO> util = new ExcelUtil<SchoolMajorLinkBO>(SchoolMajorLinkBO.class);
            List<SchoolMajorLinkBO> list = util.importExcel(file.getInputStream());

            String message = mallSchoolAcceptService.importSchoolMajorLinkData(list);
            if ("success".equals(message)){
                return AjaxResult.success();
            }else{
                return AjaxResult.error(message);
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }

    }

    /**
     * 导入专业分类
     */
    //@Log(title = "录取数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importMajorClassData")
    public AjaxResult importMajorClassData(MultipartFile file, boolean updateSupport) throws Exception
    {
        try {
            ExcelUtil<MajorClassBO> util = new ExcelUtil<MajorClassBO>(MajorClassBO.class);
            List<MajorClassBO> list = util.importExcel(file.getInputStream());
            String message = mallSchoolAcceptService.importMajorClassData(list);
            if (message.equals("success")){
                return AjaxResult.success();
            }else{
                return AjaxResult.error(message);
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }

    }



}
