package com.mortals.xhx.module.pj.service.impl;

import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.ICacheService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.pj.model.PjEvaluateQuery;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
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.pj.dao.PjEvaluateDao;
import com.mortals.xhx.module.pj.model.PjEvaluateEntity;
import com.mortals.xhx.module.pj.service.PjEvaluateService;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * PjEvaluateService
 * 评价汇总 service实现
 *
 * @author zxfei
 * @date 2024-05-27
 */
@Service("pjEvaluateService")
@Slf4j
public class PjEvaluateServiceImpl extends AbstractCRUDServiceImpl<PjEvaluateDao, PjEvaluateEntity, Long> implements PjEvaluateService {

    @Autowired
    private ISiteFeign siteFeign;

    @Autowired
    private ICacheService cacheService;

    @Override
    public void getSite(PjEvaluateEntity entity) {
        if (!ObjectUtils.isEmpty(entity.getSiteId())) {

            RedisTemplate<String, String> baseRedisTemplate = cacheService.selectDbRedisTemplate(1);

            //com:mortals:xhx:module:site:model:SiteEntity  redis调用
            String siteStr = (String) baseRedisTemplate.opsForHash().get("com:mortals:xhx:module:site:model:SiteEntity", String.valueOf(entity.getSiteId()));
            if (!ObjectUtils.isEmpty(siteStr)) {
                SitePdu sitePdu = JSON.parseObject(siteStr, SitePdu.class);
                if (!ObjectUtils.isEmpty(sitePdu)) {
                    entity.setSiteCode(sitePdu.getSiteCode());
                    entity.setSiteName(sitePdu.getSiteName());
                } else {
                    log.info("站点信息不存在,siteId={}", entity.getSiteId());
                }
            }

            //feign调用
/*            Rest<SitePdu> sitePduRest = siteFeign.info(entity.getSiteId());
            if (YesNoEnum.YES.getValue() == sitePduRest.getCode()) {
                SitePdu sitePdu = sitePduRest.getData();
                if (!ObjectUtils.isEmpty(sitePdu)) {
                    entity.setSiteCode(sitePdu.getSiteCode());
                    entity.setSiteName(sitePdu.getSiteName());
                } else {
                    log.info("站点信息不存在,siteId={}", entity.getSiteId());
                }
            }*/
        } else {
            if (!ObjectUtils.isEmpty(entity.getSiteCode())) {
                SitePdu pdu = new SitePdu();
                pdu.setSiteCode(entity.getSiteCode());

                Rest<RespData<List<SitePdu>>> sitePduRest = siteFeign.list(pdu);

                if (YesNoEnum.YES.getValue() == sitePduRest.getCode()) {
                    List<SitePdu> list = sitePduRest.getData().getData();
                    if (!ObjectUtils.isEmpty(list)) {
                        entity.setSiteId(list.get(0).getId());
                        entity.setSiteCode(list.get(0).getSiteCode());
                        entity.setSiteName(list.get(0).getSiteName());
                    } else {
                        log.info("站点信息不存在,siteId={}", entity.getSiteId());
                    }
                }
            }
        }
    }

    @Override
    public void removeDuplicate(List<PjEvaluateEntity> list) {
        //去重 关键exenum
        List<String> extNumList = list.stream().map(m -> m.getExtNum()).filter(f -> f != null).collect(Collectors.toList());
        PjEvaluateQuery pjEvaluateQuery = new PjEvaluateQuery();
        pjEvaluateQuery.setExtNumList(extNumList);
        List<PjEvaluateEntity> pjEvaluateEntities = this.find(pjEvaluateQuery);
        if (!ObjectUtils.isEmpty(pjEvaluateEntities)) {
            Set<String> existSet = pjEvaluateEntities.stream().map(item -> item.getExtNum()).collect(Collectors.toSet());
            Iterator<PjEvaluateEntity> iterator = list.iterator();
            while (iterator.hasNext()) {
                PjEvaluateEntity next = iterator.next();
                if (existSet.contains(next.getExtNum())) {
                    log.info("排号重复,extNum={}", next.getExtNum());
                    iterator.remove();
                }
            }
        }
    }
}