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.Order;
import com.winsun.bean.OrderHistory;
import com.winsun.bean.OrderView;
import com.winsun.constant.OrderStatus;
import com.winsun.mapper.OrderHistoryMapper;
import com.winsun.mapper.OrderMapper;
import com.winsun.mapper.OrderViewMapper;
import com.winsun.smsUtils.SendSmsAndMail;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

    private static OrderViewMapper orderViewMapper;
    private static OrderMapper orderMapper;
    private static OrderHistoryMapper orderHistoryMapper;
    @Autowired
    public IntelligentPlatformPushController(OrderViewMapper orderViewMapper,OrderMapper orderMapper,
                                             OrderHistoryMapper orderHistoryMapper) {
        IntelligentPlatformPushController.orderViewMapper = orderViewMapper;
        IntelligentPlatformPushController.orderMapper = orderMapper;
        IntelligentPlatformPushController.orderHistoryMapper = orderHistoryMapper;
    }

    /**
     * 智能平台推送信息接收
     *
     * @param sign
     * @param type
     * @param content
     * @return dataType：
     * 预付费 yff
     * 单宽带 dkd
     * 融合   rh
     */
    @RequestMapping(value = "/IntelligentPlatformPush/{dataType}/IntelligentPlatform",method = {RequestMethod.GET, RequestMethod.POST})
    @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())) {
            String orderId = signMap.get("orderNum").toString();
            String status = signMap.get("status").toString();

            Wrapper<Object> wrapper = new EntityWrapper<>();
            wrapper.eq("order_id", orderId);

            Wrapper<Order> orderWrappert = new EntityWrapper<>();
            orderWrappert.eq("order_id", orderId);
            List<Order> list = orderMapper.selectList(orderWrappert);

            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;
            }
            Wrapper<Order> orderWrapper = new EntityWrapper<>();
            Order order = new Order();
            order.setOrderId(orderId);
            order.setThStatus(status);
            order.setOrderStatus(orderStatus);
            Integer num = orderMapper.update(order,orderWrapper);
            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 = orderMapper.selectById(orderId).getId();
            //<script>SELECT id FROM `hhr_order` <where> ${ew.sqlSegment} </where> </script>
            //String id = orderViewMapper.selectOrderId(wrapper);
            try {
                Wrapper<OrderHistory> historyWrapper = new EntityWrapper<>();
                OrderHistory history = new OrderHistory();
                history.setOrderId(id);
                history.setCreateDate(new Date());
                history.setStatus(status);
                history.setOperator("智能平台推送");
                orderHistoryMapper.insert(history);
                //要加一个短信
                SendSmsAndMail.sendSms(list.get(0).getContactNumber(),"", "","14");
            } catch (Exception e) {
                log.error("智能平台推送信息接口插入异常",e.getMessage(), e);
            }
            return resultMap;
        } else {
            log.info("更新失败");
            resultMap.put("code", "12001");
            resultMap.put("msg", "智能平台订单号不能为空");

        }
        return resultMap;
    }
}
