package com.jeeplus.finance.projectBusinessType.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.projectBusinessType.domain.CwProjectBusinessType;
import com.jeeplus.finance.projectBusinessType.mapper.CwProjectBusinessTypeMapper;
import com.jeeplus.finance.projectBusinessType.service.dto.CwProjectBusinessTypeDTO;
import com.jeeplus.finance.projectBusinessType.service.mapstruct.CwProjectBusinessTypeWrapper;
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.ArrayList;
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_mark");
        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);
        cwProjectBusinessType.setIsShow(cwProjectBusinessTypeDTO.getIsShow());
        cwProjectBusinessType.setSortMark(cwProjectBusinessTypeDTO.getSortMark());
        if (ObjectUtil.isNotEmpty(cwProjectBusinessTypeDTO)) {
            if (StringUtils.isBlank(cwProjectBusinessTypeDTO.getId())) {
                if (StringUtils.isNotBlank(cwProjectBusinessTypeDTO.getParentId())){
                    if ("0".equals(cwProjectBusinessTypeDTO.getParentId())){
                        cwProjectBusinessType.setLevel(1);
                        cwProjectBusinessType.setIsShow("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.setIsShow("1");
                            cwProjectBusinessType.setParentIds(pbt.getParentIds()+pbt.getId()+",");
                        }
                    }
                }
            }
        }
        this.saveOrUpdate(cwProjectBusinessType);
        //查询数据下边是否有子集
        LambdaQueryWrapper<CwProjectBusinessType> cl2 = new LambdaQueryWrapper<>();
        cl2.eq(CwProjectBusinessType::getParentId,cwProjectBusinessType.getId());
        List<CwProjectBusinessType> cwProjectBusinessTypes = cwProjectBusinessTypeMapper.selectList(cl2);
        ArrayList<String> list = new ArrayList<>();
        if (ObjectUtil.isAllEmpty(cwProjectBusinessTypes)){
            return ResponseEntity.ok("保存成功");
        }else {
            for (CwProjectBusinessType projectBusinessType : cwProjectBusinessTypes) {
                list.add(projectBusinessType.getId());
            }
            List<CwProjectBusinessType>list1= cwProjectBusinessTypeMapper.selectByParentIds(list);
            if (ObjectUtil.isAllEmpty(list1)){
                cwProjectBusinessTypeMapper.updateIsShowByIds(list,cwProjectBusinessType.getIsShow());
            }else {
                for (CwProjectBusinessType projectBusinessType : list1) {
                    list.add(projectBusinessType.getId());
                }
                cwProjectBusinessTypeMapper.updateIsShowByIds(list,cwProjectBusinessType.getIsShow());
            }
        }
        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 ("删除成功");
    }



    public IPage<CwProjectBusinessTypeDTO> findListByIsShow(Page<CwProjectBusinessTypeDTO> page, QueryWrapper<CwProjectBusinessType> queryWrapper) {
        queryWrapper.eq("cw_pbt.is_show","1");
        queryWrapper.eq("cw_pbt.del_flag","0").orderByAsc("cw_pbt.sort_mark");
        IPage<CwProjectBusinessTypeDTO> list = cwProjectBusinessTypeMapper.findListByIsShow(page, queryWrapper);
        return cwProjectBusinessTypeMapper.findListByIsShow(page,queryWrapper);
    }
}
