package com.tofly.gpsboot.controller.home;

import com.tofly.api.UserTaskClient;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.UserTaskTypeEnum;
import com.tofly.pojo.comm.vo.TodoStatisticalInfoVo;
import com.tofly.pojo.comm.vo.TypeDistributionVo;
import com.tofly.pojo.message.Usertask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 首页-接口信息
 *
 * @author BZ-DJJ
 */
@RestController
@RequestMapping("/homeInformation")
@Api(tags = "首页接口信息")
@Slf4j
public class HomeInformationController {

    @Autowired
    private UserTaskClient userTaskClient;

    /**
     * 调用BASE模块 提供待办事项待完成数量统计 及 待办事项未开始的数量统计(根据时间范围处理) 及超时统计
     *
     * @return
     */
    @ApiOperation(value = "获取待办事项统计信息(所有)")
    @GetMapping("/getCommissionStatisticalInfo")
    public ResultRespone<TodoStatisticalInfoVo> getCommissionStatisticalInfo() {
        ResultRespone<TodoStatisticalInfoVo> resultRespone = new ResultRespone<>();
        // 获取传入用户下 任务
        Usertask dto = new Usertask();
        dto.setHandleUser(SecurityUtils.getUserId());

        List<Usertask> list = userTaskClient.getUserTaskInfo(dto).getResult();
        if (null != list && list.size() > 0) {
            // 巡检超时去掉
            List<Usertask> collect = list.stream()
                    .filter(u-> StringUtil.isNotBlank(u.getTkty()))
                    .filter(u -> !(u.getTkty().equals("2") && u.getIshandle().equals("3"))).collect(Collectors.toList());
            // 区分出未开始对象数量
            long count = collect.stream().filter(info -> "2".equals(info.getIshandle())).count();
            // 超时工单发现有3和4的情况
            long count1 = collect.stream().filter(info -> "4".equals(info.getIshandle()) || "3".equals(info.getIshandle())).count();
            long count2 = collect.stream().filter(info -> "0".equals(info.getIshandle())).count();
            TodoStatisticalInfoVo vo = new TodoStatisticalInfoVo();
            vo.setNotStartedNum(count);
            vo.setOverTimeNum(count1);
            vo.setPendingNum(count2);
            resultRespone.setResult(vo);
            resultRespone.setCode(1);
        }

        return resultRespone;
    }

    @ApiOperation(value = "根据代办类型类型获取对应待办事项分组信息(所有)")
    @GetMapping("/getTypeDistributionInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isHandle", value = "代办类型(0待完成，1已完成,2未开始,3已超时,4已超时待完成)", required = true, dataType = "String"),
    })
    public ResultRespone<List<TypeDistributionVo>> getTypeDistributionInfo(String isHandle) {
        List<TypeDistributionVo> voList = new ArrayList<>();
        ResultRespone<List<TypeDistributionVo>> resultRespone = new ResultRespone<>();
        List<String> isHandleList = Arrays.stream(isHandle.split(Constant.CSV))
                .collect(Collectors.toList());
        isHandleList.forEach(type->{
            Usertask dto = new Usertask();
            dto.setHandleUser(SecurityUtils.getUserId());
            dto.setIshandle(type);
            // 根据传入消息状态 获取对应消息列表数据
            List<Usertask> result = userTaskClient.getUserTaskInfo(dto).getResult();
            if (null != result && result.size() > 0) {
                // 再以消息类型分组
                Map<String, List<Usertask>> collect = result.stream().filter(info->null!=info.getTkty()).collect(Collectors.groupingBy(Usertask::getTkty));
                collect.forEach((k, v) -> {
                    // 若是巡检类型 且超时的需要过滤
                    if(!(k.equals("2") && type.equals("3"))){
                        TypeDistributionVo vo = new TypeDistributionVo();
                        String name = UserTaskTypeEnum.getCodeForValue(k);
                        String route = UserTaskTypeEnum.getCodeForRoute(k);
                        vo.setTypeCode(k);
                        vo.setTypeName(name);
                        vo.setRoute(route);
                        vo.setNum(v.size());
                        voList.add(vo);
                    }
                });
            }
        });

        resultRespone.setCode(1);
        resultRespone.setResult(voList);
        return resultRespone;
    }
}
