package com.unis.crk.hardware.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.msg.WriteCoilRequest;
import com.serotonin.modbus4j.msg.WriteCoilResponse;
import com.unis.crk.basic.entity.BasicEnum;
import com.unis.crk.hardware.entity.Devicecode;
import com.unis.crk.hardware.entity.Unithardware;
import com.unis.crk.hardware.entity.vo.UnithardwareVo;
import com.unis.crk.hardware.mapper.DevicecodeMapper;
import com.unis.crk.hardware.mapper.UnithardwareMapper;
import com.unis.crk.hardware.service.IDevicecodeService;
import com.unis.crk.hardware.service.IUnithardwareService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.utils.Modbus4jUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 单位硬件管理表 服务实现类
 * </p>
 *
 * @author ghh
 * @since 2021-09-15
 */
@Service
public class UnithardwareServiceImpl extends ServiceImpl<UnithardwareMapper, Unithardware> implements IUnithardwareService {
    @Resource
    private UnithardwareMapper unithardwareMapper;
    @Autowired
     private IDevicecodeService devicecodeService;
    @Override
    public List<Unithardware> findDeviceType(String HIID, String filterIP, String unitId,Integer IsUsed) {
        QueryWrapper<Unithardware> basicEnumQueryWrapper = new QueryWrapper<>();

        basicEnumQueryWrapper.eq("HIID",HIID);
        if(!StringUtils.isEmpty(filterIP)){
            basicEnumQueryWrapper.eq("Filter_IP",filterIP);
        }
        if(!StringUtils.isEmpty(IsUsed)){
            basicEnumQueryWrapper.eq("IsUsed",IsUsed);
        }
        basicEnumQueryWrapper.eq("UnitId",unitId);

        List<Unithardware> unithardwares = unithardwareMapper.selectList(basicEnumQueryWrapper);
        if(unithardwares !=null && unithardwares.size()>0){
            for (Unithardware unithardware:unithardwares){
                String dcid = unithardware.getDcid();
                Devicecode devicecode = devicecodeService.getById(dcid);
                unithardware.setDcid(devicecode.getDevicecode());
            }
        }
        return unithardwares;

    }

    @Override
    public List<Unithardware> getAllByUnitId(String unitId) {
        QueryWrapper<Unithardware> basicEnumQueryWrapper = new QueryWrapper<>();
        basicEnumQueryWrapper.eq("unitId",unitId);
        List<Unithardware> unithardwares = unithardwareMapper.selectList(basicEnumQueryWrapper);
        return unithardwares;
    }





    @Override
    public IPage<Map<String, Object>> getPageByUnitId(Page<Unithardware> objectPage, QueryWrapper<Unithardware> unithardwareQueryWrapper) {
        return  unithardwareMapper.getPageByUnitId(objectPage,unithardwareQueryWrapper);
    }

    @Override
    public List<Unithardware> getUnithardwareDropDown(UnithardwareVo unithardwareVo) {
        QueryWrapper<Unithardware> unithardwareQueryWrapper = new QueryWrapper<>();
        unithardwareQueryWrapper.select("UnitHIID,IPAddr,Note");
        unithardwareQueryWrapper.eq("UnitId",unithardwareVo.getUnitId());
        unithardwareQueryWrapper.eq("HIID",unithardwareVo.getHIID());
        unithardwareQueryWrapper.eq("isUsed",unithardwareVo.getIsUsed());
        unithardwareQueryWrapper.eq("Filter_IP",unithardwareVo.getFilterIp());
        return unithardwareMapper.selectList(unithardwareQueryWrapper);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public JSONObject updataIsUsed(String id, String ip) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        QueryWrapper<Unithardware> unithardwareQueryWrapper = new QueryWrapper<>();
        unithardwareQueryWrapper.eq("Filter_IP",ip);
        List<Unithardware> unithardwares = unithardwareMapper.selectList(unithardwareQueryWrapper);
        Iterator<Unithardware> iterator = unithardwares.iterator();
        while (iterator.hasNext()){
            Unithardware next = iterator.next();
            if(next.getUnitHIID().equals(id)){
                next.setIsUsed(true);
            }else{
                next.setIsUsed(false);
            }
        }

        boolean b = super.updateBatchById(unithardwares);
        if(b){
            jsonObject.set("success",true);
        }
        return jsonObject;
    }


}
