package com.ruoyi.ibs.task.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.ibs.task.domain.dto.AlterForwardRequest;
import com.ruoyi.ibs.task.domain.dto.WorkRecordDTO;
import com.ruoyi.ibs.task.domain.entity.Alter;
import com.ruoyi.ibs.task.domain.entity.AlterConfig;
import com.ruoyi.ibs.task.domain.entity.WorkRecord;
import com.ruoyi.ibs.task.domain.vo.*;
import com.ruoyi.ibs.task.service.WorkRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

/**
 * 工作清单记录Controller
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@Api(tags = "工作清单接口")
@RestController
@RequestMapping("/work/record")
public class WorkRecordController extends BaseController {

    @Resource
    private WorkRecordService workRecordService;

    /**
     * 查询我的工作清单--首页工作台
     */
    @GetMapping("/list")
    @Log(title = "工作台-查询我的工作清单")
    @ApiOperation("查询我的工作清单")
    public TableDataPageInfo<WorkRecordVO> list(WorkRecordDTO workRecordDTO) {
        Page<Object> page = startPage();
        List<WorkRecordVO> list = workRecordService.getWorkRecordList(workRecordDTO);
        return getDataTable(list, page);
    }

    /**
     * 查询我的工作清单--工作台（区分管理员与普通客户经理）
     */
    @GetMapping("/allList")
    @Log(title = "工作台-查询我的工作清单")
    @ApiOperation("查询所有工作清单")
    public TableDataPageInfo<WorkRecordVO> allList(WorkRecordDTO workRecordDTO) {
        return workRecordService.getAllWorkRecordList(workRecordDTO);
    }

    /**
     * 批量提交更新工作清单记录
     */
    @PostMapping("/submit")
    @Log(title = "工作台-批量更新工作清单")
    @ApiOperation("批量更新工作清单")
    public AjaxResult batchSubmit(@Validated @RequestBody List<WorkRecord> workRecordList) {
        return toAjax(workRecordService.batchSubmitWorkRecord(workRecordList));
    }

    /**
     * 查询权限内用户工作清单完成情况
     */
    @GetMapping(value = "/complete/status")
    @Log(title = "工作台-查询工作清单完成情况")
    @ApiOperation("查询工作清单完成情况")
    public TableDataPageInfo<WorkCompleteVO> getCompleteStatus(WorkRecordDTO workRecordDTO) {
        return workRecordService.getCompleteStatus(workRecordDTO);
    }

    /**
     * 查询我的预警信息--首页工作台
     */
    @GetMapping("/alter/list")
    @Log(title = "工作台-查询预警信息")
    @ApiOperation("查询预警信息")
    public TableDataPageInfo<AlterVO> alterList(String status, String alterType) {
        Page<Object> page = startPage();
        List<AlterVO> list = workRecordService.getAlterList(status, alterType);
        return getDataTable(list, page);
    }

    /**
     * 查询我的预警信息--工作台（区分管理员与普通客户经理）
     */
    @GetMapping("/alter/allList")
    @Log(title = "工作台-查询预警信息")
    @ApiOperation("查询所有预警信息")
    public TableDataPageInfo<AlterVO> allAlterList(String status, String alterType, String forwardFlag) {
        Page<Object> page = startPage();
        List<AlterVO> list = workRecordService.getAllAlterList(status, alterType, forwardFlag);
        return getDataTable(list, page);
    }

    @GetMapping("/alter/forward/meta")
    @Log(title = "工作台-查询预警转发元数据")
    @ApiOperation("查询预警转发元数据")
    public AjaxResult getAlterForwardMeta(@RequestParam String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .filter(org.apache.commons.lang3.StringUtils::isNotBlank)
                .map(Long::valueOf)
                .collect(Collectors.toList());
        return AjaxResult.success(workRecordService.getAlterForwardMeta(idList));
    }

    @PostMapping("/alter/forward")
    @Log(title = "工作台-转发预警信息")
    @ApiOperation("批量转发预警信息")
    public AjaxResult forwardAlter(@Validated @RequestBody AlterForwardRequest request) {
        AlterForwardResultVO result = workRecordService.forwardAlter(request);
        if (result.getSuccessCount() > 0 && result.getFailCount() > 0) {
            return AjaxResult.success("成功转发" + result.getSuccessCount() + "条，失败" + result.getFailCount() + "条", result);
        }
        if (result.getSuccessCount() > 0) {
            return AjaxResult.success("成功转发" + result.getSuccessCount() + "条", result);
        }
        return AjaxResult.error("未能成功转发，失败" + result.getFailCount() + "条");
    }

    @GetMapping("/alter/count")
    @Log(title = "工作台-查询预警推送次数")
    @ApiOperation("查询预警推送次数")
    public AlterCountVO getAlterCount(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date reportTime) {
        return workRecordService.getAlterCount(reportTime);
    }

    @GetMapping("/alterConfig/list")
    @Log(title = "工作台-查询预警配置信息")
    @ApiOperation("查询预警配置信息")
    public TableDataPageInfo<AlterConfigVO> getAlterConfigList(String alterType) {
        Page<Object> page = startPage();
        List<AlterConfigVO> list = workRecordService.getAlterConfigList(alterType);
        return getDataTable(list, page);
    }

    @PostMapping("/alterConfig/edit")
    @Log(title = "工作台-修改预警配置信息")
    @ApiOperation("修改预警配置信息")
    public AjaxResult editAlterConfig(@RequestBody AlterConfig alterConfig) {
        return toAjax(workRecordService.updateAlterConfig(alterConfig));
    }

    @PostMapping("/read")
    @Log(title = "工作台-设置状态为已读")
    @ApiOperation("设置状态为已读")
    public AjaxResult readAlter(@RequestParam Long id) {
        return toAjax(workRecordService.updateReadTime(id));
    }

    /**
     * 查询所有预警类型
     */
    @GetMapping("/alter/types")
    @Log(title = "工作台-查询预警类型")
    @ApiOperation("查询所有预警类型")
    public AjaxResult getAlterTypes() {
        List<String> types = workRecordService.getAlterTypes();
        return AjaxResult.success(types);
    }

}
