business.go 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. package proj
  2. import (
  3. "context"
  4. "os"
  5. contractDao "dashoo.cn/micro/app/dao/contract"
  6. custDao "dashoo.cn/micro/app/dao/cust"
  7. projDao "dashoo.cn/micro/app/dao/proj"
  8. contractModel "dashoo.cn/micro/app/model/contract"
  9. model "dashoo.cn/micro/app/model/proj"
  10. workflowModel "dashoo.cn/micro/app/model/workflow"
  11. workflowService "dashoo.cn/micro/app/service/workflow"
  12. "dashoo.cn/opms_libary/plugin/dingtalk"
  13. "fmt"
  14. "strconv"
  15. "strings"
  16. "dashoo.cn/micro/app/service"
  17. "dashoo.cn/opms_libary/multipart"
  18. "dashoo.cn/opms_libary/myerrors"
  19. "dashoo.cn/opms_libary/plugin/dingtalk/message"
  20. "dashoo.cn/opms_libary/plugin/dingtalk/workflow"
  21. "dashoo.cn/opms_libary/utils"
  22. "github.com/gogf/gf/database/gdb"
  23. "github.com/gogf/gf/frame/g"
  24. "github.com/gogf/gf/os/gtime"
  25. "github.com/gogf/gf/util/gconv"
  26. "github.com/shopspring/decimal"
  27. )
  28. type businessService struct {
  29. *service.ContextService
  30. Dao *projDao.ProjBusinessDao
  31. }
  32. func NewBusinessService(ctx context.Context) (svc *businessService, err error) {
  33. svc = new(businessService)
  34. if svc.ContextService, err = svc.Init(ctx); err != nil {
  35. return nil, err
  36. }
  37. svc.Dao = projDao.NewProjBusinessDao(svc.Tenant)
  38. return svc, nil
  39. }
  40. func (p *businessService) GetList(req *model.ProjBusinessSearchReq) (total int, businessList []*model.ProjBusinessRes, err error) {
  41. db := p.Dao.As("proj").DataScope(p.Ctx, "sale_id")
  42. if req.NboName != "" {
  43. db = db.WhereLike("proj."+p.Dao.C.NboName, "%"+req.NboName+"%")
  44. }
  45. if req.CustName != "" {
  46. db = db.WhereLike("proj."+p.Dao.C.CustName, "%"+req.CustName+"%")
  47. }
  48. if req.SaleName != "" {
  49. db = db.WhereLike("proj."+p.Dao.C.SaleName, "%"+req.SaleName+"%")
  50. }
  51. if req.NboType != "" {
  52. db = db.Where("proj."+p.Dao.C.NboType, req.NboType)
  53. }
  54. if req.ProductLine != "" {
  55. db = db.Where("proj."+p.Dao.C.ProductLine, req.ProductLine)
  56. }
  57. if req.NboSource != "" {
  58. db = db.Where("proj."+p.Dao.C.NboSource, req.NboSource)
  59. }
  60. if req.DistributorName != "" {
  61. db = db.WhereLike("proj."+p.Dao.C.DistributorName, "%"+req.DistributorName+"%")
  62. }
  63. if req.BeginTime != "" {
  64. db = db.WhereGTE("proj."+p.Dao.C.FilingTime, req.BeginTime)
  65. }
  66. if req.EndTime != "" {
  67. db = db.WhereLTE("proj."+p.Dao.C.FilingTime, req.EndTime)
  68. }
  69. total, err = db.Count()
  70. if err != nil {
  71. err = myerrors.DbError("获取总行数失败。")
  72. return
  73. }
  74. if req.NboType == StatusDeal {
  75. db = db.Unscoped().WhereNull(`proj.deleted_time`).
  76. LeftJoin(contractDao.CtrContract.Table, "contract", "`proj`.id=`contract`.nbo_id AND `contract`.`deleted_time` IS NULL ").
  77. Fields("`proj`.cust_city_id as cust_city_id,`contract`.contract_amount, `contract`.created_time as proj_closing_time")
  78. }
  79. db = db.Fields("`proj`.*")
  80. err = db.Page(req.GetPage()).OrderDesc("id").Scan(&businessList)
  81. return
  82. }
  83. func (p *businessService) GetEntityById(id int64) (business *model.ProjBusiness, err error) {
  84. err = p.Dao.Where(projDao.ProjBusiness.C.Id, id).Scan(&business)
  85. return
  86. }
  87. func (p *businessService) GetBusinessProduct(id int64) (productList []*model.ProjBusinessProduct, err error) {
  88. productDao := projDao.NewProjBusinessProductDao(p.Tenant)
  89. err = productDao.Where(productDao.ProjBusinessProductDao.C.BusId, id).Scan(&productList)
  90. return
  91. }
  92. func (p *businessService) GetBusinessDynamics(req *model.BusinessReq) (total int, result g.MapStrAny, err error) {
  93. result = make(g.MapStrAny, 0)
  94. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).ProjBusinessDynamicsDao.Where(projDao.ProjBusinessDynamics.C.BusId, req.BusId)
  95. total, err = dynamicsDao.Count()
  96. if err != nil {
  97. g.Log().Error(err)
  98. return
  99. }
  100. dynamicsList, err := dynamicsDao.Page(req.GetPage()).Order("created_time desc").All()
  101. if err != nil || dynamicsList == nil {
  102. return
  103. }
  104. // 数据树格式转换
  105. opnDateFlag := gtime.New(dynamicsList[0].OpnDate).Format("Y-m-d")
  106. for k, v := range dynamicsList {
  107. opnDate := gtime.New(v.OpnDate).Format("Y-m-d")
  108. if opnDateFlag == opnDate && k != 0 {
  109. result[opnDate] = append(result[opnDate].(g.ListStrAny), g.Map{
  110. "opnPeople": v.OpnPeople,
  111. "opnDate": v.OpnDate,
  112. "opnType": v.OpnType,
  113. "remark": v.Remark,
  114. "opnContent": gconv.Map(v.OpnContent),
  115. })
  116. } else {
  117. temp := make(g.ListStrAny, 0)
  118. temp = append(temp, g.Map{
  119. "opnPeople": v.OpnPeople,
  120. "opnDate": v.OpnDate,
  121. "opnType": v.OpnType,
  122. "remark": v.Remark,
  123. "opnContent": gconv.Map(v.OpnContent),
  124. })
  125. result[opnDate] = temp
  126. opnDateFlag = opnDate
  127. }
  128. }
  129. return
  130. }
  131. func (p *businessService) GetBusinessDynamicsList(req *model.BusinessDynamicsReq) (total int, list []map[string]interface{}, err error) {
  132. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).Where(projDao.ProjBusinessDynamics.C.BusId, req.BusId)
  133. if req.OpnType != "" {
  134. dynamicsDao = dynamicsDao.Where(projDao.ProjBusinessDynamics.C.OpnType+" = ?", req.OpnType)
  135. }
  136. total, err = dynamicsDao.Count()
  137. if err != nil {
  138. g.Log().Error(err)
  139. return
  140. }
  141. dynamicsList, err := dynamicsDao.Page(req.GetPage()).Order("created_time desc").All()
  142. for _, v := range dynamicsList {
  143. val := gconv.Map(v)
  144. val["opnContent"] = gconv.Map(v.OpnContent)
  145. list = append(list, val)
  146. }
  147. return
  148. }
  149. // 获取项目编号
  150. func (p *businessService) getNboCode(customerCode string) (string, error) {
  151. sequence, err := service.Sequence(p.Dao.DB, "nbo_code")
  152. if err != nil {
  153. return "", err
  154. }
  155. return customerCode + sequence, nil
  156. }
  157. func (p *businessService) Create(req *model.AddProjBusinessReq) (err error) {
  158. // 获取客户信息
  159. customer, err := custDao.NewCustCustomerDao(p.Tenant).WherePri(req.CustId).One()
  160. if err != nil {
  161. return err
  162. }
  163. if customer == nil {
  164. return myerrors.TipsError("客户不存在")
  165. }
  166. // 设置默认联系人
  167. contact := g.Map{
  168. projDao.ProjBusinessContact.C.ContactId: req.ContactId,
  169. }
  170. service.SetCreatedInfo(contact, p.GetCxtUserId(), p.GetCxtUserName())
  171. // 设置产品信息
  172. totalPrice, products, err := p.setProductInfo(0, req.Products)
  173. if err != nil {
  174. return err
  175. }
  176. // 获取项目编号
  177. nboCode, err := p.getNboCode(customer.CustCode)
  178. if err != nil {
  179. return err
  180. }
  181. // 初始化项目信息
  182. business := new(model.ProjBusiness)
  183. if err = gconv.Struct(req, business); err != nil {
  184. return
  185. }
  186. business.NboCode = nboCode
  187. //business.NboStatus = StatusOK
  188. if business.NboType == "" {
  189. business.NboType = StatusC
  190. }
  191. business.ApproStatus = ApprovalWaiting
  192. business.EstTransPrice = totalPrice
  193. business.CustProvinceId = customer.CustProvinceId
  194. business.CustProvince = customer.CustProvince
  195. business.CustCityId = customer.CustCityId
  196. business.CustCity = customer.CustCity
  197. business.CustRegionId = customer.CustRegionId
  198. business.CustRegion = customer.CustRegion
  199. business.DeptId = p.GetCxtUserDeptId()
  200. service.SetCreatedInfo(business, p.GetCxtUserId(), p.GetCxtUserName())
  201. business.FilingTime = business.CreatedTime
  202. productLine, _ := service.GetDictLabelByTypeAndValue(p.Ctx, "sys_product_line", business.ProductLine)
  203. nboSource, _ := service.GetDictLabelByTypeAndValue(p.Ctx, "proj_nbo_source", business.NboSource)
  204. salesModel, _ := service.GetDictLabelByTypeAndValue(p.Ctx, "proj_sales_model", business.SalesModel)
  205. // 事务
  206. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  207. // 添加项目
  208. lastId, err := p.Dao.TX(tx).InsertAndGetId(business)
  209. if err != nil {
  210. return err
  211. }
  212. // 创建了联系人
  213. contact[projDao.ProjBusinessContact.C.BusId] = lastId
  214. _, err = projDao.NewProjBusinessContactDao(p.Tenant).TX(tx).Insert(contact)
  215. if err != nil {
  216. return err
  217. }
  218. // 处理项目产品信息
  219. for _, v := range products {
  220. v.BusId = int(lastId)
  221. }
  222. // 添加项目产品
  223. _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Insert(products)
  224. if err != nil {
  225. return err
  226. }
  227. // 添加项目动态
  228. dynamics := model.ProjBusinessDynamics{
  229. BusId: int(lastId),
  230. OpnType: OpnCreate,
  231. Remark: business.Remark,
  232. }
  233. _, err = p.CreateProjBusinessDynamics(tx, dynamics, business)
  234. // 审批流
  235. workflowSrv, _ := workflowService.NewFlowService(p.Ctx)
  236. // OMS项目转移 审批
  237. bizCode := business.NboCode + ":" + gconv.String(lastId)
  238. _, err = workflowSrv.StartProcessInstance(bizCode, workflowModel.ProjectCreate, "", &workflow.StartProcessInstanceRequest{
  239. ProcessCode: &BusinessCreateRequestProcessCode,
  240. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  241. {
  242. Id: utils.String("TextField-K2AD4O5B"),
  243. Name: utils.String("项目编码"),
  244. Value: utils.String(business.NboCode),
  245. },
  246. {
  247. Id: utils.String("TextField_BDLSECETVSG0"),
  248. Name: utils.String("项目名称"),
  249. Value: utils.String(business.NboName),
  250. },
  251. {
  252. Id: utils.String("DDSelectField_213JKZA1OUO00"),
  253. Name: utils.String("产品线"),
  254. Value: utils.String(productLine),
  255. },
  256. {
  257. Id: utils.String("TextField_1J9BJMOZ18F40"),
  258. Name: utils.String("客户名称"),
  259. Value: utils.String(business.CustName),
  260. },
  261. {
  262. Id: utils.String("TextField_VZ64Y44LXFK0"),
  263. Name: utils.String("主要联系人"),
  264. Value: utils.String(business.ContactName),
  265. },
  266. {
  267. Id: utils.String("DDSelectField_6CQD451D3800"),
  268. Name: utils.String("项目来源"),
  269. Value: utils.String(nboSource),
  270. },
  271. {
  272. Id: utils.String("TextField_AEUWH63LJ0O0"),
  273. Name: utils.String("销售工程师"),
  274. Value: utils.String(business.SaleName),
  275. },
  276. {
  277. Id: utils.String("DDSelectField_34QSUGHO2SO0"),
  278. Name: utils.String("销售模式"),
  279. Value: utils.String(salesModel),
  280. },
  281. {
  282. Id: utils.String("TextField_1PWK6WHMGITC0"),
  283. Name: utils.String("经销商/代理商"),
  284. Value: utils.String(business.DistributorName),
  285. },
  286. {
  287. Id: utils.String("DDSelectField_5R11VVM6GI00"),
  288. Name: utils.String("是否为大项目"),
  289. Value: utils.String(yesOrNoType[business.IsBig]),
  290. },
  291. {
  292. Id: utils.String("TextareaField_1GEL8JJL3H5S0"),
  293. Name: utils.String("备注"),
  294. Value: utils.String(business.Remark),
  295. },
  296. },
  297. })
  298. if err != nil {
  299. g.Log().Error(err)
  300. return err
  301. }
  302. return err
  303. })
  304. return
  305. }
  306. // BusinessCreatedNotify 项目创建 审批结果通知
  307. func (p *businessService) BusinessCreatedNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  308. business, err := p.checkDingTalkNotify(flow, msg)
  309. if err != nil {
  310. return err
  311. }
  312. var data = g.Map{}
  313. if msg.ProcessType == "terminate" {
  314. data[p.Dao.C.ApproStatus] = ApprovalReturn
  315. }
  316. if msg.ProcessType == "finish" && msg.Result == "refuse" {
  317. data[p.Dao.C.ApproStatus] = ApprovalRejection
  318. }
  319. if msg.ProcessType == "finish" && msg.Result == "agree" {
  320. data[p.Dao.C.ApproStatus] = ApprovalOK
  321. }
  322. // 项目修改
  323. _, err = p.Dao.WherePri(business.Id).FieldsEx(service.UpdateFieldEx...).Data(data).Update()
  324. if err != nil {
  325. return err
  326. }
  327. // 添加项目动态
  328. dynamics := model.ProjBusinessDynamics{
  329. BusId: business.Id,
  330. OpnType: OpnCreatedApproval,
  331. }
  332. _, err = p.CreateProjBusinessDynamics(nil, dynamics, data)
  333. if err != nil {
  334. return err
  335. }
  336. return err
  337. }
  338. // setProductInfo 设置产品信息
  339. func (p *businessService) setProductInfo(busId int, productInfo []model.BusinessProduct) (total float64, products []*model.ProjBusinessProduct, err error) {
  340. products = make([]*model.ProjBusinessProduct, len(productInfo))
  341. if err = gconv.Structs(productInfo, &products); err != nil {
  342. return 0, nil, err
  343. }
  344. var totalPrice decimal.Decimal
  345. for _, v := range products {
  346. v.Id = 0
  347. v.BusId = busId
  348. v.TotalPrice = decimal.NewFromFloat(v.ProdPrice).Mul(decimal.NewFromInt(int64(v.ProdNum))).InexactFloat64()
  349. totalPrice = totalPrice.Add(decimal.NewFromFloat(v.TotalPrice))
  350. service.SetCreatedInfo(v, p.GetCxtUserId(), p.GetCxtUserName())
  351. }
  352. return totalPrice.InexactFloat64(), products, nil
  353. }
  354. func (p *businessService) UpdateById(req *model.UpdateProjBusinessReq) error {
  355. record, err := p.Dao.WherePri(req.Id).Count()
  356. if err != nil {
  357. return err
  358. }
  359. if record == 0 {
  360. return myerrors.TipsError("项目不存在。")
  361. }
  362. // 设置产品信息
  363. totalPrice, products, err := p.setProductInfo(req.Id, req.Products)
  364. if err != nil {
  365. return err
  366. }
  367. // 设置默认联系人
  368. contact := g.Map{
  369. projDao.ProjBusinessContact.C.BusId: req.Id,
  370. projDao.ProjBusinessContact.C.ContactId: req.ContactId,
  371. }
  372. contactFlag, err := projDao.NewProjBusinessContactDao(p.Tenant).Where(contact).Count()
  373. if err != nil {
  374. return err
  375. }
  376. if contactFlag == 0 {
  377. service.SetCreatedInfo(contact, p.GetCxtUserId(), p.GetCxtUserName())
  378. }
  379. // 设置项目信息
  380. req.EstTransPrice = totalPrice
  381. businessData := gconv.Map(req)
  382. service.SetUpdatedInfo(businessData, p.GetCxtUserId(), p.GetCxtUserName())
  383. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  384. // 更新项目
  385. updateFieldEx := append(service.UpdateFieldEx, p.Dao.C.SaleId, p.Dao.C.SaleName)
  386. _, err = p.Dao.TX(tx).FieldsEx(updateFieldEx...).WherePri(projDao.ProjBusiness.C.Id, req.Id).Update(businessData)
  387. if err != nil {
  388. return err
  389. }
  390. // 删除项目产品
  391. _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Where(projDao.ProjBusinessProduct.C.BusId, req.Id).Delete()
  392. if err != nil {
  393. return err
  394. }
  395. // 添加项目产品
  396. _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Insert(products)
  397. if err != nil {
  398. return err
  399. }
  400. // 关联联系人
  401. if contactFlag == 0 {
  402. _, err = projDao.NewProjBusinessContactDao(p.Tenant).TX(tx).Insert(contact)
  403. if err != nil {
  404. return err
  405. }
  406. }
  407. // 添加项目动态
  408. dynamics := model.ProjBusinessDynamics{
  409. BusId: req.Id,
  410. OpnType: OpnUpdate,
  411. Remark: req.Remark,
  412. }
  413. _, err = p.CreateProjBusinessDynamics(tx, dynamics, req)
  414. return err
  415. })
  416. return err
  417. }
  418. func (p *businessService) DeleteByIds(ids []int64) (err error) {
  419. _, err = p.Dao.WhereIn(projDao.ProjBusiness.C.Id, ids).Delete()
  420. return
  421. }
  422. // BusinessTransfer 项目转移
  423. func (p *businessService) BusinessTransfer(req *model.BusinessTransferReq) error {
  424. business, err := p.Dao.WherePri(req.Id).WhereNot(p.Dao.C.ApproStatus, ApprovalWaiting).One()
  425. if err != nil {
  426. return err
  427. }
  428. if business == nil {
  429. return myerrors.TipsError("项目已提交审批任务,无法重复提交。")
  430. }
  431. businessMap := g.Map{
  432. p.Dao.C.ApproStatus: ApprovalWaiting,
  433. }
  434. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  435. opnContent := gconv.Map(gconv.String(businessMap))
  436. opnContent["origSaleId"] = business.SaleId
  437. opnContent["origSaleName"] = business.SaleName
  438. opnContent["saleId"] = req.UserId
  439. opnContent["saleName"] = req.UserName
  440. opnContent["remark"] = req.Remark
  441. productLine, _ := service.GetDictLabelByTypeAndValue(p.Ctx, "sys_product_line", business.ProductLine)
  442. // 审批流
  443. workflowSrv, _ := workflowService.NewFlowService(p.Ctx)
  444. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  445. // 更新项目
  446. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.C.Id, req.Id).Data(businessMap).Update()
  447. if err != nil {
  448. return err
  449. }
  450. // 添加项目动态
  451. dynamics := model.ProjBusinessDynamics{
  452. BusId: req.Id,
  453. OpnType: OpnTransfer,
  454. Remark: req.Remark,
  455. }
  456. _, err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  457. if err != nil {
  458. g.Log().Error(err)
  459. return err
  460. }
  461. // OMS项目转移 审批
  462. bizCode := business.NboCode + ":" + strconv.Itoa(business.Id)
  463. _, err = workflowSrv.StartProcessInstance(bizCode, workflowModel.ProjectTransfer, "", &workflow.StartProcessInstanceRequest{
  464. ProcessCode: &BusinessTransferRequestProcessCode,
  465. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  466. {
  467. Id: utils.String("TextField-K2AD4O5B"),
  468. Name: utils.String("项目编码"),
  469. Value: utils.String(business.NboCode),
  470. },
  471. {
  472. Id: utils.String("TextField_7EFHRQ9DDF80"),
  473. Name: utils.String("项目名称"),
  474. Value: utils.String(business.NboName),
  475. },
  476. {
  477. Id: utils.String("TextField_1T3DEY5FWV7K0"),
  478. Name: utils.String("客户名称"),
  479. Value: utils.String(business.CustName),
  480. },
  481. {
  482. Id: utils.String("TextField_QDU06LXYKK00"),
  483. Name: utils.String("所在省"),
  484. Value: utils.String(business.CustProvince),
  485. },
  486. {
  487. Id: utils.String("TextField_MVSOO6EG6YO0"),
  488. Name: utils.String("所在市"),
  489. Value: utils.String(business.CustCity),
  490. },
  491. {
  492. Id: utils.String("DDSelectField_6OMVO1JV0980"),
  493. Name: utils.String("产品线"),
  494. Value: utils.String(productLine),
  495. },
  496. {
  497. Id: utils.String("TextField_1E1WOYGKRTDS0"),
  498. Name: utils.String("项目级别"),
  499. Value: utils.String(nboType[business.NboType]),
  500. },
  501. {
  502. Id: utils.String("TextField_NRQXWLJ17HC0"),
  503. Name: utils.String("申请人"),
  504. Value: utils.String(p.GetCxtUserName()),
  505. },
  506. {
  507. Id: utils.String("TextField_GHSQYDGD13K0"),
  508. Name: utils.String("转移原因"),
  509. Value: utils.String(req.Remark),
  510. },
  511. {
  512. Id: utils.String("TextField_76P8FPHH0UC0"),
  513. Name: utils.String("接收人"),
  514. Value: utils.String(req.UserName),
  515. },
  516. },
  517. })
  518. if err != nil {
  519. g.Log().Error(err)
  520. return err
  521. }
  522. return nil
  523. })
  524. return err
  525. }
  526. // BusinessTransferNotify 项目转移 审批结果通知
  527. func (p *businessService) BusinessTransferNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  528. business, err := p.checkDingTalkNotify(flow, msg)
  529. if err != nil {
  530. return err
  531. }
  532. var data = g.Map{}
  533. if msg.ProcessType == "terminate" {
  534. data[p.Dao.C.ApproStatus] = ApprovalReturn
  535. }
  536. if msg.ProcessType == "finish" && msg.Result == "refuse" {
  537. data[p.Dao.C.ApproStatus] = ApprovalRejection
  538. }
  539. if msg.ProcessType == "finish" && msg.Result == "agree" {
  540. // 从项目动态内获取变更信息
  541. var transferDynamics model.ProjBusinessDynamics
  542. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).Where(projDao.ProjBusinessDynamics.C.BusId, business.Id)
  543. err = dynamicsDao.Where(projDao.ProjBusinessDynamics.C.OpnType, OpnTransfer).OrderDesc("created_time").Scan(&transferDynamics)
  544. if err != nil {
  545. return err
  546. }
  547. changeData := gconv.Map(transferDynamics.OpnContent)
  548. data[p.Dao.C.SaleId] = changeData["saleId"]
  549. data[p.Dao.C.SaleName] = changeData["saleName"]
  550. data[p.Dao.C.Remark] = changeData["remark"]
  551. data[p.Dao.C.ApproStatus] = ApprovalOK
  552. }
  553. // 项目修改
  554. _, err = p.Dao.WherePri(business.Id).FieldsEx(service.UpdateFieldEx...).Data(data).Update()
  555. if err != nil {
  556. return err
  557. }
  558. // 添加项目动态
  559. dynamics := model.ProjBusinessDynamics{
  560. BusId: business.Id,
  561. OpnType: OpnTransferApproval,
  562. }
  563. _, err = p.CreateProjBusinessDynamics(nil, dynamics, data)
  564. if err != nil {
  565. return err
  566. }
  567. return err
  568. }
  569. // BusinessGradation 项目调级
  570. func (p *businessService) BusinessGradation(busId int, nboType, busType string) (*model.ProjBusiness, error) {
  571. business, err := p.Dao.WherePri(busId).WhereNot(p.Dao.C.ApproStatus, ApprovalWaiting).One()
  572. if err != nil {
  573. return nil, err
  574. }
  575. if business == nil {
  576. return nil, myerrors.TipsError("项目已提交审批任务,无法重复提交。")
  577. }
  578. if business.NboType == nboType {
  579. return nil, myerrors.TipsError("同级别无法进行调级。")
  580. }
  581. if business.NboType == StatusDeal {
  582. return nil, myerrors.TipsError("成交项目无法进行调级。")
  583. }
  584. if business.NboType == StatusReserve && nboType == StatusDeal {
  585. return nil, myerrors.TipsError("储备项目无法直接转为成交项目。")
  586. }
  587. if busType == "up" && gconv.Int(business.NboType) < gconv.Int(nboType) {
  588. return nil, myerrors.TipsError("项目级别错误。")
  589. }
  590. if busType == "down" && gconv.Int(business.NboType) > gconv.Int(nboType) {
  591. return nil, myerrors.TipsError("项目级别错误。")
  592. }
  593. return business, err
  594. }
  595. // BusinessUpgrade 项目升级
  596. func (p *businessService) BusinessUpgrade(req *model.BusinessUpgradeReq, fileMap map[string]*multipart.FileHeader) error {
  597. business, err := p.BusinessGradation(req.Id, req.NboType, "up")
  598. if err != nil {
  599. return err
  600. }
  601. businessMap := g.Map{p.Dao.C.ApproStatus: ApprovalWaiting}
  602. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  603. opnContent := gconv.Map(req)
  604. opnContent["origNboType"] = business.NboType
  605. opnContent["approStatus"] = ApprovalWaiting
  606. service.SetUpdatedInfo(opnContent, p.GetCxtUserId(), p.GetCxtUserName())
  607. if fileMap == nil {
  608. fileMap = make(map[string]*multipart.FileHeader)
  609. }
  610. if req.NboType == StatusA || req.NboType == StatusB {
  611. fileMap["quotationFile"], err = service.DownloadTempFile(req.QuotationFile)
  612. if err != nil {
  613. return err
  614. }
  615. defer os.Remove(fileMap["quotationFile"].File.Name())
  616. }
  617. if req.NboType == StatusA && req.IsAdoptDashoo == "10" {
  618. fileMap["dashooParamFile"], err = service.DownloadTempFile(req.DashooParamFile)
  619. if err != nil {
  620. return err
  621. }
  622. defer os.Remove(fileMap["dashooParamFile"].File.Name())
  623. }
  624. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  625. // 更新项目调级
  626. _, err = p.Dao.TX(tx).WherePri(req.Id).Data(businessMap).Update()
  627. if err != nil {
  628. return err
  629. }
  630. // 添加项目动态
  631. dynamics := model.ProjBusinessDynamics{
  632. BusId: business.Id,
  633. OpnType: OpnUpgrade,
  634. Remark: req.Remark,
  635. }
  636. _, err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  637. if err != nil {
  638. return err
  639. }
  640. err = p.BusUpgradeDingEvent(business, req, fileMap)
  641. return err
  642. })
  643. return err
  644. }
  645. // 获取项目的钉钉审批的升级类型
  646. func (p *businessService) getBusDingUpgradeType(dbNboType, reqNboType string) string {
  647. var upgradeType string
  648. switch true {
  649. case dbNboType == StatusReserve && reqNboType == StatusC: // 储备转C/option_0
  650. upgradeType = "储备转C"
  651. case dbNboType == StatusReserve && reqNboType == StatusB: // 储备转B/option_1
  652. upgradeType = "储备转B"
  653. case dbNboType == StatusReserve && reqNboType == StatusA: // 储备转A/option_KTAX3Y9K5340
  654. upgradeType = "储备转A"
  655. case dbNboType == StatusC && reqNboType == StatusB: // C转B/option_0
  656. upgradeType = "C转B"
  657. case dbNboType == StatusC && reqNboType == StatusA: // C转A/option_0
  658. upgradeType = "C转A"
  659. case dbNboType == StatusB && reqNboType == StatusA: // B转A/option_1
  660. upgradeType = "B转A"
  661. default:
  662. }
  663. return upgradeType
  664. }
  665. // BusUpgradeDingEvent 项目升级钉钉审批流调用
  666. func (p *businessService) BusUpgradeDingEvent(business *model.ProjBusiness, req *model.BusinessUpgradeReq, fileMap map[string]*multipart.FileHeader) error {
  667. upgradeType := p.getBusDingUpgradeType(business.NboType, req.NboType)
  668. if upgradeType == "" {
  669. return myerrors.TipsError("错误的升级类型")
  670. }
  671. productLine, _ := service.GetDictLabelByTypeAndValue(p.Ctx, "sys_product_line", business.ProductLine)
  672. // 审批流
  673. workflowSrv, _ := workflowService.NewFlowService(p.Ctx)
  674. // OMS项目升级 审批
  675. var err error
  676. var dingReq *workflow.StartProcessInstanceRequest
  677. bizCode := business.NboCode + ":" + strconv.Itoa(business.Id)
  678. switch req.NboType {
  679. case StatusC:
  680. dingReq = &workflow.StartProcessInstanceRequest{
  681. ProcessCode: &BusinessUpgradeCRequestProcessCode,
  682. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  683. {
  684. Id: utils.String("TextField-K2AD4O5B"),
  685. Name: utils.String("项目编码"),
  686. Value: utils.String(business.NboCode),
  687. },
  688. {
  689. Id: utils.String("TextField_BDLSECETVSG0"),
  690. Name: utils.String("项目名称"),
  691. Value: utils.String(business.NboName),
  692. },
  693. {
  694. Id: utils.String("DDSelectField_1MJU37HGJX4W0"),
  695. Name: utils.String("产品线"),
  696. Value: utils.String(productLine),
  697. },
  698. {
  699. Id: utils.String("DDSelectField_VSA3U380ZK00"),
  700. Name: utils.String("升级类型"),
  701. Value: utils.String(upgradeType),
  702. },
  703. {
  704. Id: utils.String("TextField_1J9BJMOZ18F40"),
  705. Name: utils.String("客户名称"),
  706. Value: utils.String(business.CustName),
  707. },
  708. {
  709. Id: utils.String("TextField_AEUWH63LJ0O0"),
  710. Name: utils.String("销售工程师"),
  711. Value: utils.String(business.SaleName),
  712. },
  713. {
  714. Id: utils.String("TextareaField_1LO81IKHH91C0"),
  715. Name: utils.String("转化原因"),
  716. Value: utils.String(req.ProjConversionReason),
  717. },
  718. },
  719. }
  720. case StatusB:
  721. quotationFile := make([]contractModel.DingFileInfo, 0)
  722. if len(fileMap) > 0 {
  723. for k, files := range fileMap {
  724. // 报价单
  725. if k == "quotationFile" {
  726. if quotationFile, err = p.txCreateBusinessDingTalkFile(business.Id, upgradeType, k, files); err != nil {
  727. return err
  728. }
  729. }
  730. }
  731. }
  732. if len(quotationFile) == 0 {
  733. return myerrors.TipsError("请上传报价单文件")
  734. }
  735. processCode := &BusinessUpgradeBRequestProcessCode
  736. dingReq = &workflow.StartProcessInstanceRequest{
  737. ProcessCode: processCode,
  738. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  739. {
  740. Id: utils.String("TextField-K2AD4O5B"),
  741. Name: utils.String("项目编码"),
  742. Value: utils.String(business.NboCode),
  743. },
  744. {
  745. Id: utils.String("TextField_BDLSECETVSG0"),
  746. Name: utils.String("项目名称"),
  747. Value: utils.String(business.NboName),
  748. },
  749. {
  750. Id: utils.String("DDSelectField_213JKZA1OUO00"),
  751. Name: utils.String("产品线"),
  752. Value: utils.String(productLine),
  753. },
  754. {
  755. Id: utils.String("DDSelectField_VSA3U380ZK00"),
  756. Name: utils.String("升级类型"),
  757. Value: utils.String(upgradeType),
  758. },
  759. {
  760. Id: utils.String("TextField_1J9BJMOZ18F40"),
  761. Name: utils.String("客户名称"),
  762. Value: utils.String(business.CustName),
  763. },
  764. {
  765. Id: utils.String("NumberField_1F88MCD0W8KG0"),
  766. Name: utils.String("项目预算"),
  767. Value: utils.String(gconv.String(req.NboBudget)),
  768. },
  769. {
  770. Id: utils.String("TextField_1PWK6WHMGITC0"),
  771. Name: utils.String("经销商/代理商"),
  772. Value: utils.String(req.DistributorName),
  773. },
  774. {
  775. Id: utils.String("TextField_X4D3QGARU7K0"),
  776. Name: utils.String("支持内容"),
  777. Value: utils.String(req.TechnicalSupportContent),
  778. },
  779. {
  780. Id: utils.String("TextField_AEUWH63LJ0O0"),
  781. Name: utils.String("销售工程师"),
  782. Value: utils.String(business.SaleName),
  783. },
  784. {
  785. Id: utils.String("DDDateField_1FW1QZQYBZVK0"),
  786. Name: utils.String("采购时间"),
  787. Value: utils.String(gconv.String(req.PurchasingTime.Format("Y-m-d"))),
  788. },
  789. {
  790. Id: utils.String("DDSelectField_21ASEWDIB3MO0"),
  791. Name: utils.String("采购方式"),
  792. Value: utils.String(gconv.String(purchasingWayType[req.PurchasingWay])),
  793. },
  794. {
  795. Id: utils.String("DDSelectField_5R11VVM6GI00"),
  796. Name: utils.String("是否我司参数"),
  797. Value: utils.String(gconv.String(yesOrNoType[req.IsAdoptDashoo])),
  798. },
  799. {
  800. Id: utils.String("DDAttachment_KZPWZJS9GHO0"),
  801. Name: utils.String("上传报价单"),
  802. Value: utils.String(gconv.String(quotationFile)),
  803. },
  804. {
  805. Id: utils.String("TextareaField_1GEL8JJL3H5S0"),
  806. Name: utils.String("备注"),
  807. Value: utils.String(req.Remark),
  808. },
  809. },
  810. }
  811. case StatusA:
  812. var dashooParamFile []contractModel.DingFileInfo
  813. var quotationFile []contractModel.DingFileInfo
  814. if len(fileMap) > 0 {
  815. for k, files := range fileMap {
  816. // 报价单
  817. if k == "quotationFile" {
  818. if quotationFile, err = p.txCreateBusinessDingTalkFile(business.Id, upgradeType, k, files); err != nil {
  819. return err
  820. }
  821. }
  822. // 大数参数文件
  823. if k == "dashooParamFile" {
  824. if dashooParamFile, err = p.txCreateBusinessDingTalkFile(business.Id, upgradeType, k, files); err != nil {
  825. return err
  826. }
  827. }
  828. }
  829. }
  830. if len(quotationFile) == 0 {
  831. return myerrors.TipsError("请上传报价单文件")
  832. }
  833. processCode := &BusinessUpgradeARequestProcessCode
  834. dingReq = &workflow.StartProcessInstanceRequest{
  835. ProcessCode: processCode,
  836. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  837. {
  838. Id: utils.String("TextField-K2AD4O5B"),
  839. Name: utils.String("项目编码"),
  840. Value: utils.String(business.NboCode),
  841. },
  842. {
  843. Id: utils.String("TextField_BDLSECETVSG0"),
  844. Name: utils.String("项目名称"),
  845. Value: utils.String(business.NboName),
  846. },
  847. {
  848. Id: utils.String("DDSelectField_L4CSUVLU1NK"),
  849. Name: utils.String("产品线"),
  850. Value: utils.String(productLine),
  851. },
  852. {
  853. Id: utils.String("DDSelectField_VSA3U380ZK00"),
  854. Name: utils.String("升级类型"),
  855. Value: utils.String(upgradeType),
  856. },
  857. {
  858. Id: utils.String("TextField_1J9BJMOZ18F40"),
  859. Name: utils.String("客户名称"),
  860. Value: utils.String(business.CustName),
  861. },
  862. {
  863. Id: utils.String("NumberField_1F88MCD0W8KG0"),
  864. Name: utils.String("项目预算"),
  865. Value: utils.String(gconv.String(req.NboBudget)),
  866. },
  867. {
  868. Id: utils.String("TextField_1PWK6WHMGITC0"),
  869. Name: utils.String("经销商/代理商"),
  870. Value: utils.String(req.DistributorName),
  871. },
  872. {
  873. Id: utils.String("TextField_X4D3QGARU7K0"),
  874. Name: utils.String("支持内容"),
  875. Value: utils.String(req.TechnicalSupportContent),
  876. },
  877. {
  878. Id: utils.String("TextField_AEUWH63LJ0O0"),
  879. Name: utils.String("销售工程师"),
  880. Value: utils.String(business.SaleName),
  881. },
  882. {
  883. Id: utils.String("DDDateField_1FW1QZQYBZVK0"),
  884. Name: utils.String("采购时间"),
  885. Value: utils.String(gconv.String(req.PurchasingTime.Format("Y-m-d"))),
  886. },
  887. {
  888. Id: utils.String("DDSelectField_21ASEWDIB3MO0"),
  889. Name: utils.String("采购方式"),
  890. Value: utils.String(gconv.String(purchasingWayType[req.PurchasingWay])),
  891. },
  892. {
  893. Id: utils.String("DDSelectField_5R11VVM6GI00"),
  894. Name: utils.String("是否我司参数"),
  895. Value: utils.String(gconv.String(yesOrNoType[req.IsAdoptDashoo])),
  896. },
  897. {
  898. Id: utils.String("DDAttachment_11Q7DBRKE6HC0"),
  899. Name: utils.String("附件"),
  900. Value: utils.String(gconv.String(dashooParamFile)),
  901. },
  902. {
  903. Id: utils.String("DDAttachment_19Y01ZRBFWXS0"),
  904. Name: utils.String("上传报价单"),
  905. Value: utils.String(gconv.String(quotationFile)),
  906. },
  907. {
  908. Id: utils.String("TextareaField_1GEL8JJL3H5S0"),
  909. Name: utils.String("备注"),
  910. Value: utils.String(req.Remark),
  911. },
  912. },
  913. }
  914. default:
  915. return nil
  916. }
  917. _, err = workflowSrv.StartProcessInstance(bizCode, workflowModel.ProjectUpGrade, "", dingReq)
  918. if err != nil {
  919. g.Log().Error(err)
  920. return err
  921. }
  922. return nil
  923. }
  924. // 项目上传文件至钉钉
  925. func (p *businessService) txCreateBusinessDingTalkFile(businessId int, upgradeType, fileType string, file *multipart.FileHeader) ([]contractModel.DingFileInfo, error) {
  926. dingTalkFiles := make([]contractModel.DingFileInfo, 0)
  927. //for _, file := range files {
  928. resp, err := dingtalk.Client.GetStorage().UploadFile(service.DingTalkSpaceId, p.GetCxtUserDingtalkId(), file.FileName, file.File.Name())
  929. if err != nil {
  930. g.Log().Error(err)
  931. return nil, myerrors.TipsError("钉钉上传文件异常")
  932. }
  933. typ := "项目" + upgradeType
  934. if fileType == "quotationFile" {
  935. typ += "上传报价单文件"
  936. }
  937. if fileType == "dashooParamFile" {
  938. typ += "上传大数技术参数文件"
  939. }
  940. g.Log().Info(typ, resp)
  941. dingTalkFiles = append(dingTalkFiles, contractModel.DingFileInfo{
  942. SpaceId: resp.Dentry.SpaceId,
  943. FileId: resp.Dentry.Id,
  944. FileName: resp.Dentry.Name,
  945. FileSize: resp.Dentry.Size,
  946. FileType: resp.Dentry.Extension,
  947. })
  948. //}
  949. err = p.txCreateBusinessFile(businessId, typ, dingTalkFiles)
  950. if err != nil {
  951. return nil, err
  952. }
  953. return dingTalkFiles, nil
  954. }
  955. // 采用大数参数文件记录
  956. func (p *businessService) txCreateBusinessFile(busId int, fileSource string, files []contractModel.DingFileInfo) error {
  957. dataList := make([]*model.ProjBusinessFile, 0)
  958. for _, v := range files {
  959. data := new(model.ProjBusinessFile)
  960. data.BusId = busId
  961. data.FileName = v.FileName
  962. data.FileSource = fileSource
  963. data.FileSize = gconv.String(v.FileSize)
  964. data.FileUrl = strings.Join([]string{"dingtalk", v.SpaceId, v.FileId}, ":")
  965. service.SetCreatedInfo(data, p.GetCxtUserId(), p.GetCxtUserName())
  966. dataList = append(dataList, data)
  967. }
  968. _, err := projDao.NewProjBusinessFileDao(p.Tenant).Insert(&dataList)
  969. return err
  970. }
  971. // BusinessUpgradeNotify 项目升级 审批结果通知
  972. func (p *businessService) BusinessUpgradeNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  973. business, err := p.checkDingTalkNotify(flow, msg)
  974. if err != nil {
  975. return err
  976. }
  977. var data = g.Map{}
  978. var remark string
  979. if msg.ProcessType == "terminate" {
  980. data[p.Dao.C.ApproStatus] = ApprovalReturn
  981. }
  982. if msg.ProcessType == "finish" && msg.Result == "refuse" {
  983. data[p.Dao.C.ApproStatus] = ApprovalRejection
  984. }
  985. if msg.ProcessType == "finish" && msg.Result == "agree" {
  986. // 从项目动态内获取变更信息
  987. dynamics := new(model.ProjBusinessDynamics)
  988. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).Where(projDao.ProjBusinessDynamics.C.BusId, business.Id)
  989. err = dynamicsDao.Where(projDao.ProjBusinessDynamics.C.OpnType, OpnUpgrade).OrderDesc("created_time").Scan(dynamics)
  990. if err != nil {
  991. return err
  992. }
  993. updateData := new(model.BusinessUpgradeReq)
  994. gconv.Struct(dynamics.OpnContent, updateData)
  995. data = gconv.Map(updateData)
  996. data[p.Dao.C.ApproStatus] = ApprovalOK
  997. remarkMap := gconv.Map(dynamics.OpnContent)
  998. remark = gconv.String(g.Map{"nboType": remarkMap["nboType"], "origNboType": remarkMap["origNboType"]})
  999. }
  1000. // 项目修改
  1001. _, err = p.Dao.WherePri(business.Id).FieldsEx(service.UpdateFieldEx...).Data(data).Update()
  1002. if err != nil {
  1003. return err
  1004. }
  1005. // 添加项目动态
  1006. dynamics := model.ProjBusinessDynamics{
  1007. BusId: business.Id,
  1008. OpnType: OpnUpgradeApproval,
  1009. Remark: remark,
  1010. }
  1011. _, err = p.CreateProjBusinessDynamics(nil, dynamics, data)
  1012. if err != nil {
  1013. return err
  1014. }
  1015. return nil
  1016. }
  1017. // 获取项目的钉钉审批的降级类型
  1018. func (p *businessService) getBusDingDowngradeType(dbNboType, reqNboType string) string {
  1019. var downgradeType string
  1020. switch true {
  1021. case dbNboType == StatusB && reqNboType == StatusC: // B转C/option_0
  1022. downgradeType = "B转C"
  1023. case dbNboType == StatusA && reqNboType == StatusB: // A转B/option_1
  1024. downgradeType = "A转B"
  1025. case dbNboType == StatusA && reqNboType == StatusC: // A转C/option_2
  1026. downgradeType = "A转C"
  1027. case dbNboType == StatusA && reqNboType == StatusReserve: // A转储备/option_YZMFJYQQK6O0
  1028. downgradeType = "A转储备"
  1029. case dbNboType == StatusB && reqNboType == StatusReserve: // B转储备/option_232GR5NMFCSG0
  1030. downgradeType = "B转储备"
  1031. case dbNboType == StatusC && reqNboType == StatusReserve: // C转储备/option_1ZV2GJLDKQOW0
  1032. downgradeType = "C转储备"
  1033. default:
  1034. }
  1035. return downgradeType
  1036. }
  1037. // BusinessDowngrade 项目降级
  1038. func (p *businessService) BusinessDowngrade(req *model.BusinessDowngradeReq) error {
  1039. business, err := p.BusinessGradation(req.Id, req.NboType, "down")
  1040. if err != nil {
  1041. return err
  1042. }
  1043. downgradeType := p.getBusDingDowngradeType(business.NboType, req.NboType)
  1044. if downgradeType == "" {
  1045. return myerrors.TipsError("错误的降级类型")
  1046. }
  1047. businessMap := g.Map{
  1048. p.Dao.C.ApproStatus: ApprovalWaiting,
  1049. }
  1050. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  1051. opnContent := gconv.Map(req)
  1052. opnContent["origNboType"] = business.NboType
  1053. opnContent["approStatus"] = ApprovalWaiting
  1054. service.SetUpdatedInfo(opnContent, p.GetCxtUserId(), p.GetCxtUserName())
  1055. productLine, _ := service.GetDictLabelByTypeAndValue(p.Ctx, "sys_product_line", business.ProductLine)
  1056. // 审批流
  1057. workflowSrv, _ := workflowService.NewFlowService(p.Ctx)
  1058. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  1059. // 更新项目调级
  1060. _, err = p.Dao.TX(tx).WherePri(req.Id).Data(businessMap).Update()
  1061. if err != nil {
  1062. return err
  1063. }
  1064. // 添加项目动态
  1065. dynamics := model.ProjBusinessDynamics{
  1066. BusId: business.Id,
  1067. OpnType: OpnDowngrade,
  1068. Remark: req.Remark,
  1069. }
  1070. _, err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  1071. if err != nil {
  1072. return err
  1073. }
  1074. // OMS项目降级 审批
  1075. bizCode := business.NboCode + ":" + strconv.Itoa(business.Id)
  1076. _, err = workflowSrv.StartProcessInstance(bizCode, workflowModel.ProjectDownGrade, "", &workflow.StartProcessInstanceRequest{
  1077. ProcessCode: &BusinessDowngradeRequestProcessCode,
  1078. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  1079. {
  1080. Id: utils.String("TextField-K2AD4O5B"),
  1081. Name: utils.String("项目编码"),
  1082. Value: utils.String(business.NboCode),
  1083. },
  1084. {
  1085. Id: utils.String("TextField_BDLSECETVSG0"),
  1086. Name: utils.String("项目名称"),
  1087. Value: utils.String(business.NboName),
  1088. },
  1089. {
  1090. Id: utils.String("TextField_1J9BJMOZ18F40"),
  1091. Name: utils.String("客户名称"),
  1092. Value: utils.String(business.CustName),
  1093. },
  1094. {
  1095. Id: utils.String("TextField_GL7MQUB723K0"),
  1096. Name: utils.String("所在省"),
  1097. Value: utils.String(business.CustProvince),
  1098. },
  1099. {
  1100. Id: utils.String("TextField_CFA88QQQUUO0"),
  1101. Name: utils.String("所在市"),
  1102. Value: utils.String(business.CustCity),
  1103. },
  1104. {
  1105. Id: utils.String("DDSelectField_VSA3U380ZK00"),
  1106. Name: utils.String("降级类型"),
  1107. Value: utils.String(downgradeType),
  1108. },
  1109. {
  1110. Id: utils.String("DDSelectField_1UCNHJ0P8C5C0"),
  1111. Name: utils.String("产品线"),
  1112. Value: utils.String(productLine),
  1113. },
  1114. {
  1115. Id: utils.String("TextField_X4D3QGARU7K0"),
  1116. Name: utils.String("支持内容"),
  1117. Value: utils.String(req.TechnicalSupportContent),
  1118. },
  1119. {
  1120. Id: utils.String("TextField_AEUWH63LJ0O0"),
  1121. Name: utils.String("销售工程师"),
  1122. Value: utils.String(business.SaleName),
  1123. },
  1124. {
  1125. Id: utils.String("TextareaField_PTGJOKD3J7K0"),
  1126. Name: utils.String("降级原因"),
  1127. Value: utils.String(req.Remark),
  1128. },
  1129. },
  1130. })
  1131. if err != nil {
  1132. g.Log().Error(err)
  1133. return err
  1134. }
  1135. return nil
  1136. })
  1137. return err
  1138. }
  1139. // BusinessDowngradeNotify 项目降级 审批结果通知
  1140. func (p *businessService) BusinessDowngradeNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  1141. business, err := p.checkDingTalkNotify(flow, msg)
  1142. if err != nil {
  1143. return err
  1144. }
  1145. var data = g.Map{}
  1146. var remark string
  1147. if msg.ProcessType == "terminate" {
  1148. data[p.Dao.C.ApproStatus] = ApprovalReturn
  1149. }
  1150. if msg.ProcessType == "finish" && msg.Result == "refuse" {
  1151. data[p.Dao.C.ApproStatus] = ApprovalRejection
  1152. }
  1153. if msg.ProcessType == "finish" && msg.Result == "agree" {
  1154. // 从项目动态内获取变更信息
  1155. dynamics := new(model.ProjBusinessDynamics)
  1156. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).Where(projDao.ProjBusinessDynamics.C.BusId, business.Id)
  1157. err = dynamicsDao.Where(projDao.ProjBusinessDynamics.C.OpnType, OpnDowngrade).OrderDesc("created_time").Scan(dynamics)
  1158. if err != nil {
  1159. return err
  1160. }
  1161. updateData := new(model.BusinessDowngradeReq)
  1162. gconv.Struct(dynamics.OpnContent, updateData)
  1163. data = gconv.Map(updateData)
  1164. data[p.Dao.C.ApproStatus] = ApprovalOK
  1165. remarkMap := gconv.Map(dynamics.OpnContent)
  1166. remark = gconv.String(g.Map{"nboType": remarkMap["nboType"], "origNboType": remarkMap["origNboType"]})
  1167. }
  1168. // 项目修改
  1169. _, err = p.Dao.WherePri(business.Id).FieldsEx(service.UpdateFieldEx...).Data(data).Update()
  1170. if err != nil {
  1171. return err
  1172. }
  1173. // 添加项目动态
  1174. dynamics := model.ProjBusinessDynamics{
  1175. BusId: business.Id,
  1176. OpnType: OpnDowngradeApproval,
  1177. Remark: remark,
  1178. }
  1179. _, err = p.CreateProjBusinessDynamics(nil, dynamics, data)
  1180. if err != nil {
  1181. return err
  1182. }
  1183. return nil
  1184. }
  1185. // SetPrimacyContact 项目设置首要联系人
  1186. func (p *businessService) SetPrimacyContact(req *model.BusinessPrimacyContactReq) (err error) {
  1187. business, err := p.Dao.Where(projDao.ProjBusiness.C.Id, req.Id).One()
  1188. if err != nil {
  1189. return err
  1190. }
  1191. if business == nil {
  1192. return myerrors.TipsError("项目不存在。")
  1193. }
  1194. businessMap := g.Map{
  1195. p.Dao.C.ContactId: req.ContactId,
  1196. p.Dao.C.ContactName: req.ContactName,
  1197. p.Dao.C.ContactPostion: req.ContactPostion,
  1198. p.Dao.C.ContactTelephone: req.ContactTelephone,
  1199. }
  1200. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  1201. opnContent := gconv.Map(gconv.String(businessMap))
  1202. opnContent["origContactId"] = business.ContactId
  1203. opnContent["origContactName"] = business.ContactName
  1204. opnContent["origContactPostion"] = business.ContactPostion
  1205. opnContent["origContactTelephone"] = business.ContactTelephone
  1206. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  1207. // 更新项目
  1208. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.C.Id, req.Id).Data(businessMap).Update()
  1209. if err != nil {
  1210. return err
  1211. }
  1212. // 添加项目动态
  1213. dynamics := model.ProjBusinessDynamics{
  1214. BusId: req.Id,
  1215. OpnType: OpnPrimacyContact,
  1216. Remark: req.Remark,
  1217. }
  1218. _, err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  1219. return err
  1220. })
  1221. return err
  1222. }
  1223. // UpdateBusinessStatus 更新项目状态
  1224. func (p *businessService) UpdateBusinessStatus(req *model.UpdateBusinessStatusReq) error {
  1225. business, err := p.Dao.WherePri(req.Id).One()
  1226. if err != nil {
  1227. return err
  1228. }
  1229. if business == nil {
  1230. return myerrors.TipsError("项目不存在。")
  1231. }
  1232. businessMap := g.Map{
  1233. p.Dao.C.NboStatus: req.NboStatus,
  1234. p.Dao.C.Remark: req.Remark,
  1235. }
  1236. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  1237. opnContent := gconv.Map(gconv.String(businessMap))
  1238. opnContent["origNboStatus"] = business.NboStatus
  1239. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  1240. // 更新项目
  1241. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.C.Id, req.Id).Data(businessMap).Update()
  1242. if err != nil {
  1243. return err
  1244. }
  1245. // 添加项目动态
  1246. dynamics := model.ProjBusinessDynamics{
  1247. BusId: req.Id,
  1248. OpnType: OpnStatus,
  1249. Remark: req.Remark,
  1250. }
  1251. _, err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  1252. return err
  1253. })
  1254. return err
  1255. }
  1256. // CreateProjBusinessDynamics 创建项目动态
  1257. func (p *businessService) CreateProjBusinessDynamics(tx *gdb.TX, dynamics model.ProjBusinessDynamics, opnContent interface{}) (int64, error) {
  1258. if v, ok := opnContent.(g.Map); ok {
  1259. opnContent = utils.MapKeySnakeCamelCase(v)
  1260. }
  1261. // 添加项目动态
  1262. dynamics.OpnPeopleId = p.GetCxtUserId()
  1263. dynamics.OpnPeople = p.GetCxtUserName()
  1264. dynamics.OpnDate = gtime.Now()
  1265. dynamics.OpnContent = gconv.String(opnContent)
  1266. service.SetCreatedInfo(&dynamics, p.GetCxtUserId(), p.GetCxtUserName())
  1267. dao := projDao.NewProjBusinessDynamicsDao(p.Tenant).M
  1268. if tx != nil {
  1269. dao = dao.TX(tx)
  1270. }
  1271. lastId, err := dao.InsertAndGetId(&dynamics)
  1272. return lastId, err
  1273. }
  1274. // ConvertToReserve 转为储备项目
  1275. func (p *businessService) ConvertToReserve(req *model.BusinessToReserveReq) error {
  1276. business, err := p.Dao.WherePri(req.Id).WhereNot(p.Dao.C.ApproStatus, ApprovalWaiting).One()
  1277. if err != nil {
  1278. return err
  1279. }
  1280. if business == nil {
  1281. return myerrors.TipsError("项目已提交审批任务,无法重复提交。")
  1282. }
  1283. productLine, _ := service.GetDictLabelByTypeAndValue(p.Ctx, "sys_product_line", business.ProductLine)
  1284. // 审批流
  1285. workflowSrv, _ := workflowService.NewFlowService(p.Ctx)
  1286. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  1287. // 更新项目
  1288. businessMap := g.Map{
  1289. p.Dao.C.ApproStatus: ApprovalWaiting,
  1290. p.Dao.C.ProjConversionTime: gtime.Now(),
  1291. p.Dao.C.ProjConversionReason: req.ProjConversionReason,
  1292. }
  1293. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  1294. _, err = p.Dao.TX(tx).WherePri(business.Id).Data(businessMap).Update()
  1295. if err != nil {
  1296. return err
  1297. }
  1298. // 添加项目动态
  1299. dynamics := model.ProjBusinessDynamics{
  1300. BusId: business.Id,
  1301. OpnType: OpnToReserve,
  1302. Remark: req.ProjConversionReason,
  1303. }
  1304. _, err = p.CreateProjBusinessDynamics(tx, dynamics, businessMap)
  1305. if err != nil {
  1306. return err
  1307. }
  1308. // OMS项目转储备 审批
  1309. bizCode := business.NboCode + ":" + strconv.Itoa(business.Id)
  1310. _, err = workflowSrv.StartProcessInstance(bizCode, workflowModel.ProjectToReserve, "", &workflow.StartProcessInstanceRequest{
  1311. ProcessCode: &ConvertToReserveRequestProcessCode,
  1312. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  1313. {
  1314. Id: utils.String("TextField-K2AD4O5B"),
  1315. Name: utils.String("项目编码"),
  1316. Value: utils.String(business.NboCode),
  1317. },
  1318. {
  1319. Id: utils.String("TextField_CMH6TBXYR5S0"),
  1320. Name: utils.String("项目名称"),
  1321. Value: utils.String(business.NboName),
  1322. },
  1323. {
  1324. Id: utils.String("TextField_YQBGGYHQPS00"),
  1325. Name: utils.String("客户名称"),
  1326. Value: utils.String(business.CustName),
  1327. },
  1328. {
  1329. Id: utils.String("DDSelectField_VBY9YAIOK5C0"),
  1330. Name: utils.String("项目级别"),
  1331. Value: utils.String(convertToReserveType[business.NboType]),
  1332. },
  1333. {
  1334. Id: utils.String("DDSelectField_1UVBB1LZHIJK0"),
  1335. Name: utils.String("产品线"),
  1336. Value: utils.String(productLine),
  1337. },
  1338. {
  1339. Id: utils.String("TextField_1NDD3TY8KJB40"),
  1340. Name: utils.String("销售工程师"),
  1341. Value: utils.String(business.SaleName),
  1342. },
  1343. {
  1344. Id: utils.String("TextareaField_15KZFM4YHQ8W0"),
  1345. Name: utils.String("转化原因"),
  1346. Value: utils.String(req.ProjConversionReason),
  1347. },
  1348. },
  1349. })
  1350. if err != nil {
  1351. g.Log().Error(err)
  1352. return err
  1353. }
  1354. return nil
  1355. })
  1356. return err
  1357. }
  1358. // ConvertToReserveNotify 转为储备项目 审批结果通知
  1359. func (p *businessService) ConvertToReserveNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  1360. business, err := p.checkDingTalkNotify(flow, msg)
  1361. if err != nil {
  1362. return err
  1363. }
  1364. var data = g.Map{}
  1365. if msg.ProcessType == "terminate" {
  1366. data[p.Dao.C.ApproStatus] = ApprovalReturn
  1367. }
  1368. if msg.ProcessType == "finish" && msg.Result == "refuse" {
  1369. data[p.Dao.C.ApproStatus] = ApprovalRejection
  1370. }
  1371. if msg.ProcessType == "finish" && msg.Result == "agree" {
  1372. data[p.Dao.C.NboType] = StatusReserve
  1373. data[p.Dao.C.ApproStatus] = ApprovalOK
  1374. }
  1375. // 项目修改
  1376. _, err = p.Dao.WherePri(business.Id).FieldsEx(service.UpdateFieldEx...).Data(data).Update()
  1377. if err != nil {
  1378. return err
  1379. }
  1380. // 添加项目动态
  1381. dynamics := model.ProjBusinessDynamics{
  1382. BusId: business.Id,
  1383. OpnType: OpnToReserveApproval,
  1384. }
  1385. _, err = p.CreateProjBusinessDynamics(nil, dynamics, data)
  1386. if err != nil {
  1387. return err
  1388. }
  1389. return err
  1390. }
  1391. // 钉钉审批通知检查
  1392. func (p *businessService) checkDingTalkNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) (*model.ProjBusiness, error) {
  1393. bizCode := strings.Split(flow.BizCode, ":")
  1394. if len(bizCode) != 2 {
  1395. return nil, fmt.Errorf("项目审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  1396. }
  1397. nboCode := bizCode[0]
  1398. busId, err := strconv.Atoi(bizCode[1])
  1399. if err != nil {
  1400. return nil, fmt.Errorf("项目审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  1401. }
  1402. if msg.ProcessType != "finish" && msg.ProcessType != "terminate" {
  1403. return nil, fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
  1404. }
  1405. if msg.Result != "agree" && msg.Result != "refuse" && msg.Result != "" {
  1406. return nil, fmt.Errorf("无法识别的 Result :%s", msg.Result)
  1407. }
  1408. fmt.Println(msg)
  1409. business, err := p.Dao.WherePri(busId).Where(p.Dao.C.NboCode, nboCode).One()
  1410. if err != nil {
  1411. return nil, err
  1412. }
  1413. if business == nil {
  1414. return nil, fmt.Errorf("项目不存在:%s Id: %d", flow.BizCode, flow.Id)
  1415. }
  1416. return business, nil
  1417. }