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

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.jeelowcode.core.framework.entity.FormButtonEntity;
import com.jeelowcode.core.framework.mapper.FormButtonMapper;
import com.jeelowcode.core.framework.service.IFormButtonService;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.utils.annotation.JeelowCodeCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * button增强相关
 */

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

    //获取按钮增强列表
    @JeelowCodeCache(cacheNames = "'FormButtonServiceImpl: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(JeeLowCodeBaseConstant.DS_JEELOWCODE)
    //复制
    @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);
        }
    }

}
