package com.yh.saas.plugin.yiliangyiyun.service.impl;


import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.winsea.svc.base.base.entity.CommonStaffVessel;
import com.winsea.svc.base.base.service.ICommonStaffVesselService;
import com.winsea.svc.base.base.service.ICommonVesselService;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.winsea.svc.notice.entity.base.BaseNotice.AcceptStatus;
import com.winsea.svc.notice.service.INoticeUtilService;
import com.yh.saas.common.support.util.query.QueryUtils;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.yh.saas.plugin.yiliangyiyun.service.INewNoticeUtilService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class NewNoticeUtilServiceImpl implements INewNoticeUtilService {
    private static final Logger log = LoggerFactory.getLogger(com.winsea.svc.notice.service.impl.NoticeUtilServiceImpl.class);
    @SofaReference
    private ICommonVesselService vesselService;
    @SofaReference
    private ICommonStaffVesselService staffVesselService;

    public NewNoticeUtilServiceImpl() {
    }

    @Override
    public Wrapper<?> getCurrentUserNoticeTaskWrapper(Wrapper<?> wrapper, Class c) {
        String staffId = AuthSecurityUtils.getCurrentUserId();
        List<CommonStaffVessel> staffVessels = this.staffVesselService.findStaffVessel(AuthSecurityUtils.getCurrentUserId());
        Map<String, List<CommonStaffVessel>> map = (Map)staffVessels.stream().collect(Collectors.groupingBy(CommonStaffVessel::getRoleId));
        Set<String> roleIds = map.keySet();
        List<String> vesselIds = (List)staffVessels.stream().map(CommonStaffVessel::getVesselId).collect(Collectors.toList());
        Wrapper<?> currentTaskWrapper = new EntityWrapper();
        currentTaskWrapper.orNew().eq("accept_type", AcceptStatus.All);
        currentTaskWrapper.orNew().in("accept_id", staffId).eq("accept_type", AcceptStatus.STAFF).and(QueryUtils.getQueryString(this.getFilterVesselWrapper(vesselIds), c), new Object[0]);
        Iterator var9 = roleIds.iterator();

        while(var9.hasNext()) {
            String roleId = (String)var9.next();
            List<CommonStaffVessel> staffRoleVessels = this.staffVesselService.findStaffVessel(AuthSecurityUtils.getCurrentUserId(), roleId);
            List<String> staffRoleVesselIds = (List)staffRoleVessels.stream().map(CommonStaffVessel::getVesselId).collect(Collectors.toList());
            currentTaskWrapper.orNew().in("accept_id", roleIds).eq("accept_type", AcceptStatus.ROLE).and(QueryUtils.getQueryString(this.getFilterVesselWrapper(staffRoleVesselIds), c), new Object[0]);
        }

        wrapper.andNew(QueryUtils.getQueryString(currentTaskWrapper, c), new Object[0]);
        return wrapper;
    }

    @Override
    public Wrapper<?> getFilterVesselWrapper(List<String> vesselIds) {
        Wrapper<?> wrapper = new EntityWrapper();
        if (CollectionUtils.isEmpty(vesselIds)) {
            wrapper.isNull("vessel_id");
        } else {
            List<String> newVesselIds = this.vesselService.getVesselIdByVesselStatusAndVesselId(vesselIds);
            wrapper.in("vessel_id", newVesselIds).or().isNull("vessel_id");
        }

        return wrapper;
    }

    @Override
    public Wrapper<?> getFilterVesselWrapper(String vesselId) {
        Wrapper<?> wrapper = new EntityWrapper();
        if (StringUtils.isEmpty(vesselId)) {
            wrapper.isNull("vessel_id");
        } else {
            List<String> newVesselIds = this.vesselService.getVesselIdByVesselStatusAndVesselId(Lists.newArrayList(new String[]{vesselId}));
            wrapper.in("vessel_id", newVesselIds).or().isNull("vessel_id");
        }

        return wrapper;
    }
}