package services

import (
	"fmt"
	"github.com/tealeg/xlsx"
	"hongze/hongze_task/models"
	"hongze/hongze_task/utils"
	"strings"
	"time"
)

//客户
func ImportCompany() {
	var err error
	defer func() {
		if err != nil {
			fmt.Println("err:", err.Error())
		}
	}()
	fileName := "./company_0717.xlsx"
	xlFile, err := xlsx.OpenFile(fileName)
	if err != nil {
		fmt.Printf("open failed: %s\n", err)
	}
	n := 0
	startDate := ""
	endDate := ""
	dateType := 3
	permissionMap := make(map[string]int)

	permissionMap["宏观经济"] = 1
	permissionMap["有色"] = 2
	permissionMap["原油"] = 3
	permissionMap["橡胶"] = 4
	permissionMap["钢材"] = 5
	permissionMap["煤炭"] = 6
	permissionMap["甲醇"] = 7
	permissionMap["沥青"] = 8
	permissionMap["PTA"] = 9
	permissionMap["MEG"] = 10
	permissionMap["聚烯烃"] = 11
	permissionMap["铁矿"] = 12
	permissionMap["苯乙烯"] = 13
	permissionMap["织造终端"] = 14

	for _, sheet := range xlFile.Sheet {
		rows := sheet.MaxRow
		for i := 0; i < rows; i++ {
			if i > 2 {
				row := sheet.Row(i)
				cells := row.Cells
				realName := cells[1].String()
				realName = strings.Trim(realName, " ")
				realName = strings.Replace(realName, " ", "", -1)

				mobile := cells[2].String()
				mobile = strings.Trim(mobile, " ")
				mobile = strings.Replace(mobile, " ", "", -1)
				fmt.Println(mobile)
				companyName := cells[6].String()
				fmt.Println(companyName)
				companyName = strings.Trim(companyName, " ")
				companyName = strings.Replace(companyName, " ", "", -1)
				company, err := models.GetCompanyByName(companyName)
				if err != nil {
					if utils.ErrNoRow() == err.Error() {
						utils.FileLog.Info("客户不存在%s", companyName)
						fmt.Println("GetCompanyByName Err:" + err.Error())
						return
					} else {
						fmt.Println("GetCompanyByName Err:" + err.Error())
						return
					}
				}
				var companyId int
				if company == nil {
					sellsId := 0
					sellsName := ""
					if sellsId <= 0 {
						fmt.Println("sellsId <= 0")
						return
					}
					if startDate == "" || endDate == "" {
						fmt.Println("startDate,endDate is empty")
						return
					}
					fmt.Println("客户不存在,", companyName)
					utils.FileLog.Info("客户不存在 %s", companyName)
					newCompany := new(models.Company)
					newCompany.CompanyName = companyName
					newCompany.CompanyType = 2
					newCompany.Enabled = 1
					newCompany.CreatedTime = time.Now()
					newCompany.LastUpdatedTime = time.Now()
					newCompany.Sort = 1
					newCompany.SellsId = sellsId
					newCompany.Seller = sellsName
					newCompany.CompanyBelong = "ficc"
					newCompany.StartDate = startDate
					newCompany.EndDate = endDate
					newCompany.FirstStartDate = startDate
					newCompany.FirstEndDate = endDate
					newCompany.DateType = dateType
					newId, err := models.AddCompany(newCompany)
					if err != nil {
						fmt.Println("新增公司失败，Err:" + err.Error())
						return
					}
					companyId = int(newId)
					//新增权限

					return
				}
				companyId = company.CompanyId
				if companyId > 0 {
					wxUser, err := models.GetWxUserByMobile(mobile)
					if err != nil && err.Error() != utils.ErrNoRow() {
						fmt.Println("GetWxUserByMobile Err:" + err.Error())
						return
					}
					if wxUser == nil {
						//用户不存在，新增
						fmt.Println("AddWxUser")
						email := ""
						fmt.Println(company.CompanyId, mobile, realName)
						err = models.AddWxUser(company.CompanyId, mobile, realName, email)
						if err != nil {
							fmt.Println("AddWxUser Err:" + err.Error())
							return
						}
					} else {
						fmt.Println("用户存在：mobile:", mobile)
						fmt.Println(company.CompanyId, mobile, realName)
					}
					fmt.Println(companyName, company.CompanyId)
				} else {
					fmt.Println("companyId<=0")
				}
				n++
			}
		}
	}
	fmt.Println("len:", n)
}

