package com.ruoyi.web.work.controller;

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.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.work.domain.Relate;
import com.ruoyi.web.work.service.IRelateService;
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;

/**
 * 关联企业
 *
 * @author lsw
 * @date 2024-04-18
 */
@RestController
@RequestMapping("/work/relate")
public class RelateController extends BaseController {
    @Autowired
    private IRelateService relateService;

    @PreAuthorize("@ss.hasPermi('work:relate:list')")
    @GetMapping("/list")
    public TableDataInfo list(Relate relate) {
        relate.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Relate> list = relateService.selectList(relate);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:relate:remove')")
    @Log(title = "解除关联用户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody Relate relate) {
        return relateService.removeUser(relate);
    }

    @Log(title = "导出关联用户", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('work:relate:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Relate relate) {
        relate.setCompanyId(getLoginUser().getUser().getCompanyId());
        PageUtils.orderBy("r.id desc");
        List<Relate> list = relateService.selectList(relate);
        ExcelUtil<Relate> util = new ExcelUtil<Relate>(Relate.class);
        util.exportExcel(response, list, "企业关联用户");
    }
}
