package logic

import (
	"fmt"

	"github.com/fonchain/fonchain-artistinfo/cmd/internal/dao"
	statement "github.com/fonchain/fonchain-artistinfo/pb/artistinfoStatement"
	db "github.com/fonchain/fonchain-artistinfo/pkg/db"
	"github.com/fonchain/fonchain-artistinfo/pkg/util"
	"gorm.io/gorm"
)

type IStatement interface {
	UploadExcelOneTx(req *statement.UploadExcelOneTxRequest) (rep *statement.UploadExcelOneTxRespond, err error)
	UploadExcelTwoTx(req *statement.UploadExcelTwoTxRequest) (rep *statement.UploadExcelTwoTxRespond, err error)
	UploadExcelOneCopy(req *statement.UploadExcelOneCopyRequest) (rep *statement.UploadExcelOneCopyRespond, err error)
	UploadExcelTwoCopy(req *statement.UploadExcelTwoCopyRequest) (rep *statement.UploadExcelTwoCopyRespond, err error)
	GetAllBatchTimeTx(req *statement.GetAllBatchTimeTxRequest) (rep *statement.GetAllBatchTimeTxRespond, err error)
	GetArtworkSoldTxDetailList(req *statement.GetArtworkSoldTxDetailListRequest) (rep *statement.GetArtworkSoldTxDetailListRespond, err error)
	StatementList(req *statement.StatementListRequest) (rep *statement.StatementListRespond, err error)
	GetTxInfoByBatchUid(req *statement.GetTxInfoByBatchUidRequest) (rep *statement.GetTxInfoByBatchUidRespond, err error)
	GetCopyInfoByBatchUid(req *statement.GetCopyInfoByBatchUidRequest) (rep *statement.GetCopyInfoByBatchUidRespond, err error)
}

func NewStatement() IStatement {
	return &Statement{}
}

type Statement struct {
}

func (a *Statement) UploadExcelOneTx(req *statement.UploadExcelOneTxRequest) (rep *statement.UploadExcelOneTxRespond, err error) {
	rep = &statement.UploadExcelOneTxRespond{}
	fmt.Println("req.ExcelOneInfo", req.ExcelOneInfo)
	//开启事务,遇到错误就回滚
	tx := db.DB.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	for _, v := range req.ExcelOneInfo {
		//查看是否已经被生成了批次,没有的就生成物权批次
		exist, artworkTx, err := dao.IsExistArtworkTx(v.BatchTime, v.ArtistUid)
		if err != nil {
			return rep, err
		}

		if exist {
			//获取批次uid
			uid1, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			//生成批次
			err = dao.CreateArtworkTx(tx, uid1, v.BatchTime, v.ArtistUid, v.ArtistName)
			if err != nil {
				return rep, err
			}
			//对账单画作物权委托详情uid
			uid2, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			//将数据插入对账单画作物权委托详情
			err = dao.CreateArtworkTxDetail(tx, uid1, uid2, v)
			if err != nil {
				return rep, err
			}
		} else {
			//查看画作是否已经被该批次录入过,被录入过就跳过
			count, err := dao.IsExistArtworkTxDetail(artworkTx.Uid, v.TfNum)
			if err != nil {
				return rep, err
			}
			if count != 1 {
				continue
			}

			//获取批次uid
			uid, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			err = dao.CreateArtworkTxDetail(tx, artworkTx.Uid, uid, v)
			if err != nil {
				return rep, err
			}
		}

	}

	return
}

