package services import ( "ERP_storage/Nats/NatsServer" "ERP_storage/conf" "ERP_storage/dto" db "ERP_storage/initialize" "ERP_storage/logs" "ERP_storage/models/Account" models "ERP_storage/models/ContractReview" "errors" "fmt" "git.baozhida.cn/ERP_libs/lib" ) type ContractReview struct { } func (e *ContractReview) GetPage(c *dto.ContractReviewPageReq) (list []models.ContractReview, cnt int64) { var err error err = db.DB.Model(&models.ContractReview{}). Scopes( dto.MakeCondition(c.GetNeedSearch()), dto.Paginate(c.GetPageSize(), c.GetPageIndex()), dto.WithNormalState(), ). Preload("ServiceItemList", "t__state = ?", 1). Preload("ServiceItemList.ServiceItem.ServiceType"). Find(&list).Limit(-1).Offset(-1). Count(&cnt).Error if err != nil { logs.Error("db error: %s ", err) return } for i := 0; i < len(list); i++ { list[i].T_submit_name = Account.Read_User_T_name_Get(list[i].T_submit) } return } func (e *ContractReview) GetFinancePage(c *dto.ContractReviewPageReq) (list []models.ContractReview, cnt int64) { var err error err = db.DB.Model(&models.ContractReview{}). Scopes( dto.MakeCondition(c.GetNeedSearch()), dto.Paginate(c.GetPageSize(), c.GetPageIndex()), dto.WithNormalState(), ). Where("t_audit in (?)", []int{2, 3, 4, 5, 6}). Preload("ServiceItemList", "t__state = ?", 1). Preload("ServiceItemList.ServiceItem.ServiceType"). Find(&list).Limit(-1).Offset(-1). Count(&cnt).Error if err != nil { logs.Error("db error: %s ", err) return } for i := 0; i < len(list); i++ { list[i].T_submit_name = Account.Read_User_T_name_Get(list[i].T_submit) } return } func (e *ContractReview) GetManagerPage(c *dto.ContractReviewPageReq) (list []models.ContractReview, cnt int64) { var err error err = db.DB.Model(&models.ContractReview{}). Scopes( dto.MakeCondition(c.GetNeedSearch()), dto.Paginate(c.GetPageSize(), c.GetPageIndex()), dto.WithNormalState(), ). Where("t_audit in (?)", []int{3, 5, 6}). Preload("ServiceItemList", "t__state = ?", 1). Preload("ServiceItemList.ServiceItem.ServiceType"). Find(&list).Limit(-1).Offset(-1). Count(&cnt).Error if err != nil { logs.Error("db error: %s ", err) return } for i := 0; i < len(list); i++ { list[i].T_submit_name = Account.Read_User_T_name_Get(list[i].T_submit) } return } func (e *ContractReview) GetSubmitUserPage(c *dto.ContractReviewSubmitUserPageReq) (list []dto.ContractReviewSubmitUserPageRes, cnt int64) { var err error if c.T_type == "Manager" { err = db.DB.Model(&models.ContractReview{}). Select("Distinct t_submit"). Scopes( dto.WithNormalState(), ). Where("t_audit in (?)", []int{3, 5, 6}). Find(&list). Count(&cnt).Error } if c.T_type == "Finance" { err = db.DB.Model(&models.ContractReview{}). Select("Distinct t_submit"). Scopes( dto.WithNormalState(), ). Where("t_audit in (?)", []int{2, 3, 4, 5, 6}). Find(&list). Count(&cnt).Error } if err != nil { logs.Error("db error: %s ", err) return } for i := 0; i < len(list); i++ { list[i].T_name = Account.Read_User_T_name_Get(list[i].T_submit) } return } func (e *ContractReview) Get(Id int) (r models.ContractReview, err error) { err = db.DB.Scopes(dto.WithNormalState()).First(&r, Id).Error if err != nil { logs.Error("db error: %s", err) return } return } // 添加 func (e *ContractReview) Insert(c *dto.ContractReviewInsertReq) (id int, err error) { var data models.ContractReview tx := db.DB.Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() c.Generate(&data) err = tx.Create(&data).Error if err != nil { logs.Error("db error: %s", err) return } // 添加 服务内容 for i := 0; i < len(c.ServiceItemList); i++ { c.ServiceItemList[i].T_contract_review_id = data.Id c.ServiceItemList[i].T_State = 1 } err = tx.Create(&c.ServiceItemList).Error if err != nil { logs.Error("db error: %s", err) return } id = data.Id return } // 修改 func (e *ContractReview) Update(c *dto.ContractReviewUpdateReq) error { var contractReview = models.ContractReview{} err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error if err != nil { logs.Error("db error: %s", err) return dto.GetFailedErr } // 财务审核通过3 总经理审核通过5 不可修改 if contractReview.T_audit == 3 || contractReview.T_audit == 5 { logs.Error("db error: %s", err) return errors.New("审核通过后不可修改") } tx := db.DB.Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() c.Generate(&contractReview) err = tx.Save(&contractReview).Error if err != nil { logs.Error("db error: %s", err) return err } // 删除之前的服务内容 err = tx.Model(models.ContractReviewServiceItem{}).Where("t_contract_review_id = ?", contractReview.Id). Updates(map[string]interface{}{ "t__state": 0, }).Error if err != nil { logs.Error("db error: %s", err) return err } // 添加 新的服务内容 for i := 0; i < len(c.ServiceItemList); i++ { c.ServiceItemList[i].Id = 0 c.ServiceItemList[i].T_contract_review_id = contractReview.Id c.ServiceItemList[i].T_State = 1 } err = tx.Create(&c.ServiceItemList).Error if err != nil { logs.Error("db error: %s", err) return err } return nil } func (e *ContractReview) SubmitAudit(c *dto.ContractReviewGetReq) error { var contractReview = models.ContractReview{} err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error if err != nil { logs.Error("db error: %s", err) return dto.GetFailedErr } // 财务审核通过3 总经理审核通过5 不可修改 if contractReview.T_audit == 3 || contractReview.T_audit == 5 { logs.Error("db error: %s", err) return errors.New("审核通过后不可提交") } tx := db.DB.Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() // 提交审核后修改为待审核 contractReview.T_audit = 2 err = tx.Save(&contractReview).Error if err != nil { logs.Error("db error: %s", err) return err } return nil } func (e *ContractReview) FinanceAudit(c *dto.ContractReviewAuditReq) error { var contractReview = models.ContractReview{} err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error if err != nil { logs.Error("db error: %s", err) return dto.GetFailedErr } // 财务审核通过3 总经理审核通过5 不可修改 if contractReview.T_audit != 2 { logs.Error("db error: %s", err) return errors.New("审核通过后不可提交") } // 财务审核 contractReview.T_audit = c.T_audit contractReview.T_finance_approval_opinion = c.T_approval_opinion err = db.DB.Save(&contractReview).Error if err != nil { logs.Error("db error: %s", err) return err } if c.T_audit == 3 { NatsServer.AddNews(contractReview.T_submit, fmt.Sprintf("【合同评审】您提交的合同评审(%s)财务审核已通过", contractReview.T_name), conf.ContractReviewAuditUrl) } if c.T_audit == 4 { NatsServer.AddNews(contractReview.T_submit, fmt.Sprintf("【合同评审】您提交的合同评审(%s)财务审核已驳回", contractReview.T_name), conf.ContractReviewAuditUrl) } return nil } func (e *ContractReview) ManagerAudit(c *dto.ContractReviewAuditReq) error { var contractReview = models.ContractReview{} err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error if err != nil { logs.Error("db error: %s", err) return dto.GetFailedErr } // 财务审核通过3 总经理审核通过5 不可修改 if contractReview.T_audit != 3 { logs.Error("db error: %s", err) return errors.New("审核通过后不可提交") } // 管理员审核 contractReview.T_audit = c.T_audit contractReview.T_manager_approval_opinion = c.T_approval_opinion err = db.DB.Save(&contractReview).Error if err != nil { logs.Error("db error: %s", err) return err } if c.T_audit == 5 { NatsServer.AddNews(contractReview.T_submit, fmt.Sprintf("【合同评审】您提交的合同评审(%s)总经理审核已通过", contractReview.T_name), conf.ContractReviewAuditUrl) } if c.T_audit == 6 { NatsServer.AddNews(contractReview.T_submit, fmt.Sprintf("【合同评审】您提交的合同评审(%s)总经理审核已驳回", contractReview.T_name), conf.ContractReviewAuditUrl) } return nil } // 删除 func (e *ContractReview) Delete(c *dto.ContractReviewDeleteReq) error { var contractReview = models.ContractReview{} err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error if err != nil { logs.Error("db error: %s", err) return dto.GetFailedErr } //if contractReview.T_audit == 3 || contractReview.T_audit == 5 { // return errors.New("审核通过后不可删除") //} contractReview.T_State = 0 err = db.DB.Save(&contractReview).Error if err != nil { logs.Error(lib.FuncName(), err) return dto.DeleteFailedErr } return nil }