package com.admin.web.controller.major;

import com.admin.common.annotation.Log;
import com.admin.common.core.controller.BaseController;
import com.admin.common.core.domain.AjaxResult;
import com.admin.common.core.page.TableDataInfo;
import com.admin.common.enums.BusinessType;
import com.admin.common.utils.poi.ExcelUtil;
import com.admin.major.domain.SpecialApply;
import com.admin.major.domain.dto.SpecialApplyViewDTO;
import com.admin.major.manager.ISpecialApplyExportManager;
import com.admin.major.manager.ISpecialApplyResultManager;
import com.admin.major.service.IEnhanceSpecialApplyService;
import com.admin.major.service.ISpecialApplyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 特别申请Controller
 * 
 * @author zewei
 * @date 2022-03-26
 */
@RestController
@RequestMapping("/major/specialApply")
public class SpecialApplyController extends BaseController
{
    @Autowired
    private ISpecialApplyService specialApplyService;

    @Autowired
    private IEnhanceSpecialApplyService enhanceSpecialApplyService;

    @Autowired
    private ISpecialApplyExportManager specialApplyExportManager;

    @Autowired
    private ISpecialApplyResultManager specialApplyResultManager;

    /**
     * 查询特别申请列表
     */
    @PreAuthorize("@ss.hasPermi('major:specialApply:list')")
    @GetMapping("/list")
    public TableDataInfo list(SpecialApply specialApply)
    {
        startPage();
//        List<SpecialApply> list = specialApplyService.selectSpecialApplyList(specialApply);
        List<SpecialApplyViewDTO> list = enhanceSpecialApplyService.viewSpecialApplyList(specialApply);
        return getDataTable(list);
    }

    /**
     * 导出特别申请列表
     */
    @PreAuthorize("@ss.hasPermi('major:specialApply:export')")
    @Log(title = "特别申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpecialApply specialApply)
    {
        List<SpecialApply> list = specialApplyService.selectSpecialApplyList(specialApply);
        ExcelUtil<SpecialApply> util = new ExcelUtil<SpecialApply>(SpecialApply.class);
        util.exportExcel(response, list, "特别申请数据");
    }

    /**
     * 导出特别申请列表文档
     */
    @PreAuthorize("@ss.hasPermi('major:specialApply:export')")
    @Log(title = "特别申请导出", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "特别申请-导出文档", notes="特别申请-导出文档")
    @PostMapping("/exportWord")
    public void exportWord(HttpServletRequest request, HttpServletResponse response, @RequestParam Object specialApplyId)
    {
        if (null != specialApplyId) {
            try {
                specialApplyExportManager.downloadSpecialTable(Integer.valueOf(specialApplyId.toString()), request, response);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取特别申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('major:specialApply:query')")
    @GetMapping(value = "/{specialApplyId}")
    public AjaxResult getInfo(@PathVariable("specialApplyId") String specialApplyId)
    {
        return AjaxResult.success(specialApplyService.selectSpecialApplyBySpecialApplyId(specialApplyId));
    }

    /**
     * 新增特别申请
     */
    @PreAuthorize("@ss.hasPermi('major:specialApply:add')")
    @Log(title = "特别申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpecialApply specialApply)
    {
        return toAjax(specialApplyService.insertSpecialApply(specialApply));
    }

    /**
     * 修改特别申请
     */
    @PreAuthorize("@ss.hasPermi('major:specialApply:edit')")
    @Log(title = "特别申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpecialApply specialApply)
    {
        return toAjax(specialApplyResultManager.updateSpecialApplyAndGetResult(specialApply));
    }

    /**
     * 删除特别申请
     */
    @PreAuthorize("@ss.hasPermi('major:specialApply:remove')")
    @Log(title = "特别申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{specialApplyIds}")
    public AjaxResult remove(@PathVariable String[] specialApplyIds)
    {
        return toAjax(specialApplyService.deleteSpecialApplyBySpecialApplyIds(specialApplyIds));
    }
}
