package com.yinet.web.controller.review;

import com.yinet.common.annotation.Log;
import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.domain.AjaxResult;
import com.yinet.common.core.page.TableDataInfo;
import com.yinet.common.enums.BusinessType;
import com.yinet.common.utils.poi.ExcelUtil;
import com.yinet.review.domain.FormReview;
import com.yinet.review.service.IFormReviewService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 评审项目Controller
 * 
 * @author yinet
 * @date 2020-04-28
 */
@Controller
@RequestMapping("/review/review")
public class FormReviewController extends BaseController
{
    private String prefix = "review/review";

    @Autowired
    private IFormReviewService formReviewService;

    @RequiresPermissions("review:review:view")
    @GetMapping()
    public String review()
    {
        return prefix + "/review";
    }

    /**
     * 查询评审项目列表
     */
    @RequiresPermissions("review:review:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FormReview formReview)
    {
        startPage();
        List<FormReview> list = formReviewService.selectFormReviewList(formReview);
        return getDataTable(list);
    }

    /**
     * 导出评审项目列表
     */
    @RequiresPermissions("review:review:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FormReview formReview)
    {
        List<FormReview> list = formReviewService.selectFormReviewList(formReview);
        ExcelUtil<FormReview> util = new ExcelUtil<FormReview>(FormReview.class);
        return util.exportExcel(list, "review");
    }

    /**
     * 导入评审项目
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */

    @RequiresPermissions("review:review:importData")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FormReview> util = new ExcelUtil<FormReview>(FormReview.class);
        List<FormReview> formReviewsList = util.importExcel(file.getInputStream());
        String message = formReviewService.importFormReviewsList(formReviewsList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 新增评审项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存评审项目
     */
    @RequiresPermissions("review:review:add")
    @Log(title = "评审项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FormReview formReview)
    {
        return toAjax(formReviewService.insertFormReview(formReview));
    }

    /**
     * 修改评审项目
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FormReview formReview = formReviewService.selectFormReviewById(id);
        mmap.put("formReview", formReview);
        return prefix + "/edit";
    }
    /**
     * 修改评审项目
     */
    @RequiresPermissions("review:review:edit")
    @GetMapping("/edit1/{id}")
    public String edit1(@PathVariable("id") Long id, ModelMap mmap)
    {
        FormReview formReview = formReviewService.selectFormReviewById(id);
        mmap.put("formReview", formReview);
        return prefix + "/xiangqing";
    }

    /**
     * 修改保存评审项目
     */
    @RequiresPermissions("review:review:edit")
    @Log(title = "评审项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormReview formReview)
    {
        return toAjax(formReviewService.updateFormReview(formReview));
    }

    /**
     * 删除评审项目
     */
    @RequiresPermissions("review:review:remove")
    @Log(title = "评审项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(formReviewService.deleteFormReviewByIds(ids));
    }
}
