
using BaseModel.Models;
using System.Threading.Tasks;

namespace Wechat.PayAPI
{
    public class OrderQuery
    {
        /***
        * 订单查询完整业务流程逻辑
        * @param order 所查定单信息
        * @return 订单查询结果
        */
        public static async Task<WxPayData> Run(ElectronicOrderModel order)
        {
            WxPayData data = new WxPayData();
            data.SetValue("out_trade_no", order.BillNumber);
            WxPayData result = await WxPayApi.OrderQuery(data, (WxPayConfig)order.Config);//提交订单查询请求给API，接收返回数据

            if (result.GetValue("return_code").ToString() == "SUCCESS"
                && result.GetValue("result_code").ToString() == "SUCCESS")
            {
                //定单状态为支付成功
                if (result.GetValue("trade_state").ToString() == "SUCCESS")
                {
                    order.TradeStatus = TradeStatus.SUCCESS;
                }
                else if ((result.GetValue("trade_state").ToString() == "CLOSED") ||
                            (result.GetValue("trade_state").ToString() == "REVOKED"))
                {
                    order.TradeStatus = TradeStatus.CLOSED;
                }
            }
            return result;
        }
    }
}