| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797 |
- package service
- import (
- "context"
- "database/sql"
- "encoding/json"
- "fmt"
- "strconv"
- "time"
- basedao "dashoo.cn/micro/app/dao/base"
- dao "dashoo.cn/micro/app/dao/contract"
- custdao "dashoo.cn/micro/app/dao/cust"
- projdao "dashoo.cn/micro/app/dao/proj"
- model "dashoo.cn/micro/app/model/contract"
- proj "dashoo.cn/micro/app/model/proj"
- workflowModel "dashoo.cn/micro/app/model/workflow"
- "dashoo.cn/micro/app/service"
- projsrv "dashoo.cn/micro/app/service/proj"
- workflowService "dashoo.cn/micro/app/service/workflow"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/plugin/dingtalk/message"
- "dashoo.cn/opms_libary/plugin/dingtalk/workflow"
- "dashoo.cn/opms_libary/request"
- "dashoo.cn/opms_libary/utils"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- )
- type CtrContractService struct {
- Dao *dao.CtrContractDao
- ProjBusinessDao *projdao.ProjBusinessDao
- CustomerDao *custdao.CustCustomerDao
- CtrProductDao *dao.CtrContractProductDao
- ProductDao *basedao.BaseProductDao
- DynamicsDao *dao.CtrContractDynamicsDao
- Tenant string
- userInfo request.UserInfo
- }
- func NewCtrContractService(ctx context.Context) (*CtrContractService, 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 {
- return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- return &CtrContractService{
- Dao: dao.NewCtrContractDao(tenant),
- ProjBusinessDao: projdao.NewProjBusinessDao(tenant),
- CustomerDao: custdao.NewCustCustomerDao(tenant),
- CtrProductDao: dao.NewCtrContractProductDao(tenant),
- ProductDao: basedao.NewBaseProductDao(tenant),
- DynamicsDao: dao.NewCtrContractDynamicsDao(tenant),
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s CtrContractService) Get(ctx context.Context, id int) (*model.CtrContractGetRsp, error) {
- ent, err := s.Dao.Where("Id = ?", id).One()
- if err != nil {
- return nil, err
- }
- if ent == nil {
- return nil, myerrors.TipsError("合同不存在")
- }
- product, err := s.CtrProductDao.Where("contract_id = ?", id).All()
- if err != nil {
- return nil, err
- }
- if product == nil {
- product = []*model.CtrContractProduct{}
- }
- return &model.CtrContractGetRsp{
- CtrContract: *ent,
- Product: product,
- }, nil
- }
- func (s CtrContractService) DynamicsList(ctx context.Context, req *model.CtrContractDynamicsListReq) (int, interface{}, error) {
- dao := &s.DynamicsDao.CtrContractDynamicsDao
- if req.SearchText != "" {
- likestr := fmt.Sprintf("%%%s%%", req.SearchText)
- dao = dao.Where("(opn_people LIKE ? || opn_content LIKE ?)", likestr, likestr)
- }
- if req.ContractId != 0 {
- dao = dao.Where("contract_id = ?", req.ContractId)
- }
- if req.OpnPeopleId != 0 {
- dao = dao.Where("opn_people_id = ?", req.OpnPeopleId)
- }
- if req.OpnPeople != "" {
- likestr := fmt.Sprintf("%%%s%%", req.OpnPeople)
- dao = dao.Where("opn_people like ?", likestr)
- }
- if req.OpnType != "" {
- dao = dao.Where("opn_type = ?", req.OpnType)
- }
- // if req.OpnContent != "" {
- // likestr := fmt.Sprintf("%%%s%%", req.OpnContent)
- // dao = dao.Where("opn_content like ?", likestr)
- // }
- 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.CtrContractDynamics{}
- err = dao.Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- ret := map[string][]*model.CtrContractDynamics{}
- for _, ent := range ents {
- date := ent.OpnDate.Format("Y-m-d")
- ret[date] = append(ret[date], ent)
- }
- return total, ret, err
- }
- func (s CtrContractService) List(ctx context.Context, req *model.CtrContractListReq) (int, []*model.CtrContractListRsp, error) {
- dao := s.Dao.DB.Table("ctr_contract a").
- LeftJoin("cust_customer b", "a.cust_id=b.id").
- Unscoped().Where("a.deleted_time is null")
- if req.SearchText != "" {
- likestr := fmt.Sprintf("%%%s%%", req.SearchText)
- dao = dao.Where("(a.contract_code LIKE ? || a.contract_name LIKE ? || a.cust_name LIKE ? || a.nbo_name LIKE ?)", likestr, likestr, likestr, likestr)
- }
- if req.ContractCode != "" {
- likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
- dao = dao.Where("a.contract_code like ?", likestr)
- }
- if req.ContractName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.ContractName)
- dao = dao.Where("a.contract_name like ?", likestr)
- }
- if req.CustId != 0 {
- dao = dao.Where("a.cust_id = ?", req.CustId)
- }
- if req.CustName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.CustName)
- dao = dao.Where("a.cust_name like ?", likestr)
- }
- if req.NboId != 0 {
- dao = dao.Where("a.nbo_id = ?", req.NboId)
- }
- if req.NboName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.NboName)
- dao = dao.Where("a.nbo_name like ?", likestr)
- }
- if req.ApproStatus != "" {
- dao = dao.Where("a.appro_status = ?", req.ApproStatus)
- }
- if req.ContractType != "" {
- dao = dao.Where("a.contract_type = ?", req.ContractType)
- }
- // if req.ContractStartTime != nil {
- // dao = dao.Where("a.contract_start_time > ?", req.ContractStartTime)
- // }
- // if req.ContractEndTime != nil {
- // dao = dao.Where("a.contract_end_time < ?", req.ContractEndTime)
- // }
- if req.InchargeId != 0 {
- dao = dao.Where("a.incharge_id = ?", req.InchargeId)
- }
- if req.InchargeName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.InchargeName)
- dao = dao.Where("a.incharge_name like ?", likestr)
- }
- if req.SignatoryId != 0 {
- dao = dao.Where("a.signatory_id = ?", req.SignatoryId)
- }
- if req.SignatoryName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.SignatoryName)
- dao = dao.Where("a.signatory_name like ?", likestr)
- }
- if req.DistributorId != 0 {
- dao = dao.Where("a.distributor_id = ?", req.DistributorId)
- }
- if req.DistributorName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.DistributorName)
- dao = dao.Where("a.distributor_name like ?", likestr)
- }
- if req.BeginTime != "" {
- dao = dao.Where("a.created_time > ?", req.BeginTime)
- }
- if req.EndTime != "" {
- dao = dao.Where("a.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 := "a.created_time desc"
- if req.OrderBy != "" {
- orderby = req.OrderBy
- }
- dao = dao.Order(orderby)
- ents := []*model.CtrContractListRsp{}
- err = dao.Fields("a.*, b.cust_province_id as CustProvinceId, b.cust_province as CustProvince, b.cust_city_id as CustCityId, b.cust_city as CustCity").Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- return total, ents, err
- }
- func (s CtrContractService) BindProduct(tx *gdb.TX, id int, product []model.CtrAddProduct) error {
- var amount float64
- for _, p := range product {
- amount += (p.TranPrice * float64(p.ProdNum))
- }
- _, err := tx.Delete("ctr_contract_product", "contract_id = ?", id)
- if err != nil {
- return err
- }
- tocreate := []model.CtrContractProduct{}
- for _, p := range product {
- product, err := s.ProductDao.Where("id = ?", p.ProdId).One()
- if err != nil {
- return err
- }
- if product == nil {
- return myerrors.TipsError(fmt.Sprintf("产品: %d 不存在", p.ProdId))
- }
- tocreate = append(tocreate, model.CtrContractProduct{
- ContractId: id,
- ProdId: p.ProdId,
- ProdCode: product.ProdCode,
- ProdName: product.ProdName,
- ProdClass: product.ProdClass,
- ProdNum: p.ProdNum,
- MaintTerm: p.MaintTerm,
- SugSalesPrice: p.SugSalesPrice,
- TranPrice: p.TranPrice,
- ContractPrive: amount,
- Remark: p.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(),
- })
- }
- if len(tocreate) != 0 {
- _, err = tx.Insert("ctr_contract_product", tocreate)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (s CtrContractService) AddDynamicsByCurrentUser(tx *gdb.TX, contractId int, opnType string, content map[string]interface{}) error {
- contentByte, err := json.Marshal(content)
- if err != nil {
- return err
- }
- _, err = tx.InsertAndGetId("ctr_contract_dynamics", model.CtrContractDynamics{
- ContractId: contractId,
- OpnPeopleId: s.userInfo.Id,
- OpnPeople: s.userInfo.NickName,
- OpnDate: gtime.Now(),
- OpnType: opnType,
- OpnContent: string(contentByte),
- Remark: "",
- CreatedBy: s.userInfo.Id,
- CreatedName: s.userInfo.NickName,
- CreatedTime: gtime.Now(),
- UpdatedBy: s.userInfo.Id,
- UpdatedName: s.userInfo.NickName,
- UpdatedTime: gtime.Now(),
- })
- return err
- }
- func (s CtrContractService) Add(ctx context.Context, req *model.CtrContractAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.TipsError(validErr.Current().Error())
- }
- c, err := s.Dao.Where("contract_name = ?", req.ContractName).One()
- if err != nil {
- return 0, err
- }
- if c != nil {
- return 0, myerrors.TipsError(fmt.Sprintf("合同名称:%s 已存在", req.ContractName))
- }
- nbo, err := s.ProjBusinessDao.Where("id = ?", req.NboId).One()
- if err != nil {
- return 0, err
- }
- if nbo == nil {
- return 0, myerrors.TipsError("项目不存在")
- }
- c, err = s.Dao.Where("nbo_id = ?", req.NboId).One()
- if err != nil {
- return 0, err
- }
- if c != nil {
- return 0, myerrors.TipsError("所选项目已添加合同")
- }
- sequence, err := service.Sequence(s.Dao.DB, "contract_code")
- if err != nil {
- return 0, err
- }
- if req.ContractCode == "" {
- req.ContractCode = fmt.Sprintf("DS%s%s%s", req.ContractType, time.Now().Format("0601"), sequence)
- }
- c, err = s.Dao.Where("contract_code = ?", req.ContractCode).One()
- if err != nil {
- return 0, err
- }
- if c != nil {
- return 0, myerrors.TipsError(fmt.Sprintf("合同编号:%s 已存在", req.ContractCode))
- }
- var contractAmount float64
- for _, p := range req.Product {
- contractAmount += (p.TranPrice * float64(p.ProdNum))
- }
- ctr := model.CtrContract{
- ContractCode: req.ContractCode,
- ContractName: req.ContractName,
- CustId: nbo.CustId,
- CustName: nbo.CustName,
- NboId: nbo.Id,
- NboName: nbo.NboName,
- ApproStatus: "10",
- ContractType: req.ContractType,
- ContractAmount: contractAmount,
- InvoiceAmount: 0,
- CollectedAmount: 0,
- ContractStartTime: req.ContractStartTime,
- ContractEndTime: req.ContractEndTime,
- InchargeId: req.InchargeId,
- InchargeName: req.InchargeName,
- SignatoryId: req.SignatoryId,
- SignatoryName: req.SignatoryName,
- SignatoryType: req.SignatoryType,
- CustSignatoryId: req.CustSignatoryId,
- CustSignatoryName: req.CustSignatoryName,
- DistributorId: req.DistributorId,
- DistributorName: req.DistributorName,
- 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(),
- }
- var id int
- txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- ctrid, err := tx.InsertAndGetId("ctr_contract", ctr)
- if err != nil {
- return err
- }
- err = s.BindProduct(tx, int(ctrid), req.Product)
- if err != nil {
- return err
- }
- err = s.AddDynamicsByCurrentUser(tx, int(ctrid), "创建合同", map[string]interface{}{})
- if err != nil {
- return err
- }
- _, err = tx.Update("proj_business", map[string]interface{}{
- "nbo_type": projsrv.StatusDeal,
- }, "id = ?", nbo.Id)
- if err != nil {
- return err
- }
- id = int(ctrid)
- return nil
- })
- return id, txerr
- }
- var ContractApplyProcessCode = "PROC-7057E20A-2066-4644-9B35-9331E4DA912C" // 创建合同
- func (s CtrContractService) Commit(ctx context.Context, req *model.CtrContractCommitReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- if !(req.ContractModel == "大数模板" || req.ContractModel == "客户模板") {
- return myerrors.TipsError("合同模板不合法")
- }
- if !(req.Terms == "接纳全部条款" || req.Terms == "不接纳全部条款") {
- return myerrors.TipsError("条款情况不合法")
- }
- if req.PayTerms == "" {
- return myerrors.TipsError("付款条件不能为空")
- }
- if len(req.File) == 0 {
- return myerrors.TipsError("附件不能为空")
- }
- 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))
- }
- fileinfoByte, err := json.Marshal(req.File)
- if err != nil {
- return err
- }
- workflowSrv, err := workflowService.NewFlowService(ctx)
- if err != nil {
- return err
- }
- bizCode := strconv.Itoa(ent.Id)
- _, err = workflowSrv.StartProcessInstance(bizCode, "30", "", &workflow.StartProcessInstanceRequest{
- ProcessCode: &ContractApplyProcessCode,
- FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
- {
- Id: utils.String("DDSelectField_ESX8H30W3VK0"),
- Name: utils.String("合同模板"),
- Value: utils.String(req.ContractModel),
- },
- {
- Id: utils.String("DDSelectField_13IQX96C2KAK0"),
- Name: utils.String("条款情况"),
- Value: utils.String(req.Terms),
- },
- {
- Id: utils.String("TextField_1A5SA7VOG5TS0"),
- Name: utils.String("合同编号"),
- Value: utils.String(ent.ContractCode),
- },
- {
- Id: utils.String("TextField_1EX61DPS3LA80"),
- Name: utils.String("客户名称"),
- Value: utils.String(ent.CustName),
- },
- {
- Id: utils.String("MoneyField_X1XV4KIR0GW0"),
- Name: utils.String("合同金额(元)"),
- Value: utils.String(strconv.FormatFloat(ent.ContractAmount, 'f', 2, 64)),
- },
- {
- Id: utils.String("TextareaField_1WZ5ILKBUVSW0"),
- Name: utils.String("付款条件"),
- Value: utils.String(req.PayTerms),
- },
- {
- Id: utils.String("DDAttachment_1051KJYC3MBK0"),
- Name: utils.String("附件"),
- // Details: productForm,
- Value: utils.String(string(fileinfoByte)),
- },
- },
- })
- if err != nil {
- return err
- }
- _, err = s.Dao.Where("id = ?", ent.Id).Data(map[string]interface{}{
- "appro_status": 20,
- }).Update()
- return err
- }
- func ContractApplyApproval(ctx context.Context, flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return fmt.Errorf("获取租户码异常:%s", err.Error())
- }
- contractDao := dao.NewCtrContractDao(tenant)
- contractId, err := strconv.Atoi(flow.BizCode)
- if err != nil {
- return fmt.Errorf("创建合同审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
- }
- contract, err := contractDao.Where("id = ?", contractId).One()
- if err != nil {
- return err
- }
- if contract == nil {
- return fmt.Errorf("合同不存在:%s Id: %d", flow.BizCode, flow.Id)
- }
- if msg.ProcessType != "finish" && msg.ProcessType != "terminate" {
- return fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
- }
- if msg.Result != "agree" && msg.Result != "refuse" && msg.Result != "" {
- return fmt.Errorf("无法识别的 Result :%s", msg.Result)
- }
- if msg.ProcessType == "terminate" {
- _, err = contractDao.Where("id = ?", contractId).Data(map[string]interface{}{
- "appro_status": "50",
- }).Update()
- return err
- }
- pass := msg.Result == "agree"
- if !pass {
- _, err = contractDao.Where("id = ?", contractId).Data(map[string]interface{}{
- "appro_status": "40",
- }).Update()
- return err
- }
- _, err = contractDao.Where("id = ?", contractId).Data(map[string]interface{}{
- "appro_status": "30",
- }).Update()
- return err
- }
- func (s CtrContractService) Update(ctx context.Context, req *model.CtrContractUpdateReq) 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.ContractCode != "" {
- // exist, err := s.Dao.Where("contract_code = ?", req.ContractCode).One()
- // if err != nil {
- // return err
- // }
- // if exist != nil && exist.Id != req.Id {
- // return myerrors.NewMsgError(nil, fmt.Sprintf("合同编号:%s 已存在", req.ContractCode))
- // }
- // }
- if req.ContractName != "" {
- exist, err := s.Dao.Where("contract_name = ?", req.ContractName).One()
- if err != nil {
- return err
- }
- if exist != nil && exist.Id != req.Id {
- return myerrors.TipsError(fmt.Sprintf("合同名称:%s 已存在", req.ContractName))
- }
- }
- var nbo *proj.ProjBusiness
- if req.NboId != 0 {
- nbo, err = s.ProjBusinessDao.Where("id = ?", req.NboId).One()
- if err != nil {
- return err
- }
- if nbo == nil {
- return myerrors.TipsError("项目不存在")
- }
- }
- toupdate := map[string]interface{}{}
- // if req.ContractCode != "" {
- // toupdate["contract_code"] = req.ContractCode
- // }
- if req.ContractName != "" {
- toupdate["contract_name"] = req.ContractName
- }
- if req.NboId != 0 {
- toupdate["cust_id"] = nbo.CustId
- toupdate["cust_name"] = nbo.CustName
- toupdate["nbo_id"] = nbo.Id
- toupdate["nbo_name"] = nbo.NboName
- }
- // if req.ApproStatus != "" {
- // toupdate["appro_status"] = req.ApproStatus
- // }
- if req.ContractType != "" {
- toupdate["contract_type"] = req.ContractType
- }
- // if req.ContractAmount != 0 {
- // toupdate["contract_amount"] = req.ContractAmount
- // }
- // if req.InvoiceAmount != 0 {
- // toupdate["invoice_amount"] = req.InvoiceAmount
- // }
- // if req.CollectedAmount != 0 {
- // toupdate["collected_amount"] = req.CollectedAmount
- // }
- if req.ContractStartTime != nil {
- toupdate["contract_start_time"] = req.ContractStartTime
- }
- if req.ContractEndTime != nil {
- toupdate["contract_end_time"] = req.ContractEndTime
- }
- if req.InchargeId != 0 {
- toupdate["incharge_id"] = req.InchargeId
- }
- if req.InchargeName != "" {
- toupdate["incharge_name"] = req.InchargeName
- }
- if req.SignatoryId != 0 {
- toupdate["signatory_id"] = req.SignatoryId
- }
- if req.SignatoryName != "" {
- toupdate["signatory_name"] = req.SignatoryName
- }
- if req.SignatoryType != "" {
- toupdate["signatory_type"] = req.SignatoryType
- }
- if req.CustSignatoryId != 0 {
- toupdate["cust_signatory_id"] = req.CustSignatoryId
- }
- if req.CustSignatoryName != "" {
- toupdate["cust_signatory_name"] = req.CustSignatoryName
- }
- if req.DistributorId != 0 {
- toupdate["distributor_id"] = req.DistributorId
- }
- if req.DistributorName != "" {
- toupdate["distributor_name"] = req.DistributorName
- }
- if req.Remark != nil {
- toupdate["remark"] = *req.Remark
- }
- if req.Product != nil {
- var contractAmount float64
- for _, p := range *req.Product {
- contractAmount += (p.TranPrice * float64(p.ProdNum))
- }
- toupdate["contract_amount"] = contractAmount
- }
- 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", toupdate, "id = ?", req.Id)
- if err != nil {
- return err
- }
- if req.Product != nil {
- err = s.BindProduct(tx, req.Id, *req.Product)
- if err != nil {
- return err
- }
- }
- return nil
- })
- if txerr != nil {
- return txerr
- }
- }
- return nil
- }
- func (s CtrContractService) Transfer(ctx context.Context, req *model.CtrContractTransferReq) error {
- if len(req.Id) == 0 {
- return nil
- }
- ents := map[int]*model.CtrContract{}
- for _, i := range req.Id {
- ent, err := s.Dao.Where("id = ?", i).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.Id))
- }
- ents[ent.Id] = ent
- }
- txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- toupdate := map[string]interface{}{
- "incharge_id": req.InchargeId,
- "incharge_name": req.InchargeName,
- }
- _, err := tx.Update("ctr_contract", toupdate, "id in (?)", req.Id)
- if err != nil {
- return err
- }
- for _, ent := range ents {
- err = s.AddDynamicsByCurrentUser(tx, ent.Id, "转移合同", map[string]interface{}{
- "toInchargeId": req.InchargeId,
- "toInchargeName": req.InchargeName,
- "fromInchargeId": ent.InchargeId,
- "fromInchargeName": ent.InchargeName,
- "operatedId": s.userInfo.Id,
- "operatedName": s.userInfo.NickName,
- })
- if err != nil {
- return err
- }
- }
- return nil
- })
- if txerr != nil {
- return txerr
- }
- return nil
- }
- func (s CtrContractService) UpdateInvoiceAmount(tx *gdb.TX, id int) error {
- ctr := model.CtrContract{}
- err := tx.GetStruct(&ctr, "select * from ctr_contract 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(invoice_amount) from ctr_contract_invoice where contract_id=? and appro_status='30' and deleted_time is null", id)
- if err != nil {
- return err
- }
- amount := v.Float64()
- _, err = tx.Update("ctr_contract",
- map[string]interface{}{
- "invoice_amount": amount,
- }, "id = ?", id)
- if err != nil {
- return err
- }
- return nil
- }
- func (s CtrContractService) UpdateCollectedAmount(tx *gdb.TX, id int) error {
- ctr := model.CtrContract{}
- err := tx.GetStruct(&ctr, "select * from ctr_contract 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 contract_id=? and appro_status='20' and deleted_time is null", id)
- if err != nil {
- return err
- }
- amount := v.Float64()
- _, err = tx.Update("ctr_contract",
- map[string]interface{}{
- "collected_amount": amount,
- }, "id = ?", id)
- if err != nil {
- return err
- }
- return nil
- }
- func (s CtrContractService) Delete(ctx context.Context, id []int) error {
- if len(id) == 0 {
- return nil
- }
- _, err := s.Dao.Where("Id IN (?)", id).Delete()
- return err
- }
|