package org.springblade.flow.visual.portal.service.impl;

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

import org.springblade.flow.base.entity.DictionaryDataEntity;
import org.springblade.flow.base.service.DictionaryDataService;
import org.springblade.flow.util.DateUtil;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.RandomUtil;
import org.springblade.flow.util.StringUtil;
import org.springblade.flow.visual.portal.entity.PortalEntity;
import org.springblade.flow.visual.portal.mapper.PortalMapper;
import org.springblade.flow.visual.portal.model.PortalPagination;
import org.springblade.flow.visual.portal.model.PortalSelectModel;
import org.springblade.flow.visual.portal.service.PortalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021/3/16
 */
@Service
public class PortalServiceImpl extends ServiceImpl<PortalMapper, PortalEntity> implements PortalService {

	@Autowired
	private PortalService portalService;
	@Autowired
	private DictionaryDataService dictionaryDataService;

	@Override
	public List<PortalEntity> getList(PortalPagination portalPagination) {
		QueryWrapper<PortalEntity> queryWrapper = new QueryWrapper<>();
		if (!StringUtil.isEmpty(portalPagination.getKeyword())) {
			queryWrapper.lambda().like(PortalEntity::getFullName, portalPagination.getKeyword());
		}
		queryWrapper.lambda().orderByDesc(PortalEntity::getCreatorTime);
		return list(queryWrapper);
	}

	@Override
	public List<PortalEntity> getList() {
		QueryWrapper<PortalEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().orderByAsc(PortalEntity::getSortCode);
		return this.list(queryWrapper);
	}

	@Override
	public PortalEntity getInfo(String id) {
		QueryWrapper<PortalEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(PortalEntity::getId, id);
		return this.getOne(queryWrapper);
	}

	@Override
	public void create(PortalEntity entity) {
		if (StringUtil.isEmpty(entity.getId())) {
			entity.setId(RandomUtil.uuId());
		}
		this.save(entity);
	}

	@Override
	public boolean update(String id, PortalEntity entity) {
		entity.setId(id);
		entity.setLastModifyTime(DateUtil.getNowDate());
		return this.updateById(entity);
	}

	@Override
	public void delete(PortalEntity entity) {
		if (entity != null) {
			this.removeById(entity.getId());
		}
	}

	@Override
	public List<PortalSelectModel> getList(String type) {
		List<PortalEntity> list = portalService.getList().stream()
				.filter(t -> "1".equals(String.valueOf(t.getEnabledMark()))).collect(Collectors.toList());
		List<PortalSelectModel> modelList = JsonUtil.getJsonToList(list, PortalSelectModel.class);
		for (PortalEntity portalEntity : list) {
			DictionaryDataEntity dictionaryDataEntity = dictionaryDataService.getInfo(portalEntity.getCategory());
			if (dictionaryDataEntity != null) {
				PortalSelectModel model = new PortalSelectModel();
				model.setId(dictionaryDataEntity.getId());
				model.setFullName(dictionaryDataEntity.getFullName());
				model.setParentId("0");
				if (!modelList.contains(model)) {
					modelList.add(model);
				}
			}
		}
		return modelList;
	}

}
