package com.mortals.xhx.module.window.service.impl;
import com.alibaba.fastjson.JSONObject;
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.*;
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<>();
        List<WindowOwnerDetailEntity> ownerDetail = this.getAllList();
        if(!CollectionUtils.isEmpty(ownerDetail)){
            Long[] windows = ownerDetail.stream().map(WindowOwnerDetailEntity::getWindowId).toArray(Long[]::new);
            pdu.setIdNotList(Arrays.asList(windows));
            if(pdu.getSiteId()==null){
                pdu.setSiteId(1l); //默认只查宜宾市民中心
            }
            pdu.setSize(-1);
            Rest<RespData<List<WindowPdu>>> respDataRest = windowFeign.list(pdu);
            log.info(JSONObject.toJSONString(respDataRest));
            if (respDataRest.getCode() == YesNoEnum.YES.getValue()) {
                subList = respDataRest.getData().getData();
            }
        }
        return subList;
    }
}