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.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.WindowOwnerDao;
import com.mortals.xhx.module.window.model.WindowOwnerEntity;
import com.mortals.xhx.module.window.service.WindowOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.window.model.WindowOwnerDetailEntity;
import com.mortals.xhx.module.window.model.WindowOwnerDetailQuery;
import com.mortals.xhx.module.window.service.WindowOwnerDetailService;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Arrays;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * WindowOwnerService
 * 窗口负责人 service实现
 *
 * @author zxfei
 * @date 2024-01-26
 */
@Service("windowOwnerService")
@Slf4j
public class WindowOwnerServiceImpl extends AbstractCRUDServiceImpl<WindowOwnerDao, WindowOwnerEntity, Long> implements WindowOwnerService {

    @Autowired
    private WindowOwnerDetailService windowOwnerDetailService;


    @Autowired
    private IWindowFeign windowFeign;

    @Override
    public List<WindowPdu> subWindowPduList(WindowPdu pdu) {
        List<WindowPdu> subList = new ArrayList<>();
        List<WindowPdu> ownerList = new ArrayList<>();
        List<WindowOwnerDetailEntity> ownerDetail = windowOwnerDetailService.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();
            }

            if(!ObjectUtils.isEmpty(pdu.getStaffId())){
                //查询当前负责人负责的窗口
                WindowOwnerDetailQuery detailQuery = new WindowOwnerDetailQuery();
                detailQuery.setOwnerId(pdu.getStaffId());
                Long[] windowIds = windowOwnerDetailService.find(detailQuery).stream().map(WindowOwnerDetailEntity::getId).toArray(Long[]::new);
                if(!ObjectUtils.isEmpty(windowIds)){
                    WindowPdu ownerWindowPdu = new WindowPdu();
                    ownerWindowPdu.setIdList(Arrays.asList(windowIds));
                    Rest<RespData<List<WindowPdu>>> ownerRest = windowFeign.list(ownerWindowPdu);
                    if (ownerRest.getCode() == YesNoEnum.YES.getValue()) {
                        ownerList=ownerRest.getData().getData();
                    }
                }
            }
        }

        ownerList.addAll(subList);
        return ownerList;
    }

    @Override
    protected void saveAfter(WindowOwnerEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getWindowOwnerDetailList())) {
            entity.getWindowOwnerDetailList().stream().peek(item -> {
                item.setOwnerId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            windowOwnerDetailService.save(entity.getWindowOwnerDetailList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(WindowOwnerEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getWindowOwnerDetailList())) {
            Long[] windowOwnerDetailIds = windowOwnerDetailService.find(new WindowOwnerDetailQuery().ownerId(entity.getId())).stream().map(WindowOwnerDetailEntity::getId).toArray(Long[]::new);
            windowOwnerDetailService.remove(windowOwnerDetailIds, context);
            entity.getWindowOwnerDetailList().stream().peek(item -> {
                item.setOwnerId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            windowOwnerDetailService.save(entity.getWindowOwnerDetailList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<WindowOwnerDetailEntity> windowOwnerDetaillist = windowOwnerDetailService.find(new WindowOwnerDetailQuery().ownerIdList(Arrays.asList(ids)));
        windowOwnerDetailService.removeList(windowOwnerDetaillist, context);
        super.removeAfter(ids, context, result);
    }
}