package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.EpsBusiness;
import com.ruoyi.system.service.IEpsBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Random;

/**
 * 业务信息Controller
 * 
 * @author gyt
 * @date 2025-08-18
 */
@RestController
@RequestMapping("/business")
public class EpsBusinessController extends BaseController
{
    @Autowired
    private IEpsBusinessService epsBusinessService;

    /**
     * 查询业务信息列表
     */
    @RequiresPermissions("system:business:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody EpsBusiness epsBusiness)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(epsBusiness.getPageNum());
        pageDomain.setPageSize(epsBusiness.getPageSize());
        startPage(pageDomain);
        List<EpsBusiness> list = epsBusinessService.selectEpsBusinessList(epsBusiness);
        return getDataTable(list);
    }

    /**
     * 查询业务信息列表
     */
    @RequiresPermissions("system:business:list")
    @PostMapping("/getAllBusinessMsg")
    public AjaxResult getAllBusinessMsg(@RequestBody EpsBusiness epsBusiness)
    {
        List<EpsBusiness> list = epsBusinessService.selectEpsBusinessList(epsBusiness);
        return success(list);
    }

    /**
     * 导出业务信息列表
     */
    @RequiresPermissions("system:business:export")
    @Log(title = "业务信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody EpsBusiness epsBusiness)
    {
        List<EpsBusiness> list = epsBusinessService.selectEpsBusinessList(epsBusiness);
        ExcelUtil<EpsBusiness> util = new ExcelUtil<EpsBusiness>(EpsBusiness.class);
        util.showColumn(epsBusiness.getProperties());
        util.exportExcel(response, list, "业务信息数据");
    }

    /**
     * 获取业务信息详细信息
     */
    @RequiresPermissions("system:business:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(epsBusinessService.selectEpsBusinessById(id));
    }

    /**
     * 新增业务信息
     */
    @RequiresPermissions("system:business:add")
    @Log(title = "业务信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EpsBusiness epsBusiness)
    {
        int rows = epsBusinessService.insertEpsBusiness(epsBusiness);
        if(-1==rows){
            return AjaxResult.error(500,"业务名称不可重复");
        }
        return toAjax(rows);
    }

    /**
     * 修改业务信息
     */
    @RequiresPermissions("system:business:edit")
    @Log(title = "业务信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EpsBusiness epsBusiness)
    {
        int rows = epsBusinessService.updateEpsBusiness(epsBusiness);
        if(-1==rows){
            return AjaxResult.error(500,"业务名称不可重复");
        }
        return toAjax(rows);
    }


    /**
     * 生成12位唯一标识
     */
    @RequiresPermissions("system:business:list")
    @GetMapping("/getBusinessCode")
    public String getBusinessCode()
    {
        long timestamp = System.currentTimeMillis() / 100; // 取前10位（精确到0.1秒）
        int random = new Random().nextInt(100); // 2位随机数（00~99）
        return String.format("%010d%02d", timestamp, random);
    }
    /**
     * 验证业务名称是否存在
     * @param epsBusiness
     * @return
     */
    @RequiresPermissions("system:business:list")
    @PostMapping("/countByBusinessName")
    public AjaxResult countByBusinessName(@RequestBody EpsBusiness epsBusiness)
    {
        int rows = epsBusinessService.countByBusinessName(epsBusiness);
        if(rows>0){
            return AjaxResult.error(500,"业务名称不可重复");
        }else {
            return AjaxResult.success("添加成功");
        }
    }

}
