package controller

import (
	"bswas/utilities"
	"strconv"
)

func RejectedFormat(data []map[string]string) ([]interface{}, [][]interface{}, map[string]string) {
	row := []interface{}{
		"日期","审核号","客户名称","订单号","姓名",
		"手机","原单单号","退回单号","退回公司","到付费用",
		"是否入库","商品条码","商品名称",
	}
	column := map[string]int{
		"created_at" : 0,
		"checked_numbers" : 1,
		"owner_name" : 2,
		"order_number" : 3,
		"sender" : 4,
		"mobile_sender" : 5,
		"logistic_number" : 6,
		"logistic_number_return" : 7,
		"logistic_name" : 8,
		"fee_collected" : 9,
		"is_loaded" : 10,
		"item_barcode" : 11,
		"item_name" : 12,
	}
	dataLength := len(data)
	if  dataLength > 0 && data[0]["id_owner"] == "94"{
		row = append(row, "寄件方省","重量")
		column["common_01"] = 13
		column["common_02"] = 14
	}
	thisRowLength := len(row)
	column["item_amount"] = thisRowLength
	column["quality_label_name"] = thisRowLength+1
	column["item_batch_number"] = thisRowLength+2
	column["item_made_at"] = thisRowLength+3
	column["item_validity_at"] = thisRowLength+4
	column["item_remark"] = thisRowLength+5
	column["remark"] = thisRowLength+6
	column["operator_name"] = thisRowLength+7
	row = append(row ,"数量","是否正品", "批次号","生产日期","效期","备注","退单备注","录入人")
	list := make([][]interface{},len(data))
	mergeRow := make(map[string]string)
	var columnName string
	var startIndex int
	rowAmount := 0
	for k,v := range data{
		if columnName == v["id"] {
			rowAmount++
		}else{
			if rowAmount != 0 {
				mergeRow[strconv.Itoa(startIndex+2)] = strconv.Itoa(startIndex+2+rowAmount)
			}
			columnName = v["id"]
			startIndex = k
			rowAmount = 0
		}
		if rowAmount != 0 {
			mergeRow[strconv.Itoa(startIndex+2)] = strconv.Itoa(startIndex+2+rowAmount)
		}
		line := make([]interface{},len(row))
		for key,value := range column {
			if key == "created_at"{
				line[value] = utilities.DateFormat(v[key],"2006-01-02T15:04:05Z")
				continue
			}
			val := v[key]
			if key == "is_loaded" {
				switch v[key] {
				case "":
					val = "无需入库"
				case "0":
					val = "否"
				case "1":
					val = "是"
				case "2":
					val = "待推单"
				case "3":
					val = "上传异常"
				}
			}
			line[value] = val
		}
		list[k] = line
	}

	return row,list,mergeRow
}

func RejectedStatisticsFormat(data []map[string]string) ([]interface{}, [][]interface{}){
	row := []interface{}{
		"货主名","退件单数","审核单数","未审核单数","入库单数","未入库单数",
	}
	column := map[string]int{
		"owner_name" : 0,
		"bounce_amount" : 1,
		"check_amount" : 2,
		"uncheck_amount" : 3,
		"in_storage_count" : 4,
		"not_in_storage_count" : 5,
	}
	list := make([][]interface{},len(data))
	for k,v := range data{
		line := make([]interface{},len(row))
		for key,value := range column {
			if key == "uncheck_amount" {
				bounce,_ := strconv.Atoi(v["bounce_amount"])
				check,_ := strconv.Atoi(v["check_amount"])
				line[value] = strconv.Itoa(bounce - check)
			}else{
				line[value] = v[key]
			}
		}
		list[k] = line
	}
	return row, list
}
