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

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.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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by wangzubo on 2022/11/1.
 */
@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 contactOrder = contactOrderService.saveContactOrder(result);
        if (contactOrder) {
            contactLogService.clearContactLog(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);
    }
}
