package com.jeeplus.finance.workClientInfo.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.finance.workClientInfo.domain.CwManageLevelType;
import com.jeeplus.finance.workClientInfo.mapper.CwManageLevelTypeMapper;
import com.jeeplus.finance.workClientInfo.service.dto.CwManageLevelTypeDTO;
import com.jeeplus.finance.workClientInfo.service.mapstruct.CwManageLevelTypeWrapper;
import org.apache.commons.lang3.StringUtils;
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 CwManageLevelTypeService extends ServiceImpl<CwManageLevelTypeMapper, CwManageLevelType> {

    @Resource
    private CwManageLevelTypeMapper cwManageLevelTypeMapper;

    public IPage<CwManageLevelTypeDTO> findList(Page<CwManageLevelTypeDTO> page, QueryWrapper<CwManageLevelType> queryWrapper){
        queryWrapper.eq("cw_mlt.del_flag","0").orderByAsc("cw_mlt.sort");
        return cwManageLevelTypeMapper.findList(page,queryWrapper);
    }

    public CwManageLevelTypeDTO queryById(String id) {

        CwManageLevelTypeDTO cwManageLevelTypeDTO = cwManageLevelTypeMapper.queryById(id);

        return cwManageLevelTypeDTO;
    }

    public List<CwManageLevelTypeDTO> getList() {
        List<CwManageLevelTypeDTO> list = cwManageLevelTypeMapper.getList();
        return list;
    }

    public ResponseEntity saveType(CwManageLevelTypeDTO cwManageLevelTypeDTO) {
        CwManageLevelType cwManageLevelType = CwManageLevelTypeWrapper.INSTANCE.toEntity(cwManageLevelTypeDTO);
        if (ObjectUtil.isNotEmpty(cwManageLevelTypeDTO)) {
            if (StringUtils.isBlank(cwManageLevelTypeDTO.getId())) {
                if (StringUtils.isNotBlank(cwManageLevelTypeDTO.getParentId())){
                    if ("0".equals(cwManageLevelTypeDTO.getParentId())){
                        cwManageLevelType.setLevel(1);
                        cwManageLevelType.setParentIds("0,");
                    }else{
                        CwManageLevelType mlt = this.getById(cwManageLevelTypeDTO.getParentId());
                        if(ObjectUtil.isNotEmpty(mlt)){
                            cwManageLevelType.setLevel(mlt.getLevel()+1);
                            cwManageLevelType.setParentIds(mlt.getParentIds()+mlt.getId()+",");
                        }
                    }
                }
            }
        }
        this.saveOrUpdate(cwManageLevelType);
        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<CwManageLevelType>().lambda().in(CwManageLevelType::getParentId, Lists.newArrayList(idArray))).stream().distinct().map(CwManageLevelType::getId).collect(Collectors.toList());
            this.removeByIds (list);
        }
        this.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除成功");
    }

    public List<CwManageLevelType> selectByName(CwManageLevelTypeDTO cwManageLevelTypeDTO) {
        QueryWrapper<CwManageLevelType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(cwManageLevelTypeDTO.getName()), CwManageLevelType::getName, cwManageLevelTypeDTO.getName());
        return cwManageLevelTypeMapper.selectByName(queryWrapper);
    }

    public List<CwManageLevelType> selectByParentIds(LambdaQueryWrapper<CwManageLevelType> queryWrapper) {
        return cwManageLevelTypeMapper.selectByParentIds(queryWrapper);
    }
}
