package com.artfess.rescue.event.controller;


import com.alibaba.fastjson.JSONObject;
import com.artfess.base.annotation.ApiGroup;
import com.artfess.base.constants.ApiGroupConsts;
import com.artfess.base.enums.ResponseErrorEnums;
import com.artfess.base.model.CommonResult;
import com.artfess.base.query.PageList;
import com.artfess.base.query.QueryFilter;
import com.artfess.base.util.JsonUtil;
import com.artfess.base.valid.AddGroup;
import com.artfess.base.valid.UpdateGroup;
import com.artfess.poi.util.ExcelUtil;
import com.artfess.rescue.event.dto.FullEventDto;
import com.artfess.rescue.event.vo.*;
import com.artfess.rescue.monitor.aop.WebSocketNotify;
import com.artfess.rescue.monitor.eunms.RefreshType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.artfess.base.controller.BaseController;
import com.artfess.rescue.event.model.BizEventInfo;
import com.artfess.rescue.event.manager.BizEventInfoManager;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 事件信息 前端控制器
 *
 * @company 阿特菲斯信息技术有限公司
 * @author 系统管理员
 * @since 2024-08-05
 */
@RestController
@RequestMapping("/event/bizEventInfo/v1/")
@Api(tags = "事件信息")
@ApiGroup(group = {ApiGroupConsts.GROUP_BIZ_RESCUE})
public class BizEventInfoController extends BaseController<BizEventInfoManager, BizEventInfo> {

    @Override
    @PostMapping("/")
    @ApiOperation("添加实体的接口")
    @WebSocketNotify(topic = RefreshType.SSSJ, operateType = "ADD")
    public CommonResult<String> create(@ApiParam(name = "model", value = "实体信息") @Validated({AddGroup.class}) @RequestBody BizEventInfo t) {
        boolean res = baseService.saveInfo(t);
        return new CommonResult<>(res?"添加成功":"添加失败");
    }

