
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.EnhanceJsEntity;
import com.lideeyunji.core.framework.entity.HistoryDbformEntity;
import com.lideeyunji.core.framework.mapper.EnhanceJsMapper;
import com.lideeyunji.core.framework.mapper.HistoryDbFormMapper;
import com.lideeyunji.core.framework.service.IEnhanceJsService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.common.constant.lideeYunJiBaseConstant;
import com.lideeyunji.tool.framework.yunji.adapter.IlideeYunjiAdapter;
import com.lideeyunji.tool.framework.yunji.annotation.lideeYunjiCache;
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.tool.framework.yunji.constant.lideeRedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * @author JX
 * @create 2024-03-06 9:55
 * @dedescription:
 */

@Service
public class EnhanceJsServiceImpl extends ServiceImpl<EnhanceJsMapper, EnhanceJsEntity> implements IEnhanceJsService {

    @Autowired
    private IlideeYunjiAdapter lideeYunJiAdapter;

    @Autowired
    private HistoryDbFormMapper historyDbFormMapper;

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

    @lideeYunjiCache(cacheNames = "'"+ lideeRedisConstants.REDIS_DBFORM + ":js:getJsList:' + #dbFormId", reflexClass = EnhanceJsEntity.class,nullIsSave = true)
    public List<EnhanceJsEntity> getJsList(Long dbFormId){
        LambdaQueryWrapper<EnhanceJsEntity> jsWrapper = new LambdaQueryWrapper<>();
        jsWrapper.eq(EnhanceJsEntity::getDbformId, dbFormId);
        List<EnhanceJsEntity> jsList = this.list(jsWrapper);
        return jsList;
    }

    /**
     * JS增强-详情
     *
     * @param dbformId
     * @param type
     * @return
     */
    @Override
    public EnhanceJsEntity getDbFormEnhanceJsDetail(Long dbformId, String type) {
        LambdaQueryWrapper<EnhanceJsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnhanceJsEntity::getDbformId, dbformId);
        wrapper.eq(EnhanceJsEntity::getJsType, type);
        wrapper.orderByDesc(EnhanceJsEntity::getId);
        List<EnhanceJsEntity> enhanceJsList = baseMapper.selectList(wrapper);
        if(Func.isEmpty(enhanceJsList)){
            return null;
        }
        return enhanceJsList.get(0);
    }

    //新增js增强
    @DSTransactional(rollbackFor = Exception.class)
    @lideeYunjiCache(cacheNames = lideeRedisConstants.REDIS_DBFORM +":*",delCache = true)
    @Override
    public void saveEnhanceJs(EnhanceJsEntity enhanceJsEntity){
        long id = IdWorker.getId();
        enhanceJsEntity.setId(id);
        this.save(enhanceJsEntity);

        //加历史记录
        this.saveHistoryLog(id,enhanceJsEntity);
    }


    //编辑js增强
    @DSTransactional(rollbackFor = Exception.class)
    @lideeYunjiCache(cacheNames = lideeRedisConstants.REDIS_DBFORM +":*",delCache = true)
    @Override
    public void updateEnhanceJs(EnhanceJsEntity enhanceJsEntity){
        this.updateById(enhanceJsEntity);
        //加历史记录
        this.saveHistoryLog(enhanceJsEntity.getId(),enhanceJsEntity);
    }


    @DS(lideeYunJiBaseConstant.DS_lideeyunji)
    @Override
    public void copy(Long sourceFormIdId,Long targetFormIdId){
        LambdaQueryWrapper<EnhanceJsEntity> jsWrapper=new LambdaQueryWrapper<>();
        jsWrapper.eq(EnhanceJsEntity::getDbformId,sourceFormIdId);
        jsWrapper.orderByAsc(EnhanceJsEntity::getId);
        List<EnhanceJsEntity> jsEntityList = this.list(jsWrapper);
        //清空默认值并修改dbformId值
        jsEntityList.stream().forEach(jsEntity->{
            jsEntity.setDbformId(targetFormIdId);
            jsEntity.setId(null);
            jsEntity.setTenantId(null);
            jsEntity.setCreateDept(null);
            jsEntity.setCreateTime(null);
            jsEntity.setCreateUser(null);
            jsEntity.setUpdateTime(null);
            jsEntity.setUpdateUser(null);
            jsEntity.setIsDeleted(null);
        });
        this.saveBatch(jsEntityList);
    }

    //保存历史记录
    private void saveHistoryLog(Long jsId, EnhanceJsEntity enhanceJsEntity){

        HistoryDbformEntity entity=new HistoryDbformEntity();
        entity.setId(IdWorker.getId());
        entity.setTenantId(lideeYunJiAdapter.getTenantId());
        entity.setCreateUser(lideeYunJiAdapter.getOnlineUserId());
        entity.setCreateTime(LocalDateTime.now());
        entity.setCreateUserName(lideeYunJiAdapter.getOnlineUserName());

        entity.setDbformId(enhanceJsEntity.getDbformId());
        entity.setServiceType("js");
        entity.setServiceId(jsId);
        entity.setServiceJson(Func.json2Str(enhanceJsEntity));

        historyDbFormMapper.insert(entity);
    }
}
