|
@@ -5,6 +5,7 @@ import (
|
|
|
"fmt"
|
|
|
"hongze/hz_crm_api/models"
|
|
|
"hongze/hz_crm_api/models/company"
|
|
|
+ "hongze/hz_crm_api/services/alarm_msg"
|
|
|
"hongze/hz_crm_api/utils"
|
|
|
"strconv"
|
|
|
"strings"
|
|
@@ -57,7 +58,7 @@ func InitCompanyContractMerge() {
|
|
|
items = append(items, item)
|
|
|
} else {
|
|
|
//续约的合同开始时间是否<上一次合同记录的结束时间 且间隔>6个月(180)天,那么就对两个合同进行合并
|
|
|
- if item.StartDate.AddDate(0, 0, +180).Before(items[len(items)-1].EndDate) && item.EndDate.After(items[len(items)-1].EndDate) {
|
|
|
+ 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) + ","
|
|
@@ -82,6 +83,9 @@ func InitCompanyContractMerge() {
|
|
|
}
|
|
|
|
|
|
var contractMergeitems []*company.CompanyContractMerge
|
|
|
+
|
|
|
+ var companyIds []int
|
|
|
+ mapcompanyIds := make(map[int]bool)
|
|
|
for _, v := range items {
|
|
|
item := new(company.CompanyContractMerge)
|
|
|
item.ContractType = v.ContractType
|
|
@@ -97,6 +101,12 @@ func InitCompanyContractMerge() {
|
|
|
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)
|
|
@@ -104,8 +114,28 @@ func InitCompanyContractMerge() {
|
|
|
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 GetCompanyContractPermissionNameMapById(mapCompamy map[int]string) (mapContractResp map[int]string, err error) {
|
|
|
if len(mapCompamy) == 0 {
|
|
|
return
|
|
@@ -213,3 +243,131 @@ func GetCompanyContractPermissionNameMapById(mapCompamy map[int]string) (mapCont
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+//
|
|
|
+//func init() {
|
|
|
+// AddCompanyContractMergeByCompanyContractId(1187)
|
|
|
+//}
|
|
|
+
|
|
|
+// 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
|
|
|
+ }
|
|
|
+ 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 {
|
|
|
+ err = errors.New("GetCompanyContractMergeDetail, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ 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, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if item.Money > detailPrevious.Money {
|
|
|
+ item.PackageDifference = "增加套餐"
|
|
|
+ }
|
|
|
+ if item.Money == detailPrevious.Money {
|
|
|
+ item.PackageDifference = "维持套餐"
|
|
|
+ }
|
|
|
+ if item.Money < detailPrevious.Money {
|
|
|
+ item.PackageDifference = "减少套餐"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ e = company.AddCompanyContractMerge(item)
|
|
|
+ if e != nil {
|
|
|
+ err = errors.New("AddCompanyContractMerge, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|