package com.jeeplus.assess.program.configuration.fileDict.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.assess.program.configuration.fileDict.domain.ProgramFileDict;
import com.jeeplus.assess.program.configuration.fileDict.mapper.ProgramFileDictMapper;
import com.jeeplus.assess.program.configuration.fileDict.service.dto.ProgramFileDictDTO;
import com.jeeplus.assess.program.configuration.fileDict.service.mapstruct.ProgramFileDictWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ProgramFileDictService extends ServiceImpl<ProgramFileDictMapper, ProgramFileDict> {

    @Resource
    private ProgramFileDictMapper programFileDictMapper;

    public IPage<ProgramFileDictDTO> findList(Page<ProgramFileDictDTO> page, QueryWrapper<ProgramFileDict> queryWrapper){
        queryWrapper.eq("pfd.del_flag","0").orderByAsc("pfd.sort");
        return programFileDictMapper.findList(page,queryWrapper);
    }

    public ProgramFileDictDTO queryById(String id) {

        ProgramFileDictDTO programFileDictDTO = programFileDictMapper.queryById(id);

        return programFileDictDTO;
    }

    public ResponseEntity saveType(ProgramFileDictDTO programFileDictDTO) {
        ProgramFileDict programFileDict = ProgramFileDictWrapper.INSTANCE.toEntity(programFileDictDTO);
        this.saveOrUpdate(programFileDict);
        return ResponseEntity.ok("保存成功");
    }

    public ResponseEntity deleteByIds(String ids) {
        String idArray[] =ids.split(",");
        if(CollectionUtil.isNotEmpty(Lists.newArrayList (idArray))){
            List<String> list = this.list(new QueryWrapper<ProgramFileDict>().lambda().in(ProgramFileDict::getParentId, Lists.newArrayList(idArray))).stream().distinct().map(ProgramFileDict::getId).collect(Collectors.toList());
            this.removeByIds (list);
        }
        this.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除成功");
    }
}
