package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.domain.PcArchivesFactory;
import cn.cecep.talroad.mapper.PcArchivesFactoryMapper;
import cn.cecep.talroad.service.PcArchivesFactoryService;
import cn.cecep.talroad.vo.PcArchivesFactoryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PcArchivesFactoryServiceImpl extends ServiceImpl<PcArchivesFactoryMapper, PcArchivesFactory> implements PcArchivesFactoryService {

    @Override
    public PcArchivesFactoryVo getByFactoryId(String factoryId) {
        List<PcArchivesFactory> archivesFactories = list(new QueryWrapper<PcArchivesFactory>()
                .eq("factory_id", factoryId)
                .orderByAsc("archives_code")
        );

        if (archivesFactories.size() < PcArchivesFactory.Archives.values().length) {
            init(factoryId);

            archivesFactories = list(new QueryWrapper<PcArchivesFactory>()
                    .eq("factory_id", factoryId)
                    .orderByAsc("archives_code")
            );
        }

        // 重点关注列表
        List<PcArchivesFactoryVo.Archives> followList = archivesFactories.stream()
                .filter(o -> o.getFollow().equals("1"))
                .map(o -> {
                    PcArchivesFactory.Archives archivesEnum = PcArchivesFactory.Archives.getByCode(o.getArchivesCode());

                    PcArchivesFactoryVo.Archives archives = new PcArchivesFactoryVo.Archives();
                    archives.setPcArchivesFactoryId(o.getId());
                    archives.setName(archivesEnum.getLabel());
                    archives.setUrl(archivesEnum.getUrl());
                    return archives;
                }).collect(Collectors.toList());

        // 非重点关注, 按照类型分组
        Map<String, List<PcArchivesFactory>> typeMap = archivesFactories.stream()
                .filter(o -> o.getFollow().equals("0"))
                .collect(Collectors.groupingBy(PcArchivesFactory::getType));

        List<PcArchivesFactoryVo.Type> typeList = typeMap.entrySet().stream().map(entry -> {
            List<PcArchivesFactoryVo.Archives> archives = new ArrayList<>();
            List<PcArchivesFactoryVo.Archives> hideArchives = new ArrayList<>();

            for (PcArchivesFactory archivesFactory : entry.getValue()) {
                PcArchivesFactory.Archives archivesEnum = PcArchivesFactory.Archives.getByCode(archivesFactory.getArchivesCode());
                PcArchivesFactoryVo.Archives vo = new PcArchivesFactoryVo.Archives();
                vo.setPcArchivesFactoryId(archivesFactory.getId());
                vo.setName(archivesEnum.getLabel());
                vo.setUrl(archivesEnum.getUrl());

                if (archivesFactory.getHide().equals("1")) {
                    hideArchives.add(vo);
                } else {
                    archives.add(vo);
                }
            }
            PcArchivesFactoryVo.Type type = new PcArchivesFactoryVo.Type();
            type.setName(PcArchivesFactory.Type.getLabelByCode(entry.getKey()));
            type.setArchives(archives);
            type.setHideArchives(hideArchives);
            return type;
        }).collect(Collectors.toList());

        PcArchivesFactoryVo vo = new PcArchivesFactoryVo();
        vo.setFollowList(followList);
        vo.setTypeList(typeList);
        return vo;
    }

    @Override
    public void deleteById(String id) {
        update(new UpdateWrapper<PcArchivesFactory>().set("is_hide", "1").eq("id", id));
    }

    @Override
    public void revert(String id) {
        update(new UpdateWrapper<PcArchivesFactory>().set("is_hide", "0").eq("id", id));
    }

    @Override
    public void follow(String id) {
        update(new UpdateWrapper<PcArchivesFactory>().set("is_follow", "1").eq("id", id));
    }

    @Override
    public void cancelFollow(String id) {
        update(new UpdateWrapper<PcArchivesFactory>().set("is_follow", "0").eq("id", id));
    }

    @Override
    public void init(String factoryId) {
        if (factoryId == null || factoryId.length() == 0) {
            throw new RuntimeException("企业id不可以为空");
        }
        List<PcArchivesFactory> archivesFactories = list(new QueryWrapper<PcArchivesFactory>().eq("factory_id", factoryId));

        List<PcArchivesFactory> addList = new ArrayList<>();

        for (PcArchivesFactory.Archives value : PcArchivesFactory.Archives.values()) {
            long count = archivesFactories.stream().filter(o -> o.getArchivesCode().equals(value.getCode())).count();
            if (count == 0) {
                PcArchivesFactory af = new PcArchivesFactory();
                af.setFactoryId(factoryId);
                af.setType(value.getType().getCode());
                af.setArchivesCode(value.getCode());
                af.setHide("0");
                af.setFollow("0");
                addList.add(af);
            }
        }
        if (!addList.isEmpty()) {
            saveBatch(addList);
        }
    }

}
