package company import ( "github.com/beego/beego/v2/client/orm" "hongze/hongze_task/models" "hongze/hongze_task/models/company_contract" "hongze/hongze_task/utils" "time" ) // TryOutToFormal 试用转正式 func TryOutToFormal(companyId, productId, sellerId, companyContractId int, startDate, endDate, sellerName, productName string, packageType, raiPackageType int) (err error) { o := orm.NewOrm() tx, err := o.Begin() if err != nil { return err } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() // 套餐类型为0时, 不更新套餐类型 sql := `UPDATE company_product SET status='正式',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,renewal_intention=0,is_suspend=0,is_formal=1,start_date=?,end_date=?,package_type=?,rai_package_type=?,modify_time=NOW(),formal_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? ` if packageType == 0 { sql = `UPDATE company_product SET status='正式',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,renewal_intention=0,is_suspend=0,is_formal=1,start_date=?,end_date=?,rai_package_type=?,modify_time=NOW(),formal_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? ` _, err = tx.Raw(sql, startDate, endDate, raiPackageType, companyId, productId).Exec() if err != nil { return } } else { _, err = tx.Raw(sql, startDate, endDate, packageType, raiPackageType, companyId, productId).Exec() if err != nil { return } } sql = `UPDATE company SET type=1,last_updated_time=NOW() WHERE company_id=? ` _, err = tx.Raw(sql, companyId).Exec() if err != nil { return } items := make([]*models.CompanyReportPermission, 0) sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? ` _, err = o.Raw(sql, companyId, productId).QueryRows(&items) for _, pv := range items { cpLog := new(models.CompanyPermissionLog) cpLog.CompanyId = companyId cpLog.ChartPermissionId = pv.ChartPermissionId cpLog.CreateTime = time.Now() cpLog.SysUserId = sellerId cpLog.SysUserName = sellerName cpLog.StartDate = pv.StartDate cpLog.EndDate = pv.EndDate cpLog.ProductId = productId cpLog.ProductName = pv.ProductName go models.AddCompanyPermissionLog(cpLog) } sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=?` _, err = tx.Raw(sql, companyId, productId).Exec() if err != nil { return } contractPermission := make([]*models.CompanyReportPermission, 0) sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? ` _, err = tx.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission) if err != nil { return } for _, pv := range contractPermission { sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time, chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status,is_upgrade,expensive_yx) VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?,?,?) ` _, err = tx.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式", pv.IsUpgrade, pv.ExpensiveYx).Exec() if err != nil { return } } return } // 服务更新 func ApplyServiceUpdateOld(companyId, productId, sellerId, companyContractId int, startDate, endDate, sellerName, productName string) (err error) { o := orm.NewOrm() tx, err := o.Begin() if err != nil { return err } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() sql := `UPDATE company_product SET status='正式',start_date=?,end_date=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? ` _, err = tx.Raw(sql, startDate, endDate, companyId, productId).Exec() if err != nil { return } //合同权限变更 items := make([]*models.CompanyReportPermission, 0) sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? ` _, err = tx.Raw(sql, companyId, productId).QueryRows(&items) if err != nil { return } for _, pv := range items { cpLog := new(models.CompanyPermissionLog) cpLog.CompanyId = companyId cpLog.ChartPermissionId = pv.ChartPermissionId cpLog.CreateTime = time.Now() cpLog.SysUserId = sellerId cpLog.SysUserName = sellerName cpLog.StartDate = pv.StartDate cpLog.EndDate = pv.EndDate cpLog.ProductId = productId cpLog.ProductName = pv.ProductName go models.AddCompanyPermissionLog(cpLog) } sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=? ` _, err = tx.Raw(sql, companyId, productId).Exec() if err != nil { return } contractPermission := make([]*models.CompanyReportPermission, 0) sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? ` _, err = tx.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission) if err != nil { return } for _, pv := range contractPermission { sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time, chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status) VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?) ` _, err = tx.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式").Exec() if err != nil { return } } return } func ApplyServiceUpdate(companyId, productId, sellerId, companyContractId int, startDate, endDate, sellerName, productName string, packageType, raiPackageType int) (err error) { o := orm.NewOrm() tx, err := o.Begin() if err != nil { return err } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() //合同权限变更 items := make([]*models.CompanyReportPermission, 0) sql := `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? ` _, err = tx.Raw(sql, companyId, productId).QueryRows(&items) if err != nil { return } //已存在的权限map nowCompanyReportPermissionMap := make(map[int]*models.CompanyReportPermission) //产品服务的开始、结束日期(非产品权限) updateStartDate := startDate updateStartDateTime, err := time.ParseInLocation(utils.FormatDate, updateStartDate, time.Local) if err != nil { return } updateEndDate := endDate updateEndDateTime, err := time.ParseInLocation(utils.FormatDate, updateEndDate, time.Local) if err != nil { return } for _, pv := range items { cpLog := new(models.CompanyPermissionLog) cpLog.CompanyId = companyId cpLog.ChartPermissionId = pv.ChartPermissionId cpLog.CreateTime = time.Now() cpLog.SysUserId = sellerId cpLog.SysUserName = sellerName cpLog.StartDate = pv.StartDate cpLog.EndDate = pv.EndDate cpLog.ProductId = productId cpLog.ProductName = pv.ProductName go models.AddCompanyPermissionLog(cpLog) //将权限插入到已存在的权限map中 nowCompanyReportPermissionMap[pv.ChartPermissionId] = pv //校验原始数据中的开始日期是否小于合同内的开始日期,如果小于,那么变更为原先的合同开始日期 tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, pv.StartDate, time.Local) if tmpErr != nil { err = tmpErr return } if tmpStartDate.Before(updateStartDateTime) { updateStartDateTime = tmpStartDate } //校验原始数据中的结束日期是否大于合同内的结束日期,如果大于,那么变更为原先的合同结束日期 tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, pv.EndDate, time.Local) if tmpErr != nil { err = tmpErr return } if tmpEndDate.After(updateEndDateTime) { updateEndDateTime = tmpEndDate } } //删除所有权限 //sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=? ` //_, err = o.Raw(sql, companyId, productId).Exec() //if err != nil { // return //} //更新客户产品信息 if packageType == 0 { // 套餐类型为0时, 不更新套餐类型 sql = `UPDATE company_product SET status='正式',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,is_formal=1,is_suspend=0,start_date=?,end_date=?,rai_package_type=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? ` _, err = tx.Raw(sql, updateStartDateTime, updateEndDateTime, raiPackageType, companyId, productId).Exec() if err != nil { return } } else { sql = `UPDATE company_product SET status='正式',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,is_formal=1,is_suspend=0,start_date=?,end_date=?,package_type=?,rai_package_type=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? ` _, err = tx.Raw(sql, updateStartDateTime, updateEndDateTime, packageType, raiPackageType, companyId, productId).Exec() if err != nil { return } } contractPermission := make([]*company_contract.CompanyContractPermission, 0) sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? ` _, err = tx.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission) if err != nil { return } for _, pv := range contractPermission { nowPermission, ok := nowCompanyReportPermissionMap[pv.ChartPermissionId] //判断是否已经存在该权限,如果存在的话,那么是修改,否则是新增 if ok { needUpdate := false if nowPermission.Status != "正式" && nowPermission.Status != "永续" { needUpdate = true } //如果 需要更新 字段 为false,那么再去校验时间 if needUpdate == false { //如果当前存该权限,那么去校验是否需要修改 nowPermissionEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, nowPermission.EndDate, time.Local) if tmpErr != nil { err = tmpErr return } contractPermissionEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, pv.EndDate, time.Local) if tmpErr != nil { err = tmpErr return } if contractPermissionEndDateTime.After(nowPermissionEndDateTime) { needUpdate = true } } //如果 需要更新 字段 为 true,那么就去更新 if needUpdate { sql = `update company_report_permission set end_date=?,last_updated_time=now(),modify_time=now(),company_contract_id=?,status=?,is_upgrade=?,expensive_yx=? where company_report_permission_id=? ` _, tmpErr := tx.Raw(sql, pv.EndDate, companyContractId, "正式", pv.IsUpgrade, pv.ExpensiveYx, nowPermission.CompanyReportPermissionId).Exec() err = tmpErr if err != nil { return } } } else { sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time, chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status,is_upgrade,expensive_yx) VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?,?,?) ` _, tmpErr := tx.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式", pv.IsUpgrade, pv.ExpensiveYx).Exec() err = tmpErr if err != nil { return } } } return }