| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- package service
- import (
- "context"
- "database/sql"
- "fmt"
- "github.com/gogf/gf/frame/g"
- 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 CtrContractCollectionPlanService struct {
- Dao *dao.CtrContractCollectionPlanDao
- CollectionDao *dao.CtrContractCollectionDao
- ContractDao *dao.CtrContractDao
- ctrSrv *CtrContractService
- Tenant string
- userInfo request.UserInfo
- DataScope g.Map `json:"dataScope"`
- }
- func NewCtrContractCollectionPlanService(ctx context.Context) (*CtrContractCollectionPlanService, 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 &CtrContractCollectionPlanService{
- Dao: dao.NewCtrContractCollectionPlanDao(tenant),
- ContractDao: dao.NewCtrContractDao(tenant),
- CollectionDao: dao.NewCtrContractCollectionDao(tenant),
- ctrSrv: ctrSrv,
- Tenant: tenant,
- userInfo: userInfo,
- DataScope: userInfo.DataScope,
- }, nil
- }
- func (s CtrContractCollectionPlanService) Get(ctx context.Context, id int) (*model.CtrContractCollectionPlanGetRsp, error) {
- ent, err := s.Dao.Where("Id = ?", id).One()
- if err != nil {
- return nil, err
- }
- if ent == nil {
- return nil, myerrors.TipsError("回款计划不存在")
- }
- c, err := s.CollectionDao.Where("plan_id = ?", id).All()
- if err != nil {
- return nil, err
- }
- if c == nil {
- c = []*model.CtrContractCollection{}
- }
- return &model.CtrContractCollectionPlanGetRsp{
- CtrContractCollectionPlan: *ent,
- Collection: c,
- }, nil
- }
- func (s CtrContractCollectionPlanService) List(ctx context.Context, req *model.CtrContractCollectionPlanListReq) (int, []*model.CtrContractCollectionPlanListRsp, error) {
- ctx = context.WithValue(ctx, "contextService", s)
- dao := s.Dao.As("plan").LeftJoin(dao.CtrContract.Table, "contract", "contract.id=plan.contract_id").
- DataScope(ctx, "incharge_id", "contract")
- if req.SearchText != "" {
- likestr := fmt.Sprintf("%%%s%%", req.SearchText)
- dao = dao.Where("(plan.cust_name LIKE ? || plan.contract_code LIKE ?)", likestr, likestr)
- }
- if req.CustId != 0 {
- dao = dao.Where("plan.cust_id = ?", req.CustId)
- }
- if req.CustName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.CustName)
- dao = dao.Where("plan.cust_name like ?", likestr)
- }
- if req.ContractId != 0 {
- dao = dao.Where("plan.contract_id = ?", req.ContractId)
- }
- if req.ContractCode != "" {
- likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
- dao = dao.Where("plan.contract_code like ?", likestr)
- }
- if req.ContractStatus != "" {
- dao = dao.Where("plan.contract_status = ?", req.ContractStatus)
- }
- if req.BeginTime != "" {
- dao = dao.Where("plan.created_time > ?", req.BeginTime)
- }
- if req.EndTime != "" {
- dao = dao.Where("plan.created_time < ?", req.EndTime)
- }
- if req.PlanBeginTime != "" {
- dao = dao.Where("plan.plan_datetime > ?", req.PlanBeginTime)
- }
- if req.PlanEndTime != "" {
- dao = dao.Where("plan.plan_datetime < ?", req.PlanEndTime)
- }
- if req.InchargeId != 0 {
- dao = dao.Where("contract.incharge_id = ?", req.InchargeId)
- }
- if req.InchargeName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.InchargeName)
- dao = dao.Where("contract.incharge_name like ?", likestr)
- }
- if req.CustProvinceId != 0 {
- dao = dao.Where("contract.cust_province_id = ?", req.CustProvinceId)
- }
- if req.CustCityId != 0 {
- dao = dao.Where("contract.cust_city_id = ?", req.CustCityId)
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.PageNum != 0 {
- dao = dao.Page(req.GetPage())
- }
- orderby := "plan.plan_datetime desc"
- if req.OrderBy != "" {
- orderby = req.OrderBy
- }
- dao = dao.Order(orderby)
- ents := []*model.CtrContractCollectionPlanListRsp{}
- err = dao.Fields("plan.*, contract.incharge_id, contract.incharge_name, contract.cust_province_id, contract.cust_city_id, contract.cust_province, contract.cust_city").Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- return total, ents, err
- }
- func (s CtrContractCollectionPlanService) Add(ctx context.Context, req *model.CtrContractCollectionPlanAddReq) (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.CtrContractCollectionPlan{
- CustId: c.CustId,
- CustName: c.CustName,
- ContractId: req.ContractId,
- ContractCode: c.ContractCode,
- ContractStatus: "10",
- PlanAmount: req.PlanAmount,
- PlanDatetime: req.PlanDatetime,
- PlanScale: req.PlanScale,
- PlanCondition: req.PlanCondition,
- CashedAmount: 0,
- CashedDatetime: nil,
- 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 {
- planid, err := tx.InsertAndGetId("ctr_contract_collection_plan", ent)
- if err != nil {
- return err
- }
- err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建回款计划", map[string]interface{}{
- "id": planid,
- "planAmount": req.PlanAmount,
- "planDatetime": req.PlanDatetime,
- })
- if err != nil {
- return err
- }
- id = int(planid)
- return nil
- })
- return id, txerr
- }
- func (s CtrContractCollectionPlanService) Update(ctx context.Context, req *model.CtrContractCollectionPlanUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- p, err := s.Dao.Where("id = ?", req.Id).One()
- if err != nil {
- //g.Log().Error(err)
- return err
- }
- if p == 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))
- // }
- // }
- dao := &s.Dao.CtrContractCollectionPlanDao
- toupdate := map[string]interface{}{}
- // if req.ContractId != 0 {
- // toupdate["cust_id"] = c.CustId
- // toupdate["cust_name"] = c.CustName
- // toupdate["contract_id"] = req.ContractId
- // toupdate["contract_code"] = c.ContractCode
- // }
- if req.PlanAmount != nil {
- toupdate["plan_amount"] = *req.PlanAmount
- }
- if req.PlanDatetime != nil {
- toupdate["plan_datetime"] = req.PlanDatetime
- }
- if req.PlanScale != nil {
- toupdate["plan_scale"] = req.PlanScale
- }
- if req.PlanCondition != nil {
- toupdate["plan_condition"] = req.PlanCondition
- }
- // if req.CashedAmount != nil {
- // toupdate["cashed_amount"] = *req.CashedAmount
- // }
- // if req.CashedDatetime != nil {
- // toupdate["cashed_datetime"] = req.CashedDatetime
- // }
- 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 CtrContractCollectionPlanService) UpdateCashedAmount(tx *gdb.TX, id int) error {
- plan := model.CtrContractCollectionPlan{}
- err := tx.GetStruct(&plan, "select * from ctr_contract_collection_plan where id = ?", id)
- if err == sql.ErrNoRows {
- return myerrors.TipsError(fmt.Sprintf("回款计划不存在 %d", id))
- }
- if err != nil {
- return err
- }
- v, err := tx.GetValue("select sum(collection_amount) from ctr_contract_collection where plan_id=? and appro_status='20' and deleted_time is null", id)
- if err != nil {
- return err
- }
- amount := v.Float64()
- var contractStatus string
- if amount < plan.PlanAmount {
- contractStatus = "20"
- } else {
- contractStatus = "30"
- }
- if amount == 0 {
- contractStatus = "10"
- }
- _, err = tx.Update("ctr_contract_collection_plan",
- map[string]interface{}{
- "contract_status": contractStatus,
- "cashed_amount": amount,
- "cashed_datetime": gtime.Now(),
- }, "id = ?", plan.Id)
- if err != nil {
- return err
- }
- return nil
- }
- func (s CtrContractCollectionPlanService) 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.CtrContractCollectionPlan{}
- err := tx.GetStruct(&ent, "select * from ctr_contract_collection_plan where id = ?", i)
- if err == sql.ErrNoRows {
- continue
- }
- if err != nil {
- return err
- }
- contractId[ent.ContractId] = true
- }
- _, err := tx.Delete("ctr_contract_collection_plan", "id in (?)", id)
- if err != nil {
- return err
- }
- _, err = tx.Delete("ctr_contract_collection", "plan_id in (?)", id)
- if err != nil {
- return err
- }
- for cid := range contractId {
- err = s.ctrSrv.UpdateCollectedAmount(tx, cid)
- if err != nil {
- return err
- }
- }
- return nil
- })
- }
|