package com.hai.goods.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.RedisUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.config.CommonConfig;
import com.hai.dao.GoodsLogisticsMapper;
import com.hai.entity.GoodsLogistics;
import com.hai.entity.GoodsLogisticsExample;
import com.hai.entity.GoodsOrder;
import com.hai.goods.service.GoodsLogisticsService;
import com.hai.goods.service.GoodsOrderExtService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service("goodsLogisticsService")
public class GoodsLogisticsServiceImpl implements GoodsLogisticsService {

    @Resource
    private GoodsLogisticsMapper goodsLogisticsMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private GoodsOrderExtService goodsOrderExtService;

    @Override
    public void insertGoodsLogistics(GoodsLogistics goodsLogistics) {
        goodsLogisticsMapper.insert(goodsLogistics);
    }

    @Override
    public void updateGoodsLogistics(GoodsLogistics goodsLogistics) {
        goodsLogisticsMapper.updateByPrimaryKey(goodsLogistics);
    }

    @Override
    public List<GoodsLogistics> getGoodsLogisticsList(Map<String, Object> map) {
        return null;
    }

    @Override
    public GoodsLogistics findGoodsLogisticsById(Long id) {
        return goodsLogisticsMapper.selectByPrimaryKey(id);
    }

    @Override
    public GoodsLogistics findGoodsLogistics(Long orderId) {
        GoodsLogisticsExample example = new GoodsLogisticsExample();
        GoodsLogisticsExample.Criteria criteria = example.createCriteria();

        criteria.andOrderIdEqualTo(orderId).andStatusEqualTo(1);

        List<GoodsLogistics> logistics = goodsLogisticsMapper.selectByExample(example);

        if (logistics.size() > 0) {
            return logistics.get(0);
        }

        return null;
    }

    @Override
    public GoodsLogistics findGoodsLogisticsByNum(String num) {
        GoodsLogisticsExample example = new GoodsLogisticsExample();
        GoodsLogisticsExample.Criteria criteria = example.createCriteria();

        criteria.andNumberEqualTo(num).andStatusEqualTo(1);

        List<GoodsLogistics> logistics = goodsLogisticsMapper.selectByExample(example);

        if (logistics.size() > 0) {
            return logistics.get(0);
        }

        return null;
    }

    @Override
    public GoodsLogistics editLogistics(JSONObject jsonObject) {

        JSONObject info = (JSONObject) jsonObject.getJSONArray("info").get(0);

        GoodsLogistics logistics = findGoodsLogisticsByNum(info.getString("mailNo"));

        if (logistics == null) {
            logistics = new GoodsLogistics();
        }

        logistics.setTaskNo(jsonObject.getString("orderNo"));

        logistics.setTheLastTime(info.getDate("theLastTime"));
        logistics.setTheLastMessage(info.getString("theLastMessage"));
        logistics.setTakeTime(info.getString("takeTime"));
        logistics.setNumber(info.getString("mailNo"));
        logistics.setLogisticsStatus(info.getString("logisticsStatus"));
        logistics.setExpressCompanyName(info.getString("logisticsCompanyName"));
        logistics.setLogisticsStatusDesc(info.getString("logisticsStatusDesc"));
        logistics.setLogisticsTraceDetails(info.getString("logisticsTraceDetailList"));
        logistics.setStatus(1);
        if ( logistics.getId() == null || !Objects.equals(logistics.getOrderId(), jsonObject.getLong("orderId"))) {
            logistics.setOrderId(jsonObject.getLong("orderId"));
            logistics.setCreateTime(new Date());
            insertGoodsLogistics(logistics);
        } else {
            logistics.setOrderId(jsonObject.getLong("orderId"));
            updateGoodsLogistics(logistics);
        }

        return logistics;
    }

    @Override
    public Object getLogisticsMsg(String num) {
        Object data = redisUtil.get("logisticsMsg" + num);
        if (data == null) {

            JSONObject jsonObjects = CommonConfig.getLogisticsMsg(num);
            redisUtil.set("logisticsMsgOl" + num , jsonObjects ,21600);
            if (jsonObjects.getInteger("code") == 200 && jsonObjects.getBoolean("success")) {

                GoodsLogistics logistics = editLogistics((JSONObject) jsonObjects.get("data"));

                GoodsOrder goodsOrder = goodsOrderExtService.findGoodsOrderByLogisticsNo(num);
                if (goodsOrder != null) {
                    goodsOrder.setUpdateTime(new Date());
                    if (Objects.equals(logistics.getLogisticsStatus(), "TRANSPORT")) {
                        goodsOrder.setStatus("3");
                    }
                    if (Objects.equals(logistics.getLogisticsStatus(), "AGENT_SIGN") || Objects.equals(logistics.getLogisticsStatus(), "SIGN")) {
                        goodsOrder.setStatus("4");
                    }
                    goodsOrderExtService.updateGoodsOrder(goodsOrder);
                }


                redisUtil.set("logisticsMsg" + num , logistics ,21600);

                return logistics;
            }

            return jsonObjects;

        } else {
            return data;
        }
    }

    @Override
    public Object getLogisticsMsg(Long orderId) {
        Object data = redisUtil.get("logisticsMsg" + orderId);
        if (data == null) {

            GoodsLogistics goodsLogistics = findGoodsLogistics(orderId);

            if (goodsLogistics == null || goodsLogistics.getNumber() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂未发货！");
            }

            JSONObject jsonObjects = CommonConfig.getLogisticsMsg(goodsLogistics.getNumber());
            redisUtil.set("logisticsMsgOl" + orderId , jsonObjects ,21600);
            if (jsonObjects.getInteger("code") == 200 && jsonObjects.getBoolean("success")) {

                jsonObjects.getJSONObject("data").put("orderId" , orderId);

                GoodsLogistics logistics = editLogistics((JSONObject) jsonObjects.get("data"));

                redisUtil.set("logisticsMsg" + orderId , logistics ,21600);

                return logistics;
            }

            return jsonObjects;

        } else {
            return data;
        }
    }
}
