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.domain.TmnLand;
import com.yunfeiyun.agmp.fms.mapper.FmsLandMapper;
import com.yunfeiyun.agmp.fms.mapper.TmnBaseMapper;
import com.yunfeiyun.agmp.fms.mapper.TmnBlockMapper;
import com.yunfeiyun.agmp.fms.mapper.TmnLandMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.TmnBaseLandReqVo;
import com.yunfeiyun.agmp.fms.service.ITmnLandService;
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 TmnLandServiceImpl implements ITmnLandService
{
    @Autowired
    private TmnLandMapper tmnLandMapper;
    @Autowired
    private FmsLandMapper fmsLandMapper;

    @Autowired
    private TmnBaseMapper tmnBaseMapper;

    @Autowired
    private TmnBlockMapper tmnBlockMapper;

    /**
     * 查询设备基地
     * 
     * @param tmnlandId 设备基地主键
     * @return 设备基地
     */
    @Override
    public TmnLand selectTmnLandByTmnlandId(String tmnlandId)
    {
        return tmnLandMapper.selectTmnLandByTmnlandId(tmnlandId);
    }

    /**
     * 查询设备基地列表
     * 
     * @param tmnLand 设备基地
     * @return 设备基地
     */
    @Override
    public List<TmnLand> selectTmnLandList(TmnLand tmnLand)
    {
        return tmnLandMapper.selectTmnLandList(tmnLand);
    }

    /**
     * 新增设备基地
     * 
     * @param tmnLand 设备基地
     * @return 结果
     */
    @Override
    public int insertTmnLand(TmnLand tmnLand)
    {
        return tmnLandMapper.insertTmnLand(tmnLand);
    }

    /**
     * 修改设备基地
     * 
     * @param tmnLand 设备基地
     * @return 结果
     */
    @Override
    public int updateTmnLand(TmnLand tmnLand)
    {
        return tmnLandMapper.updateTmnLand(tmnLand);
    }

    /**
     * 批量删除设备基地
     * 
     * @param tmnlandIds 需要删除的设备基地主键
     * @return 结果
     */
    @Override
    public int deleteTmnLandByTmnlandIds(String[] tmnlandIds)
    {
        return tmnLandMapper.deleteTmnLandByTmnlandIds(tmnlandIds);
    }

    /**
     * 解绑基地设备
     * 
     * @param tmnBaseLandReqVo 设备
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTmnLandByTmnlandId(TmnBaseLandReqVo tmnBaseLandReqVo)
    {
        String tmnlandId = tmnBaseLandReqVo.getTmnlandId();

        TmnLand tmnLand = this.tmnLandMapper.selectTmnLandByTmnlandId(tmnlandId);
        if (ObjectUtils.isEmpty(tmnLand)){
            throw new BizException(ErrorCode.TMN_DEVICE_NOT_EXIST_ERROR);
        }
        //        检测设备是否绑定到地块
        TmnBlock tmnBlock = new TmnBlock();
        tmnBlock.setTmnId(tmnLand.getTmnId());

        List<TmnBlock> tmnBlockList = this.tmnBlockMapper.selectTmnBlockList(tmnBlock);
        if (!ObjectUtils.isEmpty(tmnBlockList)){
            throw new BizException(ErrorCode.TMN_DEVICE_BIND_BLOCK_ERROR);
        }
        return tmnLandMapper.deleteTmnLandByTmnlandId(tmnlandId);
    }

    /**
     * 设备绑定/解绑基地
     *
     * @param tmnBaseLandReqVo 设备
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTmnBaseLandByBind(TmnBaseLandReqVo tmnBaseLandReqVo){
        String landId = tmnBaseLandReqVo.getLandId();
        String tmnBizcode = tmnBaseLandReqVo.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<TmnLand> tmnLandList = new ArrayList<>();
        String[] tmnIDs = new String[tmnBaseList.size()];
        int index = 0;
        for(TmnBase tmnBase: tmnBaseList){
            String tmnId = tmnBase.getTmnId();

            TmnLand tmnLand = new TmnLand();
            tmnLand.setTmnlandId(tmnLand.getId());
            tmnLand.setTmnId(tmnId);
            tmnLand.setLandId(landId);
            tmnLand.setTmnlandAssigndate(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));

            tmnLandList.add(tmnLand);

            tmnIDs[index] = tmnId;
            index += 1;
        }

        List<TmnLand> tmnList = this.tmnLandMapper.selectTmnLandByTmnId(tmnIDs);
        if (!ObjectUtils.isEmpty(tmnList)){
            throw new BizException(ErrorCode.TMN_DEVICE_BIND_ERROR);
        }
        return this.tmnLandMapper.insertTmnLandBatch(tmnLandList);
    }
}
