package com.winsun.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.bean.OrderView;
import com.winsun.constant.OrderStatus;
import com.winsun.mapper.OrderViewMapper;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chancy
 * @Date: 2020/2/21 9:22
 */

@Slf4j
@RestController
@RequestMapping("/school_center")
public class IntelligentPlatformPushController {

    private static OrderViewMapper orderViewMapper;

    @Autowired
    public IntelligentPlatformPushController(OrderViewMapper orderViewMapper) {
        IntelligentPlatformPushController.orderViewMapper = orderViewMapper;
    }

    /**
     * 智能平台推送信息接收
     *
     * @param sign
     * @param type
     * @param content
     * @return dataType：
     * 预付费 yff
     * 单宽带 dkd
     * 融合   rh
     */
    @RequestMapping(value = "/IntelligentPlatformPush/{dataType}/IntelligentPlatform")
    @ResponseBody
    public Map<String, Object> IntelligentPlatform(String sign, String type, String content) {
        Map<String, Object> signMap = (Map<String, Object>) JSON.parse(content);
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isEmpty(type)) {
            log.info("业务类型不能为空---" + type + "---" + sign + "----" + content);
            resultMap.put("code", "12001");
            resultMap.put("msg", "业务类型不能为空");
            return resultMap;
        }
        if (StringUtils.isEmpty(sign)) {
            log.info("sign不能为空---" + type + "---" + sign + "----" + content);
            resultMap.put("code", "12001");
            resultMap.put("msg", "sign不能为空");
            return resultMap;
        }
        if (signMap.get("orderNum") != null && StringUtils.isNotBlank(signMap.get("orderNum").toString())) {
            Wrapper<Object> wrapper = new EntityWrapper<>();
            wrapper.eq("order_id", signMap.get("orderNum"));
            String status = signMap.get("status").toString();
            String orderStatus = null;
            try {
                orderStatus = OrderStatus.findById(signMap.get("statusId").toString()).getName();
            } catch (Exception e) {
                resultMap.put("code", "12001");
                resultMap.put("msg", "订单状态不存在");
                log.error(e.getMessage(), e);
                return resultMap;
            }
            Integer num = orderViewMapper.IntelligentPlatformPush(wrapper, status, orderStatus);
            if (num > 0) {
                if (type.equals("dkd") || type.equals("rh")) {
                    Wrapper<OrderView> wrapper1 = new EntityWrapper<>();
                    wrapper1.eq("orderSeqTHD", signMap.get("orderNum"));
                    Map<String, Object> dataMapping = new HashMap<>();
                    dataMapping.put("status", signMap.get("statusId"));
                    try {
                        orderViewMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper1);
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }
                log.info("更新成功！！！");
                resultMap.put("code", "000001");
                resultMap.put("msg", "消息接收成功");
            } else {
                log.info("更新失败");
                resultMap.put("code", "00001");
                resultMap.put("msg", "消息接收成功更新失败");

            }
            String id = orderViewMapper.selectOrderId(wrapper);
            Date date = new Date();
            Timestamp createTime = new Timestamp(date.getTime());
            try {
                orderViewMapper.insertOrderHistory(id, status, createTime);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return resultMap;
        } else {
            log.info("更新失败");
            resultMap.put("code", "12001");
            resultMap.put("msg", "智能平台订单号不能为空");

        }
        return resultMap;
    }
}
