package com.njcn.supervision.service.databank.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.supervision.enums.SupervisionResponseEnum;
import com.njcn.supervision.mapper.databank.LibStandardMapper;
import com.njcn.supervision.pojo.param.databank.LibStandardParam;
import com.njcn.supervision.pojo.po.databank.LibStandard;
import com.njcn.supervision.service.databank.LibStandardService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * Date: 2024/9/10  14:17【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class LibStandardServiceImpl extends ServiceImpl<LibStandardMapper, LibStandard> implements LibStandardService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LibStandard add(LibStandardParam libStandardParam) {

        checkName(libStandardParam,false);
        LibStandard libStandard = new LibStandard();
        BeanUtils.copyProperties(libStandardParam, libStandard);
        libStandard.setStatus(1);
        if(StringUtils.isEmpty(libStandardParam.getPid())){
            libStandard.setPid("0");
            libStandard.setPids("0");
        }else {
            LibStandard fatherLibStandard = this.lambdaQuery().eq(LibStandard::getId, libStandardParam.getPid()).one();
            libStandard.setPids(fatherLibStandard.getPids() + "," + libStandardParam.getPid());
        }


        this.save(libStandard);
        return libStandard;

    }

    private void checkName(LibStandardParam libStandardParam, boolean isExcludeSelf) {

        String pid =StringUtils.isEmpty(libStandardParam.getPid())?"0":libStandardParam.getPid();

        LambdaQueryWrapper<LibStandard> sgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sgUserLambdaQueryWrapper
                .eq(LibStandard::getName, libStandardParam.getName())
                .eq(LibStandard::getPid,pid)
                .eq(LibStandard::getStatus, DataStateEnum.ENABLE.getCode());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (libStandardParam instanceof LibStandardParam.LibStandardParamUpdate) {
                sgUserLambdaQueryWrapper.ne(LibStandard::getId, ((  LibStandardParam.LibStandardParamUpdate) libStandardParam).getId());
            }
        }
        int countByAccount = this.count(sgUserLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new BusinessException(SupervisionResponseEnum.NAME_EXISTS);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateStandardLibrary(LibStandardParam.LibStandardParamUpdate paramUpdate) {
        checkName(paramUpdate,true);

        LibStandard libStandard = new LibStandard();
        BeanUtil.copyProperties(paramUpdate, libStandard);
        this.updateById(libStandard);
        return libStandard.getId();
    }

    @Override
    public List<LibStandard> queryAll() {
        List<LibStandard> libStandardList = this.lambdaQuery().eq(LibStandard::getStatus,1).list();
        return libStandardList.stream().filter(item -> BizParamConstant.PARENT_ID.equals(item.getPid())).peek(item -> {
            item.setChildren(getChildren(item, libStandardList));
        }).collect(Collectors.toList());
    }

    private List<LibStandard> getChildren(LibStandard libStandard, List<LibStandard> all) {
        return all.stream().filter(item -> item.getPid().equals(libStandard.getId())).peek(item -> {
            item.setChildren(getChildren(item, all));
        }).collect(Collectors.toList());
    }
}
