package org.ta.pddserver.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ta.pddserver.entity.LogOrderBuyerDetailEntity;
import org.ta.pddserver.entity.LogOrderItemEntity;
import org.ta.pddserver.entity.LogOrderMailDetailEntity;
import org.ta.pddserver.entity.OrderCreateEntity;
import org.ta.pddserver.service.impl.LogOrderBuyerDetailImpl;
import org.ta.pddserver.service.impl.LogOrderItemImpl;
import org.ta.pddserver.service.impl.LogOrderMailDetailImpl;
import org.ta.pddserver.service.impl.OrderCreateImpl;

import java.util.List;

@RestController
//@RequestMapping("/test")
@Slf4j
public class TestController {

    @Resource
    OrderCreateImpl orderCreateImpl;
    @Resource
    LogOrderBuyerDetailImpl logOrderBuyerDetailImpl;
    @Resource
    LogOrderItemImpl logOrderItemImpl;
    @Resource
    LogOrderMailDetailImpl logOrderMailDetailImpl;



//    @GetMapping("")
    public String test() {
        List<OrderCreateEntity> orderCreateEntityList = orderCreateImpl.list();
        for (OrderCreateEntity orderCreateEntity : orderCreateEntityList) {
            String logisticsOrderCode = orderCreateEntity.getLogisticsOrderCode();
            LogOrderBuyerDetailEntity buyerDetailEntity = logOrderBuyerDetailImpl.getByOrderCode(logisticsOrderCode);
            if (buyerDetailEntity == null) {
                log.info("{} 缺少购买者详情信息。   ------------", logisticsOrderCode);
                String buyerDetails =  orderCreateEntity.getBuyerDetail();
            }
            List<LogOrderMailDetailEntity> mailDetailEntityList = logOrderMailDetailImpl.getListByCode(logisticsOrderCode);
            String mailDetails = orderCreateEntity.getMailDetails();
            JSONArray mailArr = JSONArray.parseArray(mailDetails);
            if (mailDetailEntityList.size() < mailArr.size()) {
                log.info("{} 包裹信息异常 应该 {} 个， 现有{}个。", logisticsOrderCode, mailArr.size(), mailDetailEntityList.size());
//                LogOrderMailDetailEntity mailDetailEntity = new LogOrderMailDetailEntity();
//                mailDetailEntity.setUid(orderCreateEntity.getUid());
//                mailDetailEntity.setLogisticsOrderCode(logisticsOrderCode);
//                mailDetailEntity.setMailNo(mailArr.getJSONObject(0).getString("mailNo"));
//                mailDetailEntity.setExpressCode(mailArr.getJSONObject(0).getString("expressCode"));
//                mailDetailEntity.setStatus(1);
//                mailDetailEntity.setCreated(orderCreateEntity.getCreated());
//                mailDetailEntity.setUpdated(orderCreateEntity.getUpdated());
//                mailDetailEntity.setRemarks("add");
//                logOrderMailDetailImpl.save(mailDetailEntity);
            }
            String items = orderCreateEntity.getItems();
            JSONArray itemArr = JSONArray.parseArray(items);
            List<LogOrderItemEntity> itemEntityList = logOrderItemImpl.getByCode(logisticsOrderCode);
            if (itemEntityList.size() != itemArr.size()) {
                log.info("{} 商品信息异常 应该 {} 个， 现有{}个。", logisticsOrderCode, itemArr.size(), itemEntityList.size());
//                LogOrderItemEntity orderItemEntity = new LogOrderItemEntity();
//                JSONObject itemObject = itemArr.getJSONObject(0);
//                orderItemEntity.setUid(itemObject.getString("uid"));
//                orderItemEntity.setLogisticsOrderCode(orderCreateEntity.getLogisticsOrderCode());
//                orderItemEntity.setItemId(itemObject.getString("itemId"));
//                orderItemEntity.setItemName(itemObject.getString("itemName"));
//                orderItemEntity.setCategoryId(itemObject.getLong("categoryId"));
//                orderItemEntity.setCategoryName(itemObject.getString("categoryName"));
//                orderItemEntity.setTotalActualPayment(itemObject.getLong("totalActualPayment"));
//                orderItemEntity.setCurrencyUnit(itemObject.getString("currencyUnit"));
//                orderItemEntity.setCurrency(itemObject.getString("currency"));
//                orderItemEntity.setItemQuantity(itemObject.getInteger("itemQuantity"));
//                orderItemEntity.setItemPicUrl(itemObject.getString("itemPicUrl"));
//                orderItemEntity.setItemSkuProperty(itemObject.getString("itemSkuProperty"));
//                orderItemEntity.setChargedStatus(itemObject.getBoolean("chargedStatus"));
//                orderItemEntity.setGoodsType(itemObject.getString("goodsType"));
//                orderItemEntity.setMagneticStatus(itemObject.getBoolean("magneticStatus"));
//                orderItemEntity.setCreated(orderCreateEntity.getCreated());
//                orderItemEntity.setUpdated(orderCreateEntity.getUpdated());
//                orderItemEntity.setRemarks("add");
//                logOrderItemImpl.save(orderItemEntity);
            }
        }
        return "end";
    }
}
