123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- package com.huimv.admin.service.impl;
- import cn.hutool.core.codec.Base64;
- import cn.hutool.core.collection.ListUtil;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.json.JSONUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.huimv.admin.common.utils.DataUill;
- import com.huimv.admin.common.utils.Result;
- import com.huimv.admin.common.utils.ResultCode;
- import com.huimv.admin.entity.BasePigpen;
- import com.huimv.admin.entity.EnvData;
- import com.huimv.admin.entity.EnvDevice;
- import com.huimv.admin.mapper.BasePigpenMapper;
- import com.huimv.admin.mapper.EnvDataMapper;
- import com.huimv.admin.mapper.EnvDeviceMapper;
- import com.huimv.admin.service.IEnvDeviceService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpMethod;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.client.RestTemplate;
- import javax.servlet.http.HttpServletRequest;
- import java.text.NumberFormat;
- import java.util.*;
- import java.util.concurrent.CopyOnWriteArrayList;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author author
- * @since 2023-02-13
- */
- @Service
- public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice> implements IEnvDeviceService {
- @Autowired
- private EnvDeviceMapper envDeviceMapper;
- @Autowired
- private BasePigpenMapper basePigpenMapper;
- @Autowired
- private EnvDataMapper dataMapper;
- // @Autowired
- // private RestTemplate restTemplate;
- @Override
- public Result count(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("farm_id", farmId);
- Integer count = envDeviceMapper.selectCount(queryWrapper);
- JSONObject jsonObject = new JSONObject();
- if (count == 0) {
- jsonObject.put("DeviceCount", 0);
- jsonObject.put("OnDeviceCount", 0);
- jsonObject.put("OffDeviceCount", 0);
- jsonObject.put("OnDeviceRate", 0);
- } else {
- QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
- queryWrapper1.eq("device_status", 1);
- Integer count1 = envDeviceMapper.selectCount(queryWrapper1);
- Integer OffDeviceCount = count- count1;
- //创建一个数值格式化对象
- NumberFormat numberFormat = NumberFormat.getInstance();
- //设置精确到小数点后两位
- numberFormat.setMaximumFractionDigits(2);
- String onDeviceRate = numberFormat.format((float)count1 / (float) count* 100) + "%";
- jsonObject.put("DeviceCount", count);
- jsonObject.put("OnDeviceCount", count1);
- jsonObject.put("OffDeviceCount", OffDeviceCount);
- jsonObject.put("OnDeviceRate", onDeviceRate);
- }
- return new Result(ResultCode.SUCCESS, jsonObject);
- }
- @Override
- public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- String pageSize = paramsMap.get("pageSize");
- String pageNo = paramsMap.get("pageNo");
- if (pageSize==null||pageSize=="") {
- pageSize = "10";
- }
- if (pageNo==null||pageNo=="") {
- pageNo = "1";
- }
- QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("farm_id", farmId);
- Page<EnvDevice> page = new Page(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
- return new Result(ResultCode.SUCCESS,envDeviceMapper.selectPage(page, queryWrapper));
- }
- @Override
- public Result add(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- String DeviceName = paramsMap.get("DeviceName");
- String BuildLocation = paramsMap.get("BuildLocation");
- String DeviceBrand = paramsMap.get("DeviceBrand");
- String remark = paramsMap.get("remark");
- if (remark == null || remark == "") {
- remark = null;
- }
- EnvDevice envDevice = new EnvDevice();
- envDevice.setFarmId(Integer.parseInt(farmId));
- envDevice.setDeviceName(DeviceName);
- envDevice.setBuildLocation(BuildLocation);
- envDevice.setDeviceBrand(DeviceBrand);
- envDevice.setRemark(remark);
- QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("device_name", DeviceName).eq("farm_id",farmId);
- EnvDevice device = envDeviceMapper.selectOne(queryWrapper);
- if (ObjectUtil.isEmpty(device)) {
- envDeviceMapper.insert(envDevice);
- } else {
- return new Result(ResultCode.FAIL, "设备名称已存在");
- }
- return new Result(ResultCode.SUCCESS,"添加成功");
- }
- @Override
- public Result edit(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- String id = paramsMap.get("id");
- String DeviceName = paramsMap.get("DeviceName");
- String BuildLocation = paramsMap.get("BuildLocation");
- String DeviceBrand = paramsMap.get("DeviceBrand");
- String remark = paramsMap.get("remark");
- if (remark == null || remark == "") {
- remark = null;
- }
- QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("id", id);
- EnvDevice envDevice = envDeviceMapper.selectOne(queryWrapper);
- envDevice.setFarmId(Integer.parseInt(farmId));
- envDevice.setDeviceName(DeviceName);
- envDevice.setDeviceBrand(DeviceBrand);
- envDevice.setBuildLocation(BuildLocation);
- envDevice.setRemark(remark);
- QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
- queryWrapper1.eq("device_name", DeviceName);
- if (ObjectUtil.isEmpty(envDeviceMapper.selectOne(queryWrapper1))) {
- envDeviceMapper.updateById(envDevice);
- } else {
- return new Result(ResultCode.FAIL, "设备名称已存在");
- }
- return new Result(ResultCode.SUCCESS);
- }
- @Override
- public Result delete(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- String id = paramsMap.get("id");
- QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("id", id).eq("farm_id", farmId);
- envDeviceMapper.delete(queryWrapper);
- return new Result(ResultCode.SUCCESS,"删除成功");
- }
- @Override
- public Result listPigpen(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- String id = paramsMap.get("id");//楼层id
- JSONArray jsonArray = new JSONArray();
- QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("farm_id", farmId);
- if (id == null || id == "") {
- queryWrapper.eq("parent_id", 3);
- } else {
- queryWrapper.like("other2", id);
- }
- List<BasePigpen> basePigpens = basePigpenMapper.selectList(queryWrapper);//得到栋舍单元
- for (int i = 0; i < basePigpens.size(); i++) {
- /* QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
- queryWrapper1.eq("unit_name",basePigpens.get(i).getId());
- EnvDevice envDevice = envDeviceMapper.selectOne(queryWrapper1);//找到栋舍绑定的设备,利用单元id*/
- QueryWrapper<EnvData> queryWrapper2 = new QueryWrapper<>();
- queryWrapper2.eq("unit_id",basePigpens.get(i).getId()).orderByDesc("create_time").last(" limit 1");//通过设备id来拿取数据
- EnvData envData = dataMapper.selectOne(queryWrapper2);
- JSONObject jsonObject = new JSONObject();
- if (ObjectUtil.isEmpty(envData)) {
- jsonObject.put("temp", 0);//温度
- jsonObject.put("hum", 0);//湿度
- jsonObject.put("location", basePigpens.get(i).getBuildName());
- jsonObject.put("unit_id", 0);//单元id
- } else {
- jsonObject.put("temp", envData.getEnvTemp());//温度
- jsonObject.put("hum", envData.getEnvHum());//湿度
- jsonObject.put("location", basePigpens.get(i).getBuildName());
- jsonObject.put("unit_id", envData.getUnitId());//单元id
- }
- jsonArray.add(jsonObject);
- }
- return new Result(ResultCode.SUCCESS,jsonArray);
- }
- @Override
- public Result listEnv(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- String id = paramsMap.get("id");//单元id
- String type = paramsMap.get("type");//查询类型
- String startTime = paramsMap.get("startTime");
- String endTime = paramsMap.get("endTime");
- if (type == null || type == "") {
- type = "1";
- }
- QueryWrapper<BasePigpen> basePigpenQueryWrapper = new QueryWrapper<>();
- basePigpenQueryWrapper.eq("farm_id", farmId).eq("id", id);
- BasePigpen basePigpen = basePigpenMapper.selectOne(basePigpenQueryWrapper);
- QueryWrapper<EnvData> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("unit_id", id).eq("farm_id", farmId);
- Map map = new HashMap<>();
- //自定义查询
- if ("4".equals(type)) {
- startTime = startTime + " 00:00:00";
- endTime = endTime + " 23:59:59";
- queryWrapper.between("create_time", startTime, endTime);
- List<EnvData> envData = dataMapper.listDay(queryWrapper);
- map.put("location", basePigpen.getBuildName());
- map.put("data", envData);
- }
- //本月
- else if ("3".equals(type)) {
- Date timesMonthmorning = DataUill.getTimesMonthmorning();
- queryWrapper.ge("create_time", timesMonthmorning);
- List<EnvData> envData = dataMapper.listDay(queryWrapper);
- map.put("location", basePigpen.getBuildName());
- map.put("data", envData);
- }
- //本周
- else if ("2".equals(type)) {
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
- queryWrapper.ge("create_time",calendar.getTime());
- List<EnvData> envData = dataMapper.listDay(queryWrapper);
- map.put("location", basePigpen.getBuildName());
- map.put("data", envData);
- }
- //今日
- else if ("1".equals(type)) {
- Date timesmorning = DataUill.getTimesmorning();
- queryWrapper.ge("create_time", timesmorning);
- List<EnvData> envData = dataMapper.listDay(queryWrapper);
- map.put("location", basePigpen.getBuildName());
- map.put("data", envData);
- }
- return new Result(ResultCode.SUCCESS,map);
- }
- @Override
- public Result listDeviceCount(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- Integer offCount = 0;
- Integer onCount = 0;
- QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("farm_id", farmId).eq("parent_id", 0);
- List<BasePigpen> basePigpens = basePigpenMapper.selectList(queryWrapper);//得到所有的栋
- JSONArray jsonArray = new JSONArray();
- for (int i = 0; i < basePigpens.size(); i++) {
- QueryWrapper<BasePigpen> queryWrapper1 = new QueryWrapper<>();
- queryWrapper1.like("other2", basePigpens.get(i).getId());
- List<BasePigpen> basePigpens1 = basePigpenMapper.selectList(queryWrapper1);//得到所有的楼层
- for (int j = 0; j < basePigpens1.size(); j++) {
- QueryWrapper<EnvDevice> deviceQueryWrapper = new QueryWrapper<>();
- deviceQueryWrapper.eq("unit_id",basePigpens1.get(j).getId());
- EnvDevice envDevice = envDeviceMapper.selectOne(deviceQueryWrapper);
- if (ObjectUtil.isNotEmpty(envDevice)) {
- if (envDevice.getDeviceStatus() == 0) {
- offCount++;
- } else {
- onCount++;
- }
- }
- }
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("location",basePigpens.get(i).getBuildName());
- jsonObject.put("onDevice", onCount);
- jsonObject.put("offDevice", offCount);
- jsonArray.add(jsonObject);
- offCount = 0;
- onCount = 0;
- }
- return new Result(ResultCode.SUCCESS,jsonArray);
- }
- // @Override
- // @Transactional
- // public Result sync(Map<String, String> params) throws Exception {
- // String farmId = params.get("farmId");
- // //获取所有栏舍
- // Map<String, Object> map = new HashMap<String, Object>();
- // String s = HttpClientSSLUtils.doPost("https://yzwlw.loongk.com/mobile/login?username=江西增鑫&password=21218cca77804d2ba1922c33e0151105", JSON.toJSONString(map));
- // System.out.println("登录带栏舍:"+s);
- // LoginDto loginDto = JSONUtil.toBean(s, LoginDto.class);
- // DataToken token = loginDto.getData().getToken();
- // String encode = Base64.encode(token.getUserId() + "_" + token.getToken());
- // HttpHeaders headers = new HttpHeaders();
- // headers.add("Authorization",encode);
- // HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
- // List<DataShacks> shacks = loginDto.getData().getShacks();
- // // 不能删除,需做比较
- // List<String> zengXinDeviceId = shacks.stream().map(DataShacks::getId).collect(Collectors.toList());
- // List<String> huatongDeviceId = envDeviceMapper.selectDeviceCodeByfarmId(Integer.parseInt(farmId));
- //
- // CopyOnWriteArrayList<String> zengXinDeviceIdCopy = ListUtil.toCopyOnWriteArrayList(zengXinDeviceId);
- // CopyOnWriteArrayList<String> huatongDeviceIdCopy = ListUtil.toCopyOnWriteArrayList(huatongDeviceId);
- // //新增的设备
- // zengXinDeviceIdCopy.removeAll(huatongDeviceId);
- // //新增
- // if (zengXinDeviceIdCopy.size() >0){
- // for (String deviceId : zengXinDeviceIdCopy) {
- // syncConfig(deviceId,requestEntity,farmId);
- // }
- // }
- // //需要删除的设备
- // huatongDeviceIdCopy.removeAll(zengXinDeviceId);
- // if (huatongDeviceIdCopy.size() >0){
- // this.remove(new QueryWrapper<EnvDevice>().in("device_code",huatongDeviceIdCopy));
- // }
- // System.out.println("zengxin:"+zengXinDeviceIdCopy);
- // System.out.println(huatongDeviceIdCopy);
- // return new Result(10000,"同步成功",false);
- // }
- @Override
- public Result bandingUnitId(HttpServletRequest httpServletRequest, EnvDevice envDevice) {
- Integer unitId = envDevice.getUnitId();
- int count = this.count(new QueryWrapper<EnvDevice>().eq("unit_id", unitId));
- if (count>0){
- return new Result(10001,"绑定失败,该栋舍已有设备",false);
- }
- this.updateById(envDevice);
- return new Result(10000,"绑定成功",true);
- }
- @Override
- public Result listPigpenAll(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
- String farmId = paramsMap.get("farmId");
- List objects = new ArrayList<>();
- List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().eq("farm_id", farmId).eq("f_type",3));
- for (BasePigpen basePigpen : basePigpens) {
- Integer id = basePigpen.getId();
- EnvData envData = dataMapper.selectOne(new QueryWrapper<EnvData>().eq("unit_id", id).orderByDesc("id").last("limit 1"));
- JSONObject jsonObject = new JSONObject();
- if (ObjectUtil.isNotEmpty(envData)){
- jsonObject.put("temp", envData.getEnvTemp());//温度
- jsonObject.put("hum", envData.getEnvHum());//湿度
- jsonObject.put("location", basePigpen.getBuildName());
- jsonObject.put("unit_id", basePigpen.getId());//单元id
- }else {
- jsonObject.put("temp", 0);//温度
- jsonObject.put("hum", 0);//湿度
- jsonObject.put("location", basePigpen.getBuildName());
- jsonObject.put("unit_id", basePigpen.getId());//单元id
- }
- objects.add(jsonObject);
- }
- return new Result(ResultCode.SUCCESS,objects);
- }
- // //添加新的设备
- // private void syncConfig(String shackId,HttpEntity httpEntity,String farmId) {
- // try {
- // ResponseEntity<String> exchangePeizhi = restTemplate.exchange("https://yzwlw.loongk.com/mobile/loadShackConfig/"+shackId, HttpMethod.GET, httpEntity, String.class);
- // String peizhibody = exchangePeizhi.getBody();
- // ShackConfigDto shackConfigDto = JSONUtil.toBean(peizhibody, ShackConfigDto.class);
- // ShackConfigData data = shackConfigDto.getData();
- // List<ShackConfigDataSensors> sensors = data.getSensors();
- // if (ObjectUtil.isNotEmpty(sensors)){
- // EnvDevice envDevice =new EnvDevice();
- // envDevice.setFarmId(Integer.parseInt(farmId));
- // envDevice.setDeviceBrand("增鑫");
- // envDevice.setDeviceCode(data.getId());
- // envDevice.setDeviceName(data.getName());
- // for (ShackConfigDataSensors sensor : sensors) {
- // if ("TEMPERATURE".equals(sensor.getType())){
- // envDevice.setOhter1(sensor.getId());
- // }
- // if ("HUMIDITY".equals(sensor.getType())){
- // envDevice.setOhter2(sensor.getId());
- // }
- // }
- // envDeviceMapper.insert(envDevice);
- // }
- // }catch (Exception e){
- // System.out.println("设备同步异常:" + e +" deviceId"+shackId);
- // }
- //
- // }
- }
|