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 CtrContractInvoiceService struct { Dao *dao.CtrContractInvoiceDao ContractDao *dao.CtrContractDao ctrSrv *CtrContractService Tenant string userInfo request.UserInfo } func NewCtrContractInvoiceService(ctx context.Context) (*CtrContractInvoiceService, error) { tenant, err := micro_srv.GetTenant(ctx) if err != nil { return nil, fmt.Errorf("获取组合码异常:%s", err.Error()) } // 获取用户信息 userInfo, err := micro_srv.GetUserInfo(ctx) if err != nil { return nil, fmt.Errorf("获取用户信息异常:%s", err.Error()) } ctrSrv, err := NewCtrContractService(ctx) if err != nil { return nil, err } return &CtrContractInvoiceService{ Dao: dao.NewCtrContractInvoiceDao(tenant), ContractDao: dao.NewCtrContractDao(tenant), ctrSrv: ctrSrv, Tenant: tenant, userInfo: userInfo, }, nil } func (s CtrContractInvoiceService) List(ctx context.Context, req *model.CtrContractInvoiceListReq) (int, []*model.CtrContractInvoice, error) { dao := &s.Dao.CtrContractInvoiceDao if req.SearchText != "" { likestr := fmt.Sprintf("%%%s%%", req.SearchText) dao = dao.Where("(cust_name LIKE ? || contract_code LIKE ?)", likestr, likestr) } if req.CustId != 0 { dao = dao.Where("cust_id = ?", req.CustId) } if req.CustName != "" { likestr := fmt.Sprintf("%%%s%%", req.CustName) dao = dao.Where("cust_name like ?", likestr) } if req.ContractId != 0 { dao = dao.Where("contract_id = ?", req.ContractId) } if req.ContractCode != "" { likestr := fmt.Sprintf("%%%s%%", req.ContractCode) dao = dao.Where("contract_code like ?", likestr) } if req.InvoiceType != "" { dao = dao.Where("invoice_type = ?", req.InvoiceType) } if req.ApproStatus != "" { dao = dao.Where("appro_status = ?", req.ApproStatus) } if req.InvoiceCode != "" { dao = dao.Where("invoice_code = ?", req.InvoiceCode) } if req.CourierCode != "" { dao = dao.Where("courier_code = ?", req.CourierCode) } 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.CtrContractInvoice{} err = dao.Structs(&ents) if err != nil && err != sql.ErrNoRows { return 0, nil, err } return total, ents, err } func (s CtrContractInvoiceService) Add(ctx context.Context, req *model.CtrContractInvoiceAddReq) (int, error) { validErr := gvalid.CheckStruct(ctx, req, nil) if validErr != nil { return 0, myerrors.NewMsgError(nil, validErr.Current().Error()) } c, err := s.ContractDao.Where("id = ?", req.ContractId).One() if err != nil { return 0, err } if c == nil { return 0, myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId)) } if req.InvoiceCode != "" { ent, err := s.Dao.Where("invoice_code = ?", req.InvoiceCode).One() if err != nil { return 0, err } if ent != nil { return 0, myerrors.NewMsgError(nil, fmt.Sprintf("发票号码:%s 已存在", req.InvoiceCode)) } } if req.CourierCode != "" { ent, err := s.Dao.Where("courier_code = ?", req.CourierCode).One() if err != nil { return 0, err } if ent != nil { return 0, myerrors.NewMsgError(nil, fmt.Sprintf("快递单号:%s 已存在", req.CourierCode)) } } ent := model.CtrContractInvoice{ CustId: c.CustId, CustName: c.CustName, ContractId: req.ContractId, ContractCode: c.ContractCode, ContractAmount: c.ContractAmount, InvoiceAmount: req.InvoiceAmount, InvoiceDate: req.InvoiceDate, InvoiceType: req.InvoiceType, ApproStatus: "10", InvoiceCode: req.InvoiceCode, ActualInvoiceDate: req.ActualInvoiceDate, CourierCode: req.CourierCode, Remark: req.Remark, CreatedBy: int(s.userInfo.Id), CreatedName: s.userInfo.NickName, CreatedTime: gtime.Now(), UpdatedBy: int(s.userInfo.Id), UpdatedName: s.userInfo.NickName, UpdatedTime: gtime.Now(), DeletedTime: gtime.Now(), } var id int txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error { invoiceId, err := tx.InsertAndGetId("ctr_contract_invoice", ent) if err != nil { return err } err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建发票", map[string]interface{}{ "id": invoiceId, "invoiceAmount": req.InvoiceAmount, "invoiceDate": req.InvoiceDate, "invoiceType": req.InvoiceType, "invoiceCode": req.InvoiceCode, "actualInvoiceDate": req.ActualInvoiceDate, "courierCode": req.CourierCode, }) if err != nil { return err } id = int(invoiceId) return nil }) return id, txerr } func (s CtrContractInvoiceService) Update(ctx context.Context, req *model.CtrContractInvoiceUpdateReq) error { validErr := gvalid.CheckStruct(ctx, req, nil) if validErr != nil { return myerrors.NewMsgError(nil, validErr.Current().Error()) } ent, err := s.Dao.Where("id = ?", req.Id).One() if err != nil { return err } if ent == nil { return myerrors.NewMsgError(nil, fmt.Sprintf("发票不存在: %d", req.Id)) } if req.InvoiceCode != "" { exist, err := s.Dao.Where("invoice_code = ?", req.InvoiceCode).One() if err != nil { return err } if exist != nil && exist.Id != req.Id { return myerrors.NewMsgError(nil, fmt.Sprintf("发票号码: %s 已存在", req.InvoiceCode)) } } if req.CourierCode != "" { exist, err := s.Dao.Where("courier_code = ?", req.CourierCode).One() if err != nil { return err } if exist != nil && exist.Id != req.Id { return myerrors.NewMsgError(nil, fmt.Sprintf("快递单号: %s 已存在", req.CourierCode)) } } toupdate := map[string]interface{}{} // if req.CustId != 0 { // toupdate["cust_id"] = req.CustId // } // if req.CustName != 0 { // toupdate["cust_name"] = req.CustName // } // if req.ContractId != 0 { // toupdate["contract_id"] = req.ContractId // } // if req.ContractCode != 0 { // toupdate["contract_code"] = req.ContractCode // } // if req.ContractAmount != 0 { // toupdate["contract_amount"] = req.ContractAmount // } if req.InvoiceAmount != nil { toupdate["invoice_amount"] = *req.InvoiceAmount } if req.InvoiceDate != nil { toupdate["invoice_date"] = req.InvoiceDate } if req.InvoiceType != "" { toupdate["invoice_type"] = req.InvoiceType } if req.ApproStatus != "" { toupdate["appro_status"] = req.ApproStatus } if req.InvoiceCode != "" { toupdate["invoice_code"] = req.InvoiceCode } if req.ActualInvoiceDate != nil { toupdate["actual_invoice_date"] = req.ActualInvoiceDate } if req.CourierCode != "" { toupdate["courier_code"] = req.CourierCode } if req.Remark != nil { toupdate["remark"] = *req.Remark } if len(toupdate) != 0 { toupdate["updated_by"] = int(s.userInfo.Id) toupdate["updated_name"] = s.userInfo.NickName toupdate["updated_time"] = gtime.Now() txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error { _, err = tx.Update("ctr_contract_invoice", toupdate, "id= ?", req.Id) if err != nil { return err } if req.InvoiceAmount != nil || req.ApproStatus != "" { err = s.ctrSrv.UpdateInvoiceAmount(tx, ent.ContractId) if err != nil { return err } } return nil }) if txerr != nil { return txerr } } return nil } func (s CtrContractInvoiceService) Delete(ctx context.Context, id []int) error { if len(id) == 0 { return nil } return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error { contractId := map[int]bool{} for _, i := range id { ent := model.CtrContractInvoice{} err := tx.GetStruct(&ent, "select * from ctr_contract_invoice where id = ?", i) if err == sql.ErrNoRows { continue } if err != nil { return err } if ent.ApproStatus != "20" { continue } contractId[ent.ContractId] = true } _, err := tx.Delete("ctr_contract_invoice", "id in (?)", id) if err != nil { return err } for cid := range contractId { err = s.ctrSrv.UpdateInvoiceAmount(tx, cid) if err != nil { return err } } return nil }) }