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"`    //合约描述
	AuthFlag                          bool                   `json:"authFlag"`
	UpChainID                         int                    `json:"upChainId"`
	BlockHash                         string                 `json:"blockChainId"`
}

func ToCooperationContractItem(param *allied_creation_cooperation.CooperationContract, operatorOrgId int64) *CooperationContractItem {
	cooperationContractUndertakerType := param.CooperationContractUndertakerTypes
	if len(cooperationContractUndertakerType) == 0 {
		cooperationContractUndertakerType = []int{}
	}
	data := CooperationContractItem{
		CooperationContractUndertakerType: cooperationContractUndertakerType,
		CooperationContractDescription:    param.CooperationContractDescription,
		CooperationContractId:             param.CooperationContractId,
		CooperationProjectNumber:          param.CooperationProjectNumber,
		CooperationContractName:           param.CooperationContractName,
		CooperationContractNumber:         param.CooperationContractNumber,
		Status:                            param.Status,
		CreateTime:                        int(param.CreatedAt.UnixNano() / 1e6),
		AuthFlag:                          param.Org.OrgId == operatorOrgId,
	}
	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.Department.DepartmentNumber = param.Department.DepartmentNumber
	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"`        // 业务员抽成比例
	Remarks                   string  `json:"remarks"`
}

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"`            // 分红规则激励阶段,
	DividendsStageEnd         int64   `json:"dividendsStageEnd"`         // 分红规则激励阶段结束
	DividendsStageStart       int64   `json:"dividendsStageStart"`       // 分红规则激励阶段开始
	Remarks                   string  `json:"remarks"`
}

type Relevant struct {
	UserId     int               `json:"userId,string"`
	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"`
	} `json:"salesmanUser"`
	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, operatorOrgId int64) *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.UserName,
				UsersId:   int(v.UserId),
				UserCode:  v.UserInfo.UserCode,
				Phone:     v.UserInfo.UserPhone,
			},
			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,
			Remarks:                   v.Remarks,
		}
		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,
			Remarks:                   v.Remarks,
		}
		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,
				UserCode:  v.UserInfo.UserCode,
			},
		}
		if v.Salesman.UserId > 0 {
			u.SalesmanUser = &struct {
				UserId   int             `json:"userId,string,"`
				UserInfo domain.UserInfo `json:"userInfo"`
			}{}
			u.SalesmanUser.UserId = v.Salesman.UserId
			u.SalesmanUser.UserInfo.UsersId = v.Salesman.UserId
			u.SalesmanUser.UserInfo.UsersName = v.Salesman.UserName
			u.SalesmanUser.UserInfo.Phone = v.Salesman.UserPhone
		}
		if v.Referrer.UserId > 0 {
			u.ReferrerUser = &struct {
				UserId   int             `json:"userId,string,"`
				UserInfo domain.UserInfo `json:"userInfo"`
			}{}
			u.ReferrerUser.UserId = v.Referrer.UserId
			u.ReferrerUser.UserInfo.UsersId = v.Referrer.UserId
			u.ReferrerUser.UserInfo.UsersName = v.Referrer.UserName
			u.ReferrerUser.UserInfo.Phone = v.Referrer.UserPhone
		}
		contractUndertaker = append(contractUndertaker, u)
	}
	undertakerTypesUncheckedAvailable := param.UndertakerTypesUncheckedAvailable
	if len(undertakerTypesUncheckedAvailable) == 0 {
		undertakerTypesUncheckedAvailable = []int{}
	}
	data := CooperationContractInfo{
		CooperationContract:               *ToCooperationContractItem(param, operatorOrgId),
		Relevant:                          relevants,
		DividendsIncentivesRules:          dividendsIncentivesRules,
		MoneyIncentivesRules:              moneyIncentivesRules,
		ContractUndertaker:                contractUndertaker,
		UndertakerTypesUncheckedAvailable: undertakerTypesUncheckedAvailable,
	}
	return &data
}

type CooperationContractUndertake struct {
	UndertakerId              int64               `json:"undertakerId,string"`       // 承接人ID
	UndertakerCode            string              `json:"undertakerCode"`            // 承接人编号
	UndertakerName            string              `json:"undertakerName"`            // 承接人姓名
	UndertakerPhone           string              `json:"undertakerPhone"`           // 承接人电话
	CooperationContractNumber string              `json:"cooperationContractNumber"` // 项目合约编号
	CooperationContractName   string              `json:"cooperationContractName"`   // 项目合约名称
	Attachment                []domain.Attachment `json:"attachment"`                // 合同附件
	SponsorName               string              `json:"sponsorName"`               // 发起人姓名
	DepartmentName            string              `json:"departmentName"`            // 发起部门名称
	CooperationModeName       string              `json:"cooperationModeName"`       // 共创模式名称
	OrgName                   string              `json:"orgName"`                   // 组织机构名称
}