/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.crunii.com</a>
 */
package com.crunii.micro.service.sysmgr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.crunii.micro.common.constants.CommonConstants;
import com.crunii.micro.common.exception.BusinessException;
import com.crunii.micro.domain.entity.main.TsmAppButton;
import com.crunii.micro.domain.repo.main.TsmAppButtonRepo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AppButtonC;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AppButtonU;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AppButtonVo;
import com.crunii.micro.service.sysmgr.jdbc.AppButtonJdbcDao;
import com.crunii.micro.service.sysmgr.service.TsmAppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author shenhs create 2023/6/14 15:00
 */
@Slf4j
@Service
public class TsmAppServiceImpl implements TsmAppService {
	@Autowired
	private TsmAppButtonRepo buttonRepo;

	@Autowired
	private AppButtonJdbcDao buttonJdbcDao;

	@Override public void createAppButton(AppButtonC buttonC) {
		List<TsmAppButton> allByCode = buttonRepo.findAllByCodeAndState(buttonC.getCode(),CommonConstants.STATE_VALID);
		if(buttonC.getType()==TYPE_BUTTON && !allByCode.isEmpty()){
			throw new BusinessException("编码重复");
		}
		TsmAppButton t = new TsmAppButton();
		BeanUtil.copyProperties(buttonC, t);
		t.setCreateTime(new Date());
		if(buttonC.getParentId()==null){
			t.setParentId(DEF_APP_BUTTON_ID);
		}
		t.setState(CommonConstants.STATE_VALID);
		buttonRepo.save(t);
	}

	@Override public void updateAppButton(AppButtonU buttonU) {
		Optional<TsmAppButton> byId = buttonRepo.findById(buttonU.getBtnId());
		if(byId.isPresent()){
			TsmAppButton tsmAppButton = byId.get();
			String code = tsmAppButton.getCode();
			List<TsmAppButton> allByCode = buttonRepo.findAllByCodeAndState(buttonU.getCode(),CommonConstants.STATE_VALID);
			if(buttonU.getType()==TYPE_BUTTON && !buttonU.getCode().equals(code) && !allByCode.isEmpty()){
				throw new BusinessException("编码重复");
			}
		}

		TsmAppButton one = buttonRepo.getOne(buttonU.getBtnId());
		if (one==null){
			throw new BusinessException("记录不存在");
		}
		BeanUtil.copyProperties(buttonU,one);
		buttonRepo.save(one);
	}

	@Override public void deleteAppButton(Integer id) {
		TsmAppButton one = buttonRepo.getOne(id);
		if (one!=null){
//			one.setState(CommonConstants.STATE_INVALID);
			buttonRepo.delete(one);
		}
	}

	@Override public List<AppButtonVo> queryAppButton(Integer id) {
		List<TsmAppButton> allByParentId = buttonRepo.findAllByParentId(id);
		List<Integer> collect = allByParentId.stream().filter(item -> item.getType() == TYPE_PATH).map(item -> item.getBtnId())
				.collect(Collectors.toList());
		List<Integer> parentId = new ArrayList<>();
		if(!collect.isEmpty()){
			parentId = buttonJdbcDao.findParentId(collect);
		}

		List<Integer> finalParentId = parentId;
		List<AppButtonVo> collect1 = allByParentId.stream().map(item -> {
			AppButtonVo buttonVo = new AppButtonVo();
			BeanUtil.copyProperties(item, buttonVo);
			if (finalParentId.contains(item.getBtnId())) {
				buttonVo.setLeaf(false);
			} else {
				buttonVo.setLeaf(true);
			}
			return buttonVo;
		}).sorted(Comparator.comparingInt(AppButtonVo::getSort)).collect(Collectors.toList());

		return collect1;
	}
}
