package com.gic.haoban.manage.web.controller.content.adaptor;

import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.dto.StoreInfoDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.manage.api.dto.StaffClerkInfoDTO;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.api.dto.role.StoreRoleDTO;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.api.service.StaffClerkRelationApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/3/25 10:29 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component
public class ClerkStoreAdaptor {

    @Autowired
    private StaffApiService staffApiService;
    @Autowired
    private StaffClerkRelationApiService staffClerkRelationApiService;
    @Autowired
    private ClerkService clerkService;
    @Autowired
    private StoreService storeService;

    /**
     * 查询区经角色下管辖的门店权限
     *
     * @param clerkId
     * @param wxEnterpriseId
     * @return
     */
    public List<String> queryClerkStoreIds(String clerkId, String wxEnterpriseId,List<String> storeStatusList) {
        List<String> storeIds = staffApiService.getHaobanStoreIdsRolesByClerkIdAndStoreStatus(clerkId, wxEnterpriseId,storeStatusList,null);
        if (CollectionUtils.isEmpty(storeIds)) {
            // 全部门店权限
            log.info("区经有全部门店权限 {}", clerkId);
            return Collections.emptyList();
        }
        return storeIds;
    }

    /**
     * 获取超管管理门店的总数
     * @param clerkId
     * @param wxEnterpriseId
     * @return
     */
    public Integer countAdminManagerStore(String clerkId, String wxEnterpriseId) {
        StoreRoleDTO storeRole = staffApiService.getCountHaoBanStoreIdsByClerkId(clerkId, wxEnterpriseId);
        if (storeRole == null) {
            return 0;
        }
        return storeRole.getStoreCount();
    }

    public StaffDTO queryStaff(String clerkId,String wxEnterpriseId) {
        StaffClerkRelationDTO clerkRelationDTO = staffClerkRelationApiService.getOneByClerkId(clerkId,wxEnterpriseId);
        if (clerkRelationDTO != null) {
            StaffDTO staffDTO = staffApiService.selectById(clerkRelationDTO.getStaffId());
            return staffDTO;
        }
        return null;
    }

    public ClerkDTO queryClerkInfo(String enterpriseId, String clerkId) {
        return clerkService.getClerkByClerkId(clerkId);
    }

    public List<StaffClerkRelationDTO> queryStaffClerkRelationInfo(String enterpriseId, String staffId) {
        List<StaffClerkRelationDTO> staffClerkRelationDTOS = staffClerkRelationApiService.listBindCodeByStaffId(Collections.singletonList(enterpriseId), staffId);
        if (CollectionUtils.isEmpty(staffClerkRelationDTOS)) {
            return Collections.emptyList();
        }
        return staffClerkRelationDTOS;
    }


    public List<ClerkDTO> queryClerkInfos(List<String> clerkIds) {
        if (CollectionUtils.isEmpty(clerkIds)) {
            return Collections.emptyList();
        }
        List<ClerkDTO> clerkDTOS = clerkService.getClerkByClerkIds(new HashSet<>(clerkIds));
        if (CollectionUtils.isEmpty(clerkDTOS)) {
            return Collections.emptyList();
        }
        return clerkDTOS;
    }

    public Map<String, ClerkDTO> queryClerkMap(List<String> clerkIds) {
        if (CollectionUtils.isEmpty(clerkIds)) {
            return Collections.emptyMap();
        }
        List<ClerkDTO> clerkDTOS = queryClerkInfos(clerkIds);
        if (CollectionUtils.isEmpty(clerkDTOS)) {
            return Collections.emptyMap();
        }
        return clerkDTOS.stream().collect(Collectors.toMap(ClerkDTO::getClerkId, clerkDTO -> clerkDTO));
    }

    public Map<String, StoreDTO> queryStoreMap(List<String> storeIds) {
        if (CollectionUtils.isEmpty(storeIds)) {
            return Collections.emptyMap();
        }
        String[] storeIdsArry = new String[storeIds.size()];
        List<StoreDTO> storeDTOS = storeService.getStores(storeIds.toArray(storeIdsArry));
        if (CollectionUtils.isEmpty(storeDTOS)) {
            return Collections.emptyMap();
        }
        return storeDTOS.stream().collect(Collectors.toMap(StoreDTO::getStoreId, storeDTO -> storeDTO));
    }
}
