package com.huatek.busi.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.*;
import com.huatek.busi.dto.AlgorithmBasicDataDto;
import com.huatek.busi.entity.AlgorithmBasicData;
import com.huatek.busi.mapper.AlgorithmBasicDataMapper;
import com.huatek.busi.service.AlgorithmBasicDataService;
import com.huatek.busi.vo.BusiEqmWarningVo;
import com.huatek.busi.where.AlgorithmBasicDataSelectVo;
import com.huatek.torch.frame.tools.*;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service(interfaceClass = AlgorithmBasicDataService.class)
@Transactional(rollbackFor = Exception.class)
public class AlgorithmBasicDataServiceImpl extends BaseDao<AlgorithmBasicData, String> implements AlgorithmBasicDataService {

    @Resource
    private AlgorithmBasicDataMapper algorithmBasicDataMapper;

    @Override
    public Map<String, Object> findTree(AlgorithmBasicDataSelectVo algorithmBasicDataSelectVo){
        Integer page = algorithmBasicDataSelectVo.getPage();
        Integer limit = algorithmBasicDataSelectVo.getLimit();
        if(Objects.nonNull(page) && Objects.nonNull(limit)){
            PageHelper.startPage(page, limit);
        }
        Page<AlgorithmBasicDataDto> algorithmBasicDataDto = algorithmBasicDataMapper.findPage(algorithmBasicDataSelectVo);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, algorithmBasicDataDto.getTotal(), "", algorithmBasicDataDto.getResult());
    }

    @Override
    public Map<String, Object> saveOrUpdate(String token, AlgorithmBasicData algorithmBasicData){

        // 根据 token 获取当前用户信息
        LocalUser user = UserUtil.getLocalUser(token);

        // 获取父节点实体
        algorithmBasicData.setParent(get(algorithmBasicData.getParent().getId()));

        // 设置新的父节点串
        algorithmBasicData.setParentIds(algorithmBasicData.getParent().getParentIds() + algorithmBasicData.getParent().getId() + ",");

        if(StrUtil.isNotBlank(algorithmBasicData.getId())){
            if(Objects.nonNull(user)){
                algorithmBasicData.setUpdateUser(user.getUid());
                algorithmBasicData.setUpdateTime(LocalDateTime.now());
            }
            this.updateLocal(algorithmBasicData);
        }else {
            algorithmBasicData.setId(Uuid.getUUID());
            if(Objects.nonNull(user)){
                algorithmBasicData.setCreateUser(user.getUid());
                algorithmBasicData.setCreateTime(LocalDateTime.now());
            }
            this.add(algorithmBasicData);
        }

        // 获取修改前的parentIds，用于更新子节点的parentIds
        String oldParentIds = algorithmBasicData.getParentIds();

        // 更新子节点 parentIds
        AlgorithmBasicData mAlgorithmBasicData = new AlgorithmBasicData();
        mAlgorithmBasicData.setParentIds("%," + algorithmBasicData.getId() + ",%");
        List<AlgorithmBasicData> list = algorithmBasicDataMapper.findByParentIdsLike(mAlgorithmBasicData);
        for (AlgorithmBasicData data :list){
            data.setParentIds(data.getParentIds().replace(oldParentIds, algorithmBasicData.getParentIds()));
            algorithmBasicDataMapper.updateParentIds(data);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    @Override
    public Map<String, Object> delete(List<String> ids){
        for (String id : ids){
            algorithmBasicDataMapper.delete(id);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }




}
