package initdata

import (
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi/tencent_map"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/globals/api"
)

func TruncateTable(db *dao.DaoDB, tableName string) (err error) {
	_, err = dao.ExecuteSQL(db, "TRUNCATE TABLE "+tableName)
	return err
}

func insertPlace(ctx *jxcontext.Context, db *dao.DaoDB, parent *tencent_map.District, placeList []*tencent_map.District) (err error) {
	for _, v := range placeList {
		if v.Level <= tencent_map.DistrictLevelDistrict {
			place := &model.Place{
				Code:    int(utils.Str2Int64(v.Adcode)),
				Name:    v.Name,
				Level:   int8(v.Level),
				TelCode: v.CityCode,
				Enabled: 1,
			}
			if parent != nil {
				place.ParentCode = int(utils.Str2Int64(parent.Adcode))
			}
			dao.WrapAddIDCULEntity(place, ctx.GetUserName())
			if err = dao.CreateEntity(db, place); err != nil {
				return err
			}
			if err = insertPlace(ctx, db, v, v.Districts); err != nil {
				return err
			}
		}
	}
	return nil
}

func InitPlace(ctx *jxcontext.Context) (err error) {
	// 这个地方是更新全国的place表,因为两个不兼容且上传时间不会发送改变,暂时不做返回
	placeList, err2 := api.TencentMapAPI.GetDistricts(tencent_map.DistrictLevelDistrict, "", 0)
	if err = err2; err != nil {
		return err
	}
	placeList = placeList[0].Districts
	db := dao.GetDB()
	txDB, _ := dao.Begin(db)
	defer func() {
		if r := recover(); r != nil || err != nil {
			dao.Rollback(db, txDB)
			if r != nil {
				panic(r)
			}
		}
	}()
	if _, err = dao.ExecuteSQL(db, `
	DELETE t1
	FROM place t1
	WHERE code < 9000000;
	`); err != nil {
		return err
	}
	if err = insertPlace(ctx, db, nil, placeList); err != nil {
		return err
	}
	updateSqls := []string{
		`
		UPDATE place t1
		JOIN jde_city t2 ON t1.code = t2.col_tencentAddressCode
		SET t1.jd_code = t2.col_areaCode;
		`,
		`
		UPDATE place t1
		JOIN place t2 ON t1.parent_code = t2.code AND t2.jd_code != 0
		JOIN jde_district t3 ON t1.name = t3.col_areaName AND t2.jd_code = t3.col_cityCode
		SET t1.jd_code = t3.col_areaCode
		WHERE t1.level = 3;
		`,
		`
		UPDATE
		place t1
		JOIN ebde_places t2 ON t1.name = t2.col_city_name
		SET t1.ebai_code = t2.col_city_id
		WHERE t1.level = 1 OR t1.level = 2;
		`,
		`
		UPDATE
		place t1
		JOIN place t1p ON t1.parent_code = t1p.code
		JOIN ebde_places t2 ON t1.name = t2.col_city_name
		JOIN ebde_places t2p ON t2.col_parent_id = t2p.col_city_id AND t1p.ebai_code = t2p.col_city_id
		SET t1.ebai_code = t2.col_city_id
		WHERE t1.level = 3;
		`,
		`
		UPDATE
		place t1
		JOIN mtpsdeliveryprice t2 ON t1.code = t2.citycode
		SET t1.mtps_price = t2.price;
		`,
		`
		UPDATE
		place t1
		JOIN mtpsdeliveryprice t2 ON t1.name LIKE CONCAT(t2.cityname, '%')
		SET t1.mtps_price = t2.price
		WHERE t1.level = 2 AND t1.mtps_price = 0;
		`,
		`
		UPDATE place t1
		LEFT JOIN (
		SELECT DISTINCT city_code
		FROM store
		UNION DISTINCT
		SELECT DISTINCT place_code city_code
		FROM sku_name_place_bind
		) t2 ON t1.code = t2.city_code
		SET t1.enabled = 0
		WHERE t1.level = 2 AND t2.city_code IS NULL;
		`,
	}
	for _, v := range updateSqls {
		if _, err = dao.ExecuteSQL(db, v); err != nil {
			return err
		}
	}
	dao.Commit(db, txDB)
	return err
}

func InitVendorCategory(ctx *jxcontext.Context, vendorID int, isAsync bool) (hint string, err error) {
	if handler := partner.GetPurchasePlatformFromVendorID(vendorID); handler != nil {
		var cats []*model.SkuVendorCategory
		rootTask := tasksch.NewSeqTask(fmt.Sprintf("创建%s的平台分类", model.VendorChineseNames[vendorID]), ctx,
			func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
				switch step {
				case 0:
					cats, err = handler.GetVendorCategories(ctx)
					if err != nil {
						return nil, err
					}
				case 1:
					db := dao.GetDB()
					txDB, _ := dao.Begin(db)
					defer func() {
						if r := recover(); r != nil || err != nil {
							dao.Rollback(db, txDB)
							if r != nil {
								panic(r)
							}
						}
					}()
					sql := `
					DELETE
					FROM sku_vendor_category
					WHERE vendor_id = ?
					`
					if _, err = dao.ExecuteSQL(db, sql, vendorID); err != nil {
						return nil, err
					}
					for _, cat := range cats {
						dao.WrapAddIDCULEntity(cat, ctx.GetUserName())
					}
					if err = dao.CreateMultiEntities(db, cats); err != nil {
						return nil, err
					}
					dao.Commit(db, txDB)
				}
				return nil, err
			}, 2)
		tasksch.HandleTask(rootTask, nil, true).Run()
		if !isAsync {
			if _, err = rootTask.GetResult(0); err == nil {
				hint = utils.Int2Str(len(cats))
			}
		} else {
			hint = rootTask.ID
		}
	} else {
		err = fmt.Errorf("找不到平台:%d", vendorID)
	}
	return hint, err
}