//客户
func ImportCompanyUsers() {
	var err error
	defer func() {
		if err != nil {
			fmt.Println("err:", err.Error())
		}
	}()
	fileName := "./company_user_1127.xlsx"
	xlFile, err := xlsx.OpenFile(fileName)
	if err != nil {
		fmt.Printf("open failed: %s\n", err)
	}
	n := 0
	for _, sheet := range xlFile.Sheet {
		rows := sheet.MaxRow
		for i := 0; i < rows; i++ {
			if i > 2 {
				row := sheet.Row(i)
				cells := row.Cells

				realName := cells[1].String()
				realName = strings.Trim(realName, " ")
				realName = strings.Replace(realName, " ", "", -1)

				mobile := cells[2].String()
				mobile = strings.Trim(mobile, " ")
				mobile = strings.Replace(mobile, " ", "", -1)

				email := cells[5].String()
				email = strings.Trim(email, " ")
				email = strings.Replace(email, " ", "", -1)
				fmt.Println(mobile)
				var companyId int
				companyId = 5140
				if companyId > 0 {
					wxUser, err := models.GetWxUserByMobile(mobile)
					if err != nil && err.Error() != utils.ErrNoRow() {
						fmt.Println("GetWxUserByMobile Err:" + err.Error())
						return
					}
					if wxUser == nil {
						//用户不存在，新增
						fmt.Println("AddWxUser")
						err = models.AddWxUser(companyId, mobile, realName, email)
						if err != nil {
							fmt.Println("AddWxUser Err:" + err.Error())
							return
						}
					} else {
						fmt.Println("用户存在：mobile:", mobile, realName)
					}
				} else {
					fmt.Println("companyId<=0")
				}
				n++
			}
		}
	}
	fmt.Println("len:", n)
}

//导入客户信用码
func ImportCompanyCreditCode() {
	var err error
	defer func() {
		if err != nil {
			fmt.Println("err:", err.Error())
		}
	}()
	fileName := "./客户社会信用码.xlsx"
	xlFile, err := xlsx.OpenFile(fileName)
	if err != nil {
		fmt.Printf("open failed: %s\n", err)
	}
	n := 0
	for _, sheet := range xlFile.Sheet {
		rows := sheet.MaxRow
		for i := 0; i < rows; i++ {
			if i > 2 {
				row := sheet.Row(i)
				cells := row.Cells

				companyName := cells[0].String()
				companyName = strings.Trim(companyName, " ")
				companyName = strings.Replace(companyName, " ", "", -1)

				creditCode := cells[4].String()
				creditCode = strings.Trim(creditCode, " ")
				creditCode = strings.Replace(creditCode, " ", "", -1)

				industryName := cells[5].String()
				industryName = strings.Trim(industryName, " ")
				industryName = strings.Replace(industryName, " ", "", -1)

				companyItem, err := models.GetCompanyByName(companyName)
				if err != nil {
					fmt.Println("GetCompanyByName Err:" + err.Error())
					continue
				}
				if companyItem == nil {
					fmt.Println("companyItem is nil")
				}
				count,err:=models.GetCompanyCountByCreditCode(creditCode)
				if err!=nil {
					fmt.Println("GetCompanyCountByCreditCode Err:"+err.Error())
					continue
				}
				if count>0 {
					fmt.Println("creditCode exist")
					continue
				}else{
					industryItem,err:=models.GetCompanyIndustryByName(industryName)
					if err!=nil {
						fmt.Println("GetCompanyIndustryByName Err:"+err.Error())
						return
					}
					fmt.Println(industryItem.IndustryId,industryItem.IndustryName)

					err=models.ModifyCompanyCreditCode(companyItem.CompanyId,creditCode)
					if err!=nil {
						fmt.Println("Err:"+err.Error())
						return
					}
					err=models.ModifyCompanyIndustry(industryItem.IndustryId,companyItem.CompanyId,industryName)
					if err!=nil {
						fmt.Println("Err:"+err.Error())
						return
					}
				}
				fmt.Println(companyName, creditCode, industryName)
			}
		}
	}
	fmt.Println("len:", n)
}