func (a *Statement) UploadExcelTwoTx(req *statement.UploadExcelTwoTxRequest) (rep *statement.UploadExcelTwoTxRespond, err error) {
	rep = &statement.UploadExcelTwoTxRespond{}
	//开启事务,遇到错误就回滚
	tx := db.DB.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	for _, v := range req.ExcelTwoInfo {
		//查看是否已经被生成了物权批次
		exist, artworkTx, err := dao.IsExistArtworkTx(v.BatchTime, v.ArtistUid)
		if err != nil {
			return rep, err
		}
		//没有的就生成物权批次
		if exist {
			//获取批次uid
			uid1, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			//生成批次
			err = dao.CreateArtworkTx(tx, uid1, v.BatchTime, v.ArtistUid, v.ArtistName)
			if err != nil {
				return rep, err
			}

			//对账单画作物权委托详情uid
			uid2, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			//将数据插入对账单画作物权销售委托详情
			err = dao.CreateArtworkSoldTxDetail(tx, uid1, uid2, v)
			if err != nil {
				return rep, err
			}
		} else {
			//查看画作是否已经被该批次录入过,被录入过就跳过
			count, err := dao.IsExistArtworkSoldTxDetail(artworkTx.Uid, v.TfNum)
			if err != nil {
				return rep, err
			}
			if count != 1 {
				continue
			}

			//获取批次uid
			uid, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			err = dao.CreateArtworkSoldTxDetail(tx, artworkTx.Uid, uid, v)
			if err != nil {
				return rep, err
			}
		}

	}

	return
}

func (a *Statement) UploadExcelOneCopy(req *statement.UploadExcelOneCopyRequest) (rep *statement.UploadExcelOneCopyRespond, err error) {
	rep = &statement.UploadExcelOneCopyRespond{}
	//开启事务,遇到错误就回滚
	tx := db.DB.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	for _, v := range req.ExcelOneInfo {
		//查看是否已经被生成了批次,没有的就生成版权批次
		exist, artworkCopy, err := dao.IsExistArtworkCopy(v.BatchTime, v.ArtistUid)
		if err != nil {
			return rep, err
		}

		if exist {
			//获取批次uid
			uid1, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			//生成批次
			err = dao.CreateArtworkCopy(tx, uid1, v.BatchTime, v.ArtistUid, v.ArtistName)
			if err != nil {
				tx.Rollback()
				return rep, err
			}

			//对账单画作物权委托详情uid
			uid2, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			//将数据插入对账单画作物权委托详情
			err = dao.CreateArtworkCopyDetail(tx, uid1, uid2, v)
			if err != nil {
				return rep, err
			}
		} else {

			count, err := dao.IsExistArtworkCopyDetail(artworkCopy.Uid, v.TfNum)
			if err != nil {
				return rep, err
			}
			if count != 1 {
				continue
			}

			//获取批次uid
			uid, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			err = dao.CreateArtworkCopyDetail(tx, artworkCopy.Uid, uid, v)
			if err != nil {
				return rep, err
			}
		}

	}

	return
}

func (a *Statement) UploadExcelTwoCopy(req *statement.UploadExcelTwoCopyRequest) (rep *statement.UploadExcelTwoCopyRespond, err error) {
	rep = &statement.UploadExcelTwoCopyRespond{}
	//开启事务,遇到错误就回滚
	tx := db.DB.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	for _, v := range req.ExcelTwoInfo {
		//查看是否已经被生成了物权批次
		exist, artworkCopy, err := dao.IsExistArtworkCopy(v.BatchTime, v.ArtistUid)
		if err != nil {
			return rep, err
		}
		//没有的就生成物权批次
		if exist {
			//获取批次uid
			uid1, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			//生成批次
			err = dao.CreateArtworkCopy(tx, uid1, v.BatchTime, v.ArtistUid, v.ArtistName)
			if err != nil {
				tx.Rollback()
				return rep, err
			}

			//对账单画作物权委托详情uid
			uid2, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			//将数据插入对账单画作物权销售委托详情
			err = dao.CreateArtworkSoldCopyDetail(tx, uid1, uid2, v)
			if err != nil {
				return rep, err
			}
		} else {

			//查看画作是否已经被该批次录入过,被录入过就跳过
			count, err := dao.IsExistArtworkSoldCopyDetail(artworkCopy.Uid, v.TfNum)
			if err != nil {
				return rep, err
			}
			if count != 1 {
				continue
			}

			//获取批次uid
			uid, err := util.GetUid()
			if err != nil {
				return rep, err
			}
			err = dao.CreateArtworkSoldCopyDetail(tx, artworkCopy.Uid, uid, v)
			if err != nil {
				return rep, err
			}
		}

	}

	return
}

