package dto

import (
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
)

type CooperationContractItem struct {
	CooperationContractId      int               `json:"cooperationContractId,string,"`
	CooperationContractNumber  string            `json:"cooperationContractNumber"` //合约编号
	CooperationProjectNumber   string            `json:"cooperationProjectNumber"`  //项目编号
	CooperationContractName    string            `json:"cooperationContractName"`   //合约名称
	Status                     int               `json:"status"`                    //合约状态
	CreateTime                 int               `json:"createTime"`
	Department                 domain.Department `json:"department"`
	IncentivesType             string            `json:"incentivesType"` //Incentives激励方式
	CooperationContractSponsor struct {
		UserId   int    `json:"userId,string,"`
		UserName string `json:"userName"`
	} `json:"cooperationContractSponsor"` //共创发起人
	CooperationMode                   domain.CooperationMode `json:"cooperationMode"`                   //共创模式
	Org                               domain.Org             `json:"org"`                               //组织结构
	CooperationContractUndertakerType []int                  `json:"cooperationContractUndertakerType"` //共创合约承接对象,1员工,2共创用户,3公开
	CooperationContractDescription    string                 `json:"cooperationContractDescription"`    //合约描述
}

func ToCooperationContractItem(param *allied_creation_cooperation.CooperationContract) *CooperationContractItem {
	cooperationContractUndertakerType := param.CooperationContractUndertakerTypes
	if len(cooperationContractUndertakerType) == 0 {
		cooperationContractUndertakerType = []int{}
	}
	data := CooperationContractItem{
		CooperationContractUndertakerType: cooperationContractUndertakerType,
		CooperationContractDescription:    param.CooperationContractDescription,
		CooperationContractId:             param.CooperationContractId,
		CooperationProjectNumber:          param.CooperationContractNumber,
		CooperationContractName:           param.CooperationContractName,
		CooperationContractNumber:         param.CooperationContractNumber,
		Status:                            param.Status,
		CreateTime:                        int(param.CreatedAt.UnixNano() / 1e6),
	}
	data.CooperationContractSponsor.UserId = param.CooperationContractSponsor.UserId
	data.CooperationContractSponsor.UserName = param.CooperationContractSponsor.UserInfo.UserName
	data.Department.DepartmentId = param.Department.DepartmentId
	data.Department.DepartmentName = param.Department.DepartmentName
	data.Org.OrgID = int(param.Org.OrgId)
	data.Org.OrgName = param.Org.OrgName
	data.CooperationMode.CooperationModeId = param.CooperationMode.CooperationModeId
	data.CooperationMode.CooperationModeName = param.CooperationMode.CooperationModeName
	data.CooperationMode.CooperationModeNumber = param.CooperationMode.CooperationModeNumber
	if len(param.DividendsIncentivesRules) > 0 {
		data.IncentivesType = "业绩分红"
	} else if len(param.MoneyIncentivesRules) > 0 {
		data.IncentivesType = "金额激励"
	}
	return &data
}

type MoneyIncentivesRule struct {
	MoneyIncentivesRuleId     int     `json:"moneyIncentivesRuleId,string"`
	CooperationContractNumber string  `json:"cooperationContractNumber"` // 关联的共创合约编号
	MoneyIncentivesAmount     float64 `json:"moneyIncentivesAmount"`     // 激励金额
	MoneyIncentivesStage      int     `json:"moneyIncentivesStage"`      // 金额激励阶段
	MoneyIncentivesTime       int64   `json:"moneyIncentivesTime"`       // 金额激励规则时间
	ReferrerPercentage        float64 `json:"referrerPercentage"`        // 推荐人抽成比例
	SalesmanPercentage        float64 `json:"salesmanPercentage"`        // 业务员抽成比例
}

