package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"reflect"
	"strconv"
	"strings"
	"time"

	"github.com/go-sql-driver/mysql"
)

var db *sql.DB

func getDate(time string) string {
	date, _, _ := strings.Cut(time, " ")
	return date
}
func getDB() *sql.DB {

	if db != nil {
		return db
	}
	cfg := mysql.Config{
		User:                 "application",
		Passwd:               "DzORXVx5sr6faWkeoIfe",
		Net:                  "tcp",
		Addr:                 "52.8.176.243:4000",
		DBName:               "jirainterval",
		AllowNativePasswords: true,
	}
	db, err := sql.Open("mysql", cfg.FormatDSN())
	if err != nil {
		panic(err)
	}
	// See "Important settings" section.
	db.SetConnMaxLifetime(time.Minute)
	db.SetMaxOpenConns(10)
	db.SetMaxIdleConns(100)

	return db
}

type StatusCategory struct {
	Id   int
	Name string
}

type Status struct {
	StatusCategory *StatusCategory
}

type Creator struct {
	Name string
}

type Priority struct {
	Name string
}

type Reporter struct {
	Name string
}

type Assignee struct {
	Name string
}

type Issuetype struct {
	Name string
}

type Components struct {
	Name string
}

type Token2 struct {
	Created           string
	Updated           string
	Resolutiondate    string
	Status            *Status
	Creator           *Creator
	Priority          *Priority //嵌套json
	Summary           string
	Reporter          *Reporter
	Assignee          *Assignee
	Description       string
	Issuetype         *Issuetype
	Components        []*Components
	Customfield_12816 string //1 account name
	Customfield_12819 string //1 account code
	Customfield_10615 string //2 cloud tenant id
	Customfield_10321 string //1 op root cause
	Customfield_10635 string //2 cloud root cause
	Customfield_12601 string //1 internal github issue id
	Customfield_10639 string //2 tcoc github issue id
}

type Token struct {
	Id     string
	Key    string
	Fields *Token2
}

type Actress struct {
	Total  int
	Issues []*Token
}

type Author struct {
	Name string
}

// action结构体
type Tokenact struct {
	Id      string
	Author  *Author
	Body    string
	Created string
	Updated string
}

type ActressAction struct {
	Total    int
	Comments []*Tokenact
}
type Items struct {
	Field string //只需要找=status
	From  string //变化前的状态
	To    string
}

type Histories struct {
	Created string
	Items   []*Items
}

//type Changelog struct {
//	Histories []*Histories
//}

type Token3 struct {
	Total     int
	Histories []*Histories
}

type ActressIssue struct {
	Changelog *Token3
}

// issue结构体
type Tokenissue struct {
	Created        string
	Updated        string
	Resolutiondate string
	Status         *Status
	Creator        *Creator
	Priority       *Priority //嵌套json
	Summary        string
	Reporter       *Reporter
	Assignee       *Assignee
	Description    string
	Issuetype      *Issuetype
	Components     []*Components
	//Customfield_12816 string//1
	//Customfield_12819 string//1
	//Customfield_10615 string//2
}

type Tokeni struct {
	Id     string
	Key    string
	Fields *Tokenissue
}
type Tokenob struct {
	Title string
}

type Tokentitle struct {
	Id     int
	Object *Tokenob
}

