package com.jeeplus.test.cw.workClientInfo.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.workClientInfo.domain.CwOrganizationType;
import com.jeeplus.test.cw.workClientInfo.mapper.CwOrganizationTypeMapper;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwOrganizationTypeDTO;
import com.jeeplus.test.cw.workClientInfo.service.mapstruct.CwOrganizationTypeWrapper;
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 CwOrganizationTypeService extends ServiceImpl<CwOrganizationTypeMapper, CwOrganizationType> {

    @Resource
    private CwOrganizationTypeMapper cwOrganizationTypeMapper;

    public IPage<CwOrganizationTypeDTO> findList(Page<CwOrganizationTypeDTO> page, QueryWrapper<CwOrganizationType> queryWrapper){
        queryWrapper.eq("cw_ot.del_flag","0").orderByAsc("cw_ot.sort");
        return cwOrganizationTypeMapper.findList(page,queryWrapper);
    }

    public CwOrganizationTypeDTO queryById(String id) {

        CwOrganizationTypeDTO cwOrganizationTypeDTO = cwOrganizationTypeMapper.queryById(id);

        return cwOrganizationTypeDTO;
    }

    public List<CwOrganizationTypeDTO> getList() {
        List<CwOrganizationTypeDTO> list = cwOrganizationTypeMapper.getList();
        return list;
    }

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

}
