package com.muyu.system.controller;

import com.muyu.system.service.ISysFirmService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.system.domain.SysFirm;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 企业基础信息Controller
 *
 * @author muyu
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/firmInfo")
public class SysFirmController extends BaseController
{
    @Resource
    private ISysFirmService sysFirmService;


    /**
     * 查询企业基础信息列表
     */
    @RequiresPermissions("firm:firmInfo:list")
    @GetMapping("/list")
    public Result<TableDataInfo<SysFirm>> list(SysFirm sysFirm)
    {
        startPage();
        List<SysFirm> list = sysFirmService.selectSysFirmList(sysFirm);
        return getDataTable(list);
    }

    /**
     * 导出企业基础信息列表
     */
    @RequiresPermissions("firm:firmInfo:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFirm sysFirm)
    {
        List<SysFirm> list = sysFirmService.selectSysFirmList(sysFirm);
        ExcelUtil<SysFirm> util = new ExcelUtil<SysFirm>(SysFirm.class);
        util.exportExcel(response, list, "企业基础信息数据");
    }

    /**
     * 获取企业基础信息详细信息
     */
    @RequiresPermissions("firm:firmInfo:query")
    @GetMapping(value = "/{id}")
    public Result<List<SysFirm>> getInfo(@PathVariable("id") Long id)
    {
        return success(sysFirmService.selectSysFirmById(id));
    }

    /**
     * 新增企业基础信息
     */
    @RequiresPermissions("firm:firmInfo:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody SysFirm sysFirm)
    {
        if (sysFirmService.checkIdUnique(sysFirm)) {
            return error("新增 企业基础信息 '" + sysFirm + "'失败，企业基础信息已存在");
        }
        sysFirm.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysFirmService.save(sysFirm));
    }

    /**
     * 修改企业基础信息
     */
    @RequiresPermissions("firm:firmInfo:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody SysFirm sysFirm)
    {
        if (!sysFirmService.checkIdUnique(sysFirm)) {
            return error("修改 企业基础信息 '" + sysFirm + "'失败，企业基础信息不存在");
        }
        sysFirm.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysFirmService.updateById(sysFirm));
    }

    /**
     * 删除企业基础信息
     */
    @RequiresPermissions("firm:firmInfo:remove")
	@DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        sysFirmService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
