package dao

import (
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/model"
	"time"
)

func GetStoreContract(db *DaoDB, storeId int, orderId string, contractApplyId string) (*model.LakalaContract, error) {
	merchantObj := &model.LakalaContract{}
	param := []interface{}{}
	sql := ` SELECT * FROM lakala_contract WHERE 1=1`

	if storeId != 0 {
		{
			sql += ` AND store_id  = ?`
			param = append(param, storeId)
		}
	}

	if orderId != "" {
		sql += ` AND contract_id = ?`
		param = append(param, orderId)
	}

	if contractApplyId != "" {
		sql += ` AND contract_apply_id = ?`
		param = append(param, contractApplyId)
	}

	if err := GetRow(db, merchantObj, sql, param...); err != nil {
		return nil, err
	}

	return merchantObj, nil
}

// GetStoreInfoByMerchantID 门店进件/分账商户
func GetStoreInfoByMerchantID(db *DaoDB, merchantNo string, storeId int, applyId, OrderID string) (*model.LakalaIncoming, error) {
	merchantObj := &model.LakalaIncoming{}
	param := []interface{}{}
	sql := ` SELECT * FROM lakala_incoming WHERE 1=1 `

	if merchantNo != "" {
		sql += ` AND merchant_no = ? OR merchant_no2 = ? `
		param = append(param, merchantNo, merchantNo)
	}
	if storeId != model.NO {
		sql += ` AND store_id = ?`
		param = append(param, storeId)
	}
	if applyId != "" {
		sql += ` AND apply_id = ?`
		param = append(param, applyId)
	}
	if OrderID != "" {
		sql += ` AND order_id = ?`
		param = append(param, OrderID)
	}
	if err := GetRow(db, merchantObj, sql, param...); err != nil {
		return nil, err
	}

	return merchantObj, nil
}

// GetRecipientByMerchantID 获取门店接受账户详细
func GetRecipientByMerchantID(db *DaoDB, receiverNo string) (*model.LakalaRecipient, error) {
	receiverObj := &model.LakalaRecipient{}
	sql := ` SELECT * FROM lakala_recipient WHERE receiver_no = ?`

	if err := GetRow(db, receiverObj, sql, []interface{}{receiverNo}...); err != nil {
		return nil, err
	}

	return receiverObj, nil
}

// GetSeparateRecords 分账记录
func GetSeparateRecords(db *DaoDB, merchantNo, separateNo, cmsType string) (*model.LakalaSeparateAmt, error) {
	records := &model.LakalaSeparateAmt{}
	sql := ` SELECT * FROM lakala_separate_amt WHERE 1=1 `
	param := []interface{}{}
	if merchantNo != "" {
		sql += `  AND merchant_no = ?`
		param = append(param, merchantNo)
	}
	if separateNo != "" {
		sql += `  AND separate_no = ?`
		param = append(param, separateNo)
	}
	if cmsType != "" {
		sql += `  AND cmd_type = ?`
		param = append(param, cmsType)
	}
	param = append(param)

	if err := GetRow(db, records, sql, param...); err != nil {
		return nil, err
	}

	return records, nil
}

// GetEwalletWithDraw 提现记录
func GetEwalletWithDraw(db *DaoDB, mercId, drawJnl string) (*model.LakalaWithdrawal, error) {
	receiverObj := &model.LakalaWithdrawal{}
	parama := []interface{}{}
	sql := ` SELECT * FROM lakala_withdrawal WHERE 1=1 `
	if mercId != "" {
		sql += ` AND merc_id = ?`
		parama = append(parama, mercId)
	}
	if drawJnl != "" {
		sql += ` AND draw_jnl = ? `
		parama = append(parama, drawJnl)
	}

	if err := GetRow(db, receiverObj, sql, parama...); err != nil {
		return nil, err
	}

	return receiverObj, nil
}

