package com.da.dangan.controller;

import com.da.common.annotation.Log;
import com.da.common.core.controller.BaseController;
import com.da.common.core.domain.AjaxResult;
import com.da.common.core.domain.entity.DaCatalog;
import com.da.common.enums.BusinessType;
import com.da.common.utils.poi.ExcelUtil;
import com.da.dangan.service.IDaCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 档案目录Controller
 * 
 * @author hs
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/dangan/catalog")
public class DaCatalogController extends BaseController
{
    @Autowired
    private IDaCatalogService daCatalogService;

    /**
     * 查询档案目录列表
     */
    @PreAuthorize("@ss.hasPermi('dangan:catalog:list')")
    @GetMapping("/list")
    public AjaxResult list(DaCatalog daCatalog)
    {
        List<DaCatalog> catalogs = daCatalogService.selectAllDaCatalogList(daCatalog, getUserId());
        return success(catalogs);
    }

    /**
     * 获取目录下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(DaCatalog daCatalog)
    {
        List<DaCatalog> daCatalogs = daCatalogService.selectDaCatalogList(daCatalog);
        return success(daCatalogService.buildCatalogTreeSelect(daCatalogs));
    }

    /**
     * 加载用户对应勾选目录列表
     */
    @GetMapping(value = "/userMenuTreeselect/{userId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("userId") Long userId)
    {   /*根据用户查询对应列表
        List<DaCatalog> catalogs = daCatalogService.selectCatalogList(getUserId());*/
        //查询全部目录列表
        List<DaCatalog> catalogs = daCatalogService.selectDaCatalogList(new DaCatalog());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", daCatalogService.selectCatalogIdsByUserId(userId));
        ajax.put("catalogs", daCatalogService.buildCatalogTreeSelect(catalogs));
        return ajax;
    }

    /**
     * 导出档案目录列表
     */
    @PreAuthorize("@ss.hasPermi('dangan:catalog:export')")
    @Log(title = "档案目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DaCatalog daCatalog)
    {
        List<DaCatalog> list = daCatalogService.selectDaCatalogList(daCatalog);
        ExcelUtil<DaCatalog> util = new ExcelUtil<DaCatalog>(DaCatalog.class);
        util.exportExcel(response, list, "档案目录数据");
    }

    /**
     * 获取档案目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('dangan:catalog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(daCatalogService.selectDaCatalogById(id));
    }

    /**
     * 新增档案目录
     */
    @PreAuthorize("@ss.hasPermi('dangan:catalog:add')")
    @Log(title = "档案目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DaCatalog daCatalog)
    {
        if (!daCatalogService.checkDaCatalogNameUnique(daCatalog))
        {
            return error("新增部门'" + daCatalog.getMuName() + "'失败，目录名称已存在");
        }
        daCatalog.setCreateBy(getUsername());
        return toAjax(daCatalogService.insertDaCatalog(daCatalog));
    }

    /**
     * 修改档案目录
     */
    @PreAuthorize("@ss.hasPermi('dangan:catalog:edit')")
    @Log(title = "档案目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DaCatalog daCatalog)
    {
        return toAjax(daCatalogService.updateDaCatalog(daCatalog));
    }

    /**
     * 删除档案目录
     */
    @PreAuthorize("@ss.hasPermi('dangan:catalog:remove')")
    @Log(title = "档案目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return daCatalogService.deleteDaCatalogById(id);
    }

    /**
     * 批量删除档案目录
     */
    /*@PreAuthorize("@ss.hasPermi('dangan:catalog:remove')")
    @Log(title = "档案目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(daCatalogService.deleteDaCatalogByIds(ids));
    }*/
}
