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.page.TableDataInfo;
import com.da.common.enums.BusinessType;
import com.da.common.utils.StringUtils;
import com.da.common.utils.poi.ExcelUtil;
import com.da.dangan.domain.DaUserCatalog;
import com.da.dangan.service.IDaUserCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
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/userMuRelation")
public class DaUserCatalogController extends BaseController
{
    @Autowired
    private IDaUserCatalogService daUserCatalogService;

    /**
     * 查询用户目录关联列表
     */
    @PreAuthorize("@ss.hasPermi('dangan:userMuRelation:list')")
    @GetMapping("/list")
    public TableDataInfo list(DaUserCatalog daUserCatalog)
    {
        startPage();
        List<DaUserCatalog> list = daUserCatalogService.selectDaUserCatalogList(daUserCatalog);
        return getDataTable(list);
    }

    /**
     * 导出用户目录关联列表
     */
    @PreAuthorize("@ss.hasPermi('dangan:userMuRelation:export')")
    @Log(title = "用户目录关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DaUserCatalog daUserCatalog)
    {
        List<DaUserCatalog> list = daUserCatalogService.selectDaUserCatalogList(daUserCatalog);
        ExcelUtil<DaUserCatalog> util = new ExcelUtil<DaUserCatalog>(DaUserCatalog.class);
        util.exportExcel(response, list, "用户目录关联数据");
    }

    /**
     * 获取用户目录关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('dangan:userMuRelation:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(daUserCatalogService.selectDaUserCatalogByUserId(userId));
    }

    /**
     * 新增用户目录关联
     */
    @PreAuthorize("@ss.hasPermi('dangan:userMuRelation:add')")
    @Log(title = "用户目录关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DaUserCatalog daUserCatalog)
    {
        return toAjax(daUserCatalogService.insertDaUserCatalog(daUserCatalog));
    }

    /**
     * 修改用户目录关联
     */
    @PreAuthorize("@ss.hasPermi('dangan:userMuRelation:edit')")
    @Log(title = "用户目录关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DaUserCatalog daUserCatalog)
    {
        return toAjax(daUserCatalogService.updateDaUserCatalog(daUserCatalog));
    }

    /**
     * 修改用户的档案目录权限
     */
    @Log(title = "用户目录关联", businessType = BusinessType.UPDATE)
    @PutMapping("/modify")
    @Transactional
    public AjaxResult modifyUserCatalog(@RequestBody DaUserCatalog daUserCatalog)
    {
        Long[] muIds = daUserCatalog.getMuIds();
        // 更新用户权限
        if (StringUtils.isNotNull(muIds))
        {
            try {
                //根据userId删除数据
                int result = daUserCatalogService.deleteDaUserCatalogByUserId(daUserCatalog.getUserId());
                //根据userId添加数据
                for(Long muId: muIds){
                    daUserCatalog.setMuId(muId);
                    daUserCatalogService.insertDaUserCatalog(daUserCatalog);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return error("操作失败，系统错误");
            }

        }
        return success();
    }

    /**
     * 删除用户目录关联
     */
    @PreAuthorize("@ss.hasPermi('dangan:userMuRelation:remove')")
    @Log(title = "用户目录关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(daUserCatalogService.deleteDaUserCatalogByUserIds(userIds));
    }
}
