SnPartApiService.java 6.65 KB
package com.huaheng.pc.config.sn.service;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONArray;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResultLisenes;
import com.huaheng.pc.config.sn.domain.*;
import com.huaheng.pc.config.supplier.service.SupplierSyncService;
import com.huaheng.pc.config.syncTime.domain.SyncTime;
import com.huaheng.pc.config.syncTime.service.SyncTimeService;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.sn.mapper.SnPartDetailMapper;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class SnPartApiService   {
    @Resource
    private SnService snService;
    @Resource
    private SnPartDetailService snPartDetailService;
    @Resource
    private AddressService addressService;
    @Resource
    private SyncTimeService syncTimeService;
    @Resource
    private SupplierSyncService supplierSyncService;


    public AjaxResult getSnByLicenes(){
        String syncName="sn";
        SyncTime syncTime=supplierSyncService.getTimeByName(syncName);
        Date syncTimes=syncTime.getSyncTimes();
        Date date=new Date();
        // 将Date转换为Instant
        Instant instant = syncTimes.toInstant();
        // 将Instant往前推移10分钟
        Instant newInstant = instant.minus(5, ChronoUnit.MINUTES);
        // 将新的Instant转换回Date
        Date newDate = Date.from(newInstant);
        String newDatestr=DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",newDate);
        getSnLicenes(null,newDatestr);
        syncTimeService.updateStatus(syncName,1,date);
        return AjaxResult.success();
    }

    public AjaxResult snToIot(){
        List<Sn> snList=snService.getSnToLot();
        snPartToIot(snList);
//        for (Sn sn:snList){
//            snPartToIot(sn);
//        }
        return AjaxResult.success();
    }


    /**
     * 从授权服务器获取sn
     * @return
     */
    @Transactional
    public AjaxResult getSnLicenes(String snCode,String startTime){
        Address address=addressService.selectAddressByParam("SHOUQUAN",QuantityConstant.WAREHOUSE_CS);
        String url=address.getUrl();
        SnLicenesRequst snRequst=new SnLicenesRequst();
        if(StringUtils.isNotEmpty(snCode)){
            snRequst.setSn(snCode);
        }
        if(StringUtils.isNotEmpty(startTime)){
            snRequst.setStartTime(startTime);
        }
        String jsonString = JSONObject.toJSONString(snRequst);
        //请求授权服务器获取sn
        String result= OkHttpUtils.bodypost(url,jsonString, QuantityConstant.WAREHOUSE_CS,"从授权服务器获取sn");
        if (StringUtils.isEmpty(result)) {
            return AjaxResult.error("接口地址错误或服务器连接不到或返回为空");
        }
        AjaxResultLisenes ajaxResult= JSON.parseObject(result, AjaxResultLisenes.class);
        if(ajaxResult.hasErr()){
            return AjaxResult.error(ajaxResult.getMsg());
        }
        List<SnLicenesResponse> results=(List<SnLicenesResponse>) ajaxResult.getResult();
        List<Sn> list=new ArrayList<>();
        //保存sn号
        for (SnLicenesResponse sn:results) {
            if(sn.getSn()==null){
                continue;
            }
            Sn sn1=snService.getSn(sn.getSn());
            if(sn1!=null){
                continue;
            }
            Sn sn2=new Sn();
            sn2.setCode(sn.getSn());
            sn2.setCorrelatedCode(sn.getCorrelatedCode());
            sn2.setCreatedBy(ShiroUtils.getLoginName());
            sn2.setLastUpdatedBy(ShiroUtils.getLoginName());
            list.add(sn2);
        }
        snService.saveBatch(list);
        return AjaxResult.success();
    }
    /**
     * sn部件传递到iot
     * @return
     */
    @Transactional
    public AjaxResult snPartToIot(List<Sn> sns){
        SnIotResqust snIotResqust=new SnIotResqust();
        snIotResqust.setTypes("I");
        List<SnLicenesResponse> snLicenesResponses=new ArrayList<>();
        List<Sn> snList=new ArrayList<>();
        for (Sn sn:sns){
            List<SnPartDetail> snPartDetails=snPartDetailService.getSnPartByCode(sn.getCode());
            if(snPartDetails.size()<=0){
//                return AjaxResult.error(sn.getCode()+"没有部件详情");
                continue;
            }
            List<PartIOTDomain> parts=new ArrayList<>();
            for (SnPartDetail snPartDetail:snPartDetails){
                PartIOTDomain partIOTDomain=new PartIOTDomain();
                partIOTDomain.setPartCode(snPartDetail.getPart());
                parts.add(partIOTDomain);
            }
            SnLicenesResponse snLicenesResponse=new SnLicenesResponse();
            snLicenesResponse.setSn(sn.getCode());
            snLicenesResponse.setCorrelatedCode(sn.getCorrelatedCode());
            snLicenesResponse.setComponents(parts);
            snLicenesResponses.add(snLicenesResponse);
            snList.add(sn);
        }
        if(snLicenesResponses.size()<=0){
            return AjaxResult.success();
        }
        snIotResqust.setResult(snLicenesResponses);
        String jsonString = JSONObject.toJSONString(snIotResqust);
        Address address=addressService.selectAddressByParam("IOT",QuantityConstant.WAREHOUSE_CS);
        String url=address.getUrl();
        //请求授权服务器获取sn
        String result1= OkHttpUtils.bodypost(url,jsonString, QuantityConstant.WAREHOUSE_CS,"从授权服务器获取sn");
        if (StringUtils.isEmpty(result1)) {
            return AjaxResult.error("接口地址错误或服务器连接不到或返回为空");
        }
        //得到string 类型json
        result1=result1.substring(1,result1.length()-1);
        result1=result1.replaceAll("\\\\", "");
        AjaxResultLisenes ajaxResult= JSON.parseObject(result1, AjaxResultLisenes.class);
        if(ajaxResult.hasErr()){
            return AjaxResult.error(ajaxResult.getMsg());
        }
        for (Sn sn:snList){
            sn.setSyncIot(1);
            snService.updateById(sn);
        }
        return AjaxResult.success();
    }
}