// GetIncoming 京西管理系统查询进件列表
func GetIncoming(storeID int, merchantNo string, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
	db := GetDB()
	txDB, _ := Begin(db)
	defer Commit(db, txDB)
	defer func() {
		if r := recover(); r != nil {
			Rollback(db, txDB)
			panic(r)
		}
	}()

	var incoming []*model.LakalaIncoming
	var param = []interface{}{}
	sql := ` SELECT SQL_CALC_FOUND_ROWS s.name,l.* FROM lakala_incoming l 
				 JOIN store s ON l.store_id = s.id 
             WHERE 1=1 `
	if storeID != 0 {
		sql += ` AND s.id = ? `
		param = append(param, storeID)
	}
	if merchantNo != "" {
		sql += ` AND l.merchant_no = ?`
		param = append(param, merchantNo)
	}
	sql += `
	ORDER BY l.total_amt desc
	LIMIT ? OFFSET ?
	`
	param = append(param, pageSize, offset)

	if err = GetRowsTx(txDB, &incoming, sql, param...); err == nil {
		pagedInfo = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       incoming,
		}
	}

	return pagedInfo, nil
}

// GetRecipientList 京西管理系统查询分账账户列表
func GetRecipientList(orgCode, receiverNo, receiverName string, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
	db := GetDB()
	txDB, _ := Begin(db)
	defer Commit(db, txDB)
	defer func() {
		if r := recover(); r != nil {
			Rollback(db, txDB)
			panic(r)
		}
	}()

	var recipient []*model.LakalaRecipient
	var param = []interface{}{}
	sql := ` SELECT SQL_CALC_FOUND_ROWS r.* FROM lakala_recipient r WHERE 1=1 `
	if orgCode != "" {
		sql += ` AND r.org_code = ? `
		param = append(param, orgCode)
	}
	if receiverNo != "" {
		sql += ` AND r.receiver_no = ?`
		param = append(param, receiverNo)
	}
	if receiverName != "" {
		sql += ` AND r.receiver_name like ?`
		param = append(param, "%"+receiverNo+"%")
	}
	sql += `
	ORDER BY r.id desc
	LIMIT ? OFFSET ?
	`
	param = append(param, pageSize, offset)

	if err = GetRowsTx(txDB, &recipient, sql, param...); err == nil {
		pagedInfo = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       recipient,
		}
	}

	return pagedInfo, nil
}

// GetSeparateAmtByOrderID 根据订单ID查询分账记录
func GetSeparateAmtByOrderID(vendorOrderID string, cmdType string, logNo string) (*model.LakalaSeparateAmt, error) {

	var recipient *model.LakalaSeparateAmt
	sql := ` SELECT * FROM lakala_separate_amt WHERE vendor_order_id = ?`
	param := []interface{}{vendorOrderID}
	if cmdType != "" {
		sql += `  AND cmd_type = ? `
		param = append(param, cmdType)
	}
	if logNo != "" {
		sql += `  AND log_no = ? `
		param = append(param, logNo)
	}
	if err := GetRow(GetDB(), &recipient, sql, param...); err != nil {
		return nil, err
	}

	return recipient, nil
}