func (a *Statement) GetAllBatchTimeTx(req *statement.GetAllBatchTimeTxRequest) (rep *statement.GetAllBatchTimeTxRespond, err error) {
	rep = &statement.GetAllBatchTimeTxRespond{}
	//batchTime去重查询,只获取batchTime字段值
	batchTime, err := dao.GetAllBatchTimeTx()
	if err != nil {
		return
	}

	rep.BatchTime = batchTime
	return
}

func (a *Statement) GetAllBatchTimeCopy(req *statement.GetAllBatchTimeCopyRequest) (rep *statement.GetAllBatchTimeCopyRespond, err error) {
	rep = &statement.GetAllBatchTimeCopyRespond{}
	//batchTime去重查询,只获取batchTime字段值
	batchTime, err := dao.GetAllBatchTimeCopy()
	if err != nil {
		return
	}

	rep.BatchTime = batchTime
	return
}

func (a *Statement) GetArtworkSoldTxDetailList(req *statement.GetArtworkSoldTxDetailListRequest) (rep *statement.GetArtworkSoldTxDetailListRespond, err error) {
	rep = &statement.GetArtworkSoldTxDetailListRespond{}
	artistList := &statement.ArtistList{}
	artworkSoldDetailList := &statement.ArtworkSoldDetailList{}

	var query *gorm.DB = db.DB // 初始化query变量

	//画家姓名不为空,模糊查询(根据画家姓名的某个字也可以查询)
	if req.ArtistName != "" {
		query = query.Where("artist_name LiKE ?", "%"+req.ArtistName+"%")
	}
	//批次不为空
	if req.BatchTime != "" {
		query = query.Where("batch_time = ?", req.BatchTime)
	}
	//画家姓名不为空
	if req.State != 0 {
		query = query.Where("state = ?", req.State)
	}

	//合同分页查询操作
	if req.Page < 1 {
		req.Page = 1
	}
	if req.Num < 1 {
		req.Num = 15
	}
	offset := (req.Page - 1) * req.Num

	artworkTxList, err := dao.GetArtworkTxList(query, req.Num, offset)
	if err != nil {
		return
	}

	for _, v := range artworkTxList {
		artistList.BatchUid = v.Uid
		artistList.ArtistName = v.ArtistName
		artistList.BatchTime = v.BatchTime
		artistList.State = v.State

		//当已经签署了合同时
		if v.State == 3 {
			//获取合同预览和下载链接
			contract, err := dao.GetContractUrlTx(v.Uid)
			if err != nil {
				return rep, err
			}
			artistList.ViewUrl = contract.ViewUrl
			artistList.DownloadUrl = contract.DownloadUrl
		} else {
			artistList.ViewUrl = ""
			artistList.DownloadUrl = ""
		}

		//查询对账单画作物权销售委托详情
		artworkSoldTxDetail, err := dao.GetArtworkSoldTxDetailList(v.Uid)
		if err != nil {
			return rep, err
		}

		//获取该画家该批次所有的已售出的画信息
		for _, m := range artworkSoldTxDetail {
			artworkSoldDetailList.ArtworkName = m.ArtworkName
			artworkSoldDetailList.TfNum = m.TfNum
			artworkSoldDetailList.Ruler = m.Ruler
			artworkSoldDetailList.SaleNo = m.SaleNo
			artworkSoldDetailList.CompleteDate = m.CompleteDate
			artworkSoldDetailList.SalePrice = m.SalePrice
			artistList.ArtworkSoldDetailList = append(artistList.ArtworkSoldDetailList, artworkSoldDetailList)
		}
		rep.ArtistList = append(rep.ArtistList, artistList)
	}

	return
}

