123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- 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
- }
|