package com.yunfeiyun.agmp.fms.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.fms.domain.TmnBase;
import com.yunfeiyun.agmp.fms.domain.TmnBlock;
import com.yunfeiyun.agmp.fms.mapper.TmnBaseMapper;
import com.yunfeiyun.agmp.fms.mapper.TmnBlockMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.TmnBaseBlockReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.TmnBlockDeviceListReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.TmnBlockDeviceListResVo;
import com.yunfeiyun.agmp.fms.service.ITmnBlockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 设备地块Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-07-12
 */
@Service
public class TmnBlockServiceImpl implements ITmnBlockService
{
    @Autowired
    private TmnBlockMapper tmnBlockMapper;

    @Autowired
    private TmnBaseMapper tmnBaseMapper;

    /**
     * 查询设备地块
     * 
     * @param tmnblockId 设备地块主键
     * @return 设备地块
     */
    @Override
    public TmnBlock selectTmnBlockByTmnblockId(String tmnblockId)
    {
        return tmnBlockMapper.selectTmnBlockByTmnblockId(tmnblockId);
    }

    /**
     * 查询设备地块列表
     * 
     * @param tmnBlock 设备地块
     * @return 设备地块
     */
    @Override
    public List<TmnBlock> selectTmnBlockList(TmnBlock tmnBlock)
    {
        return tmnBlockMapper.selectTmnBlockList(tmnBlock);
    }

    /**
     * 新增设备地块
     * 
     * @param tmnBlock 设备地块
     * @return 结果
     */
    @Override
    public int insertTmnBlock(TmnBlock tmnBlock)
    {
        return tmnBlockMapper.insertTmnBlock(tmnBlock);
    }

    /**
     * 修改设备地块
     * 
     * @param tmnBlock 设备地块
     * @return 结果
     */
    @Override
    public int updateTmnBlock(TmnBlock tmnBlock)
    {
        return tmnBlockMapper.updateTmnBlock(tmnBlock);
    }

    /**
     * 批量删除设备地块
     * 
     * @param tmnblockIds 需要删除的设备地块主键
     * @return 结果
     */
    @Override
    public int deleteTmnBlockByTmnblockIds(String[] tmnblockIds)
    {
        return tmnBlockMapper.deleteTmnBlockByTmnblockIds(tmnblockIds);
    }

    /**
     * 删除设备地块信息
     * 
     * @param tmnBaseBlockReqVo 设备地块主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTmnBlockByTmnblockId(TmnBaseBlockReqVo tmnBaseBlockReqVo)
    {
        String tmnblockId = tmnBaseBlockReqVo.getTmnblockId();
        if (ObjectUtils.isEmpty(this.tmnBlockMapper.selectTmnBlockByTmnblockId(tmnblockId))){
            throw new BizException(ErrorCode.INVALID_PARAMETER);
        }
        return tmnBlockMapper.deleteTmnBlockByTmnblockId(tmnblockId);
    }

    /**
     * 设备绑定
     *
     * @param tmnBaseBlockReqVo 设备
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTmnBaseBlockByBind(TmnBaseBlockReqVo tmnBaseBlockReqVo){
        String blockId = tmnBaseBlockReqVo.getBlockId();
        String tmnBizcode = tmnBaseBlockReqVo.getTmnBizcode();
        String[] bizCodeList = tmnBizcode.split(",");

        if (ObjectUtils.isEmpty(bizCodeList)){
            throw new BizException(ErrorCode.INVALID_PARAMETER);
        }

        List<TmnBase> tmnBaseList = this.tmnBaseMapper.selectTmnBaseByBizCodeList(bizCodeList);
        System.out.println(tmnBaseList.size());

        if (ObjectUtils.isEmpty(tmnBaseList)){
            throw new BizException(ErrorCode.TMN_DEVICE_NOT_EXIST_ERROR);
        }

        List<TmnBlock> tmnBlockList = new ArrayList<>();
        String[] tbkeys = new String[tmnBaseList.size()];
        int index = 0;
        for(TmnBase tmnBase: tmnBaseList){
            String tmnId = tmnBase.getTmnId();

            TmnBlock tmnBlock = new TmnBlock();
            tmnBlock.setTmnblockId(tmnBlock.getId());
            tmnBlock.setBlockId(blockId);
            tmnBlock.setTmnId(tmnId);
            tmnBlock.setTmnblockAssigndate(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));

            tmnBlockList.add(tmnBlock);

            tbkeys[index] = tmnId + blockId;
            index += 1;
        }

        List<TmnBlock> tmnList = this.tmnBlockMapper.selectTmnBlockByTmnIdAndBlockId(tbkeys);
        if (!ObjectUtils.isEmpty(tmnList)){
            throw new BizException(ErrorCode.TMN_DEVICE_BIND_ERROR);
        }

        return this.tmnBlockMapper.insertTmnBlockBatch(tmnBlockList);
    }

    /**
     * 查询地块设备列表
     *
     * @param tmnBlockDeviceListReqVo 设备地块
     * @return 设备地块集合
     */
    public List<TmnBlockDeviceListResVo> selectTmnBlockDeviceList(TmnBlockDeviceListReqVo tmnBlockDeviceListReqVo){
        return this.tmnBlockMapper.selectTmnBlockDeviceList(tmnBlockDeviceListReqVo);
    }
}
