| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- 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 CtrContractCollectionService struct {
- Dao *dao.CtrContractCollectionDao
- ContractDao *dao.CtrContractDao
- planSrv *CtrContractCollectionPlanService
- ctrSrv *CtrContractService
- Tenant string
- userInfo request.UserInfo
- }
- func NewCtrContractCollectionService(ctx context.Context) (*CtrContractCollectionService, 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())
- }
- planSrv, err := NewCtrContractCollectionPlanService(ctx)
- if err != nil {
- return nil, err
- }
- ctrSrv, err := NewCtrContractService(ctx)
- if err != nil {
- return nil, err
- }
- return &CtrContractCollectionService{
- Dao: dao.NewCtrContractCollectionDao(tenant),
- ContractDao: dao.NewCtrContractDao(tenant),
- planSrv: planSrv,
- ctrSrv: ctrSrv,
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s CtrContractCollectionService) List(ctx context.Context, req *model.CtrContractCollectionListReq) (int, []*model.CtrContractCollection, error) {
- dao := &s.Dao.CtrContractCollectionDao
- if req.SearchText != "" {
- likestr := fmt.Sprintf("%%%s%%", req.SearchText)
- dao = dao.Where("(cust_name LIKE ? || contract_code LIKE ?)", likestr, likestr)
- }
- if req.PlanId != 0 {
- dao = dao.Where("plan_id = ?", req.PlanId)
- }
- 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.CollectionType != "" {
- dao = dao.Where("collection_type = ?", req.CollectionType)
- }
- if req.ApproStatus != "" {
- dao = dao.Where("appro_status = ?", req.ApproStatus)
- }
- 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.CtrContractCollection{}
- err = dao.Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- return total, ents, err
- }
- func (s CtrContractCollectionService) Add(ctx context.Context, req *model.CtrContractCollectionAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.TipsError(validErr.Current().Error())
- }
- c, err := s.ContractDao.Where("id = ?", req.ContractId).One()
- if err != nil {
- return 0, err
- }
- if c == nil {
- return 0, myerrors.TipsError(fmt.Sprintf("合同:%d 不存在", req.ContractId))
- }
- ent := model.CtrContractCollection{
- PlanId: req.PlanId,
- CustId: c.CustId,
- CustName: c.CustName,
- ContractId: req.ContractId,
- ContractCode: c.ContractCode,
- CollectionDatetime: req.CollectionDatetime,
- CollectionAmount: req.CollectionAmount,
- CollectionType: req.CollectionType,
- ApproStatus: "10",
- ContractAmount: c.ContractAmount,
- 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 {
- collectionId, err := tx.InsertAndGetId("ctr_contract_collection", ent)
- if err != nil {
- return err
- }
- err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建回款", map[string]interface{}{
- "id": collectionId,
- "collectionDatetime": req.CollectionDatetime,
- "collectionAmount": req.CollectionAmount,
- "collectionType": req.CollectionType,
- })
- if err != nil {
- return err
- }
- id = int(collectionId)
- return nil
- })
- return id, txerr
- }
- func (s CtrContractCollectionService) UpdateAmount(tx *gdb.TX, id int) error {
- ent := model.CtrContractCollection{}
- err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", id)
- if err == sql.ErrNoRows {
- return myerrors.TipsError(fmt.Sprintf("回款不存在 %d", id))
- }
- if err != nil {
- return err
- }
- if ent.PlanId != 0 {
- err = s.planSrv.UpdateCashedAmount(tx, ent.PlanId)
- if err != nil {
- return err
- }
- }
- return s.ctrSrv.UpdateCollectedAmount(tx, ent.ContractId)
- }
- func (s CtrContractCollectionService) Update(ctx context.Context, req *model.CtrContractCollectionUpdateReq) 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))
- }
- // var c *model.CtrContract
- // if req.ContractId != 0 {
- // c, err = s.ContractDao.Where("id = ?", req.ContractId).One()
- // if err != nil {
- // return err
- // }
- // if c == nil {
- // return myerrors.NewMsgError(nil, fmt.Sprintf("合同:%d 不存在", req.ContractId))
- // }
- // }
- toupdate := map[string]interface{}{}
- // if req.PlanId != 0 {
- // toupdate["plan_id"] = req.PlanId
- // }
- // if req.ContractId != 0 {
- // toupdate["cust_id"] = c.CustId
- // toupdate["cust_name"] = c.CustName
- // toupdate["contract_id"] = req.ContractId
- // toupdate["contract_code"] = c.ContractCode
- // toupdate["contract_amount"] = c.ContractAmount
- // }
- if req.CollectionDatetime != nil {
- toupdate["collection_datetime"] = req.CollectionDatetime
- }
- if req.CollectionAmount != nil {
- toupdate["collection_amount"] = *req.CollectionAmount
- }
- if req.CollectionType != "" {
- toupdate["collection_type"] = req.CollectionType
- }
- if req.ApproStatus != "" {
- toupdate["appro_status"] = req.ApproStatus
- }
- 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()
- txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- _, err = tx.Update("ctr_contract_collection", toupdate, "id= ?", req.Id)
- if err != nil {
- return err
- }
- if req.CollectionAmount != nil || req.ApproStatus != "" {
- err = s.UpdateAmount(tx, req.Id)
- if err != nil {
- return err
- }
- }
- return nil
- })
- if txerr != nil {
- return txerr
- }
- }
- return nil
- }
- func (s CtrContractCollectionService) 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 {
- planId := map[int]bool{}
- contractId := map[int]bool{}
- for _, i := range id {
- ent := model.CtrContractCollection{}
- err := tx.GetStruct(&ent, "select * from ctr_contract_collection where id = ?", i)
- if err == sql.ErrNoRows {
- continue
- }
- if err != nil {
- return err
- }
- if ent.ApproStatus != "20" {
- continue
- }
- contractId[ent.ContractId] = true
- if ent.PlanId != 0 {
- planId[ent.PlanId] = true
- }
- }
- _, err := tx.Delete("ctr_contract_collection", "id in (?)", id)
- if err != nil {
- return err
- }
- for pid := range planId {
- err = s.planSrv.UpdateCashedAmount(tx, pid)
- if err != nil {
- return err
- }
- }
- for cid := range contractId {
- err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
- if err != nil {
- return err
- }
- }
- return nil
- })
- }
|