package com.mvc.service.temp;

import com.mvc.constant.DeviceType;
import com.mvc.dao.TempServerIdenCfgMapper;
import com.mvc.entity.TempServerIdenCfg;
import com.mvc.entity.TempServerIdenCfgExample;
import com.mvc.model.TaskRemoteCfgModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TempServerIdenCfgService {
    @Resource
    private TempServerIdenCfgMapper tempServerIdenCfgMapper;

    /**
     * 根据远程配置任务保存服务侧配置
     * @param remoteCfgModel
     * @return
     */
    public TempServerIdenCfg saveTempServerIdenCfg(TaskRemoteCfgModel remoteCfgModel) {
        if (StringUtils.isEmpty(remoteCfgModel.getDeviceSn())) {
            return null;
        }

        //太原燃气表服务侧识别,记录角度与数字区域 2024-01-31 guozhao
        if (StringUtils.isEmpty(remoteCfgModel.getDeviceType())||!remoteCfgModel.getDeviceType().equals(DeviceType.GAS_METER.getCode())) {
            return null;
        }
        boolean isAdd = false;
        TempServerIdenCfg serverIdenCfg = findTempServerIdenCfgByDeviceSn(remoteCfgModel.getDeviceSn());
        if (serverIdenCfg == null) {
            serverIdenCfg = new TempServerIdenCfg();
            isAdd = true;
        }
        serverIdenCfg.setRemoteCfgId(remoteCfgModel.getId());
        serverIdenCfg.setDeviceSn(remoteCfgModel.getDeviceSn());
        serverIdenCfg.setUpdateTime(new Date());
        if (remoteCfgModel.getDigitNum() != null) {
            serverIdenCfg.setDigitNum(remoteCfgModel.getDigitNum());
        }
        if(remoteCfgModel.getDnType()!=null){
            serverIdenCfg.setDnType(remoteCfgModel.getDnType());
        }
        if(remoteCfgModel.getUnitType()!=null){
            serverIdenCfg.setUnitType(remoteCfgModel.getUnitType());
        }
        if(remoteCfgModel.getLastVal()!=null){
            serverIdenCfg.setLastVal(remoteCfgModel.getLastVal().intValue());
        }
        if (StringUtils.isNotEmpty(remoteCfgModel.getSetupDir())) {
            serverIdenCfg.setSetupDir(remoteCfgModel.getSetupDir());
        }
        if (StringUtils.isNotEmpty(remoteCfgModel.getImageConfig())) {
            serverIdenCfg.setEnableConfig(remoteCfgModel.getImageConfigFlag());
            serverIdenCfg.setImageLeanangle(remoteCfgModel.getImageLeanAngle());
            serverIdenCfg.setImageNumregionXmin(remoteCfgModel.getImageNumRegionXMin());
            serverIdenCfg.setImageNumregionXmax(remoteCfgModel.getImageNumRegionXMax());
            serverIdenCfg.setImageNumregionYmin(remoteCfgModel.getImageNumRegionYMin());
            serverIdenCfg.setImageNumregionYmax(remoteCfgModel.getImageNumRegionYMax());
        }

        if (isAdd) {
            tempServerIdenCfgMapper.insert(serverIdenCfg);
        } else {
            tempServerIdenCfgMapper.updateByExample(serverIdenCfg, getExampleByDeviceSn(remoteCfgModel.getDeviceSn()));
        }

        return serverIdenCfg;
    }

    /**
     * 根据SN获取配置
     *
     * @param deviceSn
     * @return
     */
    public TempServerIdenCfg findTempServerIdenCfgByDeviceSn(String deviceSn) {
        List<TempServerIdenCfg> list = tempServerIdenCfgMapper.selectByExample(getExampleByDeviceSn(deviceSn));
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    private TempServerIdenCfgExample getExampleByDeviceSn(String deviceSn) {
        TempServerIdenCfgExample example = new TempServerIdenCfgExample();
        TempServerIdenCfgExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(deviceSn);
        return example;
    }
}
