package jnpf.cultivate.controller.app.statistic;

import cn.hutool.core.collection.CollUtil;
import jnpf.base.ActionResult;
import jnpf.base.entity.ModuleEntity;
import jnpf.cultivate.service.FtbCultivateStatisticsService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.dto.statistics.FtbCultivateStatisticsDTO;
import jnpf.model.cultivate.vo.statistics.CultivateStatisticsForAppCommonParam;
import jnpf.model.cultivate.vo.statistics.NumberofAppSessions;
import jnpf.permission.eum.v2.OrganizeCategoryEnums;
import jnpf.permission.vo.v2.organzie.OrganizeGeneralDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * app培训统计
 *
 * @Author: peng.hao
 * @create: 2024/3/22 19:31
 */
@Slf4j
@RestController
@RequestMapping("/app/cultivate-statistics")
public class FtbCultivateStatisticsForAppController {


    @Resource
    private FtbCultivateStatisticsService ftbCultivateStatisticsService;

    @Autowired
    private UserApiV2Util userApiV2Util;

    /**
     * 培训统计
     *
     * @param dto
     * @return
     */
    @GetMapping("/query-number-sessions")
    public ActionResult<List<NumberofAppSessions>> trainingStatistics(FtbCultivateStatisticsDTO dto) {
        List<NumberofAppSessions> number = ftbCultivateStatisticsService.trainingStatistics(dto);
        return ActionResult.success(number);
    }

    /**
     * 培训统计
     *
     * @param dto
     * @return
     */
    @PostMapping("/query-number-sessions")
    public ActionResult<List<NumberofAppSessions>> trainingStatisticsPost(@RequestBody  FtbCultivateStatisticsDTO dto) {
        List<NumberofAppSessions> number = ftbCultivateStatisticsService.trainingStatistics(dto);
        return ActionResult.success(number);
    }
    /**
     * 培训统计子 菜单有数据验证
     *
     * @param dto
     * @return flag = "1"(无数据)  flag = null (有数据)
     */
    @PostMapping("/have-exit-data")
    public ActionResult<List<CultivateStatisticsForAppCommonParam>> doesTheMenuHaveDataVerification(@RequestBody FtbCultivateStatisticsDTO dto) {
        List<String> batchOrgIds = dto.getBatchOrgIds();
        List<OrganizeGeneralDetailVO> orgListForCurrUser = userApiV2Util.getOrgListForCurrUser(
                List.of(OrganizeCategoryEnums.COMPANY, OrganizeCategoryEnums.STORE, OrganizeCategoryEnums.DEPARTMENT, OrganizeCategoryEnums.TEAM),null);
        if (CollUtil.isEmpty(orgListForCurrUser)) {
            return ActionResult.success(new ArrayList<>());
        }
        if (CollUtil.isEmpty(dto.getBatchOrgIds())) {
            batchOrgIds = orgListForCurrUser.stream().map(OrganizeGeneralDetailVO::getId).collect(Collectors.toList());
        } else {
            List<String> intersection = UserApiV2Util.getIntersection(batchOrgIds, orgListForCurrUser.stream().map(OrganizeGeneralDetailVO::getId).collect(Collectors.toList()));
            if (CollUtil.isEmpty(intersection)) {
                return ActionResult.success(new ArrayList<>());
            } else {
                batchOrgIds = intersection;
            }
        }

        List<CultivateStatisticsForAppCommonParam> commonParams = batchOrgIds.stream().map(item -> {

            CultivateStatisticsForAppCommonParam param = new CultivateStatisticsForAppCommonParam();
            String flag = null;
            // 验证子集
//            ActionResult<List<String>> oneLevelById = organizeApi.getChildrenOneLevelById(item);
            List<OrganizeGeneralDetailVO> oneLevelById = userApiV2Util.organizeNextLevel(item, null);
            if (CollUtil.isNotEmpty(oneLevelById)) {
                List<String> data = oneLevelById.stream().map(OrganizeGeneralDetailVO::getId).collect(Collectors.toList());
                List<String> organizeApi = data.stream().filter(str -> !item.equals(str)).collect(Collectors.toList());
                if (CollUtil.isEmpty(organizeApi)) {
                    flag = "1";
                }
            } else {
                flag = "1";
            }
            param.setOrgId(item);
            param.setFlag(flag);
            return param;
        }).collect(Collectors.toList());
        return ActionResult.success(commonParams);
    }
}

