package services

import (
	"bytes"
	"database/sql"
	"encoding/json"
	"errors"
	"gin-vue-admin/common"
	"gin-vue-admin/global"
	"gin-vue-admin/models"
	"gin-vue-admin/repository"
	"gin-vue-admin/utils"
	"io/ioutil"
	"net/http"
)

func GetClientPayMealListService(chainIDStr, hospitalIDStr string) (interface{}, error) {

	return repository.GetClientPayMealListRepository(chainIDStr, hospitalIDStr)
}

func QueryClientState(chainIDStr, hospitalIDStr string) (interface{}, error) {

	db, err := common.NewAccountConn()
	if err != nil {
		return nil, err
	}

	chainID, err := utils.GetChainIDByStr(db, chainIDStr)
	if err != nil {
		return nil, err
	}
	hospitalID, err := utils.GetHospitalIDByStr(db, hospitalIDStr)
	if err != nil {
		return nil, err
	}

	return repository.QueryDoctorState(db, chainID, hospitalID)
}

func ClientPurchaseMeal(chainIDStr, hospitalIDStr string, m *models.ClientDoctorPurchase) (interface{}, error) {

	db, err := common.NewAccountConn()
	if err != nil {
		return nil, err
	}

	chainID, err := utils.GetChainIDByStr(db, chainIDStr)
	if err != nil {
		return nil, err
	}
	hospitalID, err := utils.GetHospitalIDByStr(db, hospitalIDStr)
	if err != nil {
		return nil, err
	}

	return ClientSmallProgramPay(db, chainID, hospitalID, m)
}

func ClientSmallProgramPay(db *sql.DB, chainID, hospitalID int, m *models.ClientDoctorPurchase) (interface{}, error) {

	// 查询套餐价格
	mealInfo, err := repository.QueryMealInfo(db, m.MealID)
	if err != nil {
		return nil, err
	}

	tx, err := db.Begin()
	if err != nil {
		return nil, err
	}

	// 写入 订单中
	billID, err := repository.InsertBDoctorBill(tx, chainID, hospitalID, mealInfo)
	if err != nil {
		return nil, err
	}

	var source uint8
	if m.PayType == 1 {
		source = 1
	} else {
		source = 5
	}

	var isServe uint
	var subMchid string
	if m.PayType == 1 {
		isServe = 1
		subMchid = "1636001492"
	} else {
		isServe = 0
		subMchid = ""
	}

	noticeURL := global.GVA_CONFIG.DoctorPay.Url
	// 吊起支付接口
	purchaseDoctor := &models.PurchaseDoctor{
		AttachInfo:   "",
		GoodsDes:     "谛宝宠医购买",
		GoodsDetail:  "谛宝宠医购买",
		GoodsPrice:   mealInfo.Money,
		SourceCode:   source,
		NoticeURL:    noticeURL,
		PayType:      1,
		PlatformType: 1,
		PlatformInfo: "谛宝宠医购买",
		IsServe:      isServe,
		SubMchid:     subMchid,
	}

	param, err := json.Marshal(purchaseDoctor)
	if err != nil {
		_ = tx.Rollback()
		return nil, err
	}

	reader := bytes.NewReader(param)
	request, err := http.NewRequest("POST", global.GVA_CONFIG.Pay.UnifiedOrder, reader)
	if err != nil {
		_ = tx.Rollback()
		return nil, err
	}
	response, err := http.DefaultClient.Do(request)
	if err != nil {
		_ = tx.Rollback()
		return nil, err
	}
	defer response.Body.Close()
	responseBody, err := ioutil.ReadAll(response.Body)
	if err != nil {
		_ = tx.Rollback()
		return nil, err
	}

	temp := &models.PayRes{}
	err = json.Unmarshal(responseBody, &temp)
	if err != nil {
		_ = tx.Rollback()
		return nil, err
	}

	if temp.Code != 0 {
		return nil, errors.New(temp.Message)
	}

	_, err = repository.UpdateBDoctorBill(tx, temp.Data.OutTradeNo, billID)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	_ = tx.Commit()

	return temp.Data, nil
}

// 试用
func ClientOnTrial(chainIDStr, hospitalIDStr string, m *models.DoctorPurchase) (interface{}, error) {

	db, err := common.NewAccountConn()
	if err != nil {
		return nil, err
	}

	chainID, err := utils.GetChainIDByStr(db, chainIDStr)
	if err != nil {
		return nil, err
	}
	hospitalID, err := utils.GetHospitalIDByStr(db, hospitalIDStr)
	if err != nil {
		return nil, err
	}

	// 查询套餐价格
	mealInfo, err := repository.QueryMealInfo(db, m.MealID)
	if err != nil {
		return nil, err
	}

	return repository.OnTrail(db, chainID, hospitalID, mealInfo)
}
