package com.gic.haoban.manage.service.service.out.impl.contact;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.manage.api.constants.Manage3Constants;
import com.gic.haoban.manage.api.dto.contact.ContactOrderDetailDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactOrderQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactOrderSearchQDTO;
import com.gic.haoban.manage.api.service.contact.ContactOrderApiService;
import com.gic.haoban.manage.service.service.contact.ContactLogService;
import com.gic.haoban.manage.service.service.contact.ContactOrderService;
import com.gic.order.api.dto.resp.OrderInfoResp;
import com.gic.order.api.service.member.MemberOrderReadApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by wangzubo on 2022/11/1.
 */
@Slf4j
@Service("contactOrderApiService")
public class ContactOrderApiServiceImpl implements ContactOrderApiService {

    @Autowired
    private ContactOrderService contactOrderService;
    @Autowired
    private ContactLogService contactLogService;
    @Autowired
    private MemberOrderReadApiService memberOrderReadApiService;

    @Override
    public ServiceResponse<Void> saveContactOrder(ContactOrderQDTO contactOrderQDTO) {
        String enterpriseId = contactOrderQDTO.getEnterpriseId();
        String memberId = contactOrderQDTO.getMemberId();
        String orderId = contactOrderQDTO.getOrderId();
        boolean isOnlineOrder = contactOrderQDTO.isOnlineOrder();
        ServiceResponse<OrderInfoResp> serviceResponse =
                memberOrderReadApiService.getOrder(enterpriseId, memberId, orderId, isOnlineOrder);
        if (!serviceResponse.isSuccess() || serviceResponse.getResult() == null) {
            return ServiceResponse.success();
        }
        OrderInfoResp result = serviceResponse.getResult();
        boolean clearFlag = CollectionUtils.isNotEmpty(result.getOrderItems()) && result.getPaidAmount() > 0
                && (result.getReceiptsDate() != null && DateUtil.compare(DateUtil.offsetDay(new Date(), -Manage3Constants.CONTACT_ORDER_LIMIT_DAY), result.getReceiptsDate()) <= 0);
        log.info("获取订单时间,orderId:{},单据时间:{},应收:{}", result.getOrderId(),result.getReceiptsDate(),result.getPaidAmount());
        boolean contactOrder = contactOrderService.saveContactOrder(result);
        log.info("clearFlag:{},contactOrder:{}", clearFlag, contactOrder);
        if (clearFlag || contactOrder) {
            contactLogService.clearContactLog(enterpriseId,memberId, 1,"");
        }
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Page<ContactOrderDetailDTO>> pageContactOrder(String enterpriseId, ContactOrderSearchQDTO searchQDTO, BasePageInfo pageInfo) {
        Page<ContactOrderDetailDTO> page = contactOrderService.pageContactOrder(enterpriseId, searchQDTO, pageInfo);
        return ServiceResponse.success(page);
    }
}
