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.system.service.ISysUserService;
import com.ruoyi.web.work.domain.Company;
import com.ruoyi.web.work.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.rmi.ServerException;
import java.util.Arrays;
import java.util.List;

/**
 * 企业管理
 *
 * @author lsw
 * @date 2024-04-16
 */
@RestController
@RequestMapping("/work/company")
public class CompanyController extends BaseController {
    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ISysUserService userService;


    @PreAuthorize("@ss.hasPermi('work:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(Company company) {
        startPage();
        List<Company> list = companyService.selectList(company);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:money:index')")
    @GetMapping("/index")
    public AjaxResult index() {
        Company company = companyService.getById(getLoginUser().getUser().getCompanyId());
        return AjaxResult.success(company);
    }

    @PreAuthorize("@ss.hasPermi('work:company:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        Company company = companyService.getById(id);
        return AjaxResult.success(company);
    }

    @PreAuthorize("@ss.hasPermi('work:company:add')")
    @Log(title = "企业管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Company company) throws ServerException {
        return companyService.add(company);
    }

    @PreAuthorize("@ss.hasPermi('work:company:edit')")
    @Log(title = "企业管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Company company) {
        return toAjax(companyService.updateById(company));
    }

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