type DividendsIncentivesRule struct {
	DividendsIncentivesRuleId int     `json:"dividendsIncentivesRuleId,string"`
	CooperationContractNumber string  `json:"cooperationContractNumber"` // 关联的项目合约编号
	ReferrerPercentage        float64 `json:"referrerPercentage"`        // 推荐人抽成比例
	SalesmanPercentage        float64 `json:"salesmanPercentage"`        // 业务员抽成比例
	DividendsPercentage       float64 `json:"dividendsPercentage"`       // 分红规则激励百分点
	DividendsStage            int     `json:"dividendsStage,string"`     // 分红规则激励阶段,
	DividendsStageEnd         int64   `json:"dividendsStageEnd"`         // 分红规则激励阶段结束
	DividendsStageStart       int64   `json:"dividendsStageStart"`       // 分红规则激励阶段开始
}

type Relevant struct {
	UserId     int               `json:"userId"`
	UserInfo   domain.UserInfo   `json:"userInfo"`
	Department domain.Department `json:"department"`
}

type ContractUndertaker struct {
	UndertakerId int                 `json:"undertakerId,string,"`
	Attachment   []domain.Attachment `json:"attachment"`
	Department   domain.Department   `json:"department"`
	HasReferrer  bool                `json:"hasReferrer"`
	HasSalesman  bool                `json:"hasSalesman"`
	ReferrerUser struct {
		UserId   int             `json:"userId,string"`
		UserInfo domain.UserInfo `json:"userInfo"`
	} `json:"referrerUser"`
	SalesmanUser struct {
		UserId   int             `json:"userId,string"`
		UserInfo domain.UserInfo `json:"userInfo"`
	}
	UserId   int             `json:"userId,string"`
	UserInfo domain.UserInfo `json:"userInfo"`
}
type CooperationContractInfo struct {
	CooperationContract               CooperationContractItem   `json:"cooperationContract"`
	Relevant                          []Relevant                `json:"relevant"`
	DividendsIncentivesRules          []DividendsIncentivesRule `json:"dividendsIncentivesRules"`
	MoneyIncentivesRules              []MoneyIncentivesRule     `json:"moneyIncentivesRules"`
	ContractUndertaker                []ContractUndertaker      `json:"contractUndertaker"`
	UndertakerTypesUncheckedAvailable []int                     `json:"undertakerTypesUncheckedAvailable"` // 可以去除勾选的共创项目承接对象列表
}

