1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
package domain

import (
	"fmt"
	"time"
)

const (
	TASK_TYPE_ROB = iota + 1 //抢单任务
	TASK_TYPE_BID            //竞标任务
)

const (
	REFERENCE_RESOURCE_TYPE_ISSUE  = iota + 1 //问题引用类型
	REFERENCE_RESOURCE_TYPE_CHANCE            //机会引用类型
)

const (
	TASK_STATUS_UNRELEASED   = iota + 1 //待发布
	TASK_STATUS_UNCLAIMED               //待领取
	TASK_STATUS_UNDERWAY                //进行中
	TASK_STATUS_UNACCEPTANCE            //待验收
	TASK_STATUS_COMPLETED               //已完成
	TASK_STATUS_CLOSED                  //关闭
)

// 任务
type Task struct {
	// 任务ID
	TaskId int64 `json:"taskId"`
	// 公司ID
	CompanyId int64 `json:"companyId"`
	// 任务名称
	TaskName string `json:"taskName"`
	// 任务类型Type
	TaskType int `json:"taskType"`
	// 任务发起者
	Sponsor *EmployeeInfo `json:"sponsor"`
	// 任务状态
	TaskStatus int `json:"taskStatus"`
	// 引用资源
	ReferenceResource *ReferenceResource `json:"referenceResource"`
	// 客户价值列表
	CustomerValue []string `json:"customerValue"`
	// 任务性质
	TaskNature string `json:"taskNature"`
	// 奖励素币
	SuMoney float64 `json:"suMoney"`
	// 验收标准
	AcceptanceStandard string `json:"acceptanceStandard"`
	// 任务描述
	TaskDescription string `json:"taskDescription"`
	// 任务图片URL列表
	TaskPictureUrls []string `json:"taskPictureUrls"`
	// 是否悬赏任务
	IsRewardTake bool `json:"isRewardTake"`
	// 抢单任务信息
	RobInfo *RobInfo `json:"robInfo"`
	// 竞标任务信息
	BidInfo *BidInfo `json:"bidInfo"`
	// 任务参与者列表
	Participators []*EmployeeInfo `json:"participators"`
	// 任务贡献占比
	TaskPercentage []*TaskPercentageItem `json:"taskPercentage"`
	// 解决报告
	SolveReport string `json:"solveReport"`
	// 解决图片URL列表
	SolvePictureUrls []string `json:"solvePictureUrls"`
	// 创建时间
	CreateTime time.Time `json:"createTime"`
	// 发布任务时间
	ReleaseTime time.Time `json:"releaseTime"`
	// 领取任务时间
	ReceiveTime time.Time `json:"receiveTime"`
	// 完成任务操作时间
	CompleteTime time.Time `json:"completeTime"`
	// 验收方验收时间
	AcceptanceTime time.Time `json:"acceptanceTime"`
	//当前状态
	CurrentStatus TaskStatus `json:"-"`
}

type TaskStatus interface {
	Update(task *Task, data map[string]interface{}) error
	Release(task *Task) error
	Rob(task *Task, receiver *EmployeeInfo) error
	Bib(task *Task, bidder *EmployeeInfo) error
	ChooseSuccessfulBidder(task *Task, successfulBidder *EmployeeInfo) error
	ApplyComplete(task *Task) error
	Off(task *Task) error
	Acceptance(task *Task, participators []*EmployeeInfo, taskPercentage []*TaskPercentageItem, referenceResourceScore []*ReferenceResourceItem, solveReport string, solvePictureUrls []string) error
}

type UnReleasedStatus struct{}

func (status *UnReleasedStatus) Update(task *Task, data map[string]interface{}) error {
	if taskName, ok := data["taskName"]; ok && taskName != "" {
		task.TaskName = taskName.(string)
	}
	if referenceResourceType, ok := data["referenceResourceType"]; ok && referenceResourceType != 0 {
		task.ReferenceResource.ReferenceResourceType = referenceResourceType.(int)
	}
	if referenceResourceItems, ok := data["referenceResourceItems"]; ok && len(referenceResourceItems.([]*ReferenceResourceItem)) != 0 {
		task.ReferenceResource.ReferenceResourceItems = referenceResourceItems.([]*ReferenceResourceItem)
	}
	if customerValue, ok := data["customerValue"]; ok && len(customerValue.([]string)) != 0 {
		task.CustomerValue = customerValue.([]string)
	}
	if taskNature, ok := data["taskNature"]; ok && taskNature != "" {
		task.TaskNature = taskNature.(string)
	}
	if suMoney, ok := data["suMoney"]; ok && suMoney.(float64) != float64(0) {
		task.SuMoney = suMoney.(float64)
	}
	if task.TaskType == TASK_TYPE_BID {
		if bidStartTime, ok := data["bidStartTime"]; ok && !bidStartTime.(time.Time).IsZero() {
			task.BidInfo.BidStartTime = bidStartTime.(time.Time)
		}
		if bidEndTime, ok := data["bidEndTime"]; ok && !bidEndTime.(time.Time).IsZero() {
			task.BidInfo.BidEndTime = bidEndTime.(time.Time)
		}
	}
	if acceptanceStandard, ok := data["acceptanceStandard"]; ok && acceptanceStandard != "" {
		task.AcceptanceStandard = acceptanceStandard.(string)
	}
	if taskDescription, ok := data["taskDescription"]; ok && taskDescription != "" {
		task.TaskDescription = taskDescription.(string)
	}
	if taskPictureUrls, ok := data["taskPictureUrls"]; ok && len(taskPictureUrls.([]string)) != 0 {
		task.TaskPictureUrls = taskPictureUrls.([]string)
	}
	if isRewardTake, ok := data["isRewardTake"]; ok && isRewardTake != false {
		task.IsRewardTake = isRewardTake.(bool)
	}
	return nil
}

