business.go 40 KB

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