package com.mortals.xhx.module.window.service.impl;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.window.WindowPdu;
import com.mortals.xhx.feign.window.IWindowFeign;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.window.dao.WindowOwnerDetailDao;
import com.mortals.xhx.module.window.model.WindowOwnerDetailEntity;
import com.mortals.xhx.module.window.service.WindowOwnerDetailService;
import lombok.extern.slf4j.Slf4j;

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

/**
* WindowOwnerDetailService
* 窗口负责人详细 service实现
*
* @author zxfei
* @date 2023-07-12
*/
@Service("windowOwnerDetailService")
@Slf4j
public class WindowOwnerDetailServiceImpl extends AbstractCRUDServiceImpl<WindowOwnerDetailDao, WindowOwnerDetailEntity, Long> implements WindowOwnerDetailService {

    @Autowired
    private IWindowFeign windowFeign;

    @Override
    public List<WindowPdu> subWindowPduList(WindowPdu pdu) {
        List<WindowPdu> subList = new ArrayList<>();
        if(pdu.getSiteId()==null){
            pdu.setSiteId(1l); //默认只查宜宾市民中心
        }
        pdu.setSize(-1);
        Rest<RespData<List<WindowPdu>>> respDataRest = windowFeign.list(pdu);
        if (respDataRest.getCode() == YesNoEnum.YES.getValue()) {
            List<WindowOwnerDetailEntity> ownerDetail = this.getAllList();
            if(!CollectionUtils.isEmpty(ownerDetail)){
                Map<Long,List<WindowOwnerDetailEntity>> windowMap = ownerDetail.stream().collect(Collectors.groupingBy(WindowOwnerDetailEntity::getWindowId));
                List<WindowPdu> allWindow = respDataRest.getData().getData();
                for(WindowPdu item:allWindow){
                    if(!windowMap.containsKey(item.getId())){
                        subList.add(item);
                    }
                }
            }
        }
        return subList;
    }
}