func (status *UnReleasedStatus) Release(task *Task) error {
	task.ReleaseTime = time.Now()
	task.TaskStatus = TASK_STATUS_UNCLAIMED
	task.CurrentStatus = &UnClaimedStatus{}
	return nil
}

func (status *UnReleasedStatus) Rob(task *Task, receiver *EmployeeInfo) error {
	return fmt.Errorf("待发布的任务不允许抢单")
}

func (status *UnReleasedStatus) Bib(task *Task, bidder *EmployeeInfo) error {
	return fmt.Errorf("待发布的任务不允许竞标")
}

func (status *UnReleasedStatus) ChooseSuccessfulBidder(task *Task, successfulBidder *EmployeeInfo) error {
	return fmt.Errorf("待发布的任务不允许选择竞标任务的中标人")
}

func (status *UnReleasedStatus) ApplyComplete(task *Task) error {
	return fmt.Errorf("待发布的任务不允许申请完成任务")
}

func (status *UnReleasedStatus) Off(task *Task) error {
	return fmt.Errorf("待发布的任务不允许关闭")
}

func (status *UnReleasedStatus) Acceptance(task *Task, participators []*EmployeeInfo, taskPercentage []*TaskPercentageItem, referenceResourceScore []*ReferenceResourceItem, solveReport string, solvePictureUrls []string) error {
	return fmt.Errorf("待发布的任务不允许验收")
}

type UnClaimedStatus struct{}

func (status *UnClaimedStatus) Update(task *Task, data map[string]interface{}) error {
	return fmt.Errorf("待领取的任务不允许编辑")
}

func (status *UnClaimedStatus) Release(task *Task) error {
	return fmt.Errorf("待领取的任务不允许重新发布")
}

func (status *UnClaimedStatus) Rob(task *Task, receiver *EmployeeInfo) error {
	if task.TaskType != TASK_TYPE_ROB {
		return fmt.Errorf("类型不符合的任务不允许抢单")
	}
	currentTime := time.Now()
	task.RobInfo = &RobInfo{
		Receiver:    receiver,
		ReceiveTime: currentTime,
	}
	task.ReceiveTime = currentTime
	task.TaskStatus = TASK_STATUS_UNDERWAY
	task.CurrentStatus = &UnderwayStatus{}
	return nil
}

func (status *UnClaimedStatus) Bib(task *Task, bidder *EmployeeInfo) error {
	if task.TaskType != TASK_TYPE_BID {
		return fmt.Errorf("类型不符合的任务不允许竞标")
	}
	currentTime := time.Now()
	if task.BidInfo == nil {
		return fmt.Errorf("无效的竞标信息")
	} else {
		bidTime := currentTime
		if bidTime.Before(task.BidInfo.BidStartTime) {
			return fmt.Errorf("竞标还没开始")
		}
		if bidTime.After(task.BidInfo.BidEndTime) {
			return fmt.Errorf("竞标已经结束")
		}
		task.BidInfo.BidderInfos = append(task.BidInfo.BidderInfos, &BidderInfo{
			Bidder:  bidder,
			BidTime: bidTime,
		})
		return nil
	}
}

func (status *UnClaimedStatus) ChooseSuccessfulBidder(task *Task, successfulBidder *EmployeeInfo) error {
	if task.TaskType != TASK_TYPE_BID {
		return fmt.Errorf("类型不符合的任务不允许选择竞中标人")
	}
	if task.BidInfo == nil {
		return fmt.Errorf("无效的竞标信息")
	} else {
		winBidTime := time.Now()
		task.BidInfo.SuccessfulBidder = successfulBidder
		task.BidInfo.WinBidTime = winBidTime
		task.ReceiveTime = winBidTime
		task.TaskStatus = TASK_STATUS_UNDERWAY
		task.CurrentStatus = &UnderwayStatus{}
		return nil
	}
}