func ToCooperationContractInfo(param *allied_creation_cooperation.CooperationContract) *CooperationContractInfo {

	relevants := []Relevant{}
	dividendsIncentivesRules := []DividendsIncentivesRule{}
	moneyIncentivesRules := []MoneyIncentivesRule{}
	contractUndertaker := []ContractUndertaker{}
	for _, v := range param.RelevantPeople {
		r := Relevant{
			UserId: int(v.UserId),
			UserInfo: domain.UserInfo{
				UsersName: v.UserInfo.UsersName,
				UsersId:   v.UserInfo.UsersId,
				UserCode:  v.UserInfo.UserCode,
			},
			Department: domain.Department{
				DepartmentNumber: v.Department.DepartmentNumber,
				DepartmentId:     int(v.Department.DepartmentId),
				DepartmentName:   v.Department.DepartmentName,
			}}
		relevants = append(relevants, r)
	}

	for _, v := range param.DividendsIncentivesRules {
		r := DividendsIncentivesRule{
			DividendsIncentivesRuleId: v.DividendsIncentivesRuleId,
			CooperationContractNumber: v.CooperationContractNumber,
			ReferrerPercentage:        v.ReferrerPercentage,
			SalesmanPercentage:        v.SalesmanPercentage,
			DividendsPercentage:       v.DividendsIncentivesPercentage,
			DividendsStage:            v.DividendsIncentivesStage,
			DividendsStageEnd:         v.DividendsIncentivesStageEnd.UnixNano() / 1e6,
			DividendsStageStart:       v.DividendsIncentivesStageStart.UnixNano() / 1e6,
		}
		dividendsIncentivesRules = append(dividendsIncentivesRules, r)
	}

	for _, v := range param.MoneyIncentivesRules {
		r := MoneyIncentivesRule{
			MoneyIncentivesRuleId:     v.MoneyIncentivesRuleId,
			CooperationContractNumber: v.CooperationContractNumber,
			MoneyIncentivesAmount:     v.MoneyIncentivesAmount,
			MoneyIncentivesStage:      v.MoneyIncentivesStage,
			MoneyIncentivesTime:       v.MoneyIncentivesTime.UnixNano() / 1e6,
			ReferrerPercentage:        v.ReferrerPercentage,
			SalesmanPercentage:        v.SalesmanPercentage,
		}
		moneyIncentivesRules = append(moneyIncentivesRules, r)
	}

	for _, v := range param.Undertakers {
		attachment := v.ContractAttachment
		if len(attachment) == 0 {
			attachment = []domain.Attachment{}
		}
		u := ContractUndertaker{
			Attachment: attachment,
			Department: domain.Department{
				DepartmentNumber: v.Department.DepartmentNumber,
				DepartmentId:     int(v.Department.DepartmentId),
				DepartmentName:   v.Department.DepartmentName,
			},
			HasReferrer:  v.Referrer.UserId > 0,
			HasSalesman:  v.Salesman.UserId > 0,
			UndertakerId: v.UndertakerId,
			UserId:       v.UserId,
			UserInfo: domain.UserInfo{
				UsersName: v.UserInfo.UserName,
				UsersId:   v.UserId,
				Phone:     v.UserInfo.UserPhone,
			},
		}
		u.SalesmanUser.UserId = v.Salesman.UserId
		u.SalesmanUser.UserInfo.UsersId = v.Salesman.UserId
		u.SalesmanUser.UserInfo.UsersName = v.Salesman.UserInfo.UserName
		u.SalesmanUser.UserInfo.Phone = v.Salesman.UserInfo.UserPhone
		u.ReferrerUser.UserId = v.Referrer.UserId
		u.ReferrerUser.UserInfo.UsersId = v.Referrer.UserId
		u.ReferrerUser.UserInfo.UsersName = v.Referrer.UserInfo.UserName
		u.ReferrerUser.UserInfo.Phone = v.Referrer.UserInfo.UserPhone
		contractUndertaker = append(contractUndertaker, u)
	}
	undertakerTypesUncheckedAvailable := param.UndertakerTypesUncheckedAvailable
	if len(undertakerTypesUncheckedAvailable) == 0 {
		undertakerTypesUncheckedAvailable = []int{}
	}
	data := CooperationContractInfo{
		CooperationContract:               *ToCooperationContractItem(param),
		Relevant:                          relevants,
		DividendsIncentivesRules:          dividendsIncentivesRules,
		MoneyIncentivesRules:              moneyIncentivesRules,
		ContractUndertaker:                contractUndertaker,
		UndertakerTypesUncheckedAvailable: undertakerTypesUncheckedAvailable,
	}
	return &data

}

type CooperationContractUndertake struct {
	Undertaker struct {
		UsersName string `json:"userName"`
		Phone     string `json:"phone"`
		UserId    int    `json:"userId,string,"`
		UserCode  string `json:"userCode"`
	} `json:"undertaker"`
	CooperationContractDescription string `json:"cooperationContractDescription"`
	CooperationContractId          int    `json:"cooperationContractId,string,"`
	CooperationContractName        string `json:"cooperationContractName"`
	CooperationContractNumber      string `json:"cooperationContractNumber"`
	CooperationContractSponsor     struct {
		UsersName string `json:"userName"`
		Phone     string `json:"phone"`
		UserId    int    `json:"userId,string,"`
		UserCode  string `json:"userCode"`
	} `json:"cooperationContractSponsor"`
	CooperationMode struct {
		CooperationModeId     int    `json:"cooperationModeId,string"`
		CooperationModeName   string `json:"cooperationModeName"`
		CooperationModeNumber string `json:"cooperationModeNumber"`
	} `json:"cooperationMode"`
	Org struct {
		OrgName string `json:"orgName"`
	} `json:"org"`
	Attachment []domain.Attachment `json:"attachment"`
}

func ToCooperationContractUndertake(allied_creation_cooperation.DataCooperationContractSearchByUndertaker) *CooperationContractUndertake {
	data := CooperationContractUndertake{}
	return &data
}