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.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.Arrays;
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:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(relateService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:relate:add')")
    @Log(title = "关联企业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Relate relate) {
        return toAjax(relateService.save(relate));
    }

    @PreAuthorize("@ss.hasPermi('work:relate:edit')")
    @Log(title = "关联企业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Relate relate) {
        return toAjax(relateService.updateById(relate));
    }

    @PreAuthorize("@ss.hasPermi('work:relate:remove')")
    @Log(title = "关联企业", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(relateService.removeByIds(Arrays.asList(ids)));
    }

    @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());
        List<Relate> list = relateService.selectList(relate);
        ExcelUtil<Relate> util = new ExcelUtil<Relate>(Relate.class);
        util.exportExcel(response, list, "企业关联用户");
    }
}