func (status *UnClaimedStatus) ApplyComplete(task *Task) error {
	return fmt.Errorf("待领取的任务不允许申请完成任务")
}

func (status *UnClaimedStatus) Off(task *Task) error {
	task.TaskStatus = TASK_STATUS_CLOSED
	task.CurrentStatus = &ClosedStatus{}
	return nil
}

func (status *UnClaimedStatus) Acceptance(task *Task, participators []*EmployeeInfo, taskPercentage []*TaskPercentageItem, referenceResourceScore []*ReferenceResourceItem, solveReport string, solvePictureUrls []string) error {
	return fmt.Errorf("待领取的任务不允许验收")
}

type UnderwayStatus struct{}

func (status *UnderwayStatus) Update(task *Task, data map[string]interface{}) error {
	return fmt.Errorf("进行中的任务不允许编辑")
}

func (status *UnderwayStatus) Release(task *Task) error {
	return fmt.Errorf("进行中的任务不允许重新发布")
}

func (status *UnderwayStatus) Rob(task *Task, receiver *EmployeeInfo) error {
	return fmt.Errorf("进行中的任务不允许抢单")
}

func (status *UnderwayStatus) Bib(task *Task, bidder *EmployeeInfo) error {
	return fmt.Errorf("进行中的任务不允许竞标")
}

func (status *UnderwayStatus) ChooseSuccessfulBidder(task *Task, successfulBidder *EmployeeInfo) error {
	return fmt.Errorf("进行中的任务不允许选择竞标任务的中标人")
}

func (status *UnderwayStatus) ApplyComplete(task *Task) error {
	task.TaskStatus = TASK_STATUS_UNACCEPTANCE
	task.CompleteTime = time.Now()
	task.CurrentStatus = &UnAcceptanceStatus{}
	return nil
}

func (status *UnderwayStatus) Off(task *Task) error {
	task.TaskStatus = TASK_STATUS_CLOSED
	task.CurrentStatus = &ClosedStatus{}
	return nil
}

func (status *UnderwayStatus) Acceptance(task *Task, participators []*EmployeeInfo, taskPercentage []*TaskPercentageItem, referenceResourceScore []*ReferenceResourceItem, solveReport string, solvePictureUrls []string) error {
	return fmt.Errorf("进行中的任务不允许验收")
}

type UnAcceptanceStatus struct{}

func (status *UnAcceptanceStatus) Update(task *Task, data map[string]interface{}) error {
	return fmt.Errorf("待验收的任务不允许编辑")
}

func (status *UnAcceptanceStatus) Release(task *Task) error {
	return fmt.Errorf("待验收的任务不允许重新发布")
}

func (status *UnAcceptanceStatus) Rob(task *Task, receiver *EmployeeInfo) error {
	return fmt.Errorf("待验收的任务不允许抢单")
}

func (status *UnAcceptanceStatus) Bib(task *Task, bidder *EmployeeInfo) error {
	return fmt.Errorf("待验收的任务不允许竞标")
}

func (status *UnAcceptanceStatus) ChooseSuccessfulBidder(task *Task, successfulBidder *EmployeeInfo) error {
	return fmt.Errorf("待验收的任务不允许选择竞标任务的中标人")
}

func (status *UnAcceptanceStatus) ApplyComplete(task *Task) error {
	return fmt.Errorf("待验收的任务不允许申请完成任务")
}

func (status *UnAcceptanceStatus) Off(task *Task) error {
	task.TaskStatus = TASK_STATUS_CLOSED
	task.CurrentStatus = &ClosedStatus{}
	return nil
}

func (status *UnAcceptanceStatus) Acceptance(task *Task, participators []*EmployeeInfo, taskPercentage []*TaskPercentageItem, referenceResourceScore []*ReferenceResourceItem, solveReport string, solvePictureUrls []string) error {
	task.Participators = participators
	task.TaskPercentage = taskPercentage
	task.SolveReport = solveReport
	task.SolvePictureUrls = solvePictureUrls
	if task.ReferenceResource != nil {
		for _, referenceResourceItem := range task.ReferenceResource.ReferenceResourceItems {
			for _, referenceResourceScoreItem := range referenceResourceScore {
				if referenceResourceItem.ReferenceResourceId == referenceResourceScoreItem.ReferenceResourceId {
					referenceResourceItem.IssueScore = referenceResourceScoreItem.IssueScore
					referenceResourceItem.SolveScore = referenceResourceScoreItem.SolveScore
				}
			}
		}
	}
	task.TaskStatus = TASK_STATUS_COMPLETED
	task.AcceptanceTime = time.Now()
	task.CurrentStatus = &CompletedStatus{}
	return nil
}