func (a *Statement) GetArtworkSoldCopyDetailList(req *statement.GetArtworkSoldCopyDetailListRequest) (rep *statement.GetArtworkSoldCopyDetailListRespond, err error) {
	rep = &statement.GetArtworkSoldCopyDetailListRespond{}
	artistList := &statement.ArtistList{}
	artworkSoldDetailList := &statement.ArtworkSoldDetailList{}

	var query *gorm.DB = db.DB // 初始化query变量

	//画家姓名不为空
	if req.ArtistName != "" {
		query = query.Where("artist_name LiKE ?", "%"+req.ArtistName+"%")
	}
	//批次不为空
	if req.BatchTime != "" {
		query = query.Where("batch_time = ?", req.BatchTime)
	}
	//画家姓名不为空
	if req.State != 0 {
		query = query.Where("state = ?", req.State)
	}

	//合同分页查询操作
	if req.Page < 1 {
		req.Page = 1
	}
	if req.Num < 1 {
		req.Num = 15
	}
	offset := (req.Page - 1) * req.Num

	artworkCopyList, err := dao.GetArtworkCopyList(query, req.Num, offset)
	if err != nil {
		return
	}

	for _, v := range artworkCopyList {
		artistList.BatchUid = v.Uid
		artistList.ArtistName = v.ArtistName
		artistList.BatchTime = v.BatchTime
		artistList.State = v.State

		//当已经签署了合同时
		if v.State == 3 {
			//获取合同预览和下载链接
			contract, err := dao.GetContractUrlTx(v.Uid)
			if err != nil {
				return rep, err
			}
			artistList.ViewUrl = contract.ViewUrl
			artistList.DownloadUrl = contract.DownloadUrl
		} else {
			artistList.ViewUrl = ""
			artistList.DownloadUrl = ""
		}

		//查询对账单画作物权销售委托详情
		artworkSoldTxDetail, err := dao.GetArtworkSoldTxDetailList(v.Uid)
		if err != nil {
			return rep, err
		}

		//获取该画家该批次所有的已售出的画信息
		for _, m := range artworkSoldTxDetail {
			artworkSoldDetailList.ArtworkName = m.ArtworkName
			artworkSoldDetailList.TfNum = m.TfNum
			artworkSoldDetailList.Ruler = m.Ruler
			artworkSoldDetailList.SaleNo = m.SaleNo
			artworkSoldDetailList.CompleteDate = m.CompleteDate
			artworkSoldDetailList.SalePrice = m.SalePrice
			artistList.ArtworkSoldDetailList = append(artistList.ArtworkSoldDetailList, artworkSoldDetailList)
		}
		rep.ArtistList = append(rep.ArtistList, artistList)
	}

	return
}

func (a *Statement) StatementList(req *statement.StatementListRequest) (rep *statement.StatementListRespond, err error) {
	//查看是否有该画家
	user, err := dao.GetArtistInfoById(req.ArtistUid)
	if err != nil {
		return
	}

	ContractData, err := dao.StatementList(user.MgmtArtistUid, req.State)
	if err != nil {
		return
	}

	rep = &statement.StatementListRespond{
		Data: ContractData,
	}

	return
}

