package com.huimv.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.entity.EnvDevice;
import com.huimv.admin.entity.EnvNh3n;
import com.huimv.admin.service.IEnvDeviceService;
import com.huimv.admin.service.IEnvNh3nService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-09-27
 */
@RestController
@RequestMapping("/env-nh3-n")
@CrossOrigin
public class EnvNh3nController {

    @Autowired
    private IEnvNh3nService nh3nService;
    @Autowired
    private IEnvDeviceService deviceService;
    @RequestMapping("/send")
    public void getNh3n(@RequestBody Map<String,String> paramsMap) {
        String result = paramsMap.get("result");
        String[] split = result.split(",");
        String nh3n = split[0];
        String[] split1 = nh3n.split("\\*");
        String deviceCode = split[1];
        QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("remark", deviceCode.substring(4)).eq("farm_id", 26);
        EnvDevice device = deviceService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(device)) {
            EnvNh3n envNh3n = new EnvNh3n();
            envNh3n.setFarmId(26);
            envNh3n.setCreateDate(new Date());
            envNh3n.setUnitId(device.getUnitId());
            envNh3n.setDeviceCode(device.getRemark());
            envNh3n.setNh3N(split1[1].substring(2));
            nh3nService.save(envNh3n);
        }
    }

//    public static void main(String[] args) {
////        String s = "0*001.7,9:0*980700100005,9:1*0000E";
//        String a = "S1:0*001.7,9:0*980700100004,9:1*0002E";
//        String[] split = a.split(",");
//        String s1 = split[0];
//        String[] split1 = s1.split("\\*");
//        System.out.println(split1[1]);
//        System.out.println(split1[1].substring(2));
//
////        System.out.println(s1.substring(4));
////        String s2 = split[1];
////        System.out.println(s2.substring(4));
//
//    }
}