type CompletedStatus struct{}

func (status *CompletedStatus) Update(task *Task, data map[string]interface{}) error {
	return fmt.Errorf("已完成的任务不允许编辑")
}

func (status *CompletedStatus) Release(task *Task) error {
	return fmt.Errorf("已完成的任务不允许重新发布")
}

func (status *CompletedStatus) Rob(task *Task, receiver *EmployeeInfo) error {
	return fmt.Errorf("已完成的任务不允许抢单")
}

func (status *CompletedStatus) Bib(task *Task, bidder *EmployeeInfo) error {
	return fmt.Errorf("已完成的任务不允许竞标")
}

func (status *CompletedStatus) ChooseSuccessfulBidder(task *Task, successfulBidder *EmployeeInfo) error {
	return fmt.Errorf("已完成的任务不允许选择竞标任务的中标人")
}

func (status *CompletedStatus) ApplyComplete(task *Task) error {
	return fmt.Errorf("已完成的任务不允许申请完成任务")
}

func (status *CompletedStatus) Off(task *Task) error {
	task.TaskStatus = TASK_STATUS_CLOSED
	task.CurrentStatus = &ClosedStatus{}
	return nil
}

func (status *CompletedStatus) Acceptance(task *Task, participators []*EmployeeInfo, taskPercentage []*TaskPercentageItem, referenceResourceScore []*ReferenceResourceItem, solveReport string, solvePictureUrls []string) error {
	return fmt.Errorf("已完成的任务不允许验收")
}

type ClosedStatus struct{}

func (status *ClosedStatus) Update(task *Task, data map[string]interface{}) error {
	return fmt.Errorf("已关闭的任务不允许编辑")
}

func (status *ClosedStatus) Release(task *Task) error {
	return fmt.Errorf("已关闭的任务不允许重新发布")
}

func (status *ClosedStatus) Rob(task *Task, receiver *EmployeeInfo) error {
	return fmt.Errorf("已关闭的任务不允许抢单")
}

func (status *ClosedStatus) Bib(task *Task, bidder *EmployeeInfo) error {
	return fmt.Errorf("已关闭的任务不允许竞标")
}

func (status *ClosedStatus) ChooseSuccessfulBidder(task *Task, successfulBidder *EmployeeInfo) error {
	return fmt.Errorf("已关闭的任务不允许选择竞标任务的中标人")
}

func (status *ClosedStatus) ApplyComplete(task *Task) error {
	return fmt.Errorf("已关闭的任务不允许申请完成任务")
}

func (status *ClosedStatus) Off(task *Task) error {
	return fmt.Errorf("已关闭的任务不允许关闭")
}

func (status *ClosedStatus) Acceptance(task *Task, participators []*EmployeeInfo, taskPercentage []*TaskPercentageItem, referenceResourceScore []*ReferenceResourceItem, solveReport string, solvePictureUrls []string) error {
	return fmt.Errorf("已关闭的任务不允许验收")
}

type TaskRepository interface {
	Save(task *Task) (*Task, error)
	Remove(task *Task) (*Task, error)
	FindOne(queryOptions map[string]interface{}) (*Task, error)
	Find(queryOptions map[string]interface{}) (int64, []*Task, error)
}

func (task *Task) Identify() interface{} {
	if task.TaskId == 0 {
		return nil
	}
	return task.TaskId
}

func (task *Task) Update(data map[string]interface{}) error {
	return task.CurrentStatus.Update(task, data)
}

func (task *Task) Release() error {
	return task.CurrentStatus.Release(task)
}

func (task *Task) Rob(receiver *EmployeeInfo) error {
	return task.CurrentStatus.Rob(task, receiver)
}

func (task *Task) Bib(bidder *EmployeeInfo) error {
	return task.CurrentStatus.Bib(task, bidder)
}

func (task *Task) ChooseSuccessfulBidder(successfulBidder *EmployeeInfo) error {
	return task.CurrentStatus.ChooseSuccessfulBidder(task, successfulBidder)
}

func (task *Task) ApplyComplete() error {
	return task.CurrentStatus.ApplyComplete(task)
}

func (task *Task) Off() error {
	return task.CurrentStatus.Off(task)
}

func (task *Task) Acceptance(participators []*EmployeeInfo, taskPercentage []*TaskPercentageItem, referenceResourceScore []*ReferenceResourceItem, solveReport string, solvePictureUrls []string) error {
	return task.CurrentStatus.Acceptance(task, participators, taskPercentage, referenceResourceScore, solveReport, solvePictureUrls)
}