package com.jeeplus.test.cw.projectBusinessType.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
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.sys.utils.StringUtils;
import com.jeeplus.test.cw.projectBusinessType.domain.CwProjectBusinessType;
import com.jeeplus.test.cw.projectBusinessType.mapper.CwProjectBusinessTypeMapper;
import com.jeeplus.test.cw.projectBusinessType.service.dto.CwProjectBusinessTypeDTO;
import com.jeeplus.test.cw.projectBusinessType.service.mapstruct.CwProjectBusinessTypeWrapper;
import com.jeeplus.test.cw.workClientInfo.domain.CwManageLevelType;
import com.jeeplus.test.cw.workClientInfo.domain.CwOrganizationType;
import com.jeeplus.test.cw.workClientInfo.mapper.CwManageLevelTypeMapper;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwManageLevelTypeDTO;
import com.jeeplus.test.cw.workClientInfo.service.mapstruct.CwManageLevelTypeWrapper;
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 CwProjectBusinessTypeService extends ServiceImpl<CwProjectBusinessTypeMapper, CwProjectBusinessType> {

    @Resource
    private CwProjectBusinessTypeMapper cwProjectBusinessTypeMapper;

    public IPage<CwProjectBusinessTypeDTO> findList(Page<CwProjectBusinessTypeDTO> page, QueryWrapper<CwProjectBusinessType> queryWrapper){
        queryWrapper.eq("cw_pbt.del_flag","0").orderByAsc("cw_pbt.sort");
        return cwProjectBusinessTypeMapper.findList(page,queryWrapper);
    }

    public CwProjectBusinessTypeDTO queryById(String id) {

        CwProjectBusinessTypeDTO cwProjectBusinessTypeDTO = cwProjectBusinessTypeMapper.queryById(id);

        return cwProjectBusinessTypeDTO;
    }

    public List<CwProjectBusinessTypeDTO> getList() {
        List<CwProjectBusinessTypeDTO> list = cwProjectBusinessTypeMapper.getList();
        return list;
    }

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

}
