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

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.jeelowcode.core.framework.entity.DesformEntity;
import com.jeelowcode.core.framework.entity.HistoryDesformEntity;
import com.jeelowcode.core.framework.mapper.DesFormMapper;
import com.jeelowcode.core.framework.mapper.HistoryDesFormMapper;
import com.jeelowcode.core.framework.params.DesFormAddOrUpdateParam;
import com.jeelowcode.core.framework.service.IDesFormService;
import com.jeelowcode.framework.utils.adapter.IJeeLowCodeAdapter;
import com.jeelowcode.framework.utils.utils.FuncBase;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 表单设计器相关
 */
@Service
public class DesFormServiceImpl extends ServiceImpl<DesFormMapper, DesformEntity> implements IDesFormService {

    @Autowired
    private HistoryDesFormMapper historyDesFormMapper;

    @Autowired
    private IJeeLowCodeAdapter jeeLowCodeAdapter;

    //保存
    @DSTransactional(rollbackFor = Exception.class)
    @Override
    public Long saveDesForm(DesFormAddOrUpdateParam param){
        long id = IdWorker.getId();

        DesformEntity desformEntity=new DesformEntity();
        desformEntity.setId(id);
        desformEntity.setTenantId(jeeLowCodeAdapter.getTenantId());
        desformEntity.setCreateUser(jeeLowCodeAdapter.getOnlineUserId());
        desformEntity.setCreateTime(LocalDateTime.now());
        desformEntity.setDesformName(param.getDesformName());
        desformEntity.setDesformJson(param.getDesformJson());
        desformEntity.setGroupDesformId(param.getGroupDesformId());
        desformEntity.setIsOpen(param.getIsOpen());
        desformEntity.setIsTemplate(param.getIsTemplate());
        desformEntity.setIsHide(param.getIsHide());
        desformEntity.setI18nData(param.getI18nData());
        baseMapper.insert(desformEntity);

        //保存历史记录
        this.saveHistoryLog(id,param);
        return id;
    }

    //修改
    @DSTransactional(rollbackFor = Exception.class)
    @Override
    public void updateDesForm(DesFormAddOrUpdateParam param){
        DesformEntity desformEntity=new DesformEntity();
        desformEntity.setId(param.getId());
        desformEntity.setUpdateUser(jeeLowCodeAdapter.getOnlineUserId());
        desformEntity.setUpdateTime(LocalDateTime.now());

        desformEntity.setDesformName(param.getDesformName());
        desformEntity.setDesformJson(param.getDesformJson());
        desformEntity.setGroupDesformId(param.getGroupDesformId());
        desformEntity.setIsOpen(param.getIsOpen());
        desformEntity.setIsTemplate(param.getIsTemplate());
        desformEntity.setI18nData(param.getI18nData());
        baseMapper.updateById(desformEntity);

        //保存历史记录
       this.saveHistoryLog(param.getId(),param);
    }

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

    //保存历史记录
    private void saveHistoryLog(Long id,DesFormAddOrUpdateParam param){
        HistoryDesformEntity historyEntity=new HistoryDesformEntity();
        historyEntity.setId(IdWorker.getId());
        historyEntity.setTenantId(jeeLowCodeAdapter.getTenantId());
        historyEntity.setCreateUser(jeeLowCodeAdapter.getOnlineUserId());
        historyEntity.setCreateTime(LocalDateTime.now());
        historyEntity.setCreateUserName(jeeLowCodeAdapter.getOnlineUserName());

        historyEntity.setDesformId(id);
        historyEntity.setDesformName(param.getDesformName());
        historyEntity.setDesformJson(param.getDesformJson());
        historyEntity.setGroupDesformId(param.getGroupDesformId());
        historyEntity.setIsOpen(param.getIsOpen());
        historyDesFormMapper.insert(historyEntity);
    }
}