func jira(flag int, start string, end string, fullflag int) {

	//var issue_id string = "80737"
	//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + issue_id

	//url := "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//url := "https://jira.tidbcloud.com/rest/api/2/search/" //2
	var url string
	var projectid string
	if flag == 1 {
		url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		projectid = "11101"                                          //1

	} else if flag == 2 {
		url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10417"                                   //2
	} else if flag == 3 {
		url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10415"                                   //2
	} else {
		url = "https://jira.tidbcloud.com/rest/api/2/search/"
		projectid = "10422"
	}
	//var val = engineer_name
	//var projectid string = "11101" //1
	//var projectid string = "10417" //2
	var payload *strings.Reader
	if fullflag == 1 {
		payload = strings.NewReader("{\n  \"jql\": \"created >= '2022/10/10'and created <'2022/10/15' and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	} else {
		payload = strings.NewReader("{\n  \"jql\": \"created >=" + "'" + start + "'" + " and created <" + "'" + end + "'" + " and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	}
	//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

	req, _ := http.NewRequest("POST", url, payload)
	req.Header.Set("User-Agent", "curl")
	req.Header.Add("Content-Type", "application/json")
	//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
	//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
	//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
	if flag == 1 {
		req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
	} else {
		req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
	}

	client := http.Client{
		Timeout: 600 * time.Second,
	}
	res, _ := client.Do(req)

	defer res.Body.Close() //修改信息
	body, _ := ioutil.ReadAll(res.Body)
	var actress Actress
	//log.Println(res)
	//log.Println(string(body))
	err := json.Unmarshal(body, &actress)
	if err != nil {
		log.Println("error:", err)
		log.Println("response body:", string(body))
		return
	}
	log.Printf("jira req success\n")
	//token := actress.Issues[2]
	var tmpcreator string
	var tmpUpdated string
	var tmpResolutiondate string
	var tmpStatusId int
	var tmpCreatorName string
	var tmpCPriorityName string
	var tmpid string
	var tmpKey string
	var tmpReporter string
	var tmpAssignee string
	var tmpSummary string
	var tmpDescription string
	var tmpType string
	var tmpStatusName string

	//Updated        string
	//Resolutiondate string
	//Status         *Status
	//Creator        *Creator
	//Priority       *Priority //嵌套json
	dbb1 := getDB()
	for i := 0; i < actress.Total; i++ {
		tmpcreator = actress.Issues[i].Fields.Created
		tmpUpdated = actress.Issues[i].Fields.Updated
		tmpResolutiondate = actress.Issues[i].Fields.Resolutiondate
		tmpStatusId = actress.Issues[i].Fields.Status.StatusCategory.Id
		tmpCreatorName = actress.Issues[i].Fields.Creator.Name
		tmpCPriorityName = actress.Issues[i].Fields.Priority.Name
		tmpid = actress.Issues[i].Id

		tmpKey = actress.Issues[i].Key
		tmpReporter = actress.Issues[i].Fields.Reporter.Name
		if actress.Issues[i].Fields.Assignee == nil {
			tmpAssignee = ""
		} else {
			tmpAssignee = actress.Issues[i].Fields.Assignee.Name
		}
		tmpSummary = actress.Issues[i].Fields.Summary
		tmpDescription = actress.Issues[i].Fields.Description
		tmpType = actress.Issues[i].Fields.Issuetype.Name
		tmpStatusName = actress.Issues[i].Fields.Status.StatusCategory.Name

		rs := []rune(tmpDescription)

		var lenn int
		if len(rs) > 1000 {
			lenn = 1000
		} else {
			lenn = len(rs)
		}
		//if tmpid == "81838" || tmpid == "81615" {
		// for 10415 and 10422, high
		if (projectid != "10415" && projectid != "10422") || (projectid == "10415" && (tmpCPriorityName == "Major" || tmpCPriorityName == "Critical")) || (projectid == "10422" && (tmpCPriorityName == "Major" || tmpCPriorityName == "Critical")) {
			if tmpResolutiondate != "" {

				sqlStr := "insert into jira_issueview (id,creator,issue_type,priority,issue_status_id,created_time,updated_time,resolution_date,flag,project_id,reporter,assignee,summary,description,issue_key,issue_status_name,fdr,component,first_comment_time,first_sc_time,frt,ttr,account_code,frt_hour,tenant_id,rootcause,remotelink_title,manual_flag,sla,github_issue) values (?,?,? ,?,?,?,?,?,1,?,?,?,?,?,?,?,null,null,null,null,null,null,null,null,null,null,null,0,null,null);"
				res, err := dbb1.Exec(sqlStr, tmpid, tmpCreatorName, tmpType, tmpCPriorityName, tmpStatusId, tmpcreator, tmpUpdated, tmpResolutiondate, projectid, tmpReporter, tmpAssignee, tmpSummary, string(rs[:lenn]), tmpKey, tmpStatusName)
				if err != nil {
					log.Println("insert jira_issueview fail: ", err)
					return
				}
				id, err := res.LastInsertId()
				if err != nil {
					log.Printf("jira_issueview lastInsertId fail: %s", err)
				}
				log.Printf("id: %d jiraissue with resolution insert success!", id)
				//if err != nil {
				//	panic(err)
				//} //清除修改
				//defer rows0.Close()
			} else {
				//getDB().Query("insert into jira_issueview values (?,?,null ,?,?,?,?,null,1,?,?,?,?,?,? ); ", tmpid, tmpCreatorName, tmpCPriorityName, tmpStatusId, tmpcreator, tmpUpdated,projectid,tmpReporter,tmpAssignee,tmpSummary,tmpDescription,tmpKey)

				sqlStr := "insert into jira_issueview (id,creator,issue_type,priority,issue_status_id,created_time,updated_time,resolution_date,flag,project_id,reporter,assignee,summary,description,issue_key,issue_status_name,fdr,component,first_comment_time,first_sc_time,frt,ttr,account_code,frt_hour,tenant_id,rootcause,remotelink_title,manual_flag,sla,github_issue) values (?,?,?,?,?,?,?,null,1,?,?,?,?,?,?,?,null,null,null,null,null,null,null,null,null,null,null,0,null,null ); "
				res, err := dbb1.Exec(sqlStr, tmpid, tmpCreatorName, tmpType, tmpCPriorityName, tmpStatusId, tmpcreator, tmpUpdated, projectid, tmpReporter, tmpAssignee, tmpSummary, string(rs[:lenn]), tmpKey, tmpStatusName)
				if err != nil {
					log.Println("res fail: ", err)
					return
				}
				id, err := res.LastInsertId()
				if err != nil {
					log.Printf("jira_issueview lastInsertId fail: %s", err)
				}
				log.Printf("id: %d jiraissue without resolution insert success!", id)
				//if err != nil {
				//	panic(err)
				//} //清除修改
				//defer rows0.Close()
			}
		} else {

			log.Printf("jump the medium alert case !")

		}

	}

	//}tmpid

	//var lenn = actress.Issues.en

	//getDB()
	//rows0, err := getDB().Query("UPDATE page_oncall set is_change=0")
	//if err != nil {
	//	panic(err)
	//} //清除修改
	//defer rows0.Close()
}

func jiraaction(flag int) {
	dbb := getDB()
	var url string
	var projectid string
	//if flag == 1 {
	//	//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//	projectid = "11101" //1
	//
	//} else {
	//	//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
	//	projectid = "10417" //2
	//}

	if flag == 1 {
		//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		projectid = "11101" //1

	} else if flag == 2 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10417" //2
	} else if flag == 3 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10415" //2
	} else {
		projectid = "10422"
	}
	rows01, err := dbb.Query("SELECT id FROM jira_issueview where flag=1 and project_id=?", projectid)
	if err != nil {
		panic(err)
	}
	defer rows01.Close()
	for rows01.Next() {
		var issueid string

		err := rows01.Scan(&issueid)
		if err != nil {
			log.Fatal(err)
		}
		log.Println(issueid + ",")
		//var issue_id string = "80737"
		if flag == 1 {
			url = "https://internal.pingcap.net/jira/rest/api/2/issue/" + issueid + "/comment" //1
		} else {
			url = "https://jira.tidbcloud.com/rest/api/2/issue/" + issueid + "/comment" //2
		}
		//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + issueid + "/comment" //1
		//url := "https://jira.tidbcloud.com/rest/api/2/issue/" + issueid + "/comment" //2
		//var val = engineer_name
		//payload := strings.NewReader("{\n   \"maxResults\": \"500\"\n }")
		//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

		req, _ := http.NewRequest("GET", url, nil)
		req.Header.Set("User-Agent", "curl")

		req.Header.Add("Content-Type", "application/json")
		//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
		//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
		req.Header.Add("User-Agent", "PostmanRuntime/7.26.8") //1
		//req.Header.Add("Cookie", "JSESSIONID=C0EDF4B0189B1AA1F543B168CE7C205D; atlassian.xsrf.token=BB4Z-TISO-454F-I1QU_bdf4f5d06bd1419d9b905b54730e4ea1138bf90e_lin") //1
		//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
		if flag == 1 {
			req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
		} else {
			req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
		}
		client := http.Client{
			Timeout: 600 * time.Second,
		}
		res, _ := client.Do(req)

		defer res.Body.Close() //修改信息
		body, _ := ioutil.ReadAll(res.Body)
		var actressAction ActressAction
		//log.Println(res)
		//log.Println(string(body))
		err2 := json.Unmarshal(body, &actressAction)
		if err2 != nil {
			log.Println("error:", err2)
			log.Println("response body:", string(body))
			return
		}
		log.Printf("jira action req\n")
		//token := actress.Issues[2]
		//var tmpcreator string
		//var tmpUpdated string
		//var tmpResolutiondate string
		//var tmpStatusId int
		//var tmpCreatorName string
		//var tmpCPriorityName string
		var tmpid string
		var tmpAuthor string
		var tmpBody string
		var tmpcreated string
		var tmpUpdated string
		dbb2 := getDB()
		for i := 0; i < actressAction.Total; i++ {
			//db.Close()
			//getDB().Close()
			tmpid = actressAction.Comments[i].Id
			tmpAuthor = actressAction.Comments[i].Author.Name
			tmpBody = actressAction.Comments[i].Body
			rs := []rune(tmpBody)

			var lenn int
			if len(rs) > 1000 {
				lenn = 1000
			} else {
				lenn = len(rs)
			}
			tmpcreated = actressAction.Comments[i].Created
			tmpUpdated = actressAction.Comments[i].Updated
			if tmpUpdated != "" {
				sqlStr := "insert into jira_actionview values (?,?,?,?,?,?,null,?);" //注意修改project_id
				res, err := dbb2.Exec(sqlStr, tmpid, issueid, tmpAuthor, string(rs[:lenn]), tmpcreated, tmpUpdated, projectid)
				if err != nil {
					log.Println("res fail: ", err)
					return
				}
				id, err := res.LastInsertId()
				if err != nil {
					log.Printf("jira_actionview lastInsertId fail: %s", err)
				}
				log.Printf("id: %d actionview insert success!", id)

				//rows1a, err := dbb2.Query("insert into jira_actionview values (?,?,?,?,?,?,null); ", tmpid, issueid, tmpAuthor, tmpBody, tmpcreated, tmpUpdated)
				//if err != nil {
				//	panic(err)
				//} //清除修改
				//defer rows1a.Close()
			} else {
				rows1b, err := dbb2.Query("insert into jira_actionview values (?,?,?,?,?,null,null,? ); ", tmpid, issueid, tmpAuthor, tmpBody, tmpcreated, projectid)
				if err != nil {
					panic(err)
				} //清除修改
				defer rows1b.Close()
			}
			//time.Sleep(3 * time.Second)
		}

	}
}

func jiraday() {
	dbb := getDB()
	var project_id string
	//project_id = "11101"
	project_id = "10000"
	rows01, err := dbb.Query("SELECT  id,date_sub(date_format(date,'%Y-%m-%d'),interval 2 day) FROM jira_board_day where project_id=?", project_id)
	if err != nil {
		panic(err)
	}
	defer rows01.Close()
	for rows01.Next() {

		var board_id int
		var board_day string

		err := rows01.Scan(&board_id, &board_day)
		if err != nil {
			log.Fatal(err)
		}
		rows02, err2 := dbb.Query("SELECT COUNT(*) FROM jira_issueview WHERE project_id=? and date_format(created_time,'%Y-%m-%d') = ? ", project_id, board_day)
		if err2 != nil {
			panic(err2)
		}
		defer rows02.Close()
		var board_day2_all string
		for rows02.Next() {

			err3 := rows02.Scan(&board_day2_all)
			if err3 != nil {
				log.Fatal(err3)
			}

		}
		rows03, err4 := dbb.Query("SELECT COUNT(id) FROM (SELECT id,created_time,resolution_date,case when resolution_date is not null then timestampdiff(HOUR,created_time,resolution_date) end time_diff FROM jira_issueview WHERE project_id=? and date_format(created_time,'%Y-%m-%d') = ?  ) AS tmp_res_date WHERE time_diff<24", project_id, board_day)
		if err4 != nil {
			panic(err4)
		}
		defer rows03.Close()
		var board_day2_res string
		for rows03.Next() {

			err5 := rows03.Scan(&board_day2_res)
			if err5 != nil {
				log.Fatal(err5)
			}

		}
		sqlStr := "UPDATE jira_board_day SET all_issue=?,res_issue=? WHERE id=? "
		res, err := dbb.Exec(sqlStr, board_day2_all, board_day2_res, board_id)
		if err != nil {
			log.Println("jira_board_day res fail: ", err)
			return
		}
		id, err := res.LastInsertId()
		if err != nil {
			log.Printf("lastInsertId fail: %s", err)
		}
		log.Printf("id: %d boardday update success!", id)

	}
}

func test() {
	dbb1 := getDB()
	for i := 0; i < 92; i++ {
		sqlStr := "INSERT INTO jira_board_day (date,all_issue,res_issue,project_id,flag) SELECT DATE_ADD(date,INTERVAL 1 DAY) ,null,null,11101,1 FROM jira_board_day WHERE project_id=11101 and flag=1 ORDER BY date DESC LIMIT 1"
		res, err := dbb1.Exec(sqlStr)
		if err != nil {
			log.Println("res fail: ", err)
			return
		}
		id, err := res.LastInsertId()
		if err != nil {
			log.Printf("lastInsertId fail: %s", err)
		}
		log.Printf("id: %d jiraboardday insert success!", id)
	}
}

func jiraUpdateComponent(flag int, start string, end string, fullflag int) {

	//var issue_id string = "80737"
	//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + issue_id

	//url := "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//url := "https://jira.tidbcloud.com/rest/api/2/search/" //2

	//var val = engineer_name
	//var projectid string = "11101" //1
	//var projectid string = "10417" //2
	fmt.Println("update component, ", flag)
	var url string
	var projectid string
	if flag == 1 {
		url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		projectid = "11101"                                          //1

	} else if flag == 2 {
		url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10417"                                   //2
	} else if flag == 3 {
		url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10415"                                   //2
	} else {
		url = "https://jira.tidbcloud.com/rest/api/2/search/"
		projectid = "10422"
	}

	var payload *strings.Reader
	if fullflag == 1 {
		payload = strings.NewReader("{\n  \"jql\": \"created >= '2022/10/01'and created <'2022/10/15' and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	} else {
		payload = strings.NewReader("{\n  \"jql\": \"created >=" + "'" + start + "'" + " and created <" + "'" + end + "'" + " and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	}
	//payload := strings.NewReader("{\n  \"jql\": \"created >=" + start + " and created <" + end + " and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")
	//payload := strings.NewReader("{\n  \"jql\": \"created >= '2022/07/04'and created <'2022/07/26' and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

	req, _ := http.NewRequest("POST", url, payload)
	req.Header.Set("User-Agent", "curl")

	req.Header.Add("Content-Type", "application/json")
	//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
	//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
	//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
	if flag == 1 {
		req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
	} else {
		req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
	}
	client := http.Client{
		Timeout: 600 * time.Second,
	}
	res, _ := client.Do(req)

	defer res.Body.Close() //修改信息
	body, _ := ioutil.ReadAll(res.Body)
	var actress Actress
	//log.Println(res)
	//log.Println(string(body))
	err := json.Unmarshal(body, &actress)
	if err != nil {
		log.Println("error:", err)
		log.Println("response body:", string(body))
		return
	}
	log.Printf("update component req success：\n")
	//token := actress.Issues[2]
	//var tmpcreator string
	//var tmpUpdated string
	//var tmpResolutiondate string
	//var tmpStatusId int
	//var tmpCreatorName string
	//var tmpCPriorityName string
	var tmpid string
	//var tmpKey string
	//var tmpReporter string
	//var tmpAssignee string
	//var tmpSummary string
	//var tmpDescription string
	//var tmpType string
	//var tmpStatusName string
	var tmpComponents string

	//var tmpid string
	//Updated        string
	//Resolutiondate string
	//Status         *Status
	//Creator        *Creator
	//Priority       *Priority //嵌套json
	dbb1 := getDB()
	for i := 0; i < actress.Total; i++ {
		//tmpcreator = actress.Issues[i].Fields.Created
		//tmpUpdated = actress.Issues[i].Fields.Updated
		//tmpResolutiondate = actress.Issues[i].Fields.Resolutiondate
		//tmpStatusId = actress.Issues[i].Fields.Status.StatusCategory.Id
		//tmpCreatorName = actress.Issues[i].Fields.Creator.Name
		//tmpCPriorityName = actress.Issues[i].Fields.Priority.Name
		tmpid = actress.Issues[i].Id
		if len(actress.Issues[i].Fields.Components) == 0 {
			tmpComponents = ""
		} else {
			if actress.Issues[i].Fields.Components[0].Name == "TiDB" && len(actress.Issues[i].Fields.Components) > 1 {
				tmpComponents = actress.Issues[i].Fields.Components[1].Name
			} else {
				tmpComponents = actress.Issues[i].Fields.Components[0].Name
			}
		}
		//tmpKey = actress.Issues[i].Key
		//tmpReporter = actress.Issues[i].Fields.Reporter.Name
		//if actress.Issues[i].Fields.Assignee == nil {
		//	tmpAssignee = ""
		//} else {
		//	tmpAssignee = actress.Issues[i].Fields.Assignee.Name
		//}
		//tmpSummary = actress.Issues[i].Fields.Summary
		//tmpDescription = actress.Issues[i].Fields.Description
		//tmpType = actress.Issues[i].Fields.Issuetype.Name
		//tmpStatusName = actress.Issues[i].Fields.Status.StatusCategory.Name
		sqlStr := "UPDATE jira_issueview set component=? WHERE id=? and project_id=? and manual_flag=0"

		_, err := dbb1.Exec(sqlStr, tmpComponents, tmpid, projectid)
		if err != nil {
			log.Println("res fail: ", err)
			return
		}

		log.Printf("id: %s component update success!", tmpid)

	}

}

func jiraUpdateCode(flag int, start string, end string, fullflag int) {
	//cloud的字段为Customfield_10615，interval的字段有两个，需要string相加处理，Customfield_12816+Customfield_12819
	//调用不同的地方，需要对struct结构体字段进行调整

	//var issue_id string = "80737"
	//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + issue_id

	//url := "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//url := "https://jira.tidbcloud.com/rest/api/2/search/" //2

	//var val = engineer_name
	//var projectid string = "11101" //1
	//var projectid string = "10417" //2
	var url string
	var projectid string
	if flag == 1 {
		url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		projectid = "11101"                                          //1

	} else if flag == 2 {
		url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10417"                                   //2
	} else if flag == 3 {
		url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10415"                                   //2
	} else {
		url = "https://jira.tidbcloud.com/rest/api/2/search/"
		projectid = "10422"
	}
	//if flag == 1 {
	//	url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//	projectid = "11101"                                          //1
	//
	//} else {
	//	url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
	//	projectid = "10417"                                   //2
	//}
	var payload *strings.Reader
	if fullflag == 1 {
		payload = strings.NewReader("{\n  \"jql\": \"created >= '2022/10/17'and created <'2022/10/19' and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	} else {
		payload = strings.NewReader("{\n  \"jql\": \"created >=" + "'" + start + "'" + " and created <" + "'" + end + "'" + " and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	}
	//payload := strings.NewReader("{\n  \"jql\": \"created >=" + start + " and created <" + end + " and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")
	//payload := strings.NewReader("{\n  \"jql\": \"created >= '2022/07/04'and created <'2022/07/26' and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")
	//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

	req, _ := http.NewRequest("POST", url, payload)
	req.Header.Set("User-Agent", "curl")

	req.Header.Add("Content-Type", "application/json")
	//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
	//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
	//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
	if flag == 1 {
		req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
	} else {
		req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
	}
	client := http.Client{
		Timeout: 600 * time.Second,
	}
	res, _ := client.Do(req)

	defer res.Body.Close() //修改信息
	body, _ := ioutil.ReadAll(res.Body)
	var actress Actress
	//log.Println(res)
	//log.Println(string(body))
	err := json.Unmarshal(body, &actress)
	if err != nil {
		log.Println("error:", err)
		log.Println("response body:", string(body))
		return
	}
	log.Printf("jira code req：\n")
	//token := actress.Issues[2]
	//var tmpcreator string
	//var tmpUpdated string
	//var tmpResolutiondate string
	//var tmpStatusId int
	//var tmpCreatorName string
	//var tmpCPriorityName string
	var tmpid string

	var tmpTenantid string //cloud
	var tmpGithubid string // used for save if there's githbub issue
	var tmpTenant2 string

	//var tmpTenantid1 string //interval
	//var tmpTenantid2 string //interval

	dbb1 := getDB()
	for i := 0; i < actress.Total; i++ {
		//tmpcreator = actress.Issues[i].Fields.Created
		//tmpUpdated = actress.Issues[i].Fields.Updated
		//tmpResolutiondate = actress.Issues[i].Fields.Resolutiondate
		//tmpStatusId = actress.Issues[i].Fields.Status.StatusCategory.Id
		//tmpCreatorName = actress.Issues[i].Fields.Creator.Name
		//tmpCPriorityName = actress.Issues[i].Fields.Priority.Name
		tmpid = actress.Issues[i].Id
		//if len(actress.Issues[i].Fields.Components) == 0 {
		//	tmpComponents = ""
		//} else {
		//	tmpComponents = actress.Issues[i].Fields.Components[0].Name
		//}
		if flag == 1 {
			tmpTenantid = actress.Issues[i].Fields.Customfield_12816 + actress.Issues[i].Fields.Customfield_12819
			tmpGithubid = actress.Issues[i].Fields.Customfield_12601
			tmpTenant2 = actress.Issues[i].Fields.Customfield_12819

		} else {
			tmpTenantid = actress.Issues[i].Fields.Customfield_10615 //cloud
			tmpGithubid = actress.Issues[i].Fields.Customfield_10639
			tmpTenant2 = actress.Issues[i].Fields.Customfield_10615
			//tmpGithubid = nil
		}

		sqlStr2 := "UPDATE jira_issueview set tenant_id=? WHERE id=? and project_id=? and manual_flag=0"

		_, err2 := dbb1.Exec(sqlStr2, tmpTenant2, tmpid, projectid)
		if err2 != nil {
			log.Println("res fail: ", err)
			return
		}

		if tmpGithubid != "" {
			sqlStr := "UPDATE jira_issueview set account_code=?,github_issue=? WHERE id=? and project_id=? and manual_flag=0"
			_, err := dbb1.Exec(sqlStr, tmpTenantid, tmpGithubid, tmpid, projectid)
			if err != nil {
				log.Println("res fail: ", err)
				return
			}
		} else {
			sqlStr := "UPDATE jira_issueview set account_code=?,github_issue=null WHERE id=? and project_id=? and manual_flag=0"
			_, err := dbb1.Exec(sqlStr, tmpTenantid, tmpid, projectid)
			if err != nil {
				log.Println("res fail: ", err)
				return
			}

		}

	}

	log.Printf(" %s accountcode update success!", tmpid)

}
func jiraUpdaterootcause(flag int, start string, end string, fullflag int) {
	//cloud的字段为Customfield_10615，interval的字段有两个，需要string相加处理，Customfield_12816+Customfield_12819
	//调用不同的地方，需要对struct结构体字段进行调整

	//var issue_id string = "80737"
	//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + issue_id

	//url := "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//url := "https://jira.tidbcloud.com/rest/api/2/search/" //2

	//var val = engineer_name
	//var projectid string = "11101" //1
	//var projectid string = "10417" //2
	var url string
	var projectid string
	if flag == 1 {
		url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		projectid = "11101"                                          //1

	} else if flag == 2 {
		url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10417"                                   //2
	} else if flag == 3 {
		url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10415"                                   //2
	} else {
		url = "https://jira.tidbcloud.com/rest/api/2/search/"
		projectid = "10422"
	}
	//if flag == 1 {
	//	url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//	projectid = "11101"                                          //1
	//
	//} else {
	//	url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
	//	projectid = "10417"                                   //2
	//}
	var payload *strings.Reader
	if fullflag == 1 {
		payload = strings.NewReader("{\n  \"jql\": \"created >= '2022/10/10'and created <'2022/10/15' and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	} else {
		payload = strings.NewReader("{\n  \"jql\": \"created >=" + "'" + start + "'" + " and created <" + "'" + end + "'" + " and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")

	}
	//payload := strings.NewReader("{\n  \"jql\": \"created >=" + start + " and created <" + end + " and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")
	//payload := strings.NewReader("{\n  \"jql\": \"created >= '2022/07/04'and created <'2022/07/26' and project=" + projectid + "\",\n \"maxResults\": \"5000\"\n }")
	//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

	req, _ := http.NewRequest("POST", url, payload)
	req.Header.Set("User-Agent", "curl")

	req.Header.Add("Content-Type", "application/json")
	//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
	//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
	//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
	if flag == 1 {
		req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
	} else {
		req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
	}
	client := http.Client{
		Timeout: 600 * time.Second,
	}
	res, _ := client.Do(req)

	defer res.Body.Close() //修改信息
	body, _ := ioutil.ReadAll(res.Body)
	var actress Actress
	//log.Println(res)
	//log.Println(string(body))
	err := json.Unmarshal(body, &actress)
	if err != nil {
		log.Println("error:", err)
		log.Println("response body:", string(body))
		return
	}
	log.Printf("req rootcause：\n")
	//token := actress.Issues[2]
	//var tmpcreator string
	//var tmpUpdated string
	//var tmpResolutiondate string
	//var tmpStatusId int
	//var tmpCreatorName string
	//var tmpCPriorityName string
	var tmpid string

	var tempRootcause string //cloud
	//var tmpTenantid1 string //interval
	//var tmpTenantid2 string //interval

	dbb1 := getDB()
	for i := 0; i < actress.Total; i++ {
		//tmpcreator = actress.Issues[i].Fields.Created
		//tmpUpdated = actress.Issues[i].Fields.Updated
		//tmpResolutiondate = actress.Issues[i].Fields.Resolutiondate
		//tmpStatusId = actress.Issues[i].Fields.Status.StatusCategory.Id
		//tmpCreatorName = actress.Issues[i].Fields.Creator.Name
		//tmpCPriorityName = actress.Issues[i].Fields.Priority.Name
		tmpid = actress.Issues[i].Id
		//if len(actress.Issues[i].Fields.Components) == 0 {
		//	tmpComponents = ""
		//} else {
		//	tmpComponents = actress.Issues[i].Fields.Components[0].Name
		//}
		if flag == 1 {
			tempRootcause = actress.Issues[i].Fields.Customfield_10321
		} else {
			tempRootcause = actress.Issues[i].Fields.Customfield_10635
		}

		//tmpTenantid1 = actress.Issues[i].Fields.Customfield_12816 //interval
		//tmpTenantid2 = actress.Issues[i].Fields.Customfield_12819 //interval
		//var resid string = tmpTenantid1 + tmpTenantid2            //interval
		//tmpKey = actress.Issues[i].Key
		//tmpReporter = actress.Issues[i].Fields.Reporter.Name
		//if actress.Issues[i].Fields.Assignee == nil {
		//	tmpAssignee = ""
		//} else {
		//	tmpAssignee = actress.Issues[i].Fields.Assignee.Name
		//}
		//tmpSummary = actress.Issues[i].Fields.Summary
		//tmpDescription = actress.Issues[i].Fields.Description
		//tmpType = actress.Issues[i].Fields.Issuetype.Name
		//tmpStatusName = actress.Issues[i].Fields.Status.StatusCategory.Name
		rs := []rune(tempRootcause)

		var lenn int
		if len(rs) > 1000 {
			lenn = 1000
		} else {
			lenn = len(rs)
		}
		if lenn > 0 {
			sqlStr := "UPDATE jira_issueview set rootcause=? WHERE id=? and project_id=? and manual_flag=0"

			_, err := dbb1.Exec(sqlStr, string(rs[:lenn]), tmpid, projectid)
			if err != nil {
				log.Println("res fail: ", err)
				return
			}

			log.Printf("id: %s rootcause update success!", tmpid)
		}

	}

}

func jiraChangelog(flag int) { //先用SQL语句中的将issue的数值插入到change表，然后再执行这个函数
	dbb := getDB()
	//var url string
	var projectid string
	if flag == 1 {
		//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		projectid = "11101" //1

	} else if flag == 2 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10417" //2
	} else if flag == 3 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		projectid = "10415" //2
	} else {
		projectid = "10422"
	}
	//if flag == 1 {
	//	//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//	projectid = "11101" //1
	//
	//} else {
	//	//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
	//	projectid = "10417" //2
	//}

	sqlStr := "INSERT into jira_issue_change (id ,project_id,change_time,flag) SELECT id,project_id,null,1 from jira_issueview WHERE project_id=? and flag=1 and manual_flag=0 on duplicate key update project_id=? "
	res2, err2 := dbb.Exec(sqlStr, projectid, projectid)
	if err2 != nil {
		log.Println("jira_issue_change res fail: ", err2)
		return
	}
	id, err2 := res2.LastInsertId()
	if err2 != nil {
		log.Printf("jira_issue_change lastInsertId fail: %s", err2)
	}
	log.Printf("id: %d jira_issue_change insert success!", id)
	//var project_id string
	//project_id = "11101"
	//project_id = "10000"

	rows01, err := dbb.Query("SELECT  id,project_id FROM jira_issue_change where project_id=? and flag=1 ", projectid)
	if err != nil {
		panic(err)
	}
	defer rows01.Close()
	for rows01.Next() {
		var log_id int
		var log_project_id string

		err := rows01.Scan(&log_id, &log_project_id)
		if err != nil {
			log.Fatal(err)
		}
		var log_idd string = strconv.Itoa(log_id)
		var url string

		if flag == 1 {
			url = "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(log_idd) + "?expand=changelog" //1
		} else {
			url = "https://jira.tidbcloud.com/rest/api/2/issue/" + string(log_idd) + "?expand=changelog" //2
		}

		//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(log_idd) + "?expand=changelog" //1
		//url := "https://jira.tidbcloud.com/rest/api/2/issue/" + string(log_idd) + "?expand=changelog" //2

		//var val = engineer_name
		//var projectid string = "11101" //1
		//var projectid string = "10000" //2
		//payload := strings.NewReader("{\n  \"jql\": \"created >= '2022/01/01'and created <'2022/02/06' and project=" + projectid + "\",\n \"maxResults\": \"500\"\n }")
		//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

		req, _ := http.NewRequest("GET", url, nil)
		req.Header.Set("User-Agent", "curl")

		req.Header.Add("Content-Type", "application/json")
		//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
		//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		if flag == 1 {
			req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1
		} else {
			req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2
		}
		client := http.Client{
			Timeout: 600 * time.Second,
		}
		res, _ := client.Do(req)

		defer res.Body.Close() //修改信息
		body, _ := ioutil.ReadAll(res.Body)
		var actressissue ActressIssue
		//log.Println(res)
		//log.Println(string(body))
		err01 := json.Unmarshal(body, &actressissue)
		if err01 != nil {
			log.Println("error:", err01)
			log.Println("response body:", string(body))
			return
		}
		log.Println("jira changelog:", log_id)
		//log.Printf("长度：%s\n", actressissue.Total)
		//token := actress.Issues[2]
		//var fromstatus string = "10000" //intercal
		//var tostatus string = "10114" //intercal
		//var fromstatus string = "10308" //cloud
		//var tostatus string = "10309" //cloud
		var tostatus string
		var needfield = "status"
		if flag == 1 {
			tostatus = "10114" //intercal
		} else if flag == 2 {
			tostatus = "10309" //tcoc
		} else {
			tostatus = "10307" //tca
		}
		//var tmpfromstatus string
		var tmptostatus string
		var tmpfield string
		var tmpStatustime string

		//Updated        string
		//Resolutiondate string
		//Status         *Status
		//Creator        *Creator
		//Priority       *Priority //嵌套json
		//dbb1 := getDB()
		//log.Printf("长度：%s\n", actressissue.Changelog.Total)
		tmpStatustimeres := ""

		var lenHistories = len(actressissue.Changelog.Histories)
		for i := 0; i < lenHistories; i++ {
			var lenItems = len(actressissue.Changelog.Histories[i].Items)
			tmptostatus = ""

			for j := 0; j < lenItems; j++ {
				tmpfield = actressissue.Changelog.Histories[i].Items[j].Field
				//tmpfromstatus = actressissue.Changelog.Histories[i].Items[j].From
				tmptostatus = actressissue.Changelog.Histories[i].Items[j].To
				tmpStatustime = actressissue.Changelog.Histories[i].Created
				//tmpStatustime=actressissue.Fields.Changelog.Histories[0].Created
				rs := []rune(tmpStatustime)

				log.Printf("memo: %s", string(rs[0:10])+" "+string(rs[11:19]))
				tmpStatustimepar, _ := time.Parse("2006-01-02 15:04:05", string(rs[0:10])+" "+string(rs[11:19]))
				tmpStatustimerespar, _ := time.Parse("2006-01-02 15:04:05", tmpStatustimeres)

				beforeOrAfter := tmpStatustimerespar.After(tmpStatustimepar)
				if tmptostatus == tostatus && tmpfield == needfield {
					if tmpStatustimeres == "" || beforeOrAfter {
						tmpStatustimeres = tmpStatustime
					}

					//tmpStatustime=""
					//满足条件，写入数据库

				} else {
					continue
				}
			}

		}
		if tmpStatustimeres != "" {
			sqlStr := "update jira_issue_change set change_time=? where id=? and project_id =?"
			_, err := dbb.Exec(sqlStr, tmpStatustimeres, log_idd, projectid)
			if err != nil {
				log.Println("update changelog res fail: ", err)
				return
			}

			log.Printf("changelog id: %s update success!", log_idd)
		}

		//for i := 0; i < actress.Total; i++ {
		//	tmpcreator = actress.Issues[i].Fields.Created
		//
		//}
	}
}

func jiraupdatestatus(flag int) {
	dbb := getDB()
	//var project_id string
	var url string
	var project_id string
	if flag == 1 {
		//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		project_id = "11101" //1

	} else if flag == 2 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		project_id = "10417" //2
	} else if flag == 3 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		project_id = "10415" //2
	} else {
		project_id = "10422"
	}
	//if flag == 1 {
	//	//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//	project_id = "11101" //1
	//
	//} else {
	//	//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
	//	project_id = "10417" //2
	//}
	//project_id = "11101" //1
	//project_id = "10417" //2
	rows01, err := dbb.Query("  SELECT id FROM jira_issueview WHERE issue_status_id<>3 and project_id=? ", project_id)
	if err != nil {
		panic(err)
	}
	defer rows01.Close()
	for rows01.Next() {
		var issue_id int

		err := rows01.Scan(&issue_id)
		if err != nil {
			log.Fatal(err)
		}

		var issue_idd string = strconv.Itoa(issue_id)
		//issue_idd = "84820"
		//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(issue_idd) //1
		//url := "https://jira.tidbcloud.com/rest/api/2/issue/" + string(issue_idd) //2
		if flag == 1 {
			url = "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(issue_idd) //1
		} else {
			url = "https://jira.tidbcloud.com/rest/api/2/issue/" + string(issue_idd) //2
		}
		//var val = engineer_name
		//var projectid string = "11101" //1
		//var projectid string = "10000" //2
		//payload := strings.NewReader("{\n  \"jql\": \"created >= '2022/01/01'and created <'2022/02/06' and project=" + projectid + "\",\n \"maxResults\": \"500\"\n }")
		//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

		req, _ := http.NewRequest("GET", url, nil)
		req.Header.Set("User-Agent", "curl")

		req.Header.Add("Content-Type", "application/json")
		//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
		//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		if flag == 1 {
			req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		} else {
			req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		}
		client := http.Client{
			Timeout: 600 * time.Second,
		}
		res, _ := client.Do(req)

		defer res.Body.Close() //修改信息
		body, _ := ioutil.ReadAll(res.Body)
		var dtsissue Tokeni
		//log.Println(res)
		//log.Println(string(body))
		err01 := json.Unmarshal(body, &dtsissue)
		if err01 != nil {
			log.Println("error:", err01)
			log.Println("response body:", body)
			return
		}
		log.Println("status id <>3  issue get:", issue_idd)
		//log.Printf("长度：%s\n", actressissue.Total)
		//token := actress.Issues[2]
		//var fromstatus string = "10000" //intercal
		//var tostatus string = "10114"   //intercal
		//var fromstatus string = "10308" //cloud

		//var tmpfromstatus string
		//var tmptostatus string
		//var tmpfield string
		//var tmpStatustime string

		//Updated        string

		var StatusID int
		var StatusName string

		StatusID = dtsissue.Fields.Status.StatusCategory.Id
		StatusName = dtsissue.Fields.Status.StatusCategory.Name
		//Status         *Status
		//Creator        *Creator
		//Priority       *Priority //嵌套json
		//dbb1 := getDB()
		//log.Printf("长度：%s\n", actressissue.Changelog.Total)
		//tmpStatustimeres := ""
		//
		//var lenHistories = len(actressissue.Changelog.Histories)
		//for i := 0; i < lenHistories; i++ {
		//	var lenItems = len(actressissue.Changelog.Histories[i].Items)
		//	tmptostatus = ""
		//
		//	for j := 0; j < lenItems; j++ {
		//		tmpfield = actressissue.Changelog.Histories[i].Items[j].Field
		//		//tmpfromstatus = actressissue.Changelog.Histories[i].Items[j].From
		//		tmptostatus = actressissue.Changelog.Histories[i].Items[j].To
		//		tmpStatustime = actressissue.Changelog.Histories[i].Created
		//		//tmpStatustime=actressissue.Fields.Changelog.Histories[0].Created
		//		rs := []rune(tmpStatustime)
		//
		//		log.Printf("lastInsertId fail: %s", string(rs[0:10])+" "+string(rs[11:19]))
		//		tmpStatustimepar, _ := time.Parse("2006-01-02 15:04:05", string(rs[0:10])+" "+string(rs[11:19]))
		//		tmpStatustimerespar, _ := time.Parse("2006-01-02 15:04:05", tmpStatustimeres)
		//
		//		beforeOrAfter := tmpStatustimerespar.After(tmpStatustimepar)
		//		if tmptostatus == tostatus && tmpfield == needfield {
		//			if tmpStatustimeres == "" || beforeOrAfter {
		//				tmpStatustimeres = tmpStatustime
		//			}
		//
		//			//tmpStatustime=""
		//			//满足条件，写入数据库
		//
		//		} else {
		//			continue
		//		}
		//	}
		//
		//}

		if StatusName != "" {
			sqlStr := "update jira_issueview set issue_status_id=? ,issue_status_name=? where id=? and project_id=? and manual_flag=0"
			_, err2 := dbb.Exec(sqlStr, StatusID, StatusName, issue_id, project_id)
			if err2 != nil {
				log.Println("res fail: ", err2)
				return
			}

			log.Printf("id: %d statusid update success!", issue_id)
		}

		//for i := 0; i < actress.Total; i++ {
		//	tmpcreator = actress.Issues[i].Fields.Created
		//
		//}
	}
}

func jiraupdaResolutiondate(flag int) {
	dbb := getDB()
	//var project_id string
	var url string
	var project_id string
	if flag == 1 {
		//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		project_id = "11101" //1

	} else if flag == 2 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		project_id = "10417" //2
	} else if flag == 3 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		project_id = "10415" //2
	} else {
		project_id = "10422"
	}
	//if flag == 1 {
	//	//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//	project_id = "11101" //1
	//
	//} else {
	//	//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
	//	project_id = "10417" //2
	//}
	//project_id = "11101" //1
	//project_id = "10417" //2
	rows01, err := dbb.Query("  SELECT id FROM jira_issueview WHERE ttr is NULL and project_id=? ", project_id)
	if err != nil {
		panic(err)
	}
	defer rows01.Close()
	for rows01.Next() {
		var issue_id int

		err := rows01.Scan(&issue_id)
		if err != nil {
			log.Fatal(err)
		}
		var issue_idd string = strconv.Itoa(issue_id)
		//issue_idd = "39207"
		//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(issue_idd) //1
		//url := "https://jira.tidbcloud.com/rest/api/2/issue/" + string(issue_idd) //2
		if flag == 1 {
			url = "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(issue_idd) //1
		} else {
			url = "https://jira.tidbcloud.com/rest/api/2/issue/" + string(issue_idd) //2
		}
		//var val = engineer_name
		//var projectid string = "11101" //1
		//var projectid string = "10000" //2
		//payload := strings.NewReader("{\n  \"jql\": \"created >= '2022/01/01'and created <'2022/02/06' and project=" + projectid + "\",\n \"maxResults\": \"500\"\n }")
		//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

		req, _ := http.NewRequest("GET", url, nil)
		req.Header.Set("User-Agent", "curl")

		req.Header.Add("Content-Type", "application/json")
		//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
		//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		if flag == 1 {
			req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		} else {
			req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		}
		client := http.Client{
			Timeout: 600 * time.Second,
		}
		res, _ := client.Do(req)

		defer res.Body.Close() //修改信息
		body, _ := ioutil.ReadAll(res.Body)
		var dtsissue Tokeni
		//log.Println(res)
		//log.Println(string(body))
		err01 := json.Unmarshal(body, &dtsissue)
		if err01 != nil {
			log.Println("error:", err01)
			log.Println("response body:", body)
			return
		}
		log.Println("ttr null issue get:", issue_idd)
		//log.Printf("长度：%s\n", actressissue.Total)
		//token := actress.Issues[2]
		//var fromstatus string = "10000" //intercal
		//var tostatus string = "10114"   //intercal
		//var fromstatus string = "10308" //cloud

		//var tmpfromstatus string
		//var tmptostatus string
		//var tmpfield string
		//var tmpStatustime string

		//Updated        string
		var Resolutiondate string

		Resolutiondate = dtsissue.Fields.Resolutiondate

		//Status         *Status
		//Creator        *Creator
		//Priority       *Priority //嵌套json
		//dbb1 := getDB()
		//log.Printf("长度：%s\n", actressissue.Changelog.Total)
		//tmpStatustimeres := ""
		//
		//var lenHistories = len(actressissue.Changelog.Histories)
		//for i := 0; i < lenHistories; i++ {
		//	var lenItems = len(actressissue.Changelog.Histories[i].Items)
		//	tmptostatus = ""
		//
		//	for j := 0; j < lenItems; j++ {
		//		tmpfield = actressissue.Changelog.Histories[i].Items[j].Field
		//		//tmpfromstatus = actressissue.Changelog.Histories[i].Items[j].From
		//		tmptostatus = actressissue.Changelog.Histories[i].Items[j].To
		//		tmpStatustime = actressissue.Changelog.Histories[i].Created
		//		//tmpStatustime=actressissue.Fields.Changelog.Histories[0].Created
		//		rs := []rune(tmpStatustime)
		//
		//		log.Printf("lastInsertId fail: %s", string(rs[0:10])+" "+string(rs[11:19]))
		//		tmpStatustimepar, _ := time.Parse("2006-01-02 15:04:05", string(rs[0:10])+" "+string(rs[11:19]))
		//		tmpStatustimerespar, _ := time.Parse("2006-01-02 15:04:05", tmpStatustimeres)
		//
		//		beforeOrAfter := tmpStatustimerespar.After(tmpStatustimepar)
		//		if tmptostatus == tostatus && tmpfield == needfield {
		//			if tmpStatustimeres == "" || beforeOrAfter {
		//				tmpStatustimeres = tmpStatustime
		//			}
		//
		//			//tmpStatustime=""
		//			//满足条件，写入数据库
		//
		//		} else {
		//			continue
		//		}
		//	}
		//
		//}

		if Resolutiondate != "" {
			sqlStr := "update jira_issueview set resolution_date=? where id=? and project_id=? and manual_flag=0"
			_, err2 := dbb.Exec(sqlStr, Resolutiondate, issue_id, project_id)
			if err2 != nil {
				log.Println("res fail: ", err2)
				return
			}

			log.Printf("id: %d resolutiondate update success!", issue_id)
		}

		//for i := 0; i < actress.Total; i++ {
		//	tmpcreator = actress.Issues[i].Fields.Created
		//
		//}
	}
}
func jiraupdateremote(flag int) {
	dbb := getDB()
	//var project_id string
	var url string
	var project_id string
	if flag == 1 {
		//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		project_id = "11101" //1

	} else if flag == 2 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		project_id = "10417" //2
	} else if flag == 3 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		project_id = "10415" //2
	} else {
		project_id = "10422"
	}
	//if flag == 1 {
	//	//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//	project_id = "11101" //1
	//
	//} else {
	//	//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
	//	project_id = "10417" //2
	//}
	//project_id = "11101" //1
	//project_id = "10417" //2
	rows01, err := dbb.Query("  SELECT id FROM jira_issueview  where project_id=? and remotelink_title is null", project_id)
	if err != nil {
		panic(err)
	}
	defer rows01.Close()
	for rows01.Next() {
		var issue_id int

		err := rows01.Scan(&issue_id)
		if err != nil {
			log.Fatal(err)
		}
		var issue_idd string = strconv.Itoa(issue_id)

		//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(issue_idd) //1
		//url := "https://jira.tidbcloud.com/rest/api/2/issue/" + string(issue_idd) //2
		if flag == 1 {
			url = "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(issue_idd) + "/remotelink" //1
		} else {
			url = "https://jira.tidbcloud.com/rest/api/2/issue/" + string(issue_idd) + "/remotelink" //2
		}
		//var val = engineer_name
		//var projectid string = "11101" //1
		//var projectid string = "10000" //2
		//payload := strings.NewReader("{\n  \"jql\": \"created >= '2022/01/01'and created <'2022/02/06' and project=" + projectid + "\",\n \"maxResults\": \"500\"\n }")
		//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

		req, _ := http.NewRequest("GET", url, nil)
		req.Header.Set("User-Agent", "curl")

		req.Header.Add("Content-Type", "application/json")
		//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
		//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		if flag == 1 {
			req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		} else {
			req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		}
		client := http.Client{
			Timeout: 600 * time.Second,
		}
		res, _ := client.Do(req)

		defer res.Body.Close() //修改信息
		body, _ := ioutil.ReadAll(res.Body)
		if len(body) > 2 {
			var dtsissue []Tokentitle
			//log.Println(res)
			//log.Println(string(body))
			err01 := json.Unmarshal(body, &dtsissue)
			if err01 != nil {
				log.Println("error:", err01)
				log.Println("response body:", body)
				return
			}

			log.Println("update id:", issue_idd)
			//log.Printf("长度：%s\n", actressissue.Total)
			//token := actress.Issues[2]
			//var fromstatus string = "10000" //intercal
			//var tostatus string = "10114"   //intercal
			//var fromstatus string = "10308" //cloud

			//var tmpfromstatus string
			//var tmptostatus string
			//var tmpfield string
			//var tmpStatustime string

			//Updated        string
			var Title string

			//Status         *Status
			//Creator        *Creator
			//Priority       *Priority //嵌套json
			//dbb1 := getDB()
			//log.Printf("长度：%s\n", actressissue.Changelog.Total)
			//tmpStatustimeres := ""
			//
			//var lenHistories = len(actressissue.Changelog.Histories)
			//for i := 0; i < lenHistories; i++ {
			//	var lenItems = len(actressissue.Changelog.Histories[i].Items)
			//	tmptostatus = ""
			//
			//	for j := 0; j < lenItems; j++ {
			//		tmpfield = actressissue.Changelog.Histories[i].Items[j].Field
			//		//tmpfromstatus = actressissue.Changelog.Histories[i].Items[j].From
			//		tmptostatus = actressissue.Changelog.Histories[i].Items[j].To
			//		tmpStatustime = actressissue.Changelog.Histories[i].Created
			//		//tmpStatustime=actressissue.Fields.Changelog.Histories[0].Created
			//		rs := []rune(tmpStatustime)
			//
			//		log.Printf("lastInsertId fail: %s", string(rs[0:10])+" "+string(rs[11:19]))
			//		tmpStatustimepar, _ := time.Parse("2006-01-02 15:04:05", string(rs[0:10])+" "+string(rs[11:19]))
			//		tmpStatustimerespar, _ := time.Parse("2006-01-02 15:04:05", tmpStatustimeres)
			//
			//		beforeOrAfter := tmpStatustimerespar.After(tmpStatustimepar)
			//		if tmptostatus == tostatus && tmpfield == needfield {
			//			if tmpStatustimeres == "" || beforeOrAfter {
			//				tmpStatustimeres = tmpStatustime
			//			}
			//
			//			//tmpStatustime=""
			//			//满足条件，写入数据库
			//
			//		} else {
			//			continue
			//		}
			//	}
			//
			//}
			Title = dtsissue[0].Object.Title
			if Title != "" {
				sqlStr := "update jira_issueview set remotelink_title=? where id=? and project_id=? and manual_flag=0"
				_, err := dbb.Exec(sqlStr, Title, issue_id, project_id)
				if err != nil {
					log.Println("res fail: ", err)
					return
				}

				log.Printf("id: %d remotelink update success!", issue_id)
			}

			//for i := 0; i < actress.Total; i++ {
			//	tmpcreator = actress.Issues[i].Fields.Created
			//
			//}
		}

	}
}

