package services

import (
	"fmt"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/services/alarm_msg"
	"hongze/hz_crm_api/utils"
	"time"
)

// 权益客户新增历史备注
func AddCompanyHistoryRemark(item *company.CompanyHistoryRemark) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("权益客户新增历史备注，失败："+err.Error()+fmt.Sprint(item), 2)
		}
	}()
	err = company.AddCompanyHistoryRemark(item)
	return
}

// 删除权益客户新增历史备注
func DelCompanyHistoryRemark(tableName string, tableId int) {
	if tableName == "" {
		return
	}
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg(fmt.Sprintf("权益客户新增历史备注，失败："+err.Error()+"tableName:", tableName, "tableId", tableId), 2)
		}
	}()
	err = company.DelCompanyHistoryRemark(tableName, tableId)
	return
}

//func init() {
//	AddCompanyHistoryRemarkInit()
//}

// 权益客户新增历史备注脚本
func AddCompanyHistoryRemarkInit() {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
		}
	}()

	sysUserList, err := system.GetAdminList()
	if err != nil {
		fmt.Println(err)
		return
	}

	mapadmin := make(map[int]string)
	for _, v := range sysUserList {
		mapadmin[v.AdminId] = v.RealName
	}

	listHistory, e := company.GetCompanyHistoryRemarkListinit()
	if e != nil {
		fmt.Println(e)
		return
	}

	makHistoryMap := make(map[string]bool)

	for _, v := range listHistory {
		makHistoryMap[fmt.Sprintf("id_", v.TableId, "name_", v.TableName)] = true
	}

	var items []*company.CompanyHistoryRemark

	recordOB := new(company.CompanyServiceRecord)
	recordCond := ""
	recordPars := make([]interface{}, 0)
	records, e := recordOB.GetItemsByCondition(recordCond, recordPars, []string{}, "")
	if e != nil {
		fmt.Println(e)
		return
	}

	//company_service_record
	for _, v := range records {
		if makHistoryMap[fmt.Sprintf("id_", v.CompanyServiceRecordId, "name_", v.TableName)] {
			continue
		}
		item := new(company.CompanyHistoryRemark)
		item.CompanyId = v.CompanyId
		item.Content = v.Content
		item.SysAdminId = v.SysAdminId
		item.SysAdminName = v.SysAdminName
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		item.ShowTime = v.CreateTime
		item.CompanyId = v.CompanyId
		item.TableId = v.CompanyServiceRecordId
		item.TableName = "company_service_record"
		items = append(items, item)
	}

	//company_product_remark
	listRemark, e := company.GetRemarkListInit()
	if e != nil {
		fmt.Println(e)
		return
	}
	for _, v := range listRemark {
		if makHistoryMap[fmt.Sprintf("id_", v.CompanyProductRemarkId, "name_", "company_product_remark")] {
			continue
		}
		item := new(company.CompanyHistoryRemark)
		item.CompanyId = v.CompanyId
		item.Content = v.Remark
		item.SysAdminId = v.SysUserId
		item.SysAdminName = v.SysRealName
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		item.ShowTime = v.CreateTime
		item.CompanyId = v.CompanyId
		item.TableId = v.CompanyProductRemarkId
		item.TableName = "company_product_remark"
		items = append(items, item)
	}
	//company_product_remark  end

	var condition string
	var pars []interface{}
	condition += ` AND product_id = 2  ORDER  BY  create_time DESC   `
	listrenewed_note, err := company.GetCompanyNoRenewedNoteList(condition, pars, 0, 0)
	if err != nil {
		fmt.Println(err)
		return
	}
	//company_no_renewed_note
	for _, v := range listrenewed_note {
		if makHistoryMap[fmt.Sprintf("id_", v.NoRenewedNoteId, "name_", "company_no_renewed_note")] {
			continue
		}
		item := new(company.CompanyHistoryRemark)
		item.CompanyId = v.CompanyId
		item.Content = v.Content
		item.SysAdminId = v.AdminId
		item.SysAdminName = mapadmin[v.AdminId]
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		item.ShowTime = utils.StrTimeToTime(v.CreateTime)
		item.CompanyId = v.CompanyId
		item.TableId = v.NoRenewedNoteId
		item.TableName = "company_no_renewed_note"
		items = append(items, item)
	}
	//company_no_renewed_note end

	//company_renewal_reason
	listRenewa, e := models.GetMoreRenewalReasoninit()
	if e != nil {
		fmt.Println(e)
		return
	}
	for _, v := range listRenewa {
		if makHistoryMap[fmt.Sprintf("id_", v.CompanyRenewalReasonId, "name_", "company_renewal_reason")] {
			continue
		}
		item := new(company.CompanyHistoryRemark)
		item.CompanyId = v.CompanyId
		item.Content = v.RenewalReason
		//item.SysAdminId = v.SysUserId
		//item.SysAdminName = v.SysRealName
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		item.ShowTime = v.CreateTime
		item.CompanyId = v.CompanyId
		item.TableId = v.CompanyRenewalReasonId
		item.TableName = "company_renewal_reason"
		items = append(items, item)
	}
	//company_renewal_reason end

	//cygx_user_feedback
	// 权益服务明细表
	listUserFeedback, e := cygx.GetCygxUserFeedbackListInit()
	if e != nil {
		fmt.Println(e)
		return
	}
	for _, v := range listUserFeedback {
		if makHistoryMap[fmt.Sprintf("id_", v.UserFeedbackId, "name_", "cygx_user_feedback")] {
			continue
		}
		item := new(company.CompanyHistoryRemark)
		item.CompanyId = v.CompanyId
		item.Content = v.Content
		item.SysAdminId = v.AdminId
		item.SysAdminName = mapadmin[v.AdminId]
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		item.ShowTime = v.CreateTime
		item.CompanyId = v.CompanyId
		item.UserId = v.UserId
		item.Mobile = v.Mobile
		item.Email = v.Email
		item.RealName = v.RealName
		item.TableId = v.UserFeedbackId
		item.TableName = "cygx_user_feedback"
		items = append(items, item)
	}

	if len(items) > 0 {
		var itemsAdd []*company.CompanyHistoryRemark
		for k, v := range items {
			v.ProductId = 2
			itemsAdd = append(itemsAdd, v)
			if len(itemsAdd)%2000 == 0 {
				fmt.Println(k)
				e = company.AddCompanyHistoryRemarkMultiinit(itemsAdd)
				if e != nil {
					fmt.Println(e)
					return
				}
				itemsAdd = make([]*company.CompanyHistoryRemark, 0)
			}
		}
		e = company.AddCompanyHistoryRemarkMultiinit(itemsAdd)
		if e != nil {
			fmt.Println(e)
			return
		}
	}

	//cygx_user_feedback end

	fmt.Println(len(items))
	fmt.Println(len(records))
	return
}
