123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- package services
- import (
- "context"
- "fmt"
- "hongze/hongze_task/models"
- "hongze/hongze_task/utils"
- "time"
- )
- // CompanyReportPermissionTryOut 产品正式权限 -> 产品权限试用
- func CompanyReportPermissionTryOut(cont context.Context) (err error) {
- //fmt.Println("开始进入产品试用权限 -> 产品权限关闭")
- endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
- items, err := models.GetCompanyReportPermissionNeedTryOut(endDate)
- if err != nil {
- utils.FileLog.Info("GetCompanyReportPermissionNeedTryOut Err:%s" + err.Error())
- return
- }
- // 根据客户id分组相关品种权限
- companyReportPermissionGroupMap := make(map[string][]*models.CompanyReportPermission)
- for _, v := range items {
- companyPermissionList, ok := companyReportPermissionGroupMap[fmt.Sprint(v.CompanyId, "-", v.ProductId)]
- if !ok {
- companyPermissionList = make([]*models.CompanyReportPermission, 0)
- }
- companyPermissionList = append(companyPermissionList, v)
- companyReportPermissionGroupMap[fmt.Sprint(v.CompanyId, "-", v.ProductId)] = companyPermissionList
- }
- // 根据客户id分组处理对应的品种权限
- for _, companyReportPermissionList := range companyReportPermissionGroupMap {
- startDateTime := time.Now()
- endDateTime := startDateTime.AddDate(0, 2, 0)
- var companyId, productId int
- for _, v := range companyReportPermissionList {
- companyId = v.CompanyId
- productId = v.ProductId
- //正式转试用用户产品权限
- v.IsUpgrade = 0
- v.Status = "试用"
- v.StartDate = startDateTime.Format(utils.FormatDate)
- v.EndDate = endDateTime.Format(utils.FormatDate)
- v.ModifyTime = time.Now().Format(utils.FormatDate)
- err = v.Update([]string{"IsUpgrade","Status", "StartDate", "EndDate", "ModifyTime"})
- //err = models.CompanyReportPermissionTryOut(v.CompanyReportPermissionId, v.CompanyId, v.ProductId)
- if err != nil {
- utils.FileLog.Info("CompanyReportPermissionTryOut Err:%s" + err.Error())
- continue
- }
- //添加正式转试用用户产品权限日志
- _ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, "正式转试用", "try_out", "正式")
- }
- //获取客户信息
- companyInfo, err := models.GetCompanyProduct(companyId, productId)
- if err != nil {
- utils.FileLog.Info("CompanyReportPermissionTryOut 查找客户信息 Err:%s" + err.Error())
- continue
- }
- // 添加试用记录
- {
- companyProductTryOutUpdateLog := &models.CompanyProductTryOutUpdateLog{
- Id: 0,
- CompanyId: companyId,
- ProductId: productId,
- Source: "formal_to_try_out",
- SellerId: companyInfo.SellerId,
- SellerName: companyInfo.SellerName,
- StartDate: startDateTime,
- EndDate: endDateTime,
- RealEndDate: startDateTime,
- IsStop: 0,
- CreateTime: time.Now(),
- }
- go models.AddCompanyProductTryOutUpdateLog(companyProductTryOutUpdateLog, companyReportPermissionList)
- }
- }
- return
- }
- //产品试用权限 -> 产品权限关闭
- func CompanyReportPermissionClose(cont context.Context) (err error) {
- //fmt.Println("开始进入产品试用权限 -> 产品权限关闭")
- endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
- items, err := models.GetCompanyReportPermissionNeedClose(endDate)
- if err != nil {
- utils.FileLog.Info("GetCompanyReportPermissionNeedClose Err:%s" + err.Error())
- return
- }
- //待处理的用户产品权限列表(主要目的是为了多个产品项的去重,避免冻结产品时的多余sql操作)
- freezeCompanyProductList := make(map[string]*models.CompanyReportPermission)
- for _, v := range items {
- //fmt.Println(k, v)
- //continue
- //查看是否有审批单,如果有审批单,那么就退出当前循环,进入下一循环
- count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
- if err != nil {
- continue
- }
- if count > 0 {
- continue
- }
- //关闭用户产品权限
- err = models.CompanyReportPermissionClose(v.CompanyReportPermissionId, v.CompanyId, v.ProductId)
- if err != nil {
- utils.FileLog.Info("CompanyReportPermissionClose Err:%s" + err.Error())
- continue
- }
- //添加关闭用户产品权限日志
- _ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, "试用转关闭", "close", v.Status)
- //如果不存在该数据,那么将该数据放入待处理用户产品权限列表
- mapKey := fmt.Sprintf("%v_%v", v.CompanyId, v.ProductId)
- if _, ok := freezeCompanyProductList[mapKey]; ok == false {
- freezeCompanyProductList[mapKey] = v
- }
- }
- //遍历待处理的用户产品权限列表,查询用户产品权限信息并冻结用户产品
- for _, companyReportPermission := range freezeCompanyProductList {
- go queryAndFreezeCompanyProduct(companyReportPermission)
- }
- return
- }
- //查询用户产品权限信息并冻结用户产品
- func queryAndFreezeCompanyProduct(companyReportPermission *models.CompanyReportPermission) {
- //查询该用户名下是否存在还在使用的权限
- count, err := models.GetCompanyReportPermissionCount(companyReportPermission.CompanyId, companyReportPermission.ProductId)
- if err != nil {
- utils.FileLog.Info("CompanyReportPermissionClose query other permission Err:%s" + err.Error())
- }
- //如果还存在使用(不管是正式还是试用)的权限,那么都直接退出当前循环,不冻结用户产品
- if count > 0 {
- return
- }
- //冻结该用户产品
- _, err = models.CompanyFreeze(companyReportPermission.CompanyId, companyReportPermission.ProductId)
- if err != nil {
- go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode+";冻结该用户产品", "services:queryAndFreezeCompanyProduct;Err"+err.Error(), utils.EmailSendToUsers)
- return
- }
- //查询用户产品信息
- companyProduct, err := models.GetCompanyProduct(companyReportPermission.CompanyId, companyReportPermission.ProductId)
- //if err!=nil && err.Error() != utils.ErrNoRow() {
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode+";查询用户产品信息", fmt.Sprint("services:queryAndFreezeCompanyProduct;Err"+err.Error(), "CompanyId:", companyReportPermission.CompanyId, ";ProductId:", companyReportPermission.ProductId), utils.EmailSendToUsers)
- }
- //go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode+";查询用户产品信息", fmt.Sprint("services:queryAndFreezeCompanyProduct;Err"+err.Error(),"CompanyId:",companyReportPermission.CompanyId,";ProductId:",companyReportPermission.ProductId), utils.EmailSendToUsers)
- return
- }
- //添加操作日志
- remark := "试用转冻结"
- operation := "freeze"
- approveContent := "冻结"
- err = AddCompanyOperationRecord(companyReportPermission.CompanyId, utils.AdminId, companyReportPermission.ProductId, utils.AdminId, companyProduct.CompanyName,
- companyProduct.ProductName, utils.RealName, remark, operation, approveContent, utils.RealName, "", companyProduct.Status)
- if err != nil {
- go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode+";添加冻结操作日志", "services:queryAndFreezeCompanyProduct;Err"+err.Error(), utils.EmailSendToUsers)
- return
- }
- return
- }
- //添加用户权限操作日志
- func AddCompanyReportPermissionLog(companyReportPermissionId int64, companyId, productId, sysUserId int, productName string, sysUserName, remark, operation, status string) (err error) {
- record := models.CompanyReportPermissionLog{
- CompanyReportPermissionId: companyReportPermissionId,
- CompanyId: companyId,
- ProductId: productId,
- ProductName: productName,
- SysUserId: sysUserId,
- SysUserName: sysUserName,
- Remark: remark,
- Operation: operation,
- Status: status,
- CreateTime: time.Now(),
- }
- _, err = models.AddCompanyReportPermissionLog(&record)
- if err != nil {
- go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "services:AddCompanyReportPermissionLog;Err"+err.Error(), utils.EmailSendToUsers)
- }
- return
- }
|