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

import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
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.List;

/**
 * @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;

    /**
     * 查询区经角色下管辖的门店权限
     *
     * @param clerkId
     * @param wxEnterpriseId
     * @return
     */
    public List<String> queryClerkStoreIds(String clerkId, String wxEnterpriseId) {
        List<String> storeIds = staffApiService.getHaoBanStoreIdsRolesByClerkId(clerkId, wxEnterpriseId);
        if (CollectionUtils.isEmpty(storeIds)) {
            log.info("根据区经id查询管辖的门店为空 {} {}", clerkId, wxEnterpriseId);
            return Collections.singletonList("-100");
        }
        if (storeIds.size() == 1 && StringUtils.equals(storeIds.get(0), "-1")) {
            // 全部门店权限
            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) {
        StaffClerkRelationDTO clerkRelationDTO = staffClerkRelationApiService.getByClerkId(clerkId);
        if (clerkRelationDTO != null) {
            StaffDTO staffDTO = staffApiService.selectById(clerkRelationDTO.getStaffId());
            return staffDTO;
        }
        return null;
    }

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

}
