package com.crunii.micro.service.sysmgr.service.impl;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.exception.BusinessException;
import com.crunii.micro.core.cache.TsmURLCache;
import com.crunii.micro.domain.entity.main.TsmUrl;
import com.crunii.micro.domain.entity.main.TsmUrlGroup;
import com.crunii.micro.domain.entity.main.TsmUrlGroupUrl;
import com.crunii.micro.domain.repo.main.TsmUrlGroupRepo;
import com.crunii.micro.domain.repo.main.TsmUrlGroupUrlRepo;
import com.crunii.micro.domain.repo.main.TsmUrlRepo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.*;
import com.crunii.micro.service.sysmgr.jdbc.UrlJdbcDao;
import com.crunii.micro.service.sysmgr.service.UrlService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 田平 create 2019年5月29日下午2:30:42
 */
@Service
public class UrlServiceImpl implements UrlService {

	@Autowired
	private TsmUrlRepo tsmUrlRepo;

	@Autowired
	private TsmUrlGroupRepo tsmUrlGroupRepo;

	@Autowired
	private TsmUrlGroupUrlRepo tsmUrlGroupUrlRepo;

	@Autowired
	private UrlJdbcDao urlJdbcDao;

	@Autowired
	private TsmURLCache cache;

	@Override
	public List<TsmUrl> getUrlsOfGroup(int groupId) {
		return urlJdbcDao.getUrlsOfGroup(groupId);
	}

	@Override
	public PageRsp<TsmUrl> pageUrl(PageReq<UrlReq> req) {
		return urlJdbcDao.pageUrl(req);
	}

	@Override
	public void createUrl(UrlC urlC) {
		TsmUrl byUrlPath = tsmUrlRepo.findByUrlPath(urlC.getUrlPath());
		if(Objects.nonNull(byUrlPath)){
			throw new BusinessException("存在相同的url地址！");
		}
		TsmUrl url = new TsmUrl();
		BeanUtils.copyProperties(urlC, url);
		url.setState(1);
		url.setLogFlag(0);
		url.setUrlType(1);
		url.setCreateTime(new Date());
		tsmUrlRepo.save(url);
		cache.reload();
	}

	@Override
	public void updateUrl(UrlU urlU) {
		TsmUrl url = tsmUrlRepo.getOne(urlU.getUrlId());
		BeanUtils.copyProperties(urlU, url);
		tsmUrlRepo.save(url);
		cache.reload();
	}

	@Override
	public PageRsp<TsmUrlGroup> pageUrlGroup(PageReq<UrlGroupReq> req) {
		return urlJdbcDao.pageUrlGroup(req);
	}

	@Override
	public void createUrlGroup(UrlGroupC urlGroupC) {
		TsmUrlGroup group = new TsmUrlGroup();
		BeanUtils.copyProperties(urlGroupC, group);
		group.setState(1);
		group.setCreateTime(new Date());
		tsmUrlGroupRepo.save(group);
	}

	@Override
	public void updateUrlGroup(UrlGroupU urlGroupU) {
		TsmUrlGroup url = tsmUrlGroupRepo.getOne(urlGroupU.getGroupId());
		BeanUtils.copyProperties(urlGroupU, url);
		tsmUrlGroupRepo.save(url);
	}

	@Override
	public void doUrlGroupRelaUrl(UrlGroupRelaUrl rela) {
		Map<String, Object> props = new HashMap<>(2);
		props.put("groupId", rela.getGroupId());
		List<TsmUrlGroupUrl> oldRelas = tsmUrlGroupUrlRepo.findByProps(props);
		tsmUrlGroupUrlRepo.deleteAll(oldRelas);
		tsmUrlGroupUrlRepo.flush();
		List<TsmUrlGroupUrl> newRelas = new ArrayList<>(rela.getUrlIds().size());
		rela.getUrlIds().forEach((urlId) -> {
			TsmUrlGroupUrl tsmUrlGroupUrl = new TsmUrlGroupUrl();
			tsmUrlGroupUrl.setGroupId(rela.getGroupId());
			tsmUrlGroupUrl.setUrlId(urlId);
			newRelas.add(tsmUrlGroupUrl);
		});
		tsmUrlGroupUrlRepo.saveAll(newRelas);
	}

	@Override
	public List<UrlGroupUrlCB> getCheckedUrlGroupUrl(int groupId) {
		List<Integer> relaUrlIds = urlJdbcDao.getUrlsOfGroup(groupId).stream().map(TsmUrl::getUrlId).collect(Collectors.toList());
		List<TsmUrl> validUrls = tsmUrlRepo.findByProp("state", 1);
		List<UrlGroupUrlCB> ugu = new ArrayList<>(validUrls.size());
		validUrls.forEach(url -> {
			UrlGroupUrlCB urlGroupUrlCb = new UrlGroupUrlCB();
			BeanUtils.copyProperties(url, urlGroupUrlCb);
			if (relaUrlIds.contains(url.getUrlId())) {
				urlGroupUrlCb.setChecked(true);
			}
			ugu.add(urlGroupUrlCb);
		});
		return ugu;
	}

	@Override
	public TsmUrl getTsmUrl(int urlId){
		return tsmUrlRepo.getOne(urlId);
	}
}
