package services import ( "errors" "fmt" "hongze/hz_crm_api/models/company" "hongze/hz_crm_api/services/alarm_msg" "hongze/hz_crm_api/utils" "time" ) //func init() { // InitCompanyContractMerge() // //GetCompanyContractPermissionNameMapById(map[int]string{6513: "182,183", 6663: "435,542"}) //} // 初始化合并合同之后的数据脚本 //func InitCompanyContractMerge() { // // var condition string // var pars []interface{} // condition = " AND status = 1 AND product_id = 2 ORDER BY company_contract_id ASC " // companyContractList, err := company.GetCompanyContractList(condition, pars) // // if err != nil { // fmt.Println(err) // return // } // mapCompanyContractItems := make(map[int][]*company.CompanyContractResp) // for _, v := range companyContractList { // //fmt.Println(v) // mapCompanyContractItems[v.CompanyId] = append(mapCompanyContractItems[v.CompanyId], v) // } // // mapCompanyMoney := make(map[int]float64) // var items []*company.CompanyContractMergeResp // for _, ContractItems := range mapCompanyContractItems { // item := new(company.CompanyContractMergeResp) // for k, v := range ContractItems { // item = new(company.CompanyContractMergeResp) // item.ContractType = v.ContractType // item.ProductId = v.ProductId // item.ProductName = v.ProductName // item.CompanyId = v.CompanyId // item.CompanyProductId = v.CompanyProductId // item.StartDate = v.StartDate // item.EndDate = v.EndDate // item.Money = v.Money // item.CompanyContractIdGroup += strconv.Itoa(v.CompanyContractId) + "," // if k == 0 { // items = append(items, item) // mapCompanyMoney[v.CompanyId] = item.Money // } else { // if v.CompanyId != items[len(items)-1].CompanyId { // items = append(items, item) // } else { // //续约的合同开始时间是否<上一次合同记录的结束时间 且间隔>6个月(180)天,那么就对两个合同进行合并 // if item.StartDate.AddDate(0, 0, +180).Before(items[len(items)-1].EndDate) && item.StartDate.Before(items[len(items)-1].EndDate) { // items[len(items)-1].Money += v.Money // items[len(items)-1].EndDate = v.EndDate // items[len(items)-1].CompanyContractIdGroup += strconv.Itoa(v.CompanyContractId) + "," // } else { // if item.ContractType == "续约合同" { // if item.Money > mapCompanyMoney[v.CompanyId] { // item.PackageDifference = "增加套餐" // } // if item.Money == mapCompanyMoney[v.CompanyId] { // item.PackageDifference = "维持套餐" // } // if item.Money < mapCompanyMoney[v.CompanyId] { // item.PackageDifference = "减少套餐" // } // } // items = append(items, item) // mapCompanyMoney[v.CompanyId] = item.Money // } // } // } // } // } // // var contractMergeitems []*company.CompanyContractMerge // // var companyIds []int // mapcompanyIds := make(map[int]bool) // for _, v := range items { // item := new(company.CompanyContractMerge) // item.ContractType = v.ContractType // item.ProductId = v.ProductId // item.ProductName = v.ProductName // item.CompanyId = v.CompanyId // item.CompanyProductId = v.CompanyProductId // item.StartDate = v.StartDate.Format(utils.FormatDate) // item.EndDate = v.EndDate.Format(utils.FormatDate) // item.Money = v.Money // item.PackageDifference = v.PackageDifference // item.CompanyContractIdGroup = strings.TrimRight(v.CompanyContractIdGroup, ",") // item.CreateTime = time.Now() // item.ModifyTime = time.Now() // contractMergeitems = append(contractMergeitems, item) // // if !mapcompanyIds[v.CompanyId] { // companyIds = append(companyIds, v.CompanyId) // mapcompanyIds[v.CompanyId] = true // } // // } // // err = company.MultiAddCompanyContractMerge(contractMergeitems) // if err != nil { // fmt.Println(err) // return // } // // for _, v := range companyIds { // var condition string // var pars []interface{} // // fmt.Println(v) // condition = " AND company_id = ? ORDER BY start_date DESC LIMIT 1 " // pars = append(pars, v) // contractMergeInfo, err := company.GetCompanyContractMergeDetail(condition, pars) // if err != nil { // fmt.Println(err) // return // } // err = company.UpdateCompanyContractMergeIsBestNewto1(contractMergeInfo.CompanyContractMergeId) // if err != nil { // fmt.Println(err) // return // } // } //} //// GetCompanyContractPermissionNameMapById 获取合并之后的合同所对应的权限种类名称 //func GetCompanyContractPermissionNameMapBymapCompamy(mapCompamy map[int]string) (mapContractResp map[int]string, err error) { // if len(mapCompamy) == 0 { // return // } // //var condition string // //var pars []interface{} // // mapContracIdCompanyId := make(map[int]int) //建立合同ID与公司ID的map对应关系 // var companyContractIds []int // var companyContractIdGroup string //多个合同ID // for k, v := range mapCompamy { // companyContractIdGroup += v + "," // sliceContract := strings.Split(v, ",") // for _, compamyContracId := range sliceContract { // companyContractIdInt, _ := strconv.Atoi(compamyContracId) // mapContracIdCompanyId[companyContractIdInt] = k // companyContractIds = append(companyContractIds, companyContractIdInt) // } // } // // contractPermissionNameMap, e := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds) // if e != nil { // err = errors.New("GetCompanyContractPermissionNameMapById, Err: " + e.Error()) // return // } // // mapContractResp = make(map[int]string, 0) // // for k, v := range mapCompamy { // // sliceContract := strings.Split(v, ",") // for _, compamyContracId := range sliceContract { // companyContractIdInt, _ := strconv.Atoi(compamyContracId) // for k2, v2 := range contractPermissionNameMap { // if companyContractIdInt == k2 { // mapContractResp[k] += v2 + "," // } // } // } // } // // //companyContractIdGroup = strings.TrimRight(companyContractIdGroup, ",") // //condition += " AND company_contract_id IN (" + companyContractIdGroup + ")" // //companyContractList, e := company.GetCompanyContractList(condition, pars) // //if e != nil && e.Error() != utils.ErrNoRow() { // // err = errors.New("GetCompanyContractList, Err: " + e.Error()) // // return // //} // // // //permissionList, e := models.GetChartPermissionList() // //if e != nil && e.Error() != utils.ErrNoRow() { // // err = errors.New("GetChartPermissionList, Err: " + e.Error()) // // return // //} // //mappermissionName := make(map[int]string) // //for _, v := range permissionList { // // mappermissionName[v.ChartPermissionId] = v.Remark // //} // // // //mapContractIdPerssion := make(map[string]string) //单个合同ID所对应的套餐或者权限名称 // //for _, v := range companyContractList { // // if v.RaiPackageType == 1 { // // mapContractIdPerssion[strconv.Itoa(v.CompanyContractId)] = "70w套餐" // // } // // if v.RaiPackageType == 2 { // // mapContractIdPerssion[strconv.Itoa(v.CompanyContractId)] = "45w套餐" // // } // //} // //mapIsUpgrade := make(map[string]bool) //合同ID对应的行业是否有升级 // //companyContractPermissionList, e := company.GetCompanyContractPermissionList(condition, pars) // //if e != nil && e.Error() != utils.ErrNoRow() { // // err = errors.New("GetCompanyContractPermissionList, Err: " + e.Error()) // // return // //} // //for _, v := range companyContractPermissionList { // // //如果开通的不是整个套餐,那么就做单独的子权限处理 // // if strings.Count(mapContractIdPerssion[strconv.Itoa(v.CompanyContractId)], "w套餐") == 0 { // // if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN { // // if v.ExpensiveYx == 1 { // // mapContractIdPerssion[strconv.Itoa(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(5w)," // // } else { // // mapContractIdPerssion[strconv.Itoa(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(3w)," // // } // // } else { // // mapContractIdPerssion[strconv.Itoa(v.CompanyContractId)] += mappermissionName[v.ChartPermissionId] + "," // // if v.IsUpgrade == 1 { // // //合同ID,权限名称形成唯一的主键索引 // // perssionName := mappermissionName[v.ChartPermissionId] // // perssionName = strings.Replace(perssionName, "(客观)", "", -1) // // perssionName = strings.Replace(perssionName, "(主观)", "", -1) // // mapIsUpgrade[fmt.Sprint(v.CompanyContractId, "perssionName", perssionName)] = true // // } // // } // // } // //} // // // ////合并客观权限 // //for k, v := range mapContractIdPerssion { // // perssionName := v // // if strings.Count(v, utils.YI_YAO_NAME) == 2 { // // perssionName = strings.Replace(perssionName, "医药(主观)", "医药", -1) // // perssionName = strings.Replace(perssionName, "医药(客观)", "", -1) // // } // // if strings.Count(v, utils.XIAO_FEI_NAME) == 2 { // // perssionName = strings.Replace(perssionName, "消费(主观)", "消费", -1) // // perssionName = strings.Replace(perssionName, "消费(客观)", "", -1) // // } // // if strings.Count(v, utils.KE_JI_NAME) == 2 { // // perssionName = strings.Replace(perssionName, "科技(主观)", "科技", -1) // // perssionName = strings.Replace(perssionName, "科技(客观)", "", -1) // // } // // if strings.Count(v, utils.ZHI_ZAO_NAME) == 2 { // // perssionName = strings.Replace(perssionName, "智造(主观)", "智造", -1) // // perssionName = strings.Replace(perssionName, "智造(客观)", "", -1) // // } // // perssionName = strings.TrimRight(perssionName, ",") // // mapContractIdPerssion[k] = perssionName // //} // //mapContractResp = make(map[int]string, 0) // //for k, v := range mapContractIdPerssion { // // mapContractResp[mapContracIdCompanyId[k]] += v + "," // //} // // // ////过滤多余的"," // //for k, v := range mapContractResp { // // sliceName := strings.Split(v, ",") // // var nameArr []string // // for _, vName := range sliceName { // // if vName == "" { // // continue // // } // // if mapIsUpgrade[fmt.Sprint(k, "perssionName", vName)] { // // vName += "(升级)" // // } // // nameArr = append(nameArr, vName) // // } // // mapContractResp[k] = strings.Join(nameArr, ",") // //} // return //} //func init() { // AddCompanyContractMergeByCompanyContractId(1219) //} // AddCompanyContractMergeByCompanyContractId 通过合同ID对合同进行合并 //func AddCompanyContractMergeByCompanyContractId(companyContractId int) { // var err error // defer func() { // if err != nil { // fmt.Println(err) // go alarm_msg.SendAlarmMsg("通过合同ID对合同进行合并,失败"+err.Error()+fmt.Sprint("companyContractId", companyContractId), 2) // } // }() // contractInfo, e := company.GetCompanyContractDetailByCompanyContractId(companyContractId) // if e != nil { // err = errors.New("GetCompanyContractDetailByCompanyContractId, Err: " + e.Error()) // return // } // // var condition string // var pars []interface{} // // condition = " AND company_id = ? ORDER BY start_date DESC LIMIT 1 " // pars = append(pars, contractInfo.CompanyId) // contractMergeInfo, e := company.GetCompanyContractMergeDetail(condition, pars) // if e != nil && e.Error() != utils.ErrNoRow() { // err = errors.New("GetCompanyContractMergeDetail, Err: " + e.Error()) // return // } // //fmt.Println(contractMergeInfo) // e = company.UpdateCompanyContractMergeIsBestNew(0, contractInfo.CompanyId) // if e != nil { // err = errors.New("UpdateCompanyContractMergeIsBestNew, Err: " + e.Error()) // return // } // //fmt.Println(contractInfo) // item := new(company.CompanyContractMerge) // item.ContractType = contractInfo.ContractType // item.ProductId = contractInfo.ProductId // item.ProductName = contractInfo.ProductName // item.CompanyId = contractInfo.CompanyId // item.CompanyProductId = contractInfo.CompanyProductId // item.IsBestNew = 1 // item.CreateTime = time.Now() // item.ModifyTime = time.Now() // //如果不存在就是新的数据 // if contractMergeInfo == nil { // item.StartDate = contractInfo.StartDate // item.EndDate = contractInfo.EndDate // item.Money = contractInfo.Money // item.CompanyContractIdGroup = strconv.Itoa(companyContractId) // e = company.AddCompanyContractMerge(item) // if e != nil { // err = errors.New("AddCompanyContractMerge, Err: " + e.Error()) // return // } // } else { // newStartDate := utils.StrDateToDate(contractInfo.StartDate) // //newEndDate := utils.StrDateToDate(contractInfo.EndDate) // oldEndDate := utils.StrDateToDate(contractMergeInfo.EndDate) // // //续约的合同开始时间是否<上一次合同记录的结束时间 且间隔>6个月(180)天,那么就对两个合同进行合并 // if newStartDate.AddDate(0, 0, +180).Before(oldEndDate) && newStartDate.Before(oldEndDate) { // item.Money = contractInfo.Money + contractMergeInfo.Money // item.EndDate = contractInfo.EndDate // item.CompanyContractIdGroup = contractMergeInfo.CompanyContractIdGroup + "," + strconv.Itoa(companyContractId) // //如果是续约合同就对比跟上一份合并之后的合同的差距 // if item.ContractType == "续约合同" { // pars = make([]interface{}, 0) // condition = " AND company_id = ? AND company_contract_merge_id != ? ORDER BY start_date DESC LIMIT 1 " // pars = append(pars, contractMergeInfo.CompanyId, contractMergeInfo.CompanyContractMergeId) // detailPrevious, e := company.GetCompanyContractMergeDetail(condition, pars) // if e != nil && e.Error() != utils.ErrNoRow() { // err = errors.New("GetCompanyContractMergeDetail-Update, Err: " + e.Error()) // return // } // if detailPrevious == nil { // item.PackageDifference = "" // } else { // if item.Money > detailPrevious.Money { // item.PackageDifference = "增加套餐" // } // if item.Money == detailPrevious.Money { // item.PackageDifference = "维持套餐" // } // if item.Money < detailPrevious.Money { // item.PackageDifference = "减少套餐" // } // } // } // item.CompanyContractMergeId = contractMergeInfo.CompanyContractMergeId // e = company.UpdateCompanyContractMerge(item) // if e != nil { // err = errors.New("AddCompanyContractMerge, Err: " + e.Error()) // return // } // } else { // item.StartDate = contractInfo.StartDate // item.EndDate = contractInfo.EndDate // item.Money = contractInfo.Money // item.CompanyContractIdGroup = strconv.Itoa(companyContractId) // //如果是续约合同就对比跟上一份合并之后的合同的差距 // if item.ContractType == "续约合同" { // //pars = make([]interface{}, 0) // //condition = " AND company_id = ? AND company_contract_merge_id != ? ORDER BY start_date DESC LIMIT 1 " // //pars = append(pars, contractMergeInfo.CompanyId, contractMergeInfo.CompanyContractMergeId) // //detailPrevious, e := company.GetCompanyContractMergeDetail(condition, pars) // //if e != nil { // // err = errors.New("GetCompanyContractMergeDetail——add, Err: " + e.Error()) // // return // //} // if item.Money > contractMergeInfo.Money { // item.PackageDifference = "增加套餐" // } // if item.Money == contractMergeInfo.Money { // item.PackageDifference = "维持套餐" // } // if item.Money < contractMergeInfo.Money { // item.PackageDifference = "减少套餐" // } // } // e = company.AddCompanyContractMerge(item) // if e != nil { // err = errors.New("AddCompanyContractMerge, Err: " + e.Error()) // return // } // } // } // // return //} //func init() { // UpdateCompanyContractSellerMove(7034, 2) //} // UpdateCompanyContractSellerMove 权益客户移动所属销售之后,更新所关联的销售信息 func UpdateCompanyContractSellerMove(companyId, productId int) { if productId != utils.COMPANY_PRODUCT_RAI_ID { return } time.Sleep(1 * time.Second) // 延迟1秒 var err error defer func() { if err != nil { fmt.Println(err) go alarm_msg.SendAlarmMsg("权益客户移动所属销售之后,更新所关联的销售信息失败,UpdateCompanyContractSellerMove "+fmt.Sprint("companyId:", companyId, ";err:", err), 3) } }() cp, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, productId) if e != nil { err = errors.New("GetCompanyProductByCompanyIdAndProductId Err: " + e.Error()) return } var updateCompanyProduct bool //更新还没有生效的合同 { var condition string var pars []interface{} pars = make([]interface{}, 0) condition = " AND company_id = ? AND product_id = ? AND start_date > ? " pars = append(pars, companyId, productId, time.Now()) list, e := company.GetCompanyContracList(condition, pars) // 获取还没有生效的合同信息 if e != nil { err = errors.New("GetCompanyContracList, Err: " + e.Error()) return } //fmt.Println("list1", len(list)) if len(list) > 0 { for _, v := range list { e = company.UpdateCompanyContractSellerNotEffective(cp.SellerId, cp.ShareSellerId, cp.SellerName, cp.ShareSeller, v.CompanyContractId) if e != nil { err = errors.New("UpdateCompanyContractSellerNotEffective, Err: " + e.Error()) return } } e = company.UpdateCompanyProductSellerUnexpired(cp.SellerId, cp.ShareSellerId, cp.SellerName, cp.ShareSeller, cp.CompanyId) if e != nil { err = errors.New("UpdateCompanyProductSellerUnexpired, Err: " + e.Error()) return } updateCompanyProduct = true } } //更新还没有到期的合同 { var condition string var pars []interface{} pars = make([]interface{}, 0) condition = " AND company_id = ? AND product_id = ? AND start_date < ? AND end_date > ? " pars = append(pars, companyId, productId, time.Now(), time.Now()) list, e := company.GetCompanyContracList(condition, pars) // 获取还没有到期的合同信息 if e != nil { err = errors.New("GetCompanyContracList, Err: " + e.Error()) return } //fmt.Println("list2", len(list)) if len(list) > 0 { for _, v := range list { e = company.UpdateCompanyContractSellerUnexpired(cp.SellerId, cp.ShareSellerId, cp.SellerName, cp.ShareSeller, v.CompanyContractId) if e != nil { err = errors.New("UpdateCompanyContractSellerUnexpired, Err: " + e.Error()) return } } if !updateCompanyProduct { e = company.UpdateCompanyProductSellerUnexpired(cp.SellerId, cp.ShareSellerId, cp.SellerName, cp.ShareSeller, cp.CompanyId) if e != nil { err = errors.New("UpdateCompanyProductSellerUnexpired, Err: " + e.Error()) return } } } } return } // 权益客户创建新的有效合同之后,更新所关联的销售信息 func UpdateCompanyProductSellerMove(companyId, productId int) { if productId != utils.COMPANY_PRODUCT_RAI_ID { return } time.Sleep(1 * time.Second) // 延迟1秒 var err error defer func() { if err != nil { fmt.Println(err) go alarm_msg.SendAlarmMsg("权益客户创建新的有效合同之后,更新所关联的销售信息,UpdateCompanyProductSellerMove "+fmt.Sprint("companyId:", companyId, ";err:", err), 3) } }() cp, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, productId) if e != nil { err = errors.New("GetCompanyProductByCompanyIdAndProductId Err: " + e.Error()) return } e = company.UpdateCompanyProductSellerUnexpired(cp.SellerId, cp.ShareSellerId, cp.SellerName, cp.ShareSeller, cp.CompanyId) if e != nil { err = errors.New("UpdateCompanyProductSellerUnexpired, Err: " + e.Error()) return } return }