package com.storlead.sales.controller.report;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.enums.ResourceTypeEnum;
import com.storlead.sales.pojo.dto.task.TaskQueryDTO;
import com.storlead.sales.pojo.entity.task.TaskEntity;
import com.storlead.sales.service.customer.BusinessService;
import com.storlead.sales.service.task.TaskService;
import io.swagger.annotations.Api;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: sp-sales-platform
 * @description: 首页
 * @author: chenkq
 * @create: 2024-10-21 14:14
 */
@RestController
@RequestMapping("/analysis/report/home")
@Api(tags = "报表: 数据分析报表")
public class HomeDataAnalysisApiController {

    @Resource
    private TaskService taskService;

    @Resource
    private BusinessService businessService;
    /**
     * 获取任务日程
     * @return
     */
    public Result getTaskScheduleData(@RequestBody TaskQueryDTO param) {
        LambdaQueryWrapper<TaskEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(TaskEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        if (StrUtil.isNotBlank(param.getBlurry())) {
            queryWrapper.like(TaskEntity::getTaskContent, param.getBlurry());
        }
        if (StrUtil.isNotBlank(param.getResourceType())) {
            if (param.getResourceType().equals(ResourceTypeEnum.CUSTOMER.code) || param.getResourceType().equals(ResourceTypeEnum.CLUS.code)) {
                queryWrapper.eq(TaskEntity::getCustomerId, param.getResourceTypeId());
            } else {
                queryWrapper.eq(TaskEntity::getResourceType, param.getResourceType());
                queryWrapper.eq(TaskEntity::getResourceTypeId, param.getResourceTypeId());
            }
        }
        if (Objects.nonNull(param.getTaskStatus())) {
            queryWrapper.eq(TaskEntity::getTaskStatus, param.getTaskStatus());
        }
        if (Objects.nonNull(param.getBeginTime())) {
            queryWrapper.ge(TaskEntity::getTaskBeginTime, param.getBeginTime());
        }
        if (Objects.nonNull(param.getEndTime())) {
            queryWrapper.le(TaskEntity::getTaskBeginTime, param.getEndTime());
        }
        queryWrapper.orderByDesc(TaskEntity::getTaskBeginTime);
        List<TaskEntity> taskls = taskService.listWithScope(queryWrapper);
        if (!CollectionUtils.isEmpty(taskls)) {
            taskls.stream().forEach(e -> {
                String dataStr = e.getTaskBeginTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                e.setTaskData(dataStr);
            });
            Map<String,List<TaskEntity>> taskMap = taskls.stream().collect(Collectors.groupingBy(TaskEntity::getTaskData));
            return Result.result(taskMap);
        }
        return Result.result(Collections.EMPTY_MAP);
    }
}
