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.Company;
import com.ruoyi.web.work.domain.dto.LockDto;
import com.ruoyi.web.work.domain.dto.StatisticsDto;
import com.ruoyi.web.work.mapper.CommonMapper;
import com.ruoyi.web.work.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.rmi.ServerException;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private CommonMapper commonMapper;


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

    @GetMapping("/statistics")
    public AjaxResult statistics() {
        Map<String, Object> company = commonMapper.company(getLoginUser().getUser().getCompanyId());
        return AjaxResult.success(company);
    }

    @GetMapping("/statistics/chart")
    public AjaxResult statistics_chart(@Validated StatisticsDto dto) {
        dto.setCompanyId(getLoginUser().getUser().getCompanyId());
        return AjaxResult.success(commonMapper.record(dto));
    }

    @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.selectCompanyById(id);
        return AjaxResult.success(company);
    }

    @PreAuthorize("@ss.hasPermi('work:company:add')")
    @Log(title = "新增企业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @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(@Validated @RequestBody Company company) throws ServerException {
        return companyService.edit(company);
    }

    @PreAuthorize("@ss.hasPermi('work:company:lock')")
    @Log(title = "锁定/解锁企业", businessType = BusinessType.UPDATE)
    @PostMapping("/lock")
    public AjaxResult lock(@Validated @RequestBody LockDto dto) throws ServerException {
        return companyService.lock(dto);
    }

    @PreAuthorize("@ss.hasPermi('work:company:remove')")
    @Log(title = "删除企业", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id) throws ServerException {
        return companyService.remove(id);
    }

    @Log(title = "导出企业", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('work:company:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Company company) {
        PageUtils.orderBy("c.id desc");
        List<Company> list = companyService.selectList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        util.exportExcel(response, list, "企业信息");
    }
}
