package org.ta.pddserver.serviceutil;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.ta.pdd.entity.GDAttachmentEntity;
import org.ta.pdd.entity.GDTicketEntity;
import org.ta.pddserver.model.pddgd.response.GDTicketRepsonse;
import org.ta.pddserver.service.impl.GDAttachmentImpl;
import org.ta.pddserver.service.impl.GDTicketImpl;
import org.ta.pddserver.utils.PddGDUtil;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
@EnableScheduling
public class GDOperator {

    @Resource
    GDTicketImpl gdTicketImpl;
    @Resource
    private GDAttachmentImpl gDAttachmentImpl;

    @Value("${pdd.gd}")
    private Integer gd;

    @Scheduled(fixedRate = 1000 * 60 * 5, initialDelay = 1000) // 每5分钟执行一次
    public void getGdTickets() {
        if (gd > 0) {
            List<GDTicketRepsonse> list = PddGDUtil.getTickets();
            log.info("{} 工单数量：{}", new Date().toLocaleString(), list.size());
            for (GDTicketRepsonse gdTicketRepsonse : list) {
                List<GDTicketEntity> ticketEntities = gdTicketImpl.getListByTicketId(gdTicketRepsonse.getTicket_id());
                boolean find = false;
                for (GDTicketEntity gdTicketEntity : ticketEntities) {
                    if (gdTicketEntity.getUpdate_at().equals(gdTicketRepsonse.getUpdated_at())) {
                        find = true;
                        break;
                    }
                }
                if (find) {
                    continue;
                }
                GDTicketEntity  gdTicketEntity = new GDTicketEntity();
                gdTicketEntity.setTicket_id(gdTicketRepsonse.getTicket_id());
                gdTicketEntity.setCreate_type(gdTicketRepsonse.getCreate_type());
                gdTicketEntity.setReceive_address(gdTicketRepsonse.getReceive_address());
                gdTicketEntity.setSend_address(gdTicketRepsonse.getSend_address());
                gdTicketEntity.setPre_delivery_id(gdTicketRepsonse.getPre_delivery_id());
                gdTicketEntity.setHandle_result(gdTicketRepsonse.getHandle_result());
                gdTicketEntity.setExpress_company_id((long)gdTicketRepsonse.getExpress_company_id());
                gdTicketEntity.setCompensate_state((int)gdTicketRepsonse.getCompensate_state());
                gdTicketEntity.setCompensate_amount(gdTicketRepsonse.getCompensate_amount().intValue());
                gdTicketEntity.setSign_state((int)gdTicketRepsonse.getSign_state());
                gdTicketEntity.setExpress_dealer(gdTicketRepsonse.getExpress_dealer());
                gdTicketEntity.setExpress_dealer_contact(gdTicketRepsonse.getExpress_dealer_contact());
                gdTicketEntity.setRetreat_count(gdTicketRepsonse.getRetreat_count());
                gdTicketEntity.setReceive_name(gdTicketRepsonse.getReceive_name());
                gdTicketEntity.setReceive_contact(gdTicketRepsonse.getReceive_contact());

                gdTicketEntity.setCreate_at(gdTicketRepsonse.getCreated_at());
                gdTicketEntity.setUpdate_at(gdTicketRepsonse.getUpdated_at());
                gdTicketEntity.setTitle(gdTicketRepsonse.getTitle());
                gdTicketEntity.setDescription(gdTicketRepsonse.getDescription());
                gdTicketEntity.setSource(gdTicketRepsonse.getSource());
                gdTicketEntity.setWaybill_no(gdTicketRepsonse.getWaybill_no());
                gdTicketEntity.setUrgent_type(gdTicketRepsonse.getUrgent_type());
                gdTicketEntity.setType_id(gdTicketRepsonse.getType_id());
                gdTicketEntity.setStatus(gdTicketRepsonse.getStatus());
                gdTicketEntity.setCabinet_code(gdTicketRepsonse.getCabinet_code());
                gdTicketEntity.setMail_order_sn(gdTicketRepsonse.getMail_order_sn());
                gdTicketEntity.setPay_amount(gdTicketRepsonse.getPay_amount());
                gdTicketEntity.setArea_name(gdTicketRepsonse.getArea_name());

                gdTicketImpl.save(gdTicketEntity);

                //
                List<GDTicketRepsonse.ExpressAttachment> expressAttachmentList = gdTicketRepsonse.getExpress_attachment();
                if (expressAttachmentList == null) {
                    return;
                }
                for(GDTicketRepsonse.ExpressAttachment expressAttachmentLists: expressAttachmentList){
                    GDAttachmentEntity  gdAttachmentEntity = new GDAttachmentEntity();
                    gdAttachmentEntity.setTickId(gdTicketEntity.getTicket_id());
                    gdAttachmentEntity.setUrl(expressAttachmentLists.getUrl());
                    gdAttachmentEntity.setWaybill_no(gdTicketEntity.getWaybill_no());

                    gDAttachmentImpl.save(gdAttachmentEntity);
                }
            }
        }
    }
}
