
package com.lideeyunji.core.framework.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lideeyunji.core.framework.entity.TabEntity;
import com.lideeyunji.core.framework.entity.TabJsEntity;
import com.lideeyunji.core.framework.entity.TabOptionEntity;
import com.lideeyunji.core.framework.mapper.TabJsMapper;
import com.lideeyunji.core.framework.mapper.TabMapper;
import com.lideeyunji.core.framework.mapper.TabOptionMapper;
import com.lideeyunji.core.framework.params.TabAddOrUpdateParam;
import com.lideeyunji.core.framework.params.vo.*;
import com.lideeyunji.core.framework.service.ITabService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.yunji.annotation.lideeYunjiCache;
import com.lideeyunji.tool.framework.yunji.constant.lideeRedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 模块组合相关
 */
@Service
public class TabServiceImpl extends ServiceImpl<TabMapper, TabEntity> implements ITabService {

    @Autowired
    private TabOptionMapper tabOptionMapper;

    @Autowired
    private TabJsMapper jsMapper;

    private TabServiceImpl getSelf() {
        return SpringUtil.getBean(getClass());
    }

    //获取配置列表

    @lideeYunjiCache(cacheNames = "'"+ lideeRedisConstants.REDIS_TAB+":tabWebConfig:' + #tabId", reflexClass = TabWebConfigVo.class)
    @Override
    public TabWebConfigVo getWebConfig(Long tabId){
        TabEntity tabEntity = baseMapper.selectById(tabId);

        LambdaQueryWrapper<TabOptionEntity> optionWrapper=new LambdaQueryWrapper<>();
        optionWrapper.eq(TabOptionEntity::getTabId,tabId);
        List<TabOptionEntity> optionEntityList = tabOptionMapper.selectList(optionWrapper);


        LambdaQueryWrapper<TabJsEntity> jsWrapper = new LambdaQueryWrapper<>();
        jsWrapper.eq(TabJsEntity::getTabId, tabId);
        List<TabJsEntity> jsList = jsMapper.selectList(jsWrapper);

        TabWebConfigVo vo=new TabWebConfigVo();
        vo.setTab(BeanUtil.copyProperties(tabEntity, TabVo.class));
        vo.setOptionList(BeanUtil.copyToList(optionEntityList, TabOptionVo.class));
        vo.setJsList(BeanUtil.copyToList(jsList, TabJsVo.class));
        return vo;
    }

    @lideeYunjiCache(cacheNames = lideeRedisConstants.REDIS_TAB +":*",delCache = true)
    @DSTransactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateTab(Long tabId,TabAddOrUpdateParam param){
        TabVo tabParam = param.getTab();
        TabEntity tabEntity=new TabEntity();
        tabEntity.setModuleName(tabParam.getModuleName());
        tabEntity.setGroupModuleId(tabParam.getGroupModuleId());
        tabEntity.setTabsPosition(tabParam.getTabsPosition());
        tabEntity.setTabsType(tabParam.getTabsType());
        if(Func.isNotEmpty(tabParam.getId())){
            tabEntity.setId(tabParam.getId());
            baseMapper.updateById(tabEntity);
        }else{
            baseMapper.insert(tabEntity);
        }

        //先清再加
        LambdaQueryWrapper<TabOptionEntity> optionWrapper=new LambdaQueryWrapper<>();
        optionWrapper.eq(TabOptionEntity::getTabId,tabEntity.getId());
        tabOptionMapper.delete(optionWrapper);

        List<TabOptionVo> optionList = param.getOptionList();
        if(Func.isEmpty(optionList)){
            return;
        }


        for(TabOptionVo optionVo:optionList){
            TabOptionEntity optionEntity=new TabOptionEntity();
            optionEntity.setTabId(tabEntity.getId());
            optionEntity.setTabName(optionVo.getTabName());
            optionEntity.setTabCode(optionVo.getTabCode());
            optionEntity.setTabIcon(optionVo.getTabIcon());
            optionEntity.setModuleType(optionVo.getModuleType());
            optionEntity.setModuleValue(optionVo.getModuleValue());
            optionEntity.setIsLazy(optionVo.getIsLazy());
            optionEntity.setIsRefresh(optionVo.getIsRefresh());
            optionEntity.setTabI18n(optionVo.getTabI18n());
            tabOptionMapper.insert(optionEntity);
        }

    }

    //删除配置
    @DSTransactional(rollbackFor = Exception.class)
    @lideeYunjiCache(cacheNames = lideeRedisConstants.REDIS_TAB +":*",delCache = true)
    @Override
    public void delTabList(List<Long> tabIdList){
        TabServiceImpl tabService = getSelf();
        for(Long tabId:tabIdList){
            baseMapper.deleteById(tabId);

            LambdaQueryWrapper<TabOptionEntity> optionWrapper=new LambdaQueryWrapper<>();
            optionWrapper.eq(TabOptionEntity::getTabId,tabId);
            tabOptionMapper.delete(optionWrapper);
        }
    }


    //获取详情
    @Override
    public TabDetailVo getDetail(Long id){
        TabEntity tabEntity = baseMapper.selectById(id);

        LambdaQueryWrapper<TabOptionEntity> optionWrapper=new LambdaQueryWrapper<>();
        optionWrapper.eq(TabOptionEntity::getTabId,id);
        List<TabOptionEntity> optionEntityList = tabOptionMapper.selectList(optionWrapper);

        TabDetailVo vo=new TabDetailVo();
        vo.setTab(tabEntity);
        vo.setOptionList(optionEntityList);
        return vo;
    }

    //------------ js相关 -----------------
    //Js增强 详情
    @Override
    public TabJsEntity getJsDetail(Long tabId, String type){
        LambdaQueryWrapper<TabJsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TabJsEntity::getTabId, tabId);
        wrapper.eq(TabJsEntity::getJsType, type);
        wrapper.orderByDesc(TabJsEntity::getId);
        List<TabJsEntity> jsList = jsMapper.selectList(wrapper);
        if(Func.isEmpty(jsList)){
            return null;
        }
        return jsList.get(0);
    }

    //新增js增强
    @lideeYunjiCache(cacheNames = lideeRedisConstants.REDIS_TAB +":*",delCache = true)
    @Override
    public void saveJs(TabJsEntity jsEntity){
        long id = IdWorker.getId();
        jsEntity.setId(id);
        jsMapper.insert(jsEntity);
    }

    //编辑js增强
    @lideeYunjiCache(cacheNames = lideeRedisConstants.REDIS_TAB +":*",delCache = true)
    @Override
    public void updateJs(TabJsEntity jsEntity){
        jsMapper.updateById(jsEntity);
    }

}