// GetSeparateAmt 交易流水查询
func GetSeparateAmt(merchantNo, cmdType, status, separateNo, vendorOrderID string, storeId int, separateTimeStart, separateTimeEnd time.Time, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
	db := GetDB()
	txDB, _ := Begin(db)
	defer Commit(db, txDB)
	defer func() {
		if r := recover(); r != nil {
			Rollback(db, txDB)
			panic(r)
		}
	}()

	var recipient []*model.LakalaSeparateAmt
	var param = []interface{}{}
	sql := ` SELECT SQL_CALC_FOUND_ROWS r.* FROM lakala_separate_amt r WHERE 1=1 `
	if merchantNo != "" {
		sql += ` AND r.merchant_no = ? `
		param = append(param, merchantNo)
	}
	if vendorOrderID != "" {
		sql += ` AND r.vendor_order_id = ? `
		param = append(param, vendorOrderID)
	}
	if storeId != 0 {
		sql += ` AND r.store_id = ? `
		param = append(param, storeId)
	}
	if cmdType != "" {
		sql += ` AND r.cmd_type = ?`
		param = append(param, cmdType)
	}
	if status != "" {
		sql += ` AND r.status = ?`
		param = append(param, status)
	}
	if separateNo != "" {
		sql += ` AND r.separate_no = ?`
		param = append(param, separateNo)
	}
	if !separateTimeStart.IsZero() {
		sql += ` AND r.log_date >= ?`
		param = append(param, separateTimeStart)
	}
	if !separateTimeEnd.IsZero() {
		sql += ` AND r.log_date <= ?`
		param = append(param, separateTimeEnd)
	}

	sql += `
	ORDER BY r.id desc
	LIMIT ? OFFSET ?
	`
	param = append(param, pageSize, offset)

	if err = GetRowsTx(txDB, &recipient, sql, param...); err == nil {
		pagedInfo = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       recipient,
		}
	}

	return pagedInfo, nil
}

// WithdrawalList 提现记录
func WithdrawalList(merchantNo, drawJnl, acctName string, startTime, endTime time.Time, pageSize, offset int) (pagedInfo *model.PagedInfo, err error) {
	db := GetDB()
	txDB, _ := Begin(db)
	defer Commit(db, txDB)
	defer func() {
		if r := recover(); r != nil {
			Rollback(db, txDB)
			panic(r)
		}
	}()

	var recipient []*model.LakalaWithdrawal
	var param = []interface{}{}
	sql := ` SELECT SQL_CALC_FOUND_ROWS r.* FROM lakala_withdrawal r WHERE 1=1 `
	if merchantNo != "" {
		sql += ` AND r.merchant_no = ? `
		param = append(param, merchantNo)
	}
	if drawJnl != "" {
		sql += ` AND r.draw_jnl = ?`
		param = append(param, drawJnl)
	}
	if acctName != "" {
		sql += ` AND r.acct_name like ?`
		param = append(param, "%"+acctName+"%")
	}

	if !startTime.IsZero() {
		sql += ` AND r.created_time >= ?`
		param = append(param, startTime)
	}
	if !endTime.IsZero() {
		sql += ` AND r.created_time <= ?`
		param = append(param, endTime)
	}

	sql += `
	ORDER BY r.id desc
	LIMIT ? OFFSET ?
	`
	param = append(param, pageSize, offset)

	if err = GetRowsTx(txDB, &recipient, sql, param...); err == nil {
		pagedInfo = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       recipient,
		}
	}

	return pagedInfo, nil

}

// QueryApplyContractList 门店合同生气记录
func QueryApplyContractList(orderNo string, storeID, pageSize, offset int) (pagedInfo *model.PagedInfo, err error) {
	db := GetDB()
	txDB, _ := Begin(db)
	defer Commit(db, txDB)
	defer func() {
		if r := recover(); r != nil {
			Rollback(db, txDB)
			panic(r)
		}
	}()

	var recipient []*model.LakalaContract
	var param = []interface{}{}
	sql := ` SELECT SQL_CALC_FOUND_ROWS r.* FROM lakala_contract r WHERE 1=1 `

	if storeID != 0 {
		sql += ` AND r.store_id = ?`
		param = append(param, storeID)
	}
	if orderNo != "" {
		sql += ` AND r.contract_id = ? `
		param = append(param, orderNo)
	}

	sql += `
	ORDER BY r.id desc
	LIMIT ? OFFSET ?
	`
	pageSize = jxutils.FormalizePageSize(pageSize)
	offset = jxutils.FormalizePageOffset(offset)
	param = append(param, pageSize, offset)

	if err = GetRowsTx(txDB, &recipient, sql, param...); err == nil {
		pagedInfo = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       recipient,
		}
	}

	return pagedInfo, nil

}
