package com.dcsoft.system.ladder.service.impl;

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

import com.dcsoft.common.core.constant.Constants;
import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.system.ladder.domain.DevicePositionVo;
import com.dcsoft.system.ladder.domain.DeviceTreeVo;
import com.dcsoft.system.ladder.domain.TLadderRule;
import com.dcsoft.system.ladder.mapper.TLadderRuleMapper;
import com.dcsoft.system.ladder.service.ITLadderRuleService;
import com.dcsoft.system.utils.TreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


/**
 * 梯控规则Service业务层处理
 *
 * @author dcsoft
 * @date 2023-12-26
 */
@Service
public class TLadderRuleServiceImpl implements ITLadderRuleService {

    @Autowired
    private TLadderRuleMapper tLadderRuleMapper;

    /**
     * 查询梯控规则
     *
     * @param id 梯控规则主键
     * @return 梯控规则
     */
    @Override
    public TLadderRule selectTLadderRuleById(String id) {
        TLadderRule tLadderRule = tLadderRuleMapper.selectTLadderRuleById(id);
        if(StringUtils.isNotEmpty(tLadderRule.getPointId())) {
            tLadderRule.setPointIds(Arrays.asList(tLadderRule.getPointId().split(",")));
        }
        return tLadderRule;
    }

    /**
     * 查询梯控规则列表
     *
     * @param tLadderRule 梯控规则
     * @return 梯控规则
     */
    @Override
    public List<TLadderRule> selectTLadderRuleList(TLadderRule tLadderRule) {
        return tLadderRuleMapper.selectTLadderRuleList(tLadderRule);
    }

    /**
     * 新增梯控规则
     *
     * @param tLadderRule 梯控规则
     * @return 结果
     */
    @Override
    public int insertTLadderRule(TLadderRule tLadderRule) {
        tLadderRule.setCreateTime(DateUtils.getNowDate());
        tLadderRule.setId(UUID.randomUUID().toString().replace("-", ""));
        if(!CollectionUtils.isEmpty(tLadderRule.getPointIds())) {
            tLadderRule.setSpaceId(String.join(",", tLadderRule.getPointIds()));
        }
        return tLadderRuleMapper.insertTLadderRule(tLadderRule);
    }

    /**
     * 修改梯控规则
     *
     * @param tLadderRule 梯控规则
     * @return 结果
     */
    @Override
    public int updateTLadderRule(TLadderRule tLadderRule) {
        tLadderRule.setUpdateTime(DateUtils.getNowDate());
        if(!CollectionUtils.isEmpty(tLadderRule.getPointIds())) {
            tLadderRule.setSpaceId(String.join(",", tLadderRule.getPointIds()));
        }
        return tLadderRuleMapper.updateTLadderRule(tLadderRule);
    }

    /**
     * 批量删除梯控规则
     *
     * @param ids 需要删除的梯控规则主键
     * @return 结果
     */
    @Override
    public int deleteTLadderRuleByIds(String[] ids) {
        // 规则授权不可以删除
        List<TLadderRule> rule = tLadderRuleMapper.queryEmpower(ids);
        if(!CollectionUtils.isEmpty(rule)) {
            for (TLadderRule tLadderRule : rule) {
                throw new ServiceException(tLadderRule.getName() + "规则已授权不可删除");
            }
        }
        return tLadderRuleMapper.deleteTLadderRuleByIds(ids);
    }

    /**
     * 删除梯控规则信息
     *
     * @param id 梯控规则主键
     * @return 结果
     */
    @Override
    public int deleteTLadderRuleById(Long id) {
        return tLadderRuleMapper.deleteTLadderRuleById(id);
    }

    /**
     * 查询设备位置信息
     */
    @Override
    public List<DeviceTreeVo> queryDevicePositionTree() {
        List<DeviceTreeVo> deviceTreeVos = tLadderRuleMapper.queryDevicePositionTree();
        return TreeUtil.buildTree(deviceTreeVos, Constants.ZERO);
    }


}
