package com.gyee.runeconomy.service.auto.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.mapper.auto.ProBasicEquipmentMapper;
import com.gyee.runeconomy.model.auto.ProBasicEquipment;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.service.auto.IProBasicEquipmentService;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 风机&光伏 服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-11-22
 */
@Service
public class ProBasicEquipmentServiceImpl extends ServiceImpl<ProBasicEquipmentMapper, ProBasicEquipment> implements IProBasicEquipmentService {
    @Resource
    private TokenService tokenService;

    @Override
    public List<ProBasicEquipment> getProBasicEquipmentList(String companyId, String windpowerstationIds, String projectIds, String lineIds, String type) {

        //
        //        List<ProBasicEquipment> list = CacheContext.wtls;
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        List<ProBasicEquipment> list = tokenService.getWtls(request);
        if (StringUtils.isNotEmpty(type) && !type.equals("0")) {
            String finalType = type.equals("-1") ? "_FDC" : "_GDC";
            list = list.stream().filter(eq -> eq.getWindpowerstationId().indexOf(finalType) >= 0).collect(Collectors.toList());
        }

        //判断后缀是区域还是公司
        if (StringUtils.isNotEmpty(companyId) &&(companyId.endsWith("_ZGS"))||companyId.endsWith("_FGS")) {
            list = list.stream().filter(eq -> companyId.equals(eq.getCompanyId())).collect(Collectors.toList());
        }
        if (StringUtils.isNotEmpty(companyId) && companyId.endsWith("_RGN")) {
            list = list.stream().filter(eq -> companyId.equals(eq.getRegionId())).collect(Collectors.toList());
        }


        if (StringUtils.isNotEmpty(windpowerstationIds)) {
            list = list.stream().filter(eq -> windpowerstationIds.contains(eq.getWindpowerstationId())).collect(Collectors.toList());
        }

        if (StringUtils.isNotEmpty(projectIds)) {
            list = list.stream().filter(eq -> projectIds.contains(eq.getProjectId())).collect(Collectors.toList());
        }

        if (StringUtils.isNotEmpty(lineIds)) {
            list = list.stream().filter(eq -> lineIds.contains(eq.getLineId())).collect(Collectors.toList());
        }

        SortUtils.sort(list, "nemCode", SortUtils.ASC);
        return list;
    }

    @Override
    public List<ProBasicEquipment> getProBasicEquipmentList(String wpid) {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        List<ProBasicEquipment> list = tokenService.getWtls(request);
        return list.stream().filter(i -> i.getWindpowerstationId().equals(wpid)).collect(Collectors.toList());
    }

    @Override
    public List<ProBasicEquipment> getProBasicEquipmentList2(String wpid) {
        List<ProBasicEquipment> wtlist = new ArrayList<>();
        for (ProBasicEquipment wt : CacheContext.wtls) {
            ProBasicEquipment newWt = new ProBasicEquipment();
            BeanUtil.copyProperties(wt, newWt);
            wtlist.add(newWt);
        }
        return wtlist;
    }

}
