package com.ruoyi.frequency.enterprise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.frequency.enterprise.entity.Enterprise;
import com.ruoyi.frequency.enterprise.model.result.EnterpriseCount;
import com.ruoyi.frequency.enterprise.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业信息Controller
 *
 * @author liwenlong
 * @date 2023-10-26
 */
@RestController
@RequestMapping("/frequency/enterprise")
public class EnterpriseController extends BaseController
{
    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 查询企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('frequency:enterprise:list')")
    @PostMapping("/list")
    public TableDataInfo<Enterprise> list(@RequestBody Enterprise enterprise)
    {
        startPage();
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        return getDataTable(list);
    }

    /**
     * 统计
     */
    @PostMapping("/statistics")
    public AjaxResult statistics(@RequestBody Enterprise enterprise)
    {
        EnterpriseCount count = enterpriseService.statistics(enterprise);
        return AjaxResult.success(count);
    }

    /**
     * 导出企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('frequency:enterprise:export')")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Enterprise enterprise)
    {
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        ExcelUtil<Enterprise> util = new ExcelUtil<Enterprise>(Enterprise.class);
        return util.exportExcel(list, "企业信息数据");
    }

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

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

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

    /**
     * 删除企业信息
     */
    @PreAuthorize("@ss.hasPermi('frequency:enterprise:remove')")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}/{delFlag}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids,@PathVariable("delFlag") Integer delFlag)
    {
        return toAjax(enterpriseService.deleteEnterpriseByIds(ids,delFlag));
    }

    /**
     * 企业端--获取企业信息
     */
    @PostMapping("/enterpriseInfo")
    public AjaxResult enterpriseInfo()
    {
        Long userId = SecurityUtils.getUserId();
        Enterprise one = enterpriseService.getOne(new QueryWrapper<Enterprise>().lambda().eq(Enterprise::getSysUserId, userId));

        if (one == null){
            return AjaxResult.error("当前登入账号错误");
        }
        return AjaxResult.success(enterpriseService.selectEnterpriseById(one.getId()));
    }
}
