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

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.lideeyunji.core.framework.entity.FormButtonEntity;
import com.lideeyunji.core.framework.mapper.FormButtonMapper;
import com.lideeyunji.core.framework.service.IFormButtonService;
import com.lideeyunji.tool.framework.common.constant.lideeYunJiBaseConstant;
import com.lideeyunji.tool.framework.yunji.annotation.lideeYunjiCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lideeyunji.tool.framework.yunji.constant.lideeRedisConstants;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * button增强相关
 */

@Service
public class FormButtonServiceImpl extends ServiceImpl<FormButtonMapper, FormButtonEntity> implements IFormButtonService {

    @Override
    public List<FormButtonEntity> getSlectList(Long dbFormId){
        LambdaQueryWrapper<FormButtonEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormButtonEntity::getDbformId, dbFormId);
        List<FormButtonEntity> list = this.list(wrapper);
        return list;
    }

    //获取按钮增强列表
    @lideeYunjiCache(cacheNames = "'"+ lideeRedisConstants.REDIS_DBFORM +":getButtonList:' + #dbFormId", reflexClass = FormButtonEntity.class,nullIsSave = true)
    public List<FormButtonEntity> getButtonList(Long dbFormId){
        LambdaQueryWrapper<FormButtonEntity> buttonWrapper = new LambdaQueryWrapper<>();
        buttonWrapper.eq(FormButtonEntity::getDbformId, dbFormId);
        List<FormButtonEntity> buttonList = this.list(buttonWrapper);
        return buttonList;
    }


    @DS(lideeYunJiBaseConstant.DS_lideeyunji)
    //复制
    @Override
    public void copy(Long sourceFormIdId,Long targetFormIdId){

        LambdaQueryWrapper<FormButtonEntity> buttonWrapper=new LambdaQueryWrapper<>();
        buttonWrapper.eq(FormButtonEntity::getDbformId,sourceFormIdId);
        buttonWrapper.orderByAsc(FormButtonEntity::getId);
        List<FormButtonEntity> buttonEntityList = this.list(buttonWrapper);
        //清空默认值并修改dbformId值
        buttonEntityList.stream().forEach(buttonEntity->{
            buttonEntity.setDbformId(targetFormIdId);
            buttonEntity.setId(null);
            buttonEntity.setTenantId(null);
            buttonEntity.setCreateDept(null);
            buttonEntity.setCreateTime(null);
            buttonEntity.setCreateUser(null);
            buttonEntity.setUpdateTime(null);
            buttonEntity.setUpdateUser(null);
            buttonEntity.setIsDeleted(null);
        });
        this.saveBatch(buttonEntityList);
    }


    //批量删除
    @DSTransactional(rollbackFor = Exception.class)
    @Override
    public void removeByIds(List<Long> ids){
        for(Long id:ids){
            baseMapper.deleteById(id);
        }
    }

}
