package com.jeeplus.assess.program.serviceType.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.assess.program.serviceType.domain.ProgramServiceType;
import com.jeeplus.assess.program.serviceType.mapper.ProgramServiceTypeMapper;
import com.jeeplus.assess.program.serviceType.service.dto.ProgramServiceTypeDTO;
import com.jeeplus.assess.program.serviceType.service.mapstruct.ProgramServiceTypeWrapper;
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(rollbackFor = Exception.class)
public class ProgramServiceTypeService extends ServiceImpl<ProgramServiceTypeMapper, ProgramServiceType> {

    @Resource
    private ProgramServiceTypeMapper programServiceTypeMapper;

    public IPage<ProgramServiceTypeDTO> findList(Page<ProgramServiceTypeDTO> page, QueryWrapper<ProgramServiceType> queryWrapper){
        queryWrapper.eq("pst.del_flag","0").orderByAsc("pst.sort");
        return programServiceTypeMapper.findList(page,queryWrapper);
    }

    public ProgramServiceTypeDTO queryById(String id) {

        ProgramServiceTypeDTO programServiceTypeDTO = programServiceTypeMapper.queryById(id);

        return programServiceTypeDTO;
    }

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

    public List<ProgramServiceTypeDTO> getList() {
        List<ProgramServiceTypeDTO> list = programServiceTypeMapper.getList();
        return list;
    }

}
