package com.fs.company.controller.fastGpt;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.fastGpt.domain.FastgptEventLogTotal;
import com.fs.fastGpt.param.FastgptEventLogTotalParam;
import com.fs.fastGpt.service.IFastGptRoleService;
import com.fs.fastGpt.service.IFastgptEventLogTotalService;
import com.fs.fastGpt.vo.FastgptEventLogTotalVo;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ai事件埋点统计Controller
 * 
 * @author fs
 * @date 2025-06-26
 */
@RestController
@RequestMapping("/fastGpt/fastgptEventLogTotal")
public class FastgptEventLogTotalController extends BaseController
{
    @Autowired
    private IFastgptEventLogTotalService fastgptEventLogTotalService;

    @Autowired
    private IFastGptRoleService roleService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询ai事件埋点统计列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastgptEventLogTotal:list')")
    @PostMapping("/list")
    public R pageList(@RequestBody FastgptEventLogTotalParam param)
    {

        FastgptEventLogTotalVo eventLogTotalVo = new FastgptEventLogTotalVo();
        BeanUtils.copyProperties(param,eventLogTotalVo);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        eventLogTotalVo.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FastgptEventLogTotalVo> list = fastgptEventLogTotalService.selectFastgptEventLogTotalVoInfoList(eventLogTotalVo);

        //统计对应的list的合
        FastgptEventLogTotalVo totalVo = fastgptEventLogTotalService.totalFastgptEventLog(list);

        int pageNum = param.getPageNum();
        int pageSize = param.getPageSize();

        int total = 0;
        if (list != null) {
            total = list.size();
        }

        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);
        PageInfo<FastgptEventLogTotalVo> pageInfo = new PageInfo<>();
        if (list != null) {
            List<FastgptEventLogTotalVo> paginatedList = list.subList(fromIndex, toIndex);
            if(totalVo != null){
                paginatedList.add(totalVo);
            }
            pageInfo = new PageInfo<>(paginatedList);
        }else{
            pageInfo = new PageInfo<>();
        }
        pageInfo.setTotal(total);
        return R.ok().put("data",pageInfo);
    }

    /**
     * 查询appKey
     * @return  list
     */
    @GetMapping("/getFastGptRoleAppKeyList")
    public R getFastGptRoleAppKeyList() {
        return R.ok().put("data", roleService.selectFastGptRoleAppKeyList());
    }
    /**
     * 导出ai事件埋点统计列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastgptEventLogTotal:export')")
    @Log(title = "ai事件埋点统计", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FastgptEventLogTotal fastgptEventLogTotal)
    {
        List<FastgptEventLogTotal> list = fastgptEventLogTotalService.selectFastgptEventLogTotalList(fastgptEventLogTotal);
        ExcelUtil<FastgptEventLogTotal> util = new ExcelUtil<FastgptEventLogTotal>(FastgptEventLogTotal.class);
        return util.exportExcel(list, "ai事件埋点统计数据");
    }

    /**
     * 获取ai事件埋点统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastgptEventLogTotal:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fastgptEventLogTotalService.selectFastgptEventLogTotalById(id));
    }

    /**
     * 新增ai事件埋点统计
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastgptEventLogTotal:add')")
    @Log(title = "ai事件埋点统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FastgptEventLogTotal fastgptEventLogTotal)
    {
        return toAjax(fastgptEventLogTotalService.insertFastgptEventLogTotal(fastgptEventLogTotal));
    }

    /**
     * 修改ai事件埋点统计
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastgptEventLogTotal:edit')")
    @Log(title = "ai事件埋点统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FastgptEventLogTotal fastgptEventLogTotal)
    {
        return toAjax(fastgptEventLogTotalService.updateFastgptEventLogTotal(fastgptEventLogTotal));
    }

    /**
     * 删除ai事件埋点统计
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastgptEventLogTotal:remove')")
    @Log(title = "ai事件埋点统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fastgptEventLogTotalService.deleteFastgptEventLogTotalByIds(ids));
    }
}
