package com.bonus.bracelet.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.bonus.bracelet.mapper.ConsControlMapper;
import com.bonus.bracelet.service.IConsControlService;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.bracelet.BraceletParamsDto;
import com.bonus.common.core.utils.BuildTreeUtil;
import com.bonus.common.entity.bracelet.vo.BallTreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @className:ConsControlServiceImpl
 * @author:cwchen
 * @date:2024-08-08-9:56
 * @version:1.0
 * @description:施工管控
 */
@Service(value = "IConsControlService")
@Slf4j
public class ConsControlServiceImpl implements IConsControlService {

    @Resource(name = "ConsControlMapper")
    private ConsControlMapper mapper;

    @Override
    public AjaxResult getBallDeviceLists(BraceletParamsDto dto) {
        JSONArray resultTree = new JSONArray();
        try {
            List<BallTreeVo> list = new ArrayList<>();
            list = mapper.getBallDeviceLists(dto);
            for (BallTreeVo vo : list) {
                //过滤工程数据
                if (Objects.equals(vo.getParentId(), BusinessConstants.PARENT_ID)) {
                    continue;
                }
                String[] valArr = vo.getLabel().split("-");
                if (StringUtils.isNotEmpty(valArr[1])) {
                    vo.setLabel(valArr[0] + "-" + Sm4Utils.decode(valArr[1]));
                }
            }
            //构建球机树
            resultTree = BuildTreeUtil.listInfoToTree(JSONArray.parseArray(JSON.toJSONString(list)), "id", "parentId", "children");
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(resultTree);
    }
}
