package jnpf.personnels.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.model.personnels.dto.black.FtbPersonnelsBlacklistTypeAddDTO;
import jnpf.model.personnels.dto.black.FtbPersonnelsBlacklistTypeUpdateDTO;
import jnpf.model.personnels.po.FtbPersonnelsBlacklistType;
import jnpf.model.personnels.vo.black.FtbPersonnelsBlacklistTypeListVO;
import jnpf.personnels.mapper.FtbPersonnelsBlacklistTypeMapper;
import jnpf.personnels.service.FtbPersonnelsBlacklistTypeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 人事黑名单类型表 服务实现类
 * </p>
 *
 * @author wcx
 * @since 2024-05-08
 */
@Service
public class FtbPersonnelsBlacklistTypeServiceImpl extends ServiceImpl<FtbPersonnelsBlacklistTypeMapper, FtbPersonnelsBlacklistType> implements FtbPersonnelsBlacklistTypeService {

    @Override
    public void add(FtbPersonnelsBlacklistTypeAddDTO ftbPersonnelsBlacklistTypeAddDTO) {
        LambdaQueryWrapper<FtbPersonnelsBlacklistType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsBlacklistType::getEnableMark, 0);
        queryWrapper.eq(FtbPersonnelsBlacklistType::getRegularName, ftbPersonnelsBlacklistTypeAddDTO.getRegularName());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("此黑名单类型已存在！");
        }
        FtbPersonnelsBlacklistType ftbPersonnelBlacklistType = new FtbPersonnelsBlacklistType();
        ftbPersonnelBlacklistType.setStatus(0);
        ftbPersonnelBlacklistType.setRegularName(ftbPersonnelsBlacklistTypeAddDTO.getRegularName());
        save(ftbPersonnelBlacklistType);
    }

    @Override
    public void delete(String id) {
        LambdaUpdateWrapper<FtbPersonnelsBlacklistType> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, id);
        updateWrapper.set(FtbPersonnelsBlacklistType::getEnableMark, 1);
        baseMapper.update(new FtbPersonnelsBlacklistType(), updateWrapper);
    }

    @Override
    public void updateData(FtbPersonnelsBlacklistTypeUpdateDTO ftbPersonnelsBlacklistTypeUpdateDTO) {
        LambdaQueryWrapper<FtbPersonnelsBlacklistType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.ne(SuperBaseEntity.SuperIBaseEntity::getId, ftbPersonnelsBlacklistTypeUpdateDTO.getId());
        queryWrapper.eq(FtbPersonnelsBlacklistType::getEnableMark, 0);
        queryWrapper.eq(FtbPersonnelsBlacklistType::getRegularName, ftbPersonnelsBlacklistTypeUpdateDTO.getRegularName());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("此黑名单类型已存在！");
        }
        LambdaUpdateWrapper<FtbPersonnelsBlacklistType> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, ftbPersonnelsBlacklistTypeUpdateDTO.getId());
        updateWrapper.set(FtbPersonnelsBlacklistType::getRegularName, ftbPersonnelsBlacklistTypeUpdateDTO.getRegularName());
        baseMapper.update(new FtbPersonnelsBlacklistType(), updateWrapper);
    }

    @Override
    public List<FtbPersonnelsBlacklistTypeListVO> listData() {
        LambdaQueryWrapper<FtbPersonnelsBlacklistType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsBlacklistType::getEnableMark, 0);
        queryWrapper.orderByDesc(SuperBaseEntity.SuperIBaseEntity::getId);
        List<FtbPersonnelsBlacklistType> ftbPersonnelsBlacklistTypes = baseMapper.selectList(queryWrapper);
        return ftbPersonnelsBlacklistTypes.stream().map(a -> {
            FtbPersonnelsBlacklistTypeListVO ftbPersonnelsBlacklistTypeListVO = new FtbPersonnelsBlacklistTypeListVO();
            ftbPersonnelsBlacklistTypeListVO.setId(a.getId());
            ftbPersonnelsBlacklistTypeListVO.setRegularName(a.getRegularName());
            return ftbPersonnelsBlacklistTypeListVO;
        }).collect(Collectors.toList());
    }


}
