package com.zhongshu.card.server.core.service.openAPI;

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.net.ResultContent;
import com.github.microservice.types.OrderModeType;
import com.github.microservice.types.OrderState;
import com.zhongshu.card.client.model.payment.ExpenseFlowSearch;
import com.zhongshu.card.client.openApi.model.ExpenseFlowOpenApiModel;
import com.zhongshu.card.client.openApi.params.ExpenseFlowOpenApiSearch;
import com.zhongshu.card.client.openApi.params.PaymentNoParam;
import com.zhongshu.card.server.core.dao.payment.ExpenseFlowDao;
import com.zhongshu.card.server.core.domain.payment.ExpenseFlow;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * 订单相关的 openAPI
 *
 * @author TRX
 * @date 2024/11/12
 */
@Slf4j
@Service
public class ExpenseFlowOpenApiService extends SuperService {

    @Autowired
    private ExpenseFlowDao expenseFlowDao;

    /**
     * 订单列表
     *
     * @param apiSearch
     * @return
     */
    public ResultContent<Page<ExpenseFlowOpenApiModel>> page(ExpenseFlowOpenApiSearch apiSearch) {
        if (StringUtils.isEmpty(apiSearch.getProjectCode())) {
            return ResultContent.buildFail("projectCode不能为空");
        }
        String projectOid = getOidByCode(apiSearch.getProjectCode());
        if (StringUtils.isEmpty(projectOid)) {
            projectOid = "unKnow";
        }
        ExpenseFlowSearch param = new ExpenseFlowSearch();
        BeanUtils.copyProperties(apiSearch, param);
        param.setIsPaySuccess(Boolean.TRUE);
        param.setOrderModeType(OrderModeType.Consume);
        param.setProjectOid(projectOid);

        Pageable pageable = PageRequest.of(apiSearch.getNumber(), apiSearch.getSize());
        Page<ExpenseFlow> page = expenseFlowDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    /**
     * 根据订单号 查询详情
     *
     * @param param
     * @return
     */
    public ResultContent<ExpenseFlowOpenApiModel> getDetailByPaymentNo(PaymentNoParam param) {
        if (StringUtils.isEmpty(param.getPaymentNo())) {
            return ResultContent.buildFail("paymentNo不能为空");
        }
        ExpenseFlow expenseFlow = expenseFlowDao.findTopByPaymentNo(param.getPaymentNo());
        if (ObjectUtils.isEmpty(expenseFlow)) {
            return ResultContent.buildFail("订单不存在");
        }
        return ResultContent.buildSuccess(toModel(expenseFlow));
    }

    /**
     * 关闭订单
     *
     * @param param
     * @return
     */
    public ResultContent closeOrder(PaymentNoParam param) {
        if (StringUtils.isEmpty(param.getPaymentNo())) {
            return ResultContent.buildFail("paymentNo不能为空");
        }
        ExpenseFlow expenseFlow = expenseFlowDao.findTopByPaymentNo(param.getPaymentNo());
        if (ObjectUtils.isEmpty(expenseFlow)) {
            return ResultContent.buildFail("订单不存在");
        }
        OrderState orderType = expenseFlow.getOrderState();
        if (orderType == OrderState.CLOSED) {
            return ResultContent.buildFail("订单已关闭");
        }
        if (expenseFlow.getIsPaySuccess() != null && expenseFlow.getIsPaySuccess()) {
            return ResultContent.buildFail("订单已支付成功，不能关闭");
        }
        expenseFlow.setOrderState(OrderState.CLOSED);
        expenseFlowDao.save(expenseFlow);
        return ResultContent.buildSuccess();
    }

    //----------------------------数据默认转换 start-----------------

    public ExpenseFlowOpenApiModel toModel(ExpenseFlow entity) {
        ExpenseFlowOpenApiModel model = new ExpenseFlowOpenApiModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

}