    @DeleteMapping("/")
    @ApiOperation("根据id集合批量删除")
    @WebSocketNotify(topic = RefreshType.SSSJ, operateType = "DELETE")
    public CommonResult<String> deleteByIds(@ApiParam(name = "ids", value = "实体集合") @RequestParam String... ids) {
        boolean result = baseService.removeByIds(Arrays.asList(ids));
        if (!result) {
            return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "删除实体失败");
        }
        return new CommonResult<>();
    }

    @Override
    @PutMapping("/")
    @ApiOperation("更新实体")
    @WebSocketNotify(topic = RefreshType.DLYXJC, operateType = "UPDATE")
    public CommonResult<String> updateById(@ApiParam(name = "model", value = "实体信息") @Validated({UpdateGroup.class}) @RequestBody BizEventInfo t) {
        boolean res = baseService.updateInfo(t);
        if (!res) {
            return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "更新实体失败");
        }
        return new CommonResult<>("更新实体成功");
    }

    @RequestMapping(value = "appoint", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "事件委派", httpMethod = "POST", notes = "事件委派")
    @WebSocketNotify(topic = RefreshType.SSSJ, operateType = "ADD")
    public CommonResult<String> appoint(@RequestBody FullEventDto entity) {
        boolean res = baseService.appoint(entity);
        return new CommonResult<>(res,res?"委派成功":"委派失败");
    }

    @RequestMapping(value = "cancel", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "事件撤销", httpMethod = "POST", notes = "事件撤销")
    public CommonResult<String> cancel(@RequestParam("id") String id) {
        boolean res = baseService.cancel(id);
        return new CommonResult<>(res,res?"撤销成功":"撤销失败");
    }

    @RequestMapping(value = "end", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "事件完结", httpMethod = "POST", notes = "事件完结")
    public CommonResult<String> cancel(@RequestBody BizEventInfo t) {
        boolean res = baseService.end(t);
        return new CommonResult<>(res,res?"完结成功":"完结失败");
    }

    @Override
    @GetMapping("/{id}")
    @ApiOperation("根据id查询实体-用于app端")
    public EventInfoVO getById(@ApiParam(name = "id", value = "实体id") @PathVariable String id) {
        return baseService.getInfoById(id);
    }

    @GetMapping("/full/{id}")
    @ApiOperation("根据id查询实体-用于pc端")
    public EventInfoVO getFullById(@ApiParam(name = "id", value = "实体id") @PathVariable String id) {
        return baseService.getFullInfoById(id);
    }

    @Override
    @RequestMapping(value = "query", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "分页查询", httpMethod = "POST", notes = "分页查询")
    public PageList<BizEventInfo> query(@RequestBody QueryFilter<BizEventInfo> queryFilter) {
        return baseService.queryByIPage(queryFilter);
    }

    @RequestMapping(value = "queryByPower", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "带权限分页查询", httpMethod = "POST", notes = "带权限分页查询")
    public PageList<BizEventInfo> queryByPower(@RequestBody QueryFilter<BizEventInfo> queryFilter) {
        return baseService.queryByPower(queryFilter);
    }

    @PostMapping("/ledger/export")
    @ApiOperation(value = "台账导出", httpMethod = "POST", notes = "台账导出")
    public ResponseEntity<?> exportToBrowser(@RequestBody QueryFilter<BizEventInfo> queryFilter){
        return baseService.exportLedger(queryFilter);
    }

    @PostMapping("/ledger")
    @ApiOperation(value = "台账详情", httpMethod = "POST", notes = "台账详情")
    public EventLedgerVO getEventTaskLedger(@RequestBody QueryFilter<BizEventInfo> queryFilter){
        return baseService.getEventTaskLedger(queryFilter);
    }

    @PostMapping("/ledger/excelExport")
    @ApiOperation(value = "台账详情", httpMethod = "POST", notes = "台账详情导出")
    public void getEventTaskLedger(HttpServletResponse response,
                                   @RequestBody QueryFilter<BizEventInfo> queryFilter){
         baseService.EventLedgerExport(response,queryFilter);
    }

    @RequestMapping(value = "/count/type", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "事件统计-部门事件统计分类表", httpMethod = "POST", notes = "部门事件统计分类表")
    public CommonResult<JSONObject> countEventByType(@RequestBody QueryFilter<BizEventInfo> queryFilter) {
        return baseService.countEventByType(queryFilter);
    }

    @PostMapping(value = "/export/type")
    @ApiOperation(value = "excel导出-部门事件统计分类表", httpMethod = "POST", notes = "巡检统计-巡查部门汇总统计")
    public void exportByContent(HttpServletResponse response, @RequestBody QueryFilter<BizEventInfo> queryFilter) throws Exception {
        CommonResult<JSONObject> result = baseService.countEventByType(queryFilter);
        JSONObject object= result.getValue();
        List<CountEventByTypeVO> voList = (List<CountEventByTypeVO>) object.get("data");
        List<Map<String, Object>> list = new ArrayList<>();
        if (voList != null && voList.size() > 0) {
            int i = 1;
            for (CountEventByTypeVO vo : voList) {
                String jsonStr = JsonUtil.toJson(vo);
                Map<String, Object> map = JsonUtil.toMap(jsonStr);
                //序号
                map.put("sn", i);
                i++;
                list.add(map);
            }
        }
        String tempName = "路段事件统计分类表";
        Map<String, String> exportMaps = new LinkedHashMap<>();
        exportMaps.put("sn", "序号");
        exportMaps.put("roadName", "路段名称");
        exportMaps.put("roadAssetLoss", "路产损失(元)");
        exportMaps.put("accidentDisaster", "事故灾难(件)");
        exportMaps.put("constructEvent", "养护施工(件)");
        exportMaps.put("geologicalDisaster", "地质灾害(件)");
        exportMaps.put("meteorologicalDisaster", "气象灾害(件)");
        exportMaps.put("socialSecurityEvent", "社会安全事件(件)");
        exportMaps.put("otherEvent", "其他事件(件)");
        HSSFWorkbook book = ExcelUtil.exportExcel(tempName, 24,exportMaps, list);
        ExcelUtil.downloadExcel(book, tempName, response);
    }

    @RequestMapping(value = "/count/time", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "事件统计-事件处置时间统计", httpMethod = "POST", notes = "事件处置时间统计")
    public CommonResult<List<CountEventByTimeVO>> countEventByTime(@RequestBody QueryFilter<BizEventInfo> queryFilter) {
        return baseService.countEventByTime(queryFilter);
    }

    @RequestMapping(value = "/dayReport", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "运行信息-日报", httpMethod = "POST", notes = "运行信息-日报")
    public PageList<TrafficControlIlboVO> dayReport(@RequestBody QueryFilter<BizEventInfo> queryFilter) {
        return baseService.runInfoDayReport(queryFilter);
    }
    @RequestMapping(value = "/monthReport", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "运行信息-月报", httpMethod = "POST", notes = "运行信息-月报")
    public CommonResult<JSONObject> monthReport(@RequestBody QueryFilter<BizEventInfo> queryFilter) {
        JSONObject object = baseService.runInfoMonthReport(queryFilter);
        return new CommonResult<>(true,"",object);
    }
}
