package com.ruoyi.web.controller.system;

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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Dangan;
import com.ruoyi.system.domain.DanganFile;
import com.ruoyi.system.service.IDanganFileService;
import com.ruoyi.system.service.IDanganService;
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 java.util.List;

/**
 * 档案管理Controller
 * 
 * @author ruoyi
 * @date 2022-10-20
 */
@Controller
@RequestMapping("/system/dangan")
public class DanganController extends BaseController
{
    private String prefix = "system/dangan";

    @Autowired
    private IDanganService danganService;
    @Autowired
    private IDanganFileService danganFileService;

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

    /**
     * 查询档案管理列表
     */
    @RequiresPermissions("system:dangan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Dangan dangan)
    {
        startPage();
        List<Dangan> list = danganService.selectDanganList(dangan);
        return getDataTable(list);
    }

    /**
     * 导出档案管理列表
     */
    @RequiresPermissions("system:dangan:export")
    @Log(title = "档案管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Dangan dangan)
    {
        List<Dangan> list = danganService.selectDanganList(dangan);
        ExcelUtil<Dangan> util = new ExcelUtil<Dangan>(Dangan.class);
        return util.exportExcel(list, "dangan");
    }

    /**
     * 新增档案管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存档案管理
     */
    @RequiresPermissions("system:dangan:add")
    @Log(title = "档案管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Dangan dangan)
    {
        return toAjax(danganService.insertDangan(dangan));
    }

    /**
     * 修改档案管理
     */
    @GetMapping("/edit/{danganId}")
    public String edit(@PathVariable("danganId") Long danganId, ModelMap mmap)
    {
        Dangan dangan = danganService.selectDanganById(danganId);
        mmap.put("dangan", dangan);

        //查询附件
        DanganFile danganFile  = new DanganFile();
        danganFile.setDanganId(dangan.getDanganId());
        List<DanganFile> files = danganFileService.selectDanganFileList(danganFile);
        mmap.put("fileList", files);

        return prefix + "/edit";
    }

    /**
     * 查看
     */
    @GetMapping("/look/{danganId}")
    public String look(@PathVariable("danganId") Long danganId, ModelMap mmap)
    {
        Dangan dangan = danganService.selectDanganById(danganId);
        mmap.put("dangan", dangan);

        //查询附件
        DanganFile danganFile  = new DanganFile();
        danganFile.setDanganId(dangan.getDanganId());
        List<DanganFile> files = danganFileService.selectDanganFileList(danganFile);
        mmap.put("fileList", files);
        return prefix + "/look";
    }

    /**
     * 修改保存档案管理
     */
    @RequiresPermissions("system:dangan:edit")
    @Log(title = "档案管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave( Dangan dangan)
    {
        return toAjax(danganService.updateDangan(dangan));
    }

    /**
     * 删除档案管理
     */
    @RequiresPermissions("system:dangan:remove")
    @Log(title = "档案管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(danganService.deleteDanganByIds(ids));
    }

    /**
     * 检验档案名称
     * @param dangan
     * @return
     */
    @PostMapping("/checkDanganNameUnique")
    @ResponseBody
    public String checkDanganNameUnique(Dangan dangan)
    {
        return danganService.checkDanganNameUnique(dangan.getDanganName());
    }

}
