package com.storlead.knowledge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.knowledge.entity.ConfigVersionEntity;
import com.storlead.knowledge.mapper.ConfigVersionMapper;
import com.storlead.knowledge.pojo.dto.ConfigVersionDTO;
import com.storlead.knowledge.pojo.vo.ConfigVersionVO;
import com.storlead.knowledge.service.ConfigVersionService;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 配置版本表 服务实现类
 * </p>
 *
 * @author storlead
 * @since 2026-06-09
 */
@Service
public class ConfigVersionServiceImpl extends MyBaseServiceImpl<ConfigVersionMapper, ConfigVersionEntity> implements ConfigVersionService {

    @Override
    public ConfigVersionEntity getByAppCodeAndKey(String appCode, String key) {
        LambdaQueryWrapper<ConfigVersionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ConfigVersionEntity::getAppCode, appCode)
               .eq(ConfigVersionEntity::getKey, key)
               .eq(ConfigVersionEntity::getIsDelete, 0)
               .orderByDesc(ConfigVersionEntity::getCreateTime)
               .last("LIMIT 1");
        return this.getOne(wrapper);
    }

    @Override
    public IPage<ConfigVersionVO> pageVO(Integer pageIndex, Integer pageSize, String appCode, String keyword) {
        Page<ConfigVersionEntity> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<ConfigVersionEntity> wrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        wrapper.eq(ConfigVersionEntity::getIsDelete, 0);
        if (StringUtils.hasText(appCode)) {
            wrapper.eq(ConfigVersionEntity::getAppCode, appCode);
        }
        if (StringUtils.hasText(keyword)) {
            wrapper.and(w -> w.like(ConfigVersionEntity::getName, keyword)
                    .or()
                    .like(ConfigVersionEntity::getKey, keyword)
                    .or()
                    .like(ConfigVersionEntity::getVersion, keyword));
        }
        wrapper.orderByDesc(ConfigVersionEntity::getCreateTime);
        
        IPage<ConfigVersionEntity> entityPage = this.page(page, wrapper);
        
        // 转换为VO
        DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        IPage<ConfigVersionVO> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize());
        voPage.setTotal(entityPage.getTotal());
        voPage.setRecords(mapperFactory.getMapperFacade().mapAsList(entityPage.getRecords(), ConfigVersionVO.class));
        
        return voPage;
    }

    @Override
    public boolean saveByDTO(ConfigVersionDTO dto) {
        DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        ConfigVersionEntity entity = mapperFactory.getMapperFacade().map(dto, ConfigVersionEntity.class);
        return this.save(entity);
    }

    @Override
    public boolean updateByDTO(ConfigVersionDTO dto) {
        DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        ConfigVersionEntity entity = mapperFactory.getMapperFacade().map(dto, ConfigVersionEntity.class);
        return this.updateById(entity);
    }
}