package com.unis.base.server.service.impl;

import java.net.InetAddress;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.unis.base.common.util.HttpRequestUtil;
import com.unis.common.utils.JsonUtils;
import com.unis.common.utils.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.unis.base.common.vo.GranaryVO;
import com.unis.base.server.domain.GranaryDomain;
import com.unis.base.server.service.GranaryService;

/**
 * @author huangxing
 * @version 1.0
 * @Title DAjxxsjyServiceImpl
 * @ProjectName DAjxxsjyServiceImpl
 * @Description
 * @date 2019/07/22 17点26分
 */
@Slf4j
@Service
public class GranaryServiceImpl implements GranaryService {

    @Autowired
    private GranaryDomain granaryDomain;

    /**
     * 根据参数获取廒间信息
     * @param granaryVO
     * @return
     */
    @Override
    public List<GranaryVO> getGranaryList(GranaryVO granaryVO) {
        //DCfxxsjyDO dCfxxsjyDO = dCfxxsjyService.getDCfxxxsjyInfo(granaryVO.getCfbh());
        return granaryDomain.getGranaryList(granaryVO);
    }

    /**
     * 分页获取廒间信息列表
     * @param granaryVO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<GranaryVO> getGranaryPage(GranaryVO granaryVO, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return granaryDomain.getGranaryList(granaryVO);
    }

    /**
     * 添加廒间信息
     * @param granaryVO
     * @return
     */
    @Override
    public int insert(GranaryVO granaryVO) {
        return granaryDomain.insert(granaryVO);
    }

    /**
     * 更新廒间信息
     *
     * @param granaryVO
     * @return
     */
    @Override
    public int update(GranaryVO granaryVO) {

        return granaryDomain.update(granaryVO);
    }

    /**
     * 删除廒间信息
     * @param ajbh 廒间编号
     * @return
     */
    @Override
    public int delete(String ajbh) {
        //根据廒间编号获取廒间信息
        GranaryVO granaryVO = new GranaryVO();
        granaryVO.setAjbh(ajbh);
        granaryVO.setStatus(0);
        return granaryDomain.update(granaryVO);
    }

    /**
     * 根据廒间编号获取廒间信息
     * @param ajbh 廒间编号
     * @return
     */
    @Override
    public GranaryVO getGranaryByajbh(String ajbh) {
        return granaryDomain.getGranaryByajbh(ajbh);
    }

    @Override
    public ResponseWrapper depotPush(GranaryVO granaryVO) {
        String noticeStr = null;
        try {
            JSONObject ajxxJsonObject = new JSONObject();
            // 廒间代码
            ajxxJsonObject.put("ajdh", granaryVO.getAjbh());
            // 廒间名称
            ajxxJsonObject.put("ajmc", granaryVO.getAjmc());
            // 仓房(或油罐)编码
            ajxxJsonObject.put("cfbh", granaryVO.getCfbh());
            // 廒间长度
            ajxxJsonObject.put("ajcd", granaryVO.getAjcd());
            // 廒间宽度
            ajxxJsonObject.put("ajkd", granaryVO.getAjkd());
            // 廒间高度
            ajxxJsonObject.put("ajgd", granaryVO.getAjgd());
            // 廒间设计仓容
            ajxxJsonObject.put("ajsjcr", granaryVO.getAjsjcr());
            // 廒间联系电话
            ajxxJsonObject.put("ajlxdh", granaryVO.getLxdh());
            // 廒间启用日期
            ajxxJsonObject.put("ajqyrq", granaryVO.getQyrq());
            // 廒间状态
            ajxxJsonObject.put("ajzt", granaryVO.getAjzt());
            // 最后更新时间
            ajxxJsonObject.put("zhgxsj", granaryVO.getRefreshTime());
            noticeStr = URLEncoder.encode(JsonUtils.serialize(ajxxJsonObject), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> noticeMap = new HashMap<>();
        noticeMap.put("notice", noticeStr);
        InetAddress ip = null;
        try {
            ip = InetAddress.getLocalHost();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String ips = ip.toString();
        String url = "";
        if (ips.split("/")[1].contains("172")) {
            url = "http://101.36.160.140:10316/";
        } else {
            url = "";
            String resultStr = HttpRequestUtil.doFormPost(url, noticeMap);
            log.debug("仓房信息推送结果: {}", resultStr);
        }
        return ResponseWrapper.success();
    }
}