func (a *Statement) GetTxInfoByBatchUid(req *statement.GetTxInfoByBatchUidRequest) (rep *statement.GetTxInfoByBatchUidRespond, err error) {

	rep = &statement.GetTxInfoByBatchUidRespond{}

	//获取画家名
	artworkTx, err := dao.GetTxArtistNameByBatchUid(req.BatchUid)
	if err != nil {
		return
	}
	rep.ArtistName = artworkTx.ArtistName

	//获取物权画作详细信息
	artworkTxDetails, err := dao.GetArtworkTxDetailByBatchUid(req.BatchUid)
	if err != nil {
		return
	}
	rep.ArtistName = artworkTx.ArtistName

	//获取物权画作被售卖详细信息
	artworkSoldTxDetails, err := dao.GetArtworkSoldTxDetailByBatchUid(req.BatchUid)
	if err != nil {
		return
	}

	artworkTxDetail := new(statement.ArtworkTxDetail)
	artworkSoldTxDetail := new(statement.ArtworkSoldTxDetail)

	for _, v := range artworkTxDetails {
		artworkTxDetail.TfNum = v.TfNum
		artworkTxDetail.ArtworkName = v.ArtworkName
		artworkTxDetail.Ruler = v.Ruler
		artworkTxDetail.MinPrice = v.MinPrice
		artworkTxDetail.GuaranteePrice = v.GuaranteePrice
		rep.ArtworkTxDetail = append(rep.ArtworkTxDetail, artworkTxDetail)
	}

	for _, v := range artworkSoldTxDetails {
		artworkSoldTxDetail.TfNum = v.TfNum
		artworkSoldTxDetail.ArtworkName = v.ArtworkName
		artworkSoldTxDetail.Ruler = v.Ruler
		artworkSoldTxDetail.SaleNo = v.SaleNo
		artworkSoldTxDetail.CompleteDate = v.CompleteDate
		artworkSoldTxDetail.SalePrice = v.SalePrice
		rep.ArtworkSoldTxDetail = append(rep.ArtworkSoldTxDetail, artworkSoldTxDetail)
	}

	return
}

func (a *Statement) GetCopyInfoByBatchUid(req *statement.GetCopyInfoByBatchUidRequest) (rep *statement.GetCopyInfoByBatchUidRespond, err error) {

	rep = &statement.GetCopyInfoByBatchUidRespond{}

	//获取画家名
	artworkTx, err := dao.GetCopyArtistNameByBatchUid(req.BatchUid)
	if err != nil {
		return
	}
	rep.ArtistName = artworkTx.ArtistName

	//获取版权画作详细信息
	artworkTxDetails, err := dao.GetArtworkCopyDetailByBatchUid(req.BatchUid)
	if err != nil {
		return
	}
	rep.ArtistName = artworkTx.ArtistName

	//获取版权画作被售卖详细信息
	artworkSoldTxDetails, err := dao.GetArtworkSoldCopyDetailByBatchUid(req.BatchUid)
	if err != nil {
		return
	}

	artworkCopyDetail := new(statement.ArtworkCopyDetail)
	artworkSoldCopyDetail := new(statement.ArtworkSoldCopyDetail)

	for _, v := range artworkTxDetails {
		artworkCopyDetail.TfNum = v.TfNum
		artworkCopyDetail.ArtworkName = v.ArtworkName
		artworkCopyDetail.Ruler = v.Ruler
		artworkCopyDetail.MinPrice = v.MinPrice
		artworkCopyDetail.GuaranteePrice = v.GuaranteePrice
		rep.ArtworkCopyDetail = append(rep.ArtworkCopyDetail, artworkCopyDetail)
	}

	for _, v := range artworkSoldTxDetails {
		artworkSoldCopyDetail.TfNum = v.TfNum
		artworkSoldCopyDetail.ArtworkName = v.ArtworkName
		artworkSoldCopyDetail.Ruler = v.Ruler
		artworkSoldCopyDetail.SaleNo = v.SaleNo
		artworkSoldCopyDetail.CompleteDate = v.CompleteDate
		artworkSoldCopyDetail.MinPrice = v.MinPrice
		artworkSoldCopyDetail.SalePrice = v.SalePrice
		artworkSoldCopyDetail.GuaranteePrice = v.GuaranteePrice
		rep.ArtworkSoldCopyDetail = append(rep.ArtworkSoldCopyDetail, artworkSoldCopyDetail)
	}

	return
}