| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package service
- import (
- "context"
- dao "dashoo.cn/micro/app/dao/contract"
- model "dashoo.cn/micro/app/model/contract"
- "database/sql"
- "fmt"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/request"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- )
- type CtrContractInvoiceService struct {
- Dao *dao.CtrContractInvoiceDao
- 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())
- }
- return &CtrContractInvoiceService{
- Dao: dao.NewCtrContractInvoiceDao(tenant),
- 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("(doc_code LIKE ? || doc_name LIKE ?)", likestr, likestr)
- }
- if req.DocType != "" {
- dao = dao.Where("doc_type = ?", req.DocType)
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.Page != nil {
- if req.Page.Current == 0 {
- req.Page.Current = 1
- }
- if req.Page.Size == 0 {
- req.Page.Size = 10
- }
- dao = dao.Page(req.Page.Current, req.Page.Size)
- }
- if req.OrderBy == nil {
- req.OrderBy = &model.OrderBy{}
- }
- if req.OrderBy.Value == "" {
- req.OrderBy.Value = "created_time"
- req.OrderBy.Type = "desc"
- }
- if req.OrderBy != nil && req.OrderBy.Value != "" {
- order := "asc"
- if req.OrderBy.Type == "desc" {
- order = "desc"
- }
- dao = dao.Order(req.OrderBy.Value, order)
- }
- 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())
- }
- t, err := s.Dao.Where("doc_code = ?", req.DocCode).One()
- if err != nil {
- return 0, err
- }
- if t != nil {
- return 0, myerrors.NewMsgError(nil, fmt.Sprintf("文档编码:%s 已存在", req.DocCode))
- }
- id, err := s.Dao.InsertAndGetId(model.CtrContractInvoice{
- CustId: req.CustId,
- CustName: req.CustName,
- ContractId: req.ContractId,
- ContractCode: req.ContractCode,
- ContractAmount: req.ContractAmount,
- InvoiceAmount: req.InvoiceAmount,
- InvoiceDate: req.InvoiceDate,
- InvoiceType: req.InvoiceType,
- ApproStatus: req.ApproStatus,
- 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(),
- })
- if err != nil {
- return 0, err
- }
- return int(id), err
- }
- 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.DocCode != "" {
- exist, err := s.Dao.Where("doc_code = ?", req.DocCode).One()
- if err != nil {
- return err
- }
- if exist != nil && exist.Id != req.Id {
- return myerrors.NewMsgError(nil, fmt.Sprintf("文档编码: %s 已存在", req.DocCode))
- }
- }
- dao := &s.Dao.CtrContractInvoiceDao
- 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 != 0 {
- toupdate["invoice_amount"] = req.InvoiceAmount
- }
- if req.InvoiceDate != 0 {
- toupdate["invoice_date"] = req.InvoiceDate
- }
- if req.InvoiceType != 0 {
- toupdate["invoice_type"] = req.InvoiceType
- }
- if req.ApproStatus != 0 {
- toupdate["appro_status"] = req.ApproStatus
- }
- if req.InvoiceCode != 0 {
- toupdate["invoice_code"] = req.InvoiceCode
- }
- if req.ActualInvoiceDate != 0 {
- toupdate["actual_invoice_date"] = req.ActualInvoiceDate
- }
- if req.CourierCode != 0 {
- 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()
- _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (s CtrContractInvoiceService) Delete(ctx context.Context, id []int) error {
- if len(id) == 0 {
- return nil
- }
- _, err := s.Dao.Where("Id IN (?)", id).Delete()
- return err
- }
|