| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783 |
- package service
- import (
- "context"
- worksrv "dashoo.cn/micro/app/service/work"
- "database/sql"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "github.com/gogf/gf/util/gconv"
- "io"
- "net/http"
- "strconv"
- "strings"
- "sync"
- "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"
- workflowdao "dashoo.cn/micro/app/dao/workflow"
- 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"
- baseService "dashoo.cn/micro/app/service/base"
- projsrv "dashoo.cn/micro/app/service/proj"
- workflowService "dashoo.cn/micro/app/service/workflow"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/multipart"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/plugin/dingtalk"
- "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/frame/g"
- "github.com/gogf/gf/os/gcron"
- "github.com/gogf/gf/os/glog"
- "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
- WorkflowDao *workflowdao.PlatWorkflowDao
- AppendDao *dao.CtrContractAppendDao
- GoalDao *dao.CtrContractGoalDao
- ShareDao *dao.CtrContractShareDao
- TargetDao *dao.CtrContractSaleTargetDao
- Tenant string
- userInfo request.UserInfo
- DataScope g.Map `json:"dataScope"`
- }
- var mtx sync.Mutex
- 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),
- WorkflowDao: workflowdao.NewPlatWorkflowDao(tenant),
- AppendDao: dao.NewCtrContractAppendDao(tenant),
- GoalDao: dao.NewCtrContractGoalDao(tenant),
- ShareDao: dao.NewCtrContractShareDao(tenant),
- TargetDao: dao.NewCtrContractSaleTargetDao(tenant),
- Tenant: tenant,
- userInfo: userInfo,
- DataScope: userInfo.DataScope,
- }, 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) {
- ctx = context.WithValue(ctx, "contextService", s)
- dao := s.Dao.DataScope(ctx, "incharge_id").As("a")
- 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)
- }
- if req.ContractEndTimeStart != "" {
- dao = dao.Where("a.contract_end_time >= ?", req.ContractEndTimeStart)
- }
- if req.ContractEndTimeEnd != "" {
- dao = dao.Where("a.contract_end_time <= ?", req.ContractEndTimeEnd)
- }
- if req.ContractSignTimeStart != "" {
- dao = dao.Where("a.contract_sign_time >= ?", req.ContractSignTimeStart)
- }
- if req.ContractSignTimeEnd != "" {
- dao = dao.Where("a.contract_sign_time <= ?", req.ContractSignTimeEnd)
- }
- if req.CustProvinceId != 0 {
- dao = dao.Where("a.cust_province_id = ?", req.CustProvinceId)
- }
- if req.CustCityId != 0 {
- dao = dao.Where("a.cust_city_id = ?", req.CustCityId)
- }
- if req.ProductLine != "" {
- dao = dao.Where("a.product_line = ?", req.ProductLine)
- }
- if req.SoftwareMaintenanceBeginTimeStart != nil {
- dao = dao.Where("DATE(a.software_maintenance_begin_time) >= DATE(?)", req.SoftwareMaintenanceBeginTimeStart)
- }
- if req.SoftwareMaintenanceBeginTimeEnd != nil {
- dao = dao.Where("DATE(a.software_maintenance_begin_time) <= DATE(?)", req.SoftwareMaintenanceBeginTimeEnd)
- }
- if req.SoftwareMaintenanceEndTimeStart != nil {
- dao = dao.Where("DATE(a.software_maintenance_end_time) >= DATE(?)", req.SoftwareMaintenanceEndTimeStart)
- }
- if req.SoftwareMaintenanceEndTimeEnd != nil {
- dao = dao.Where("DATE(a.software_maintenance_end_time) <= DATE(?)", req.SoftwareMaintenanceEndTimeEnd)
- }
- if req.HardwareMaintenanceBeginTimeStart != nil {
- dao = dao.Where("DATE(a.hardware_maintenance_begin_time) >= DATE(?)", req.HardwareMaintenanceBeginTimeStart)
- }
- if req.HardwareMaintenanceBeginTimeEnd != nil {
- dao = dao.Where("DATE(a.hardware_maintenance_begin_time) <= DATE(?)", req.HardwareMaintenanceBeginTimeEnd)
- }
- if req.HardwareMaintenanceEndTimeStart != nil {
- dao = dao.Where("DATE(a.hardware_maintenance_end_time) >= DATE(?)", req.HardwareMaintenanceEndTimeStart)
- }
- if req.HardwareMaintenanceEndTimeEnd != nil {
- dao = dao.Where("DATE(a.hardware_maintenance_end_time) <= DATE(?)", req.HardwareMaintenanceEndTimeEnd)
- }
- if req.IsNotMaintenanceContract == "10" {
- dao = dao.Where("a.contract_id = 0 or a.contract_id is NULL")
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.PageNum != 0 {
- dao = dao.Page(req.GetPage())
- }
- orderby := "a.contract_sign_time desc"
- if req.OrderBy != "" {
- orderby = req.OrderBy
- }
- dao = dao.Order(orderby)
- ents := []*model.CtrContractListRsp{}
- err = dao.Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- return total, ents, err
- }
- func (s CtrContractService) RenewList(ctx context.Context, req *model.CtrRenewContractListReq) ([]*model.CtrContractListRsp, error) {
- ctx = context.WithValue(ctx, "contextService", s)
- dao := s.Dao.DataScope(ctx, "incharge_id").As("a").Where("contract_id=?", req.ContractId)
- 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)
- }
- orderby := "a.contract_sign_time desc"
- dao = dao.Order(orderby)
- ents := []*model.CtrContractListRsp{}
- err := dao.Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return nil, err
- }
- return ents, err
- }
- func (s CtrContractService) BindProduct(tx *gdb.TX, id int, code string, 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,
- ContractCode: code,
- 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())
- }
- if len(req.Product) == 0 {
- return 0, myerrors.TipsError("产品不能为空")
- }
- for _, p := range req.Product {
- if p.ProdNum < 1 {
- return 0, myerrors.TipsError("产品数量必须大于 0")
- }
- }
- // 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("所选项目已添加合同")
- // }
- var sequence int
- if req.ContractType == "XS" { // 销售合同
- sequence, err = service.SequenceYearRest(s.Dao.DB, "contract_code_xs")
- if err != nil {
- return 0, err
- }
- } else if req.ContractType == "JS" { // 技术合同
- sequence, err = service.SequenceYearRest(s.Dao.DB, "contract_code_js")
- if err != nil {
- return 0, err
- }
- } else {
- return 0, myerrors.TipsError("不支持的合同类型")
- }
- if req.ContractCode == "" {
- req.ContractCode = fmt.Sprintf("DH%s%s-%03d", 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,
- IsBig: nbo.IsBig,
- ProductLine: nbo.ProductLine,
- CustProvinceId: nbo.CustProvinceId,
- CustProvince: nbo.CustProvince,
- CustCityId: nbo.CustCityId,
- CustCity: nbo.CustCity,
- ApproStatus: "10",
- ContractType: req.ContractType,
- ContractAmount: contractAmount,
- InvoiceAmount: 0,
- CollectedAmount: 0,
- ContractStartTime: req.ContractStartTime,
- ContractEndTime: req.ContractEndTime,
- ContractSignTime: req.ContractSignTime,
- InchargeId: req.InchargeId,
- InchargeName: req.InchargeName,
- SignatoryId: req.SignatoryId,
- SignatoryName: req.SignatoryName,
- SignatoryType: req.SignatoryType,
- SignatoryUnit: req.SignatoryUnit,
- EarnestMoney: req.EarnestMoney,
- CustSignatoryId: req.CustSignatoryId,
- CustSignatoryName: req.CustSignatoryName,
- DistributorId: req.DistributorId,
- DistributorName: req.DistributorName,
- Remark: req.Remark,
- ServiceFeeAgreement: req.ServiceFeeAgreement,
- SoftwareMaintenanceLimit: req.SoftwareMaintenanceLimit,
- HardwareMaintenanceLimit: req.HardwareMaintenanceLimit,
- MaintenanceClause: req.MaintenanceClause,
- 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 {
- ctrid, err := tx.InsertAndGetId("ctr_contract", ctr)
- if err != nil {
- return err
- }
- err = s.BindProduct(tx, int(ctrid), ctr.ContractCode, 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
- }
- func (s CtrContractService) ReNew(ctx context.Context, req *model.CtrContractReNewReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.TipsError(validErr.Current().Error())
- }
- if len(req.Product) == 0 {
- return 0, myerrors.TipsError("产品不能为空")
- }
- for _, p := range req.Product {
- if p.ProdNum < 1 {
- return 0, myerrors.TipsError("产品数量必须大于 0")
- }
- }
- nbo, err := s.ProjBusinessDao.Where("id = ?", req.NboId).One()
- if err != nil {
- return 0, err
- }
- if nbo == nil {
- return 0, myerrors.TipsError("项目不存在")
- }
- oldContract, err := s.Dao.Where("id = ?", req.ContractId).One()
- if err != nil {
- return 0, err
- }
- if oldContract == nil {
- return 0, myerrors.TipsError("合同不存在")
- }
- var sequence int
- if req.ContractType == "XS" { // 销售合同
- sequence, err = service.SequenceYearRest(s.Dao.DB, "contract_code_xs")
- if err != nil {
- return 0, err
- }
- } else if req.ContractType == "JS" { // 技术合同
- sequence, err = service.SequenceYearRest(s.Dao.DB, "contract_code_js")
- if err != nil {
- return 0, err
- }
- } else {
- return 0, myerrors.TipsError("不支持的合同类型")
- }
- if req.ContractCode == "" {
- req.ContractCode = fmt.Sprintf("DH%s%s-%03d", 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))
- }
- softSub := req.SoftwareMaintenanceEndTime.Sub(req.SoftwareMaintenanceBeginTime).Hours() / 24 / 365
- req.SoftwareMaintenanceLimit = int(softSub)
- hardSub := req.HardwareMaintenanceEndTime.Sub(req.HardwareMaintenanceBeginTime).Hours() / 24 / 365
- req.HardwareMaintenanceLimit = int(hardSub)
- ctr := model.CtrContract{
- ContractCode: req.ContractCode,
- ContractName: req.ContractName,
- CustId: nbo.CustId,
- CustName: nbo.CustName,
- ContractId: req.ContractId,
- NboId: nbo.Id,
- NboName: nbo.NboName,
- IsBig: nbo.IsBig,
- ProductLine: nbo.ProductLine,
- CustProvinceId: nbo.CustProvinceId,
- CustProvince: nbo.CustProvince,
- CustCityId: nbo.CustCityId,
- CustCity: nbo.CustCity,
- ApproStatus: "10",
- ContractType: req.ContractType,
- ContractAmount: contractAmount,
- InvoiceAmount: 0,
- CollectedAmount: 0,
- ContractStartTime: req.ContractStartTime,
- ContractEndTime: req.ContractEndTime,
- ContractSignTime: req.ContractSignTime,
- InchargeId: req.InchargeId,
- InchargeName: req.InchargeName,
- SignatoryId: req.SignatoryId,
- SignatoryName: req.SignatoryName,
- SignatoryType: req.SignatoryType,
- SignatoryUnit: req.SignatoryUnit,
- EarnestMoney: req.EarnestMoney,
- CustSignatoryId: req.CustSignatoryId,
- CustSignatoryName: req.CustSignatoryName,
- DistributorId: req.DistributorId,
- DistributorName: req.DistributorName,
- Remark: req.Remark,
- ServiceFeeAgreement: req.ServiceFeeAgreement,
- SoftwareMaintenanceBeginTime: req.SoftwareMaintenanceBeginTime,
- SoftwareMaintenanceEndTime: req.SoftwareMaintenanceEndTime,
- HardwareMaintenanceBeginTime: req.HardwareMaintenanceBeginTime,
- HardwareMaintenanceEndTime: req.HardwareMaintenanceEndTime,
- SoftwareMaintenanceLimit: req.SoftwareMaintenanceLimit,
- HardwareMaintenanceLimit: req.HardwareMaintenanceLimit,
- MaintenanceClause: req.MaintenanceClause,
- 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 {
- ctrid, err := tx.InsertAndGetId("ctr_contract", ctr)
- if err != nil {
- return err
- }
- err = s.BindProduct(tx, int(ctrid), ctr.ContractCode, req.Product)
- if err != nil {
- return err
- }
- //ContractApplyApproval
- 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
- }
- err = s.AddDynamicsByCurrentUser(tx, int(req.ContractId), "创建续签合同", map[string]interface{}{
- "contractName": req.ContractName,
- })
- if err != nil {
- return err
- }
- id = int(ctrid)
- return nil
- })
- return id, txerr
- }
- func (s CtrContractService) UpdateReNew(ctx context.Context, req *model.CtrContractUpdateReNewReq) 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.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.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
- toupdate["is_big"] = nbo.IsBig
- toupdate["product_line"] = nbo.ProductLine
- toupdate["cust_province_id"] = nbo.CustProvinceId
- toupdate["cust_province"] = nbo.CustProvince
- toupdate["cust_city_id"] = nbo.CustCityId
- toupdate["cust_city"] = nbo.CustCity
- }
- if req.ContractType != "" {
- toupdate["contract_type"] = req.ContractType
- }
- if req.ContractStartTime != nil {
- toupdate["contract_start_time"] = req.ContractStartTime
- }
- if req.ContractEndTime != nil {
- toupdate["contract_end_time"] = req.ContractEndTime
- }
- if req.ContractSignTime != nil {
- toupdate["contract_sign_time"] = req.ContractSignTime
- }
- 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.SignatoryUnit != "" {
- toupdate["signatory_unit"] = req.SignatoryUnit
- }
- if req.EarnestMoney != 0 {
- toupdate["earnest_money"] = req.EarnestMoney
- }
- 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.SoftwareMaintenanceBeginTime != nil {
- toupdate["software_maintenance_begin_time"] = req.SoftwareMaintenanceBeginTime
- }
- if req.SoftwareMaintenanceEndTime != nil {
- toupdate["software_maintenance_end_time"] = req.SoftwareMaintenanceEndTime
- }
- if req.HardwareMaintenanceBeginTime != nil {
- toupdate["hardware_maintenance_begin_time"] = req.HardwareMaintenanceBeginTime
- }
- if req.HardwareMaintenanceEndTime != nil {
- toupdate["hardware_maintenance_end_time"] = req.HardwareMaintenanceEndTime
- }
- softSub := req.SoftwareMaintenanceEndTime.Sub(req.SoftwareMaintenanceBeginTime).Hours() / 24 / 365
- req.SoftwareMaintenanceLimit = int(softSub)
- hardSub := req.HardwareMaintenanceEndTime.Sub(req.HardwareMaintenanceBeginTime).Hours() / 24 / 365
- req.HardwareMaintenanceLimit = int(hardSub)
- if req.SoftwareMaintenanceLimit != 0 {
- toupdate["software_maintenance_limit"] = req.SoftwareMaintenanceLimit
- }
- if req.HardwareMaintenanceLimit != 0 {
- toupdate["hardware_maintenance_limit"] = req.HardwareMaintenanceLimit
- }
- if req.Remark != "" {
- 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, ent.ContractCode, req.Product)
- if err != nil {
- return err
- }
- }
- return nil
- })
- if txerr != nil {
- return txerr
- }
- }
- return nil
- }
- 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
- }
- // var spaceId = "21042518430"
- var spaceId = "21077726250"
- func (s CtrContractService) CommitWithFile(ctx context.Context, formData *multipart.Form) error {
- if s.userInfo.DingtalkUid == "" {
- return fmt.Errorf("该用户钉钉 uid 为空")
- }
- if len(formData.File) == 0 {
- return myerrors.TipsError("文件不能为空")
- }
- if _, ok := formData.File["file"]; !ok {
- return myerrors.TipsError("文件不能为空")
- }
- if formData.File["file"].FileName == "" {
- return fmt.Errorf("文件名称不能为空")
- }
- if formData.File["file"].File == nil {
- return fmt.Errorf("文件不能为空")
- }
- if formData.File["file"].File.Name() == "" {
- return fmt.Errorf("文件路径不能为空")
- }
- contractId, err := strconv.Atoi(formData.Value["contractId"])
- if err != nil {
- return fmt.Errorf("合同 Id 不合法 %s", formData.Value["contractId"])
- }
- //fmt.Println(args.File.Name(), args.FileName, args.FileSize, args.Meta)
- //fmt.Println(spaceId, s.userInfo.DingtalkId, args.FileName, args.File.Name())
- // resp, err := s.UploadFile("21042518430", "8xljy04PZiS9iPxp5PhDnUzQiEiE", "引物导入模板-000000.xlsx", "/Users/chengjian/Downloads/引物导入模板.xlsx")
- resp, err := dingtalk.Client.GetStorage().UploadFile(spaceId, s.userInfo.DingtalkId, formData.File["file"].FileName, formData.File["file"].File.Name())
- if err != nil {
- return fmt.Errorf("钉钉上传文件异常 %s", err.Error())
- }
- err = s.Commit(ctx, &model.CtrContractCommitReq{
- Id: contractId,
- ContractModel: formData.Value["contractModel"],
- Terms: formData.Value["terms"],
- PayTerms: formData.Value["payTerms"],
- File: []model.DingFileInfo{
- {
- SpaceId: resp.Dentry.SpaceId,
- FileId: resp.Dentry.Id,
- FileName: resp.Dentry.Name,
- FileSize: resp.Dentry.Size,
- FileType: resp.Dentry.Extension,
- },
- },
- })
- if err != nil {
- return err
- }
- // workflow, err := s.WorkflowDao.Where("id = ?", workflowId).One()
- // if err != nil {
- // return err
- // }
- // instance, err := dingtalk.Client.GetWorkflow().QueryProcessInstanceDetail(workflow.ProcessInstId)
- // if err != nil {
- // return fmt.Errorf("查询审批实例详情错误: %s", err.Error())
- // }
- // fmt.Println(workflow.ProcessInstId, instance.Result.ApproverUserIds)
- // approverUserIds := g.Config().GetStrings("dingtalk.approver-user-ids")
- // fmt.Println(approverUserIds)
- // for _, uid := range approverUserIds {
- // resp, err := dingtalk.Client.GetStorage().AddPermission(
- // dingtalk.Client.Context.CorpId, spaceId, uid, "DOWNLOADER", "USER")
- // if err != nil {
- // return fmt.Errorf("添加审核附件权限异常: %s", err.Error())
- // }
- // fmt.Println(uid, resp)
- // }
- appendSrv, err := NewCtrContractAppendService(ctx)
- if err != nil {
- return err
- }
- _, err = appendSrv.Add(ctx, &model.CtrContractAppendAddReq{
- ContractId: contractId,
- FileName: resp.Dentry.Name,
- FileType: resp.Dentry.Extension,
- FileUrl: strings.Join([]string{"dingtalk", resp.Dentry.SpaceId, resp.Dentry.Id}, ":"),
- Remark: "",
- })
- if err != nil {
- return err
- }
- return nil
- }
- func (s CtrContractService) CommitWithFileUrl(ctx context.Context, req *model.CtrContractCommitWithFileUrlReq) error {
- // 数据校验
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- // 校验销售分成数据
- if len(req.Shares) == 0 {
- return myerrors.TipsError("销售分成信息不能为空")
- }
- fileinfoByte, err := baseService.UploadDingtalk(s.userInfo.DingtalkId, req.FileUrl, req.FileName)
- if err != nil {
- return err
- }
- var fileinfo = []model.DingFileInfo{}
- err = json.Unmarshal(fileinfoByte, &fileinfo)
- if err != nil {
- return err
- }
- err = s.Commit(ctx, &model.CtrContractCommitReq{
- Id: req.Id,
- ContractModel: req.ContractModel,
- Terms: req.Terms,
- PayTerms: req.PayTerms,
- File: fileinfo,
- })
- if err != nil {
- return err
- }
- appendSrv, err := NewCtrContractAppendService(ctx)
- if err != nil {
- return err
- }
- _, err = appendSrv.Add(ctx, &model.CtrContractAppendAddReq{
- ContractId: req.Id,
- FileName: fileinfo[0].FileName,
- FileType: fileinfo[0].FileType,
- FileUrl: strings.Join([]string{"dingtalk", fileinfo[0].SpaceId, fileinfo[0].FileId}, ":"),
- Remark: "",
- })
- if err != nil {
- return err
- }
- // 保存销售分成信息
- if len(req.Shares) > 0 {
- saleIds := ""
- amountMap := make(map[int]float64)
- for _, share := range req.Shares {
- share.CreatedBy = s.userInfo.Id
- share.CreatedName = s.userInfo.NickName
- share.CreatedTime = gtime.Now()
- share.UpdatedBy = s.userInfo.Id
- share.UpdatedName = s.userInfo.NickName
- share.UpdatedTime = gtime.Now()
- amountMap[share.SaleId] += share.ShareAmount
- if saleIds == "" {
- saleIds += gconv.String(share.SaleId)
- } else {
- saleIds += "," + gconv.String(share.SaleId)
- }
- }
- year := req.Shares[0].ContractSignTime.Format("Y")
- month := req.Shares[0].ContractSignTime.Format("m")
- // 查询需要更新的销售签约指标
- if saleIds != "" {
- targets, err := s.TargetDao.Where(fmt.Sprintf("sale_id IN (%v) AND year='%v'", saleIds, year)).FindAll()
- if err != nil {
- return err
- }
- // 更新数据
- for _, target := range targets {
- switch month {
- case "01":
- target.Complete1 += amountMap[target.SaleId]
- target.QuarterComplete1 = target.Complete1 + target.Complete2 + target.Complete3
- if target.QuarterPlan1 != 0 {
- target.QuarterCompleteRatio1 = target.QuarterComplete1 / target.QuarterPlan1 * 100
- }
- break
- case "02":
- target.Complete2 += amountMap[target.SaleId]
- target.QuarterComplete1 = target.Complete1 + target.Complete2 + target.Complete3
- if target.QuarterPlan1 != 0 {
- target.QuarterCompleteRatio1 = target.QuarterComplete1 / target.QuarterPlan1 * 100
- }
- break
- case "03":
- target.Complete3 += amountMap[target.SaleId]
- target.QuarterComplete1 = target.Complete1 + target.Complete2 + target.Complete3
- if target.QuarterPlan1 != 0 {
- target.QuarterCompleteRatio1 = target.QuarterComplete1 / target.QuarterPlan1 * 100
- }
- break
- case "04":
- target.Complete4 += amountMap[target.SaleId]
- target.QuarterComplete2 = target.Complete4 + target.Complete5 + target.Complete6
- if target.QuarterPlan2 != 0 {
- target.QuarterCompleteRatio2 = target.QuarterComplete2 / target.QuarterPlan2 * 100
- }
- break
- case "05":
- target.Complete5 += amountMap[target.SaleId]
- target.QuarterComplete2 = target.Complete4 + target.Complete5 + target.Complete6
- if target.QuarterPlan2 != 0 {
- target.QuarterCompleteRatio2 = target.QuarterComplete2 / target.QuarterPlan2 * 100
- }
- break
- case "06":
- target.Complete6 += amountMap[target.SaleId]
- target.QuarterComplete2 = target.Complete4 + target.Complete5 + target.Complete6
- if target.QuarterPlan2 != 0 {
- target.QuarterCompleteRatio2 = target.QuarterComplete2 / target.QuarterPlan2 * 100
- }
- break
- case "07":
- target.Complete7 += amountMap[target.SaleId]
- target.QuarterComplete3 = target.Complete7 + target.Complete8 + target.Complete9
- if target.QuarterPlan3 != 0 {
- target.QuarterCompleteRatio3 = target.QuarterComplete3 / target.QuarterPlan3 * 100
- }
- break
- case "08":
- target.Complete8 += amountMap[target.SaleId]
- target.QuarterComplete3 = target.Complete7 + target.Complete8 + target.Complete9
- if target.QuarterPlan3 != 0 {
- target.QuarterCompleteRatio3 = target.QuarterComplete3 / target.QuarterPlan3 * 100
- }
- break
- case "09":
- target.Complete9 += amountMap[target.SaleId]
- target.QuarterComplete3 = target.Complete7 + target.Complete8 + target.Complete9
- if target.QuarterPlan3 != 0 {
- target.QuarterCompleteRatio3 = target.QuarterComplete3 / target.QuarterPlan3 * 100
- }
- break
- case "10":
- target.Complete10 += amountMap[target.SaleId]
- break
- case "11":
- target.Complete11 += amountMap[target.SaleId]
- break
- case "12":
- target.Complete12 += amountMap[target.SaleId]
- break
- }
- target.UpdatedTime = gtime.Now()
- target.UpdatedBy = s.userInfo.Id
- target.UpdatedName = s.userInfo.NickName
- // 年度数据
- target.YearComplete = target.QuarterComplete1 + target.QuarterComplete2 + target.QuarterComplete3 + target.Complete10 + target.Complete11 + target.Complete12
- if target.YearPlan != 0 {
- target.YearCompleteRatio = target.YearComplete / target.YearPlan * 100
- }
- }
- // 保存销售签约指标数据
- if len(targets) > 0 {
- _, err = s.TargetDao.Save(targets)
- if err != nil {
- return err
- }
- }
- }
- // 保存分成数据
- _, err = s.ShareDao.Save(req.Shares)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (s CtrContractService) DownloadDingtalkFile(ctx context.Context, id int) (string, error) {
- ent, err := s.AppendDao.Where("id= ?", id).One()
- if err != nil {
- return "", err
- }
- if ent == nil {
- return "", myerrors.TipsError("附件不存在")
- }
- if !strings.HasPrefix(ent.FileUrl, "dingtalk") {
- return "", myerrors.TipsError("此附件不是钉钉附件")
- }
- fileInfo := strings.Split(ent.FileUrl, ":")
- if len(fileInfo) != 3 {
- return "", myerrors.TipsError("钉钉附件地址不合法")
- }
- spaceId := fileInfo[1]
- fileId := fileInfo[2]
- // res, err := dingtalk.Client.GetStorage().AddPermission(dingtalk.Client.Context.CorpId, spaceId, s.userInfo.DingtalkId, "EDITOR", "USER")
- // fmt.Println(res, err)
- // if err != nil {
- // return "", fmt.Errorf("设置权限异常 %s", err.Error())
- // }
- resp, err := dingtalk.Client.GetStorage().QueryFileDownloadInfo(spaceId, fileId, s.userInfo.DingtalkId)
- if err != nil {
- return "", myerrors.TipsError("获取文件下载信息异常")
- }
- fmt.Println(resp, err)
- req, err := http.NewRequest("GET", resp.HeaderSignatureInfo.ResourceUrls[0], nil)
- if err != nil {
- return "", fmt.Errorf("构建文件下载请求异常 %s", err.Error())
- }
- for k, v := range resp.HeaderSignatureInfo.Headers {
- req.Header.Add(k, v)
- }
- fileresp, err := http.DefaultClient.Do(req)
- if err != nil {
- return "", fmt.Errorf("文件下载异常 %s", err.Error())
- }
- data, err := io.ReadAll(fileresp.Body)
- if err != nil {
- return "", fmt.Errorf("读取下载内容异常 %s", err.Error())
- }
- return base64.StdEncoding.EncodeToString(data), nil
- }
- func ContractApplyApproval(ctx context.Context, flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
- mtx.Lock()
- defer mtx.Unlock()
- 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)
- }
- var status string
- if msg.ProcessType == "terminate" {
- status = "50"
- } else {
- pass := msg.Result == "agree"
- if !pass {
- status = "40"
- } else {
- status = "30"
- }
- }
- _, err = contractDao.Where("id = ?", contractId).Data(map[string]interface{}{
- "appro_status": status,
- }).Update()
- if err != nil {
- return err
- }
- if status != "30" {
- return nil
- }
- return contractDao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- // 交付状态(0发起 10项目立项 15进行中 20 完成 30审批拒绝40关闭)
- _, err = worksrv.DeliverOrderAdd(tx, contractId, request.UserInfo{}, nil, "10")
- if err != nil {
- return err
- }
- if contract.ContractId != 0 {
- oldContract, err := contractDao.TX(tx).Where("id = ?", contract.ContractId).One()
- if err != nil {
- return err
- }
- softSub := contract.SoftwareMaintenanceEndTime.Sub(oldContract.SoftwareMaintenanceBeginTime).Hours() / 24 / 365
- SoftwareMaintenanceLimit := int(softSub)
- hardSub := contract.HardwareMaintenanceEndTime.Sub(oldContract.HardwareMaintenanceBeginTime).Hours() / 24 / 365
- HardwareMaintenanceLimit := int(hardSub)
- opnContent := map[string]interface{}{
- "contractId": contract.ContractId,
- "contractName": contract.ContractName,
- "contractCode": contract.ContractCode,
- "oldSoftwareMaintenanceBeginTime": oldContract.SoftwareMaintenanceBeginTime,
- "oldSoftwareMaintenanceEndTime": oldContract.SoftwareMaintenanceEndTime,
- "oldHardwareMaintenanceBeginTime": oldContract.HardwareMaintenanceBeginTime,
- "oldHardwareMaintenanceEndTime": oldContract.HardwareMaintenanceEndTime,
- "newSoftwareMaintenanceBeginTime": contract.SoftwareMaintenanceBeginTime,
- "newSoftwareMaintenanceEndTime": contract.SoftwareMaintenanceEndTime,
- "newHardwareMaintenanceBeginTime": contract.HardwareMaintenanceBeginTime,
- "newHardwareMaintenanceEndTime": contract.HardwareMaintenanceEndTime,
- }
- dynamics := model.CtrContractDynamics{
- ContractId: contract.ContractId,
- OpnPeople: "系统自动创建",
- OpnDate: gtime.Now(),
- OpnType: "续签合同审批通过",
- OpnContent: gconv.String(opnContent),
- }
- _, err = contractDao.DB.Model("ctr_contract_dynamics").TX(tx).Data(dynamics).InsertAndGetId()
- if err != nil {
- return err
- }
- _, err = contractDao.TX(tx).Where("id = ?", contract.ContractId).Data(map[string]interface{}{
- "software_maintenance_end_time": contract.SoftwareMaintenanceEndTime,
- "hardware_maintenance_end_time": contract.HardwareMaintenanceEndTime,
- "software_maintenance_limit": SoftwareMaintenanceLimit,
- "hardware_maintenance_limit": HardwareMaintenanceLimit,
- }).Update()
- if err != nil {
- return err
- }
- }
- return nil
- })
- }
- 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
- toupdate["is_big"] = nbo.IsBig
- toupdate["product_line"] = nbo.ProductLine
- toupdate["cust_province_id"] = nbo.CustProvinceId
- toupdate["cust_province"] = nbo.CustProvince
- toupdate["cust_city_id"] = nbo.CustCityId
- toupdate["cust_city"] = nbo.CustCity
- }
- // 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.ContractSignTime != nil {
- toupdate["contract_sign_time"] = req.ContractSignTime
- }
- //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.SignatoryUnit != "" {
- toupdate["signatory_unit"] = req.SignatoryUnit
- }
- if req.EarnestMoney != nil {
- toupdate["earnest_money"] = req.EarnestMoney
- }
- 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, ent.ContractCode, *req.Product)
- if err != nil {
- return err
- }
- }
- return nil
- })
- if txerr != nil {
- return txerr
- }
- }
- return nil
- }
- func (s CtrContractService) UpdateProduct(ctx context.Context, req *model.CtrContractUpdateProductReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- ent, err := s.CtrProductDao.Where("id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError(fmt.Sprintf("合同产品记录不存在: %d", req.Id))
- }
- toupdate := map[string]interface{}{}
- if req.MaintainPeriod != nil {
- toupdate["maintain_period"] = req.MaintainPeriod
- }
- if req.WarrantPeriod != nil {
- toupdate["warrant_period"] = req.WarrantPeriod
- }
- if req.MaintainStartTime != nil {
- toupdate["maintain_start_time"] = req.MaintainStartTime
- }
- if req.MaintainRemark != nil {
- toupdate["maintain_remark"] = req.MaintainRemark
- }
- if req.AcceptTime != nil {
- toupdate["accept_time"] = req.AcceptTime
- }
- if req.PurchaseCost != nil {
- toupdate["purchase_cost"] = req.PurchaseCost
- }
- if req.DevCost != nil {
- toupdate["dev_cost"] = req.DevCost
- }
- if req.MaintainCost != nil {
- toupdate["maintain_cost"] = req.MaintainCost
- }
- if req.DirectCost != nil {
- toupdate["direct_cost"] = req.DirectCost
- }
- 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_product", toupdate, "id = ?", req.Id)
- if err != nil {
- return err
- }
- err = s.AddDynamicsByCurrentUser(tx, ent.ContractId, "更新合同产品信息", toupdate)
- 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
- }
- // GetShares 获取销售分成数据
- func (s CtrContractService) GetShares(ctx context.Context, req *model.GetContractShareReq) ([]*model.CtrContractShare, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return nil, myerrors.TipsError(validErr.Current().Error())
- }
- return s.ShareDao.Where("contract_id=?", req.ContractId).FindAll()
- }
- func init() {
- tenant := g.Config().GetString("micro_srv.tenant")
- if tenant == "" {
- panic("定时任务租户码未设置,请前往配置")
- }
- contractDao := dao.NewCtrContractDao(tenant)
- produceDao := dao.NewCtrContractProductDao(tenant)
- job := func() {
- alert := map[int]*[]model.CtrContractProduct{
- 180: {},
- 90: {},
- 30: {},
- 10: {},
- // 7: {},
- }
- where := `DATE_FORMAT(DATE_ADD(maintain_start_time, INTERVAL ? day), "%Y-%m-%d") = DATE_FORMAT(NOW(), "%Y-%m-%d")`
- for day, p := range alert {
- err := produceDao.Where(where, day).Structs(p)
- if err != nil {
- glog.Error(err)
- }
- glog.Infof("运维期到期 %d 天提醒,产品个数 %d", day, len(*p))
- }
- for day, p := range alert {
- for _, i := range *p {
- ctr, err := contractDao.Where("id = ?", i.ContractId).One()
- if err != nil {
- glog.Error(err)
- }
- text := fmt.Sprintf("合同:%s-%s 中的产品:%s 距离运维到期还有 %d 天",
- ctr.ContractCode, ctr.ContractName, i.ProdName, day)
- msg := g.MapStrStr{
- "msgTitle": "运维期到期提醒",
- "msgContent": text,
- "msgType": "20",
- "recvUserIds": strconv.Itoa(ctr.InchargeId),
- "msgStatus": "10",
- "sendType": "10",
- }
- if err := service.CreateSystemMessage(msg); err != nil {
- glog.Error("消息提醒异常:", err)
- }
- glog.Infof("%s", text)
- }
- }
- }
- // 每天凌晨2点执行
- gcron.AddSingleton("0 0 2 * * *", job)
- // job()
- }
- func (s CtrContractService) CtrContractMaintenanceExport(ctx context.Context, req *model.CtrContractListReq) (path string, err error) {
- req.PageNum = 0
- _, list, err := s.List(ctx, req)
- if err != nil {
- return path, err
- }
- exportsData := make([]*model.CtrContractMaintenanceExport, 0)
- if err = gconv.Struct(&list, &exportsData); err != nil {
- return
- }
- path, err = service.CommonExportExcel(ctx, "项目运维信息", model.CtrContractMaintenanceExport{}, gconv.Maps(exportsData), func(item map[string]interface{}, header string) interface{} {
- if header == "softwareMaintenanceBeginTime" {
- return gconv.GTime(item[header]).Format("Y-m-d")
- }
- if header == "softwareMaintenanceEndTime" {
- return gconv.GTime(item[header]).Format("Y-m-d")
- }
- if header == "hardwareMaintenanceBeginTime" {
- return gconv.GTime(item[header]).Format("Y-m-d")
- }
- if header == "hardwareMaintenanceEndTime" {
- return gconv.GTime(item[header]).Format("Y-m-d")
- }
- return item[header]
- })
- if err != nil {
- return path, err
- }
- return path, nil
- }
|