func UploadImg4Vendors(ctx *jxcontext.Context, isAsync, isContinueWhenError bool) (hint string, err error) {
	// db := dao.GetDB()
	// rootTask := tasksch.NewSeqTask("合并SkuName图片至DataResource", ctx,
	// 	func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
	// 		switch step {
	// 		case 0: // 计算SkuName中缺失的hashCode（注意是DescImg不是Img）
	// 			var skuNameList []*model.SkuName
	// 			if err = dao.GetRows(db, &skuNameList, `
	// 			SELECT t1.*
	// 			FROM sku_name t1
	// 			WHERE t1.desc_img <> '' AND t1.img_hash_code = ''
	// 			`); err == nil && len(skuNameList) > 0 {
	// 				calcTask := tasksch.NewParallelTask("UploadImg4Vendors calc hashCode",
	// 					tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError).SetParallelCount(5), ctx,
	// 					func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
	// 						skuName := batchItemList[0].(*model.SkuName)
	// 						_, skuName.ImgHashCode, err = jxutils.DownloadFileByURL(skuName.DescImg)
	// 						if err == nil {
	// 							dao.UpdateEntity(db, skuName, "ImgHashCode")
	// 						}
	// 						return retVal, err
	// 					}, skuNameList)
	// 				tasksch.HandleTask(calcTask, task, false).Run()
	// 				_, err = calcTask.GetResult(0)
	// 			}
	// 		case 1: // 从SkuName添加缺失的图片至DataResource
	// 			_, err = dao.ExecuteSQL(db, `
	// 			INSERT INTO data_resource(created_at, updated_at, last_operator, hash_code,
	// 				resource_type, name, main_url, ebai_url, qiniu_url, use_type)
	// 			SELECT t1.created_at, t1.created_at, t1.last_operator, t1.img_hash_code,
	// 			CASE
	// 				WHEN INSTR(t1.desc_img, ".jpg") > 0 OR INSTR(t1.desc_img, ".jpeg") > 0 THEN
	// 					'image/jpeg'
	// 				WHEN INSTR(t1.desc_img, ".png") > 0 OR INSTR(t1.desc_img, ".peg") > 0 THEN
	// 					'image/png'
	// 				WHEN INSTR(t1.desc_img, ".gif") THEN
	// 					'image/gif'
	// 				ELSE
	// 					''
	// 			END resource_type,
	// 			CONCAT(t1.name, '_desc'), desc_img main_url, t1.desc_img_ebai ebai_url,
	// 			IF(INSTR(t1.desc_img, "image.jxc4.com") > 0, t1.desc_img, '') qiniu_url, 2
	// 			FROM sku_name t1
	// 			JOIN (
	// 				SELECT img_hash_code, MAX(id) id, COUNT(*) ct
	// 				FROM sku_name
	// 				WHERE img_hash_code <> '' AND desc_img <> ''
	// 				GROUP BY 1
	// 			) t3 ON t3.id = t1.id
	// 			LEFT JOIN data_resource t2 ON (t2.main_url <> '' AND t2.main_url = t1.desc_img)
	// 			WHERE t1.desc_img <> '' AND t1.img_hash_code <> '' AND t2.id IS NULL;
	// 			`)
	// 		case 2: // 统一SkuName中同hashCode，不同图片地址至同一地址
	// 			_, err = dao.ExecuteSQL(db, `
	// 			UPDATE sku_name t1
	// 			JOIN data_resource t2 ON t2.hash_code = t1.img_hash_code AND t2.main_url <> ''
	// 			SET t1.desc_img = t2.main_url
	// 			WHERE t1.img_hash_code <> '' AND t1.desc_img <> t2.main_url;
	// 			`)
	// 		case 3: // 上传DataResource中缺失的平台图片
	// 			dataResList, err2 := dao.GetNeedUploadDataResource(db)
	// 			if err = err2; err == nil && len(dataResList) > 0 {
	// 				uploadTask := tasksch.NewParallelTask("批量上传图片至平台",
	// 					tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError).SetParallelCount(2), ctx,
	// 					func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
	// 						dataRes := batchItemList[0].(*model.DataResource)
	// 						_, err = datares.UploadImage2Vendors(ctx, task, dataRes, nil, false)
	// 						return nil, err
	// 					}, dataResList)
	// 				tasksch.HandleTask(uploadTask, task, true).Run()
	// 				_, err = uploadTask.GetResult(0)
	// 			}
	// 		}
	// 		return result, err
	// 	}, 4)
	// tasksch.HandleTask(rootTask, nil, true).Run()
	// if !isAsync {
	// 	if _, err = rootTask.GetResult(0); err == nil {
	// 		hint = "1"
	// 	}
	// } else {
	// 	hint = rootTask.ID
	// }
	return hint, err
}

func getSkuNameKey(prefix, name, comment, specUnit, unit string, specQuality float32) string {
	return fmt.Sprintf("%s-%s-%f-%s-%s", prefix, name, specQuality, specUnit, unit)
}
