package com.hz.pm.api.expert.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hz.pm.api.common.model.CommonPage;
import com.hz.pm.api.expert.assembler.ExpertUserInfoAssembler;
import com.hz.pm.api.expert.constant.ExpertAccountStatusEnum;
import com.hz.pm.api.expert.entity.ExpertUserFullInfo;
import com.hz.pm.api.expert.mapper.ExpertAdminManageMapper;
import com.hz.pm.api.expert.model.cmd.ExpertAdminExpertManageQueryCmd;
import com.hz.pm.api.expert.model.dto.ExpertAdminExpertListDTO;
import com.hz.pm.api.expert.model.query.ExpertDictionaryQuery;
import com.hz.pm.api.expert.model.query.ExpertTagQuery;
import com.hz.pm.api.expert.model.query.ListExpertQuery;
import com.hz.pm.api.expert.service.ExpertAdminManageService;
import com.hz.pm.api.expert.service.IExpertUserFullInfoService;
import com.hz.pm.api.meta.model.entity.ExpertDictionary;
import com.hz.pm.api.meta.service.IExpertDictionaryService;
import com.hz.pm.api.meta.service.IExpertTagService;
import com.ningdatech.basic.util.CollUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liuxinxin
 * @date 2023/3/10 上午10:37
 */
@Service
@RequiredArgsConstructor
public class ExpertAdminManageServiceImpl implements ExpertAdminManageService {

    private final IExpertUserFullInfoService iExpertUserFullInfoService;
    private final IExpertDictionaryService iExpertDictionaryService;
    private final IExpertTagService iExpertTagService;
    private final ExpertAdminManageMapper expertAdminManageMapper;

    private List<Long> listExpertUserId(ListExpertQuery listExpertQuery) {
        List<Long> userIdList = expertAdminManageMapper.listExpertUserId(listExpertQuery);

        List<ExpertTagQuery> expertTagQueryList = listExpertQuery.getExpertTagQueryList();
        if (CollectionUtils.isNotEmpty(expertTagQueryList)) {
            List<Long> tagUserIdList = expertAdminManageMapper.listExpertTagUserId(listExpertQuery);
            userIdList.retainAll(tagUserIdList);
        }

        List<ExpertDictionaryQuery> expertDictQueryList = listExpertQuery.getExpertDictionaryQueryList();
        if (CollectionUtils.isNotEmpty(expertDictQueryList)) {
            List<Long> dictionaryUserIdList = expertAdminManageMapper.listExpertDictionaryUserId(listExpertQuery);
            userIdList.retainAll(dictionaryUserIdList);
        }
        return userIdList;
    }


    @Override
    public CommonPage<ExpertAdminExpertListDTO> getExpertLibraryList(ExpertAdminExpertManageQueryCmd req) {
        ListExpertQuery listExpertQuery = buildListExpertQuery(req);
        List<Long> userIdList = listExpertUserId(listExpertQuery);

        CommonPage<ExpertAdminExpertListDTO> commonPage = new CommonPage<>();
        commonPage.setCurrentPageNumber(req.getPageNumber());
        commonPage.setItemsTotal((long) userIdList.size());

        List<ExpertAdminExpertListDTO> recordList = new ArrayList<>();

        if (!userIdList.isEmpty()) {
            List<Long> currPageUserIdList = CollUtil.page(req.getPageNumber() - 1, req.getPageSize(), userIdList);
            List<ExpertUserFullInfo> expertUserList = iExpertUserFullInfoService.listByUserIds(currPageUserIdList);

            // 获取专家字典数据
            List<ExpertDictionary> expertDictionaryList = iExpertDictionaryService.listByUserId(userIdList);
            Map<Long, List<ExpertDictionary>> expertDictMap = CollUtils.group(expertDictionaryList, ExpertDictionary::getUserId);

            // 获取专家标签数据
            for (ExpertUserFullInfo expertUserFullInfo : expertUserList) {
                ExpertAdminExpertListDTO expertAdminExpertManageListDTO = ExpertUserInfoAssembler.convert(expertUserFullInfo, expertDictMap);
                recordList.add(expertAdminExpertManageListDTO);
            }
        }
        commonPage.setItems(recordList);
        return commonPage;
    }

    public ListExpertQuery buildListExpertQuery(ExpertAdminExpertManageQueryCmd queryCmd) {
        ListExpertQuery query = new ListExpertQuery();
        query.setExpertName(queryCmd.getExpertName());
        query.setCompany(queryCmd.getCompany());
        query.setExpertDictionaryQueryList(queryCmd.getExpertDictionaryQueryList());
        query.setExpertTagQueryList(queryCmd.getExpertTagQueryList());
        query.setRegionContainsList(queryCmd.getRegionContainsList());
        query.setIntentionRegionList(queryCmd.getIntentionRegionList());
        query.setIsDingUser(queryCmd.getIsDingUser());
        query.setLimit(queryCmd.getLimit());
        query.setOffset(queryCmd.getOffset());
        query.setIsDingUser(queryCmd.getIsDingUser());
        if (CollUtil.isNotEmpty(queryCmd.getExpertAccountStatusList())) {
            query.setExpertAccountStatusList(CollUtils.fieldList(queryCmd.getExpertAccountStatusList(), ExpertAccountStatusEnum::getKey));
        }
        return query;
    }

    @Override
    public List<Long> filterExpertUserIdList(ExpertAdminExpertManageQueryCmd queryCmd) {
        ListExpertQuery listExpertQuery = buildListExpertQuery(queryCmd);
        return listExpertUserId(listExpertQuery);
    }
}
