package com.pm.web.controller.enterpriseInfo;

import com.pm.common.annotation.Log;
import com.pm.common.config.PageData;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.core.page.TableDataInfo;
import com.pm.common.enums.BusinessType;
import com.pm.common.utils.StringUtils;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.enterpriseInfo.service.IEnterpriseService;
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.util.List;

@RestController
@RequestMapping("/system/enterprise")
public class EnterpriseController extends BaseController {

    @Autowired
    private IEnterpriseService enterpriseService;

    /**
     * 查询企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = enterpriseService.selectEnterpriseList(pd);
        return getDataTable(list);
    }

    /**
     * 导出企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:export')")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        PageData pd = this.getPageData();
        List<PageData> list = enterpriseService.selectEnterpriseList(pd);
        ExcelUtil<PageData> util = new ExcelUtil<PageData>(PageData.class);
        util.exportExcel(response, list, "企业信息数据");
    }

    /**
     * 获取企业信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:query')")
    @GetMapping(value = "/{enterpriseId}")
    public AjaxResult getInfo(@PathVariable("enterpriseId") Long enterpriseId) {
        return AjaxResult.success(enterpriseService.selectEnterpriseByEnterpriseId(enterpriseId));
    }

    /**
     * 新增企业信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:add')")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PageData pd) {
        pd.put("createBy", getUsername());
        return toAjax(enterpriseService.insertEnterprise(pd));
    }

    /**
     * 修改企业信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:edit')")
    @Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PageData pd) {
        pd.put("updateBy", getUsername());
        return toAjax(enterpriseService.updateEnterprise(pd));
    }

    /**
     * 删除企业信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:remove')")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{enterpriseIds}")
    public AjaxResult remove(@PathVariable Long[] enterpriseIds) {
        return toAjax(enterpriseService.deleteEnterpriseByEnterpriseIds(enterpriseIds));
    }
    /**
     * 获取企业统计数据
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:list')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics() {
        PageData statistics = enterpriseService.getEnterpriseStatistics();
        return success(statistics);
    }
}