// this function is not finished
func jiraupdaassignee(flag int) {
	dbb := getDB()
	//var project_id string
	var url string
	var project_id string
	if flag == 1 {
		//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
		project_id = "11101" //1

	} else if flag == 2 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		project_id = "10417" //2
	} else if flag == 3 {
		//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
		project_id = "10415" //2
	} else {
		project_id = "10422"
	}
	//if flag == 1 {
	//	//url = "https://internal.pingcap.net/jira/rest/api/2/search/" //1
	//	project_id = "11101" //1
	//
	//} else {
	//	//url = "https://jira.tidbcloud.com/rest/api/2/search/" //2
	//	project_id = "10417" //2
	//}
	//project_id = "11101" //1
	//project_id = "10417" //2
	rows01, err := dbb.Query("  SELECT id FROM jira_issueview  where project_id=? and flag=1 ", project_id)
	if err != nil {
		panic(err)
	}
	defer rows01.Close()
	for rows01.Next() {
		var issue_id int

		err := rows01.Scan(&issue_id)
		if err != nil {
			log.Fatal(err)
		}
		var issue_idd string = strconv.Itoa(issue_id)

		//url := "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(issue_idd) //1
		//url := "https://jira.tidbcloud.com/rest/api/2/issue/" + string(issue_idd) //2
		if flag == 1 {
			url = "https://internal.pingcap.net/jira/rest/api/2/issue/" + string(issue_idd) //1
		} else {
			url = "https://jira.tidbcloud.com/rest/api/2/issue/" + string(issue_idd) //2
		}
		//var val = engineer_name
		//var projectid string = "11101" //1
		//var projectid string = "10000" //2
		//payload := strings.NewReader("{\n  \"jql\": \"created >= '2022/01/01'and created <'2022/02/06' and project=" + projectid + "\",\n \"maxResults\": \"500\"\n }")
		//payload := strings.NewReader("{\n  \"user\": {\n    \"type\": \"user\",\n    \"name\": \"Earline Greenholt\",\n    \"email\": \"125.greenholt.earline@graham.name\",\n    \"time_zone\": \"America/Lima\",\n    \"color\": \"green\",\n    \"role\": \"admin\",\n    \"job_title\": \"Director of Engineering\",\n    \"avatar_url\": \"https://secure.gravatar.com/avatar/1d1a39d4635208d5664082a6c654a73f.png?d=mm&r=PG\",\n    \"description\": \"I'm the boss\"\n  }\n}")

		req, _ := http.NewRequest("GET", url, nil)
		req.Header.Set("User-Agent", "curl")

		req.Header.Add("Content-Type", "application/json")
		//req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
		//req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		//req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		if flag == 1 {
			req.Header.Add("Authorization", "Bearer OTM3MzY4Njg2MzQ1OqX7iWrVPpaFZGz2JL0ljTFPvhTs") //1internal
		} else {
			req.Header.Add("Authorization", "Bearer NzA4MjQ5NTE2NjY5OrhikLI528+hZgX8pEO7pwiH7P3O") //2cloud
		}
		client := http.Client{
			Timeout: 600 * time.Second,
		}
		res, _ := client.Do(req)

		defer res.Body.Close() //修改信息
		body, _ := ioutil.ReadAll(res.Body)
		if len(body) > 2 {
			var dtsissue []Token
			//log.Println(res)
			//log.Println(string(body))
			err01 := json.Unmarshal(body, &dtsissue)
			if err01 != nil {
				log.Println("error:", err01)
				log.Println("response body:", body)
				return
			}
			log.Println("assignee:", issue_idd)
			//log.Printf("长度：%s\n", actressissue.Total)
			//token := actress.Issues[2]
			//var fromstatus string = "10000" //intercal
			//var tostatus string = "10114"   //intercal
			//var fromstatus string = "10308" //cloud

			//var tmpfromstatus string
			//var tmptostatus string
			//var tmpfield string
			//var tmpStatustime string

			//Updated        string
			var Assignee string
			//Status         *Status
			//Creator        *Creator
			//Priority       *Priority //嵌套json
			//dbb1 := getDB()
			//log.Printf("长度：%s\n", actressissue.Changelog.Total)
			//tmpStatustimeres := ""
			//
			//var lenHistories = len(actressissue.Changelog.Histories)
			//for i := 0; i < lenHistories; i++ {
			//	var lenItems = len(actressissue.Changelog.Histories[i].Items)
			//	tmptostatus = ""
			//
			//	for j := 0; j < lenItems; j++ {
			//		tmpfield = actressissue.Changelog.Histories[i].Items[j].Field
			//		//tmpfromstatus = actressissue.Changelog.Histories[i].Items[j].From
			//		tmptostatus = actressissue.Changelog.Histories[i].Items[j].To
			//		tmpStatustime = actressissue.Changelog.Histories[i].Created
			//		//tmpStatustime=actressissue.Fields.Changelog.Histories[0].Created
			//		rs := []rune(tmpStatustime)
			//
			//		log.Printf("lastInsertId fail: %s", string(rs[0:10])+" "+string(rs[11:19]))
			//		tmpStatustimepar, _ := time.Parse("2006-01-02 15:04:05", string(rs[0:10])+" "+string(rs[11:19]))
			//		tmpStatustimerespar, _ := time.Parse("2006-01-02 15:04:05", tmpStatustimeres)
			//
			//		beforeOrAfter := tmpStatustimerespar.After(tmpStatustimepar)
			//		if tmptostatus == tostatus && tmpfield == needfield {
			//			if tmpStatustimeres == "" || beforeOrAfter {
			//				tmpStatustimeres = tmpStatustime
			//			}
			//
			//			//tmpStatustime=""
			//			//满足条件，写入数据库
			//
			//		} else {
			//			continue
			//		}
			//	}
			//
			//}
			// this place has a run logic, stop to call it first
			Assignee = dtsissue[0].Fields.Assignee.Name
			if Assignee != "" {
				sqlStr := "update jira_issueview set remotelink_title=? where id=? and manual_flag=0"
				res2, err2 := dbb.Exec(sqlStr, Assignee, issue_id)
				if err != nil {
					log.Println("res fail: ", err)
					return
				}
				id, err2 := res2.LastInsertId()
				if err2 != nil {
					log.Printf("lastInsertId fail: %s", err2)
				}
				log.Printf("id: %d assignee update success!", id)
			}

			//for i := 0; i < actress.Total; i++ {
			//	tmpcreator = actress.Issues[i].Fields.Created
			//
			//}
		}

	}
}
func jiracompele() {
	dbb := getDB()
	sqlStr := "UPDATE jira_issueview,(select issue_id,project_id,min(created_time) first_time from jira_actionview group by issue_id,project_id) b  set jira_issueview.first_comment_time=b.first_time WHERE jira_issueview.id=b.issue_id and jira_issueview.project_id = b.project_id and jira_issueview.manual_flag=0 " //将firstcommenttime的数值插入到issue表
	res2, err2 := dbb.Exec(sqlStr)
	if err2 != nil {
		log.Println("err2 res fail: ", err2)
		return
	}
	id, err2 := res2.LastInsertId()
	if err2 != nil {
		log.Printf("lastInsertId fail: %s", err2)
	}
	log.Printf("id: %d firstcomment update success!", id)
	sqlStr1 := "UPDATE jira_issueview,jira_issue_change a set first_sc_time=a.change_time WHERE jira_issueview.id=a.id and jira_issueview.project_id = a.project_id and jira_issueview.manual_flag=0" //将changetime的数值插入到issue表first_sc_time
	_, err3 := dbb.Exec(sqlStr1)
	if err3 != nil {
		log.Println("err 3res fail: ", err3)
		return
	}
	sqlStr6 := "update jira_issueview set ttr = round(timestampdiff(hour,created_time,resolution_date)/24,2) WHERE ttr is NULL and manual_flag=0" //计算ttr
	_, err8 := dbb.Exec(sqlStr6)
	if err8 != nil {
		log.Println("err8 res fail: ", err8)
		return
	}

	sqlStr2 := "update jira_issueview set fdr=1 where ttr<=1 and manual_flag=0 " //计算fdr ture
	_, err4 := dbb.Exec(sqlStr2)
	if err4 != nil {
		log.Println("err4 res fail: ", err4)
		return
	}
	sqlStr3 := "update jira_issueview set fdr=0 where (ttr>1 or ttr is null) and manual_flag=0 " //计算fdr false
	_, err5 := dbb.Exec(sqlStr3)
	if err5 != nil {
		log.Println("err5 res fail: ", err5)
		return
	}

	sqlStr5 := "update jira_issueview set frt = first_comment_time where ifnull(first_comment_time,now()) < ifnull(first_sc_time,now()) and manual_flag=0 " //处理frt comment
	sqlStr55 := "update jira_issueview set frt = first_sc_time where ifnull(first_comment_time,now()) >= ifnull(first_sc_time,now()) and manual_flag=0 "    //处理frt sc
	_, err7 := dbb.Exec(sqlStr5)
	if err7 != nil {
		log.Println("err7 res fail: ", err7)
		return
	}
	_, err77 := dbb.Exec(sqlStr55)
	if err77 != nil {
		log.Println("err77 res fail: ", err77)
		return
	}
	sqlStr4 := "update jira_issueview set frt_hour = round(timestampdiff(minute,created_time,frt)/60,2) where manual_flag=0" //frt_hour
	_, err6 := dbb.Exec(sqlStr4)
	if err6 != nil {
		log.Println("err6 res fail: ", err6)
		return
	}
	sqlStr7 := "UPDATE jira_issueview set flag=0 where manual_flag=0 and jira_issueview.flag=1" //设置flag
	_, err9 := dbb.Exec(sqlStr7)
	if err9 != nil {
		log.Println("err9 res fail: ", err9)
		return
	}
	sqlStr71 := "update jira_issueview set sla=1 where priority ='P0' and frt_hour<= 0.25 and timestampdiff(minute,created_time,now())>= 15 and sla is null and manual_flag=0" //set sla
	sqlStr72 := "update jira_issueview set sla=1 where priority ='P1' and frt_hour<= 1 and timestampdiff(minute,created_time,now())>= 60 and sla is null and manual_flag=0"
	sqlStr73 := "update jira_issueview set sla=1 where priority ='P2' and frt_hour<= 12 and timestampdiff(minute,created_time,now())>= 720 and sla is null and manual_flag=0"
	sqlStr74 := "update jira_issueview set sla=1 where priority ='P3' and frt_hour<= 24 and timestampdiff(minute,created_time,now())>= 1440 and sla is null and manual_flag=0"
	sqlStr75 := "update jira_issueview set sla=0 where priority ='P0' and (frt_hour> 0.25 or frt_hour is null) and timestampdiff(minute,created_time,now())>= 15 and sla is null and manual_flag=0"
	sqlStr76 := "update jira_issueview set sla=0 where priority ='P1' and (frt_hour> 1 or frt_hour is null) and timestampdiff(minute,created_time,now())>= 60 and sla is null and manual_flag=0"
	sqlStr77 := "update jira_issueview set sla=0 where priority ='P2' and (frt_hour> 12 or frt_hour is null) and timestampdiff(minute,created_time,now())>= 720 and sla is null and manual_flag=0"
	sqlStr78 := "update jira_issueview set sla=0 where priority ='P3' and (frt_hour> 24 or frt_hour is null) and timestampdiff(minute,created_time,now())>= 1440 and sla is null and manual_flag=0"
	_, err91 := dbb.Exec(sqlStr71)
	if err91 != nil {
		log.Println("err91 res fail: ", err91)
		return
	}
	_, err92 := dbb.Exec(sqlStr72)
	if err92 != nil {
		log.Println("err92 res fail: ", err92)
		return
	}
	_, err93 := dbb.Exec(sqlStr73)
	if err93 != nil {
		log.Println("err93 res fail: ", err93)
		return
	}
	_, err94 := dbb.Exec(sqlStr74)
	if err94 != nil {
		log.Println("err94 res fail: ", err94)
		return
	}
	_, err95 := dbb.Exec(sqlStr75)
	if err95 != nil {
		log.Println("err95 res fail: ", err95)
		return
	}
	_, err96 := dbb.Exec(sqlStr76)
	if err96 != nil {
		log.Println("err96 res fail: ", err96)
		return
	}
	_, err97 := dbb.Exec(sqlStr77)
	if err97 != nil {
		log.Println("err97 res fail: ", err97)
		return
	}
	_, err98 := dbb.Exec(sqlStr78)
	if err98 != nil {
		log.Println("err98 res fail: ", err98)
		return
	}

	sqlStr8 := "UPDATE jira_issue_change set flag=0 where flag=1" //设置flag
	_, err10 := dbb.Exec(sqlStr8)
	if err10 != nil {
		log.Println("err10 res fail: ", err10)
		return
	}
	log.Println("completed！ ")
}
func jiraAdd(issueflag int, start string, end string, fullflag int) { //issueflag表示更新inter  2表示更新dbcloud
	//var issueflag = 1

	jira(issueflag, start, end, fullflag) //新增jiraissue
	jiraUpdateComponent(issueflag, start, end, fullflag)
	jiraUpdateCode(issueflag, start, end, fullflag)
	jiraupdatestatus(issueflag)
	jiraupdaResolutiondate(issueflag) //重新查询未关单的是否有被关闭的，并更新ttr
	jiraaction(issueflag)
	jiraChangelog(issueflag)
	jiraupdateremote(issueflag)
	jiraUpdaterootcause(issueflag, start, end, fullflag)
}
func main() {

	t := time.Now().Unix()
	log.Println(reflect.TypeOf(t))
	var end string = time.Unix(t, 0).Format("2006/01/02")
	t -= 86400 //减一天
	var start string = time.Unix(t, 0).Format("2006/01/02")
	//log.Println(end)
	//log.Println(start)
	var fullflag = 2 //为1表示全量取数据，需要自己指定日期，为2表示按天增量
	//start = "2022/09/07"
	//end = "2022/09/08"
	/*
		i := 1
		j := 5
		for i < 31 {
			start = "2022/" + strconv.Itoa(j) + "/" + strconv.Itoa(i)
			i++
			end = "2022/" + strconv.Itoa(j) + "/" + strconv.Itoa(i)
			log.Println(end)
			log.Println(start)
			jiraUpdateCode(1, start, end, fullflag)
		}
	*/
	log.Println("start date:", start)
	log.Println("start date:", end)

	jiraAdd(1, start, end, fullflag) //op
	jiraAdd(2, start, end, fullflag) // tcoc
	jiraAdd(3, start, end, fullflag) // tca
	jiraAdd(4, start, end, fullflag) // o11y
	//jiraupdatestatus(1)
	//jiraupdaResolutiondate(2)
	jiracompele()
	//jiraUpdateCode(1, start, end, fullflag)
	//log.Println(time.Unix(t, 0).Format("2006-01-02"))
	//t -= 86400 //增加一天
	//log.Println(time.Unix(t, 0).Format("2006/01/02"))
	//jiraUpdateCode(1, start, end, fullflag)
	//jiraupdaremotelink(1)
	//jiraUpdaterootcause(1)
	//test()
	//jiraday()//计算每天解决率的，用作可视化展示用，暂时未用到

	//jiraUpdateComponent(1, start, end)
	//jiraUpdateCode()
	//jiraupdaResolutiondate(1)

	//jiraAdd() //增量
}

//做增量：
//jira  jiraUpdateComponent jiraUpdateCode jiraupdaDTS jiraaction jiraChangelog 再sql插入计算
