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

import java.net.InetAddress;
import java.net.URLEncoder;
import java.util.Date;
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.base.common.vo.DCfxxsjyVO;
import com.unis.base.server.domain.DCfxxsjyDomain;
import com.unis.common.utils.JsonUtils;
import com.unis.common.utils.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.unis.base.common.vo.DHwxxsjyVO;
import com.unis.base.common.vo.GranaryVO;
import com.unis.base.server.domain.DHwxxsjyDomain;
import com.unis.base.server.entity.DHwxxsjyDO;
import com.unis.base.server.entity.DAjxxsjyDO;
import com.unis.base.server.service.DHwxxsjyService;
import com.unis.base.server.service.GranaryService;

/**
 * @author huangxing
 * @version 1.0
 * @Title DYgxxsjyServiceImpl
 * @ProjectName 货位服务实现层
 * @Description
 * @date 2019/07/22 17点26分
 */
@Slf4j
@Service
public class DHwxxsjyServiceImpl implements DHwxxsjyService {

    @Autowired
    private DHwxxsjyDomain dHwxxsjyDomain;

    @Autowired
    private GranaryService dAjxxsjyService;

    @Autowired
    private DCfxxsjyDomain dCfxxsjyDomain;

    /**
     * 根据参数查询货位列表
     *
     * @param dHwxxsjyVO
     * @return
     * @author huangxing
     */
    @Override
    public List<DHwxxsjyVO> getDHwxxsjyList(DHwxxsjyVO dHwxxsjyVO) {
        //DCfxxsjyDO dCfxxsjyDO = dCfxxsjyService.getDCfxxxsjyInfo(dHwxxsjyVO.getCfbh());
        return dHwxxsjyDomain.getDHwxxsjyList(dHwxxsjyVO);
    }

    /**
     * 根据参数分页查询货位列表
     *
     * @param dHwxxsjyVO
     * @return
     * @author huangxing
     */
    @Override
    public List<DHwxxsjyVO> getDHwxxsjyPage(DHwxxsjyVO dHwxxsjyVO, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return dHwxxsjyDomain.getDHwxxsjyList(dHwxxsjyVO);
    }

    /**
     * 新增货位信息
     *
     * @param dHwxxsjyVO
     * @return
     */
    @Override
    public int insert(DHwxxsjyVO dHwxxsjyVO) {
        //根据仓房编号获取仓房信息
//		GranaryVO granaryVO = dAjxxsjyService.getGranaryByajbh(dHwxxsjyVO.getCfbh());
        //GranaryVO granaryVO = dAjxxsjyService.getGranaryByajbh(dHwxxsjyVO.getHwbh());
        if (StringUtils.isEmpty(dHwxxsjyVO.getCfbh())) {
            DCfxxsjyVO dCfxxsjyVO = dCfxxsjyDomain.getDCfxxxsjyInfo(dHwxxsjyVO.getCfbh());
            if (dCfxxsjyVO == null) {
                return -1;
            }
            dHwxxsjyVO.setCfmc(dCfxxsjyVO.getCfmc());
            //dHwxxsjyVO.setHwmc(dCfxxsjyVO.getCfmc());
        }
        return dHwxxsjyDomain.insert(dHwxxsjyVO);
    }

    /**
     * 更新货位信息
     *
     * @param dHwxxsjyVO
     * @return
     */
    @Override
    public int update(DHwxxsjyVO dHwxxsjyVO) {
        //根据廒间编号获取廒间信息
        DHwxxsjyDO dHwxxsjyDO = dHwxxsjyDomain.getDHwxxsjyInfo(dHwxxsjyVO.getHwbh());
        if (dHwxxsjyDO == null) {//廒间不存在
            return 0;
        }
        return dHwxxsjyDomain.update(dHwxxsjyVO);
    }

    /**
     * 删除货位信息
     *
     * @param dHwxxsjyVO
     * @return
     * @author huangxing
     */
    @Override
    public int delete(String hwbh) {
        //根据廒间编号获取廒间信息
        DHwxxsjyDO dHwxxsjyDO = dHwxxsjyDomain.getDHwxxsjyInfo(hwbh);
        if (dHwxxsjyDO == null) {//廒间不存在
            return -1;
        }
        DHwxxsjyVO dHwxxsjyVO = new DHwxxsjyVO();
        dHwxxsjyVO.setHwbh(hwbh);
        dHwxxsjyVO.setStatus(0);
        return dHwxxsjyDomain.update(dHwxxsjyVO);
    }

    /**
     * 根据货位编号获取油罐信息
     *
     * @param hwbh
     * @return
     */
    @Override
    public DHwxxsjyDO getDHwxxsjyByhwbh(String hwbh) {
        return dHwxxsjyDomain.getDHwxxsjyInfo(hwbh);
    }

    @Override
    public ResponseWrapper depotPush(DHwxxsjyVO dHwxxsjyVO) {
        String noticeStr = null;
        try {
            JSONObject hwxxJsonObject = new JSONObject();
            // 货位代码
            hwxxJsonObject.put("hwdm", dHwxxsjyVO.getHwbh());
            // 货位名称
            hwxxJsonObject.put("hwmc", dHwxxsjyVO.getHwmc());
            // 廒间代码
            hwxxJsonObject.put("ajdm", "Y");
            // 货位启用日期
            hwxxJsonObject.put("hwqyrq", dHwxxsjyVO.getQyrq());
            // 货位容量
            hwxxJsonObject.put("hwrl", dHwxxsjyVO.getHwrl());
            // 保管单位
            hwxxJsonObject.put("bgdw", "N");
            // 保管员
            hwxxJsonObject.put("bgy", "N");
            // 最后更新时间
            Date refreshTime = dHwxxsjyVO.getRefreshTime();
            hwxxJsonObject.put("zhgxsj", "");
            noticeStr = URLEncoder.encode(JsonUtils.serialize(hwxxJsonObject), "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();
    }
}
