| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- package service
- import (
- "context"
- "database/sql"
- "fmt"
- dao "dashoo.cn/micro/app/dao/contract"
- model "dashoo.cn/micro/app/model/contract"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/request"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- )
- type CtrContractAppendService struct {
- Dao *dao.CtrContractAppendDao
- ContractDao *dao.CtrContractDao
- ctrSrv *CtrContractService
- Tenant string
- userInfo request.UserInfo
- }
- func NewCtrContractAppendService(ctx context.Context) (*CtrContractAppendService, error) {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- err = myerrors.TipsError(fmt.Sprintf("获取租户码异常:%s", err.Error()))
- return nil, err //fmt.Errorf("获取租户码异常:%s", err.Error())
- }
- // 获取用户信息
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- err = myerrors.TipsError(fmt.Sprintf("获取用户信息异常:%s", err.Error()))
- return nil, err //fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- ctrSrv, err := NewCtrContractService(ctx)
- if err != nil {
- return nil, err
- }
- return &CtrContractAppendService{
- Dao: dao.NewCtrContractAppendDao(tenant),
- ContractDao: dao.NewCtrContractDao(tenant),
- ctrSrv: ctrSrv,
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s CtrContractAppendService) List(ctx context.Context, req *model.CtrContractAppendListReq) (int, []*model.CtrContractAppend, error) {
- dao := &s.Dao.CtrContractAppendDao
- if req.FileName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.FileName)
- dao = dao.Where("file_name LIKE ?", likestr)
- }
- if req.ContractId != 0 {
- dao = dao.Where("contract_id = ?", req.ContractId)
- }
- if req.BeginTime != "" {
- dao = dao.Where("created_time > ?", req.BeginTime)
- }
- if req.EndTime != "" {
- dao = dao.Where("created_time < ?", req.EndTime)
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.PageNum != 0 {
- dao = dao.Page(req.GetPage())
- }
- orderby := "created_time desc"
- if req.OrderBy != "" {
- orderby = req.OrderBy
- }
- dao = dao.Order(orderby)
- ents := []*model.CtrContractAppend{}
- err = dao.Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- return total, ents, err
- }
- func (s CtrContractAppendService) Add(ctx context.Context, req *model.CtrContractAppendAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.TipsError(validErr.Current().Error())
- }
- ent, err := s.ContractDao.Where("id = ?", req.ContractId).One()
- if err != nil {
- return 0, err
- }
- if ent == nil {
- return 0, myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.ContractId))
- }
- contractAppend := model.CtrContractAppend{
- ContractId: req.ContractId,
- FileName: req.FileName,
- FileType: req.FileType,
- FileUrl: req.FileUrl,
- Remark: req.Remark,
- CreatedBy: s.userInfo.Id,
- CreatedName: s.userInfo.NickName,
- CreatedTime: gtime.Now(),
- UpdatedBy: s.userInfo.Id,
- UpdatedName: s.userInfo.NickName,
- UpdatedTime: gtime.Now(),
- }
- var id int
- txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- appendId, err := tx.InsertAndGetId("ctr_contract_append", contractAppend)
- if err != nil {
- return err
- }
- err = s.ctrSrv.AddDynamicsByCurrentUser(tx, int(req.ContractId), "创建合同附件", map[string]interface{}{
- "id": appendId,
- "fileName": contractAppend.FileName,
- "fileType": contractAppend.FileType,
- "fileUrl": contractAppend.FileUrl,
- })
- if err != nil {
- return err
- }
- id = int(appendId)
- return nil
- })
- return id, txerr
- }
- func (s CtrContractAppendService) Update(ctx context.Context, req *model.CtrContractAppendUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- ent, err := s.Dao.Where("id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError(fmt.Sprintf("合同附件不存在: %d", req.Id))
- }
- if req.ContractId != 0 {
- ent, err := s.ContractDao.Where("id = ?", req.ContractId).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.ContractId))
- }
- }
- dao := &s.Dao.CtrContractAppendDao
- toupdate := map[string]interface{}{}
- if req.ContractId != 0 {
- toupdate["contract_id"] = req.ContractId
- }
- if req.FileName != "" {
- toupdate["file_name"] = req.FileName
- }
- if req.FileType != "" {
- toupdate["file_type"] = req.FileType
- }
- if req.FileUrl != "" {
- toupdate["file_url"] = req.FileUrl
- }
- if req.Remark != nil {
- toupdate["remark"] = *req.Remark
- }
- if len(toupdate) != 0 {
- toupdate["updated_by"] = s.userInfo.Id
- toupdate["updated_name"] = s.userInfo.NickName
- toupdate["updated_time"] = gtime.Now()
- _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (s CtrContractAppendService) Delete(ctx context.Context, id []int) error {
- if len(id) == 0 {
- return nil
- }
- _, err := s.Dao.Where("Id IN (?)", id).Delete()
- return err
- }
|