package com.winsun.TimingTask;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.bean.Order;
import com.winsun.mapper.*;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import com.winsun.utils.XbkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@Configuration
@EnableScheduling
public class OrderTask {
    private static OrderMapper orderMapper;


    @Autowired
    public OrderTask(OrderMapper orderMapper) {
        OrderTask.orderMapper = orderMapper;
    }


    @Scheduled(cron = "0 0 0 * * ? ")
    //@Scheduled(cron = "0/5 * * * * ? ")
    //@Scheduled(fixedRate=1000*60)
    @PostMapping("/orderTask")
    public void general() throws ParseException {
        Wrapper<Order> wrapper = new EntityWrapper();
        wrapper.eq("order_status", "待识别");
        List<Order> orders = orderMapper.selectList(wrapper);
        orders.forEach(order -> {
            try {
                Map<String, Object> dataMapping = new HashMap<>();
                JSONObject jsonObject = XbkUtil.orderInfoJT0006(order.getXbOrderId());
                String dataArray = jsonObject.getString("dataArray");
                Map<String,Object> Map = JSON.parseObject(dataArray, HashMap.class);
                Wrapper<Order> wrapper1 = new EntityWrapper();
                wrapper1.eq("xb_order_id",Map.get("orderCode"));
                wrapper1.eq("id",order.getId());
                if(Map.get("orderState").toString().equals("3") || Map.get("orderState").toString().equals("13") ||
                        Map.get("orderState").toString().equals("8") || Map.get("orderState").toString().equals("22") ||
                        Map.get("orderState").toString().equals("21") ){
                    dataMapping.put("order_status", "待处理");
                }
                if(Map.get("orderState").toString().equals("10") || Map.get("orderState").toString().equals("11")){
                    dataMapping.put("order_status", "异常单");
                    dataMapping.put("success_time", new Date());
                }
                orderMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper1);
                log.info(order.getXbOrderId()+"订单已更新");
            } catch (Exception e) {
                log.error(order.getXbOrderId()+"订单更新失败",e.getMessage());
            }
        });

        Wrapper<Order> wrapper2 = new EntityWrapper();
        wrapper2.eq("order_status", "待活体");
        List<Order> orders1 = orderMapper.selectList(wrapper);
        orders1.forEach(order -> {
            try {
                JSONObject jsonObject = XbkUtil.orderInfoJT0006(order.getXbOrderId());
                String dataArray = jsonObject.getString("dataArray");
                Map<String,Object> Map = JSON.parseObject(dataArray, HashMap.class);
                Wrapper<Order> wrapper1 = new EntityWrapper();
                wrapper1.eq("xb_order_id",Map.get("orderCode"));
                wrapper1.eq("id",order.getId());
                Map<String, Object> dataMapping = new HashMap<>();
                if(Map.get("orderState").toString().equals("3") || Map.get("orderState").toString().equals("13")){
                    dataMapping.put("order_status", "已完成");
                }
                if(Map.get("orderState").toString().equals("10") || Map.get("orderState").toString().equals("11")){
                    dataMapping.put("order_status", "异常单");
                }
                dataMapping.put("success_time", new Date());
                orderMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper1);
                log.info(order.getXbOrderId()+"订单已更新");
            } catch (Exception e) {
                log.error(order.getXbOrderId()+"订单更新失败",e.getMessage());
            }
        });

    }

}
