package com.hssx.pcbms.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hssx.pcbms.entity.RackData;
import com.hssx.pcbms.mapper.RackDataMapper;
import com.hssx.pcbms.service.RackDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.pcbms.util.HttpKit;
import com.hssx.pcbms.util.HttpRespMsg;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @author 吴涛涛
 * @since 2019-11-14
 */
@Service
@EnableTransactionManagement
@Transactional
public class RackDataServiceImpl extends ServiceImpl<RackDataMapper, RackData> implements RackDataService {
    @Resource
    private RackDataMapper rackDataMapper;
    @Value("${open.door.url}")
    private String url;
    @Override
    public HttpRespMsg updateInfo(RackData rackData) {
        HttpRespMsg msg = new HttpRespMsg();
        rackDataMapper.update(rackData,new QueryWrapper<RackData>().eq("goods_id",rackData.getGoodsId()));
        //调用远程开门接口
        try {
            String s = HttpKit.get(url+"?"+rackData.getGoodsId(), true);
            //取出多余的\
            s = StringEscapeUtils.unescapeJava(s);
            JSONObject json = (JSONObject) JSON.parse(s);
            if(!json.containsValue("ok")){
                msg.setError("开门失败");
                return msg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }
}
