package com.zcxk.charge.web.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.charge.core.dao.SceneMapper;
import com.zcxk.charge.core.dao.SceneTypeMapper;
import com.zcxk.charge.core.entity.SceneTypeEntity;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * 场景类型Service接口
 *
 * @author: WYY
 * @date 2020-11-17 19:31
 */
@Service
public class SceneTypeService extends ServiceImpl<SceneTypeMapper, SceneTypeEntity> {

    @Resource
    private SceneTypeMapper sceneTypeMapper;

    /**
     * 查列表
     */
    public List<SceneTypeEntity> findListByParentId(SceneTypeEntity sceneTypeEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        sceneTypeEntity.setTenantId(currentUser.getTenantId());
        List<SceneTypeEntity> sceneTypeEntities = sceneTypeMapper.findList(sceneTypeEntity);
        return sceneTypeEntities;
    }
    /**
     * 查列表
     */
    public List<SceneTypeEntity> findList(SceneTypeEntity sceneTypeEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        sceneTypeEntity.setTenantId(currentUser.getTenantId());
        List<SceneTypeEntity> sceneTypeEntities = sceneTypeMapper.findList(sceneTypeEntity);
        List<SceneTypeEntity> sceneTypeTree = getSceneTypeTree(sceneTypeEntities, Long.valueOf(0), 1);
        return sceneTypeTree;
    }

    /**
     * 查列表
     */
    public List<SceneTypeEntity> findList2(SceneTypeEntity sceneTypeEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        sceneTypeEntity.setTenantId(currentUser.getTenantId());
        List<SceneTypeEntity> sceneTypeEntities = sceneTypeMapper.findList(sceneTypeEntity);
        return sceneTypeEntities;
    }
    public static List<SceneTypeEntity> getSceneTypeTree(List<SceneTypeEntity> list, Long id, Integer leve) {
        List<SceneTypeEntity> temList = newArrayList();
        if (list != null) {
            for (SceneTypeEntity sceneTypeEntity : list) {
                if (id.equals(sceneTypeEntity.getParentScenetypeId())) {
                    List<SceneTypeEntity> chidren = getSceneTypeTree(list, sceneTypeEntity.getId(), ++leve);
                    SceneTypeEntity temMeanu = new SceneTypeEntity();
                    BeanUtils.copyProperties(sceneTypeEntity, temMeanu);
                    temMeanu.setChildren(chidren);
                    temList.add(temMeanu);
                    leve--;
                }

            }
        }
        if (temList.size() > 0) {
            return temList;
        } else {
            return null;
        }
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delSceneTypeByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }
    /**
     * 保存
     */
    public boolean addSceneType(SceneTypeEntity sceneType) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        sceneType.setTenantId(currentUser.getTenantId());
        sceneType.setStatus(1);
        if (this.save(sceneType)) {
            return true;
        }
        return false;
    }

    /**
     * 修改根据ID
     */
    public boolean updateSceneTypeById(SceneTypeEntity sceneType) {
        if (this.updateById(sceneType)) {
            return true;
        }
        return false;
    }

    /**
     * 根居ID获取对象
     */
    public SceneTypeEntity findSceneTypeById(Long id) {
        return sceneTypeMapper.findSceneTypeById(id);
    }

    public List<SceneTypeEntity> findByParentSceneTypeIds(Long[] ids) {
        return sceneTypeMapper.findByParentSceneTypeIds(ids);
    }
}
