package com.jeeplus.test.knowledge.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.core.domain.BaseEntity;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.knowledge.domain.KnowledgeShareType;
import com.jeeplus.test.knowledge.mapper.KnowledgeShareTypeMapper;
import com.jeeplus.test.knowledge.service.dto.KnowledgeShareTypeDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TypeService {

    @Resource
    private KnowledgeShareTypeMapper mapper;

    public IPage<KnowledgeShareTypeDto> list(Page<KnowledgeShareTypeDto> page, KnowledgeShareType type) {
        return mapper.findList(page, type.getKlgsType());
    }

    public Boolean save(KnowledgeShareType type) {
        List<KnowledgeShareType> list = mapper.checkList(type);
        if (CollectionUtils.isNotEmpty(list)) {
            return false;
        }
        if (StringUtils.isNotEmpty(type.getId())) {
            return update(type);
        } else {
            return add(type);
        }
    }

    public Boolean update(KnowledgeShareType type) {
        // 获取当前登录人
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        type.setUpdateBy(userDTO.getId());
        type.setUpdateDate(new Date());
        mapper.updateById(type);
        return true;
    }

    public Boolean add(KnowledgeShareType type) {
        // 获取当前登录人
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        // 生成id
        String id = UUID.randomUUID().toString().replace("-", "");
        type.setId(id);
        type.setCreateBy(userDTO.getId());
        type.setCreateDate(new Date());
        type.setUpdateBy(userDTO.getId());
        type.setUpdateDate(new Date());
        type.setDelFlag(0);
        mapper.insert(type);
        return true;
    }

}
