package com.zcloud.accident.web;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.event.producer.RemoteEventPublisher;
import com.zcloud.accident.api.AccidentServiceI;
import com.zcloud.accident.command.convertor.AccidentExportConvertor;
import com.zcloud.accident.command.convertor.EventExportConvertor;
import com.zcloud.accident.dto.AccidentAddCmd;
import com.zcloud.accident.dto.AccidentCountQry;
import com.zcloud.accident.dto.AccidentPageQry;
import com.zcloud.accident.dto.AccidentUpdateCmd;
import com.zcloud.accident.dto.clientobject.AccidentCO;
import com.zcloud.accident.dto.clientobject.AccidentCorpCountStatCO;
import com.zcloud.accident.dto.clientobject.AccidentCountStatCO;
import com.zcloud.accident.dto.clientobject.AccidentExportCO;
import com.zcloud.accident.dto.clientobject.EventExportCO;
import com.zcloud.accident.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * web-adapter
 *
 * @Author fangjiakai
 * @Date 2025-10-29 14:28:30
 */
@Api(tags = "事故记录表")
@RequestMapping("/${application.gateway}/accident")
@RestController
@AllArgsConstructor
public class AccidentController {
    private final AccidentServiceI accidentService;
    private final AccidentExportConvertor accidentExportConvertor;
    private final EventExportConvertor eventExportConvertor;
    @ApiOperation("新增")
    @PostMapping("/save")
    public SingleResponse<AccidentCO> add(@Validated @RequestBody AccidentAddCmd cmd) {
        cmd.setDepartmentId(AuthContext.getOrgId());
        return accidentService.add(cmd);
    }

    @ApiOperation("分页")
    @PostMapping("/list")
    public PageResponse<AccidentCO> page(@RequestBody AccidentPageQry qry) {
        return accidentService.listPage(qry);
    }

    @ApiOperation("所有数据")
    @GetMapping("/listAll")
    public MultiResponse<AccidentCO> listAll() {
        return MultiResponse.of(new ArrayList<AccidentCO>());
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public SingleResponse<AccidentCO> getInfoById(@PathVariable("id") Long id) {
        return SingleResponse.of(accidentService.queryById(id));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Response remove(@PathVariable("id") Long id) {
        accidentService.remove(id);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("删除多个")
    @DeleteMapping("/ids")
    public Response removeBatch(@RequestParam Long[] ids) {
        accidentService.removeBatch(ids);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("修改")
    @PutMapping("/edit")
    public SingleResponse edit(@Validated @RequestBody AccidentUpdateCmd accidentUpdateCmd) {
        accidentService.edit(accidentUpdateCmd);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("导出Excel")
    @GetMapping("/export")
    public void export(AccidentPageQry qry, HttpServletResponse response) throws IOException {
        if(ObjectUtil.isNull(qry.getEqCorpinfoId())){
            qry.setEqCorpinfoId(AuthContext.getTenantId());
        }
        // 使用listAll方法获取所有符合条件的数据
        List<AccidentCO> accidentCOList = accidentService.listAll(qry);
        if(qry.getEqType() == 1){
            // 转换为导出CO
            List<EventExportCO> exportCOList = eventExportConvertor.convertToExportCOList(accidentCOList);
            // 生成文件名
            String fileName = "事件数据_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            // 调用工具类导出Excel
            ExcelUtils.exportExcel(response, EventExportCO.class, fileName, exportCOList);
        }else{
            // 转换为导出CO
            List<AccidentExportCO> exportCOList = accidentExportConvertor.convertToExportCOList(accidentCOList);
            // 生成文件名
            String fileName = "事故数据_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            // 调用工具类导出Excel
            ExcelUtils.exportExcel(response, AccidentExportCO.class, fileName, exportCOList);
        }
    }

    @ApiOperation("按公司统计事件和事故数量")
    @PostMapping("/countByCorpinfoAndType")
    public MultiResponse<AccidentCorpCountStatCO> countByCorpinfoAndType(@RequestBody AccidentCountQry accidentCountQry) {
        return accidentService.countByCorpinfo(accidentCountQry);
    }

    @ApiOperation("按照事故类型钻取统计个数")
    @PostMapping("/countByIncidentType")
    public MultiResponse<AccidentCountStatCO> countByCorpinfoIdAndIncidentType(@RequestBody AccidentCountQry accidentCountQry) {
        return accidentService.countByCorpinfoIdAndIncidentType(accidentCountQry);
    }
}

