package com.jeeplus.test.program.configuration.typeDict.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.google.common.collect.Maps;
import com.jeeplus.core.service.TreeService;
import com.jeeplus.sys.constant.CommonConstants;
import com.jeeplus.sys.domain.Menu;
import com.jeeplus.sys.service.dto.MenuDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.changes.dimission.domain.ChangesDimission;
import com.jeeplus.test.changes.dimission.domain.ChangesDimissionAudit;
import com.jeeplus.test.changes.dimission.domain.ChangesDimissionFiles;
import com.jeeplus.test.changes.dimission.domain.ChangesDimissionRea;
import com.jeeplus.test.changes.dimission.mapper.ChangesDimissionReaMapper;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionDTO;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionFilesDTO;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionReaDTO;
import com.jeeplus.test.changes.dimission.service.mapstruct.ChangesDimissionFilesWrapper;
import com.jeeplus.test.changes.dimission.service.mapstruct.ChangesDimissionWrapper;
import com.jeeplus.test.program.configuration.fileDict.domain.ProgramFileDict;
import com.jeeplus.test.program.configuration.typeDict.domain.ProgramTypeDict;
import com.jeeplus.test.program.configuration.typeDict.mapper.ProgramTypeDictMapper;
import com.jeeplus.test.program.configuration.typeDict.service.dto.ProgramTypeDictDTO;
import com.jeeplus.test.program.configuration.typeDict.service.mapstruct.ProgramTypeDictWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class ProgramTypeDictService extends ServiceImpl<ProgramTypeDictMapper, ProgramTypeDict> {

    @Resource
    private ProgramTypeDictMapper programTypeDictMapper;

    public IPage<ProgramTypeDictDTO> findList(Page<ProgramTypeDictDTO> page, QueryWrapper<ProgramTypeDict> queryWrapper){
        queryWrapper.eq("ptd.del_flag","0").orderByAsc("ptd.sort");
        return programTypeDictMapper.findList(page,queryWrapper);
    }

    public ProgramTypeDictDTO queryById(String id) {

        ProgramTypeDictDTO programTypeDictDTO = programTypeDictMapper.queryById(id);

        return programTypeDictDTO;
    }

    public List<ProgramTypeDictDTO> getList() {
        List<ProgramTypeDictDTO> list = programTypeDictMapper.getList();
        return list;
    }

    public ResponseEntity saveType(ProgramTypeDictDTO programTypeDictDTO) {
        ProgramTypeDict programTypeDict = ProgramTypeDictWrapper.INSTANCE.toEntity(programTypeDictDTO);
        this.saveOrUpdate(programTypeDict);
        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<ProgramTypeDict>().lambda().in(ProgramTypeDict::getParentId, Lists.newArrayList(idArray))).stream().distinct().map(ProgramTypeDict::getId).collect(Collectors.toList());
            this.removeByIds (list);
        }
        this.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除成功");
    }
}
