business.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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.GetPage()).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. var file []contractModel.DingFileInfo
  656. if args != nil {
  657. resp, err := dingtalk.Client.GetStorage().UploadFile(service.DingTalkSpaceId, p.GetCxtUserDingtalkId(), args.FileName, args.File.Name())
  658. if err != nil {
  659. return fmt.Errorf("钉钉上传文件异常 %s", err.Error())
  660. }
  661. g.Log().Info("项目转A类提交大数参数文件", resp)
  662. file = []contractModel.DingFileInfo{{
  663. SpaceId: resp.Dentry.SpaceId,
  664. FileId: resp.Dentry.Id,
  665. FileName: resp.Dentry.Name,
  666. FileSize: resp.Dentry.Size,
  667. FileType: resp.Dentry.Extension,
  668. }}
  669. err = p.txCreateBusinessFile(business.Id, file)
  670. if err != nil {
  671. return err
  672. }
  673. }
  674. processCode := &BusinessUpgradeARequestProcessCode
  675. dingReq = &workflow.StartProcessInstanceRequest{
  676. ProcessCode: processCode,
  677. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  678. {
  679. Id: utils.String("TextField-K2AD4O5B"),
  680. Name: utils.String("项目编码"),
  681. Value: utils.String(business.NboCode),
  682. },
  683. {
  684. Id: utils.String("TextField_BDLSECETVSG0"),
  685. Name: utils.String("项目名称"),
  686. Value: utils.String(business.NboName),
  687. },
  688. {
  689. Id: utils.String("DDSelectField_VSA3U380ZK00"),
  690. Name: utils.String("升级类型"),
  691. Value: utils.String(upgradeType),
  692. },
  693. {
  694. Id: utils.String("TextField_1J9BJMOZ18F40"),
  695. Name: utils.String("客户名称"),
  696. Value: utils.String(business.CustName),
  697. },
  698. {
  699. Id: utils.String("NumberField_1F88MCD0W8KG0"),
  700. Name: utils.String("项目预算"),
  701. Value: utils.String(gconv.String(req.NboBudget)),
  702. },
  703. {
  704. Id: utils.String("TextField_1PWK6WHMGITC0"),
  705. Name: utils.String("经销商/代理商"),
  706. Value: utils.String(req.DistributorName),
  707. },
  708. {
  709. Id: utils.String("TextField_X4D3QGARU7K0"),
  710. Name: utils.String("支持内容"),
  711. Value: utils.String(req.TechnicalSupportContent),
  712. },
  713. {
  714. Id: utils.String("TextField_AEUWH63LJ0O0"),
  715. Name: utils.String("销售工程师"),
  716. Value: utils.String(business.SaleName),
  717. },
  718. {
  719. Id: utils.String("DDDateField_1FW1QZQYBZVK0"),
  720. Name: utils.String("采购时间"),
  721. Value: utils.String(gconv.String(req.PurchasingTime.Format("Y-m-d"))),
  722. },
  723. {
  724. Id: utils.String("DDSelectField_21ASEWDIB3MO0"),
  725. Name: utils.String("采购方式"),
  726. Value: utils.String(gconv.String(purchasingWayType[req.PurchasingWay])),
  727. },
  728. {
  729. Id: utils.String("DDSelectField_5R11VVM6GI00"),
  730. Name: utils.String("是否我司参数"),
  731. Value: utils.String(gconv.String(yesOrNoType[req.IsAdoptDashoo])),
  732. },
  733. {
  734. Id: utils.String("DDAttachment_11Q7DBRKE6HC0"),
  735. Name: utils.String("附件"),
  736. Value: utils.String(gconv.String(file)),
  737. },
  738. {
  739. Id: utils.String("TextareaField_1GEL8JJL3H5S0"),
  740. Name: utils.String("备注"),
  741. Value: utils.String(req.Remark),
  742. },
  743. },
  744. }
  745. default:
  746. return nil
  747. }
  748. _, err = workflowSrv.StartProcessInstance(bizCode, workflowModel.ProjectUpGrade, "", dingReq)
  749. if err != nil {
  750. g.Log().Error(err)
  751. return err
  752. }
  753. return nil
  754. }
  755. // 采用大数参数文件记录
  756. func (p *businessService) txCreateBusinessFile(busId int, files []contractModel.DingFileInfo) error {
  757. dataList := make([]*model.ProjBusinessFile, 0)
  758. for _, v := range files {
  759. data := new(model.ProjBusinessFile)
  760. data.BusId = busId
  761. data.FileName = v.FileName
  762. data.FileSource = "项目转为A类采用大数参数文件"
  763. data.FileSize = gconv.String(v.FileSize)
  764. data.FileUrl = strings.Join([]string{"dingtalk", v.SpaceId, v.FileId}, ":")
  765. service.SetCreatedInfo(data, p.GetCxtUserId(), p.GetCxtUserName())
  766. dataList = append(dataList, data)
  767. }
  768. _, err := projDao.NewProjBusinessFileDao(p.Tenant).Insert(&dataList)
  769. return err
  770. }
  771. // BusinessUpgradeNotify 项目升级 审批结果通知
  772. func (p *businessService) BusinessUpgradeNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  773. business, err := p.checkDingTalkNotify(flow, msg)
  774. if err != nil {
  775. return err
  776. }
  777. var data = g.Map{}
  778. if msg.ProcessType == "terminate" {
  779. data[p.Dao.C.ApproStatus] = ApprovalReturn
  780. }
  781. if msg.ProcessType == "finish" && msg.Result == "refuse" {
  782. data[p.Dao.C.ApproStatus] = ApprovalRejection
  783. }
  784. if msg.ProcessType == "finish" && msg.Result == "agree" {
  785. // 从项目动态内获取变更信息
  786. dynamics := new(model.ProjBusinessDynamics)
  787. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).Where(projDao.ProjBusinessDynamics.C.BusId, business.Id)
  788. err = dynamicsDao.Where(projDao.ProjBusinessDynamics.C.OpnType, OpnUpgrade).OrderDesc("created_time").Scan(dynamics)
  789. if err != nil {
  790. return err
  791. }
  792. updateData := new(model.BusinessUpgradeReq)
  793. gconv.Struct(dynamics.OpnContent, updateData)
  794. data = gconv.Map(updateData)
  795. data[p.Dao.C.ApproStatus] = ApprovalOK
  796. }
  797. // 项目修改
  798. _, err = p.Dao.WherePri(business.Id).FieldsEx(service.UpdateFieldEx...).Data(data).Update()
  799. if err != nil {
  800. return err
  801. }
  802. // 添加项目动态
  803. dynamics := model.ProjBusinessDynamics{
  804. BusId: business.Id,
  805. OpnType: OpnUpgradeApproval,
  806. }
  807. _, err = p.CreateProjBusinessDynamics(nil, dynamics, data)
  808. if err != nil {
  809. return err
  810. }
  811. return nil
  812. }
  813. // 获取项目的钉钉审批的降级类型
  814. func (p *businessService) getBusDingDowngradeType(dbNboType, reqNboType string) string {
  815. var downgradeType string
  816. switch true {
  817. case dbNboType == StatusB && reqNboType == StatusC: // B转C/option_0
  818. downgradeType = "B转C"
  819. case dbNboType == StatusA && reqNboType == StatusB: // A转B/option_1
  820. downgradeType = "A转B"
  821. case dbNboType == StatusA && reqNboType == StatusC: // A转C/option_2
  822. downgradeType = "A转C"
  823. case dbNboType == StatusA && reqNboType == StatusReserve: // A转储备/option_YZMFJYQQK6O0
  824. downgradeType = "A转储备"
  825. case dbNboType == StatusB && reqNboType == StatusReserve: // B转储备/option_232GR5NMFCSG0
  826. downgradeType = "B转储备"
  827. case dbNboType == StatusC && reqNboType == StatusReserve: // C转储备/option_1ZV2GJLDKQOW0
  828. downgradeType = "C转储备"
  829. default:
  830. }
  831. return downgradeType
  832. }
  833. // BusinessDowngrade 项目降级
  834. func (p *businessService) BusinessDowngrade(req *model.BusinessDowngradeReq) error {
  835. business, err := p.BusinessGradation(req.Id, req.NboType, "down")
  836. if err != nil {
  837. return err
  838. }
  839. downgradeType := p.getBusDingDowngradeType(business.NboType, req.NboType)
  840. if downgradeType == "" {
  841. return myerrors.TipsError("错误的降级类型")
  842. }
  843. businessMap := g.Map{
  844. p.Dao.C.ApproStatus: ApprovalWaiting,
  845. }
  846. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  847. opnContent := gconv.Map(req)
  848. opnContent["origNboType"] = business.NboType
  849. opnContent["approStatus"] = ApprovalWaiting
  850. service.SetUpdatedInfo(opnContent, p.GetCxtUserId(), p.GetCxtUserName())
  851. // 审批流
  852. workflowSrv, _ := workflowService.NewFlowService(p.Ctx)
  853. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  854. // 更新项目调级
  855. _, err = p.Dao.TX(tx).WherePri(req.Id).Data(businessMap).Update()
  856. if err != nil {
  857. return err
  858. }
  859. // 添加项目动态
  860. dynamics := model.ProjBusinessDynamics{
  861. BusId: business.Id,
  862. OpnType: OpnDowngrade,
  863. Remark: req.Remark,
  864. }
  865. _, err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  866. if err != nil {
  867. return err
  868. }
  869. // OMS项目降级 审批
  870. bizCode := business.NboCode + ":" + strconv.Itoa(business.Id)
  871. _, err = workflowSrv.StartProcessInstance(bizCode, workflowModel.ProjectDownGrade, "", &workflow.StartProcessInstanceRequest{
  872. ProcessCode: &BusinessDowngradeRequestProcessCode,
  873. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  874. {
  875. Id: utils.String("TextField-K2AD4O5B"),
  876. Name: utils.String("项目编码"),
  877. Value: utils.String(business.NboCode),
  878. },
  879. {
  880. Id: utils.String("TextField_BDLSECETVSG0"),
  881. Name: utils.String("项目名称"),
  882. Value: utils.String(business.NboName),
  883. },
  884. {
  885. Id: utils.String("TextField_1J9BJMOZ18F40"),
  886. Name: utils.String("客户名称"),
  887. Value: utils.String(business.CustName),
  888. },
  889. {
  890. Id: utils.String("TextField_GL7MQUB723K0"),
  891. Name: utils.String("所在省"),
  892. Value: utils.String(business.CustProvince),
  893. },
  894. {
  895. Id: utils.String("TextField_CFA88QQQUUO0"),
  896. Name: utils.String("所在市"),
  897. Value: utils.String(business.CustCity),
  898. },
  899. {
  900. Id: utils.String("DDSelectField_VSA3U380ZK00"),
  901. Name: utils.String("降级类型"),
  902. Value: utils.String(downgradeType),
  903. },
  904. {
  905. Id: utils.String("TextField_X4D3QGARU7K0"),
  906. Name: utils.String("支持内容"),
  907. Value: utils.String(req.TechnicalSupportContent),
  908. },
  909. {
  910. Id: utils.String("TextField_AEUWH63LJ0O0"),
  911. Name: utils.String("销售工程师"),
  912. Value: utils.String(business.SaleName),
  913. },
  914. {
  915. Id: utils.String("TextareaField_PTGJOKD3J7K0"),
  916. Name: utils.String("降级原因"),
  917. Value: utils.String(req.Remark),
  918. },
  919. },
  920. })
  921. if err != nil {
  922. g.Log().Error(err)
  923. return err
  924. }
  925. return nil
  926. })
  927. return err
  928. }
  929. // BusinessDowngradeNotify 项目降级 审批结果通知
  930. func (p *businessService) BusinessDowngradeNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  931. business, err := p.checkDingTalkNotify(flow, msg)
  932. if err != nil {
  933. return err
  934. }
  935. var data = g.Map{}
  936. if msg.ProcessType == "terminate" {
  937. data[p.Dao.C.ApproStatus] = ApprovalReturn
  938. }
  939. if msg.ProcessType == "finish" && msg.Result == "refuse" {
  940. data[p.Dao.C.ApproStatus] = ApprovalRejection
  941. }
  942. if msg.ProcessType == "finish" && msg.Result == "agree" {
  943. // 从项目动态内获取变更信息
  944. dynamics := new(model.ProjBusinessDynamics)
  945. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).Where(projDao.ProjBusinessDynamics.C.BusId, business.Id)
  946. err = dynamicsDao.Where(projDao.ProjBusinessDynamics.C.OpnType, OpnDowngrade).OrderDesc("created_time").Scan(dynamics)
  947. if err != nil {
  948. return err
  949. }
  950. updateData := new(model.BusinessDowngradeReq)
  951. gconv.Struct(dynamics.OpnContent, updateData)
  952. data = gconv.Map(updateData)
  953. data[p.Dao.C.ApproStatus] = ApprovalOK
  954. }
  955. // 项目修改
  956. _, err = p.Dao.WherePri(business.Id).FieldsEx(service.UpdateFieldEx...).Data(data).Update()
  957. if err != nil {
  958. return err
  959. }
  960. // 添加项目动态
  961. dynamics := model.ProjBusinessDynamics{
  962. BusId: business.Id,
  963. OpnType: OpnDowngradeApproval,
  964. }
  965. _, err = p.CreateProjBusinessDynamics(nil, dynamics, data)
  966. if err != nil {
  967. return err
  968. }
  969. return nil
  970. }
  971. // SetPrimacyContact 项目设置首要联系人
  972. func (p *businessService) SetPrimacyContact(req *model.BusinessPrimacyContactReq) (err error) {
  973. business, err := p.Dao.Where(projDao.ProjBusiness.C.Id, req.Id).One()
  974. if err != nil {
  975. return err
  976. }
  977. if business == nil {
  978. return myerrors.TipsError("项目不存在。")
  979. }
  980. businessMap := g.Map{
  981. p.Dao.C.ContactId: req.ContactId,
  982. p.Dao.C.ContactName: req.ContactName,
  983. p.Dao.C.ContactPostion: req.ContactPostion,
  984. p.Dao.C.ContactTelephone: req.ContactTelephone,
  985. }
  986. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  987. opnContent := businessMap
  988. opnContent["origContactId"] = business.ContactId
  989. opnContent["origContactName"] = business.ContactName
  990. opnContent["origContactPostion"] = business.ContactPostion
  991. opnContent["origContactTelephone"] = business.ContactTelephone
  992. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  993. // 更新项目
  994. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.C.Id, req.Id).Data(businessMap).Update()
  995. if err != nil {
  996. return err
  997. }
  998. // 添加项目动态
  999. dynamics := model.ProjBusinessDynamics{
  1000. BusId: req.Id,
  1001. OpnType: OpnPrimacyContact,
  1002. Remark: req.Remark,
  1003. }
  1004. _, err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  1005. return err
  1006. })
  1007. return err
  1008. }
  1009. // UpdateBusinessStatus 更新项目状态
  1010. func (p *businessService) UpdateBusinessStatus(req *model.UpdateBusinessStatusReq) error {
  1011. business, err := p.Dao.WherePri(req.Id).One()
  1012. if err != nil {
  1013. return err
  1014. }
  1015. if business == nil {
  1016. return myerrors.TipsError("项目不存在。")
  1017. }
  1018. businessMap := g.Map{
  1019. p.Dao.C.NboStatus: req.NboStatus,
  1020. p.Dao.C.Remark: req.Remark,
  1021. }
  1022. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  1023. opnContent := businessMap
  1024. opnContent["origNboStatus"] = business.NboStatus
  1025. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  1026. // 更新项目
  1027. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.C.Id, req.Id).Data(businessMap).Update()
  1028. if err != nil {
  1029. return err
  1030. }
  1031. // 添加项目动态
  1032. dynamics := model.ProjBusinessDynamics{
  1033. BusId: req.Id,
  1034. OpnType: OpnStatus,
  1035. Remark: req.Remark,
  1036. }
  1037. _, err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  1038. return err
  1039. })
  1040. return err
  1041. }
  1042. // CreateProjBusinessDynamics 创建项目动态
  1043. func (p *businessService) CreateProjBusinessDynamics(tx *gdb.TX, dynamics model.ProjBusinessDynamics, opnContent interface{}) (int64, error) {
  1044. if v, ok := opnContent.(g.Map); ok {
  1045. opnContent = utils.MapKeySnakeCamelCase(v)
  1046. }
  1047. // 添加项目动态
  1048. dynamics.OpnPeopleId = p.GetCxtUserId()
  1049. dynamics.OpnPeople = p.GetCxtUserName()
  1050. dynamics.OpnDate = gtime.Now()
  1051. dynamics.OpnContent = gconv.String(opnContent)
  1052. service.SetCreatedInfo(&dynamics, p.GetCxtUserId(), p.GetCxtUserName())
  1053. dao := projDao.NewProjBusinessDynamicsDao(p.Tenant).M
  1054. if tx != nil {
  1055. dao = dao.TX(tx)
  1056. }
  1057. lastId, err := dao.InsertAndGetId(&dynamics)
  1058. return lastId, err
  1059. }
  1060. // ConvertToReserve 转为储备项目
  1061. func (p *businessService) ConvertToReserve(req *model.BusinessToReserveReq) error {
  1062. business, err := p.Dao.WherePri(req.Id).WhereNot(p.Dao.C.ApproStatus, ApprovalWaiting).One()
  1063. if err != nil {
  1064. return err
  1065. }
  1066. if business == nil {
  1067. return myerrors.TipsError("项目已提交审批任务,无法重复提交。")
  1068. }
  1069. // 审批流
  1070. workflowSrv, _ := workflowService.NewFlowService(p.Ctx)
  1071. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  1072. // 更新项目
  1073. businessMap := g.Map{
  1074. p.Dao.C.ApproStatus: ApprovalWaiting,
  1075. p.Dao.C.ProjConversionTime: gtime.Now(),
  1076. p.Dao.C.ProjConversionReason: req.ProjConversionReason,
  1077. }
  1078. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  1079. _, err = p.Dao.TX(tx).WherePri(business.Id).Data(businessMap).Update()
  1080. if err != nil {
  1081. return err
  1082. }
  1083. // 添加项目动态
  1084. dynamics := model.ProjBusinessDynamics{
  1085. BusId: business.Id,
  1086. OpnType: OpnToReserve,
  1087. Remark: req.ProjConversionReason,
  1088. }
  1089. _, err = p.CreateProjBusinessDynamics(tx, dynamics, businessMap)
  1090. if err != nil {
  1091. return err
  1092. }
  1093. // OMS项目转储备 审批
  1094. bizCode := business.NboCode + ":" + strconv.Itoa(business.Id)
  1095. _, err = workflowSrv.StartProcessInstance(bizCode, workflowModel.ProjectToReserve, "", &workflow.StartProcessInstanceRequest{
  1096. ProcessCode: &ConvertToReserveRequestProcessCode,
  1097. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  1098. {
  1099. Id: utils.String("TextField-K2AD4O5B"),
  1100. Name: utils.String("项目编码"),
  1101. Value: utils.String(business.NboCode),
  1102. },
  1103. {
  1104. Id: utils.String("TextField_CMH6TBXYR5S0"),
  1105. Name: utils.String("项目名称"),
  1106. Value: utils.String(business.NboName),
  1107. },
  1108. {
  1109. Id: utils.String("TextField_YQBGGYHQPS00"),
  1110. Name: utils.String("客户名称"),
  1111. Value: utils.String(business.CustName),
  1112. },
  1113. {
  1114. Id: utils.String("DDSelectField_VBY9YAIOK5C0"),
  1115. Name: utils.String("项目级别"),
  1116. Value: utils.String(convertToReserveType[business.NboType]),
  1117. },
  1118. {
  1119. Id: utils.String("TextField_1NDD3TY8KJB40"),
  1120. Name: utils.String("销售工程师"),
  1121. Value: utils.String(business.SaleName),
  1122. },
  1123. {
  1124. Id: utils.String("TextareaField_15KZFM4YHQ8W0"),
  1125. Name: utils.String("转化原因"),
  1126. Value: utils.String(req.ProjConversionReason),
  1127. },
  1128. },
  1129. })
  1130. if err != nil {
  1131. g.Log().Error(err)
  1132. return err
  1133. }
  1134. return nil
  1135. })
  1136. return err
  1137. }
  1138. // ConvertToReserveNotify 转为储备项目 审批结果通知
  1139. func (p *businessService) ConvertToReserveNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  1140. business, err := p.checkDingTalkNotify(flow, msg)
  1141. if err != nil {
  1142. return err
  1143. }
  1144. var data = g.Map{}
  1145. if msg.ProcessType == "terminate" {
  1146. data[p.Dao.C.ApproStatus] = ApprovalReturn
  1147. }
  1148. if msg.ProcessType == "finish" && msg.Result == "refuse" {
  1149. data[p.Dao.C.ApproStatus] = ApprovalRejection
  1150. }
  1151. if msg.ProcessType == "finish" && msg.Result == "agree" {
  1152. data[p.Dao.C.NboType] = StatusReserve
  1153. data[p.Dao.C.ApproStatus] = ApprovalOK
  1154. }
  1155. // 项目修改
  1156. _, err = p.Dao.WherePri(business.Id).FieldsEx(service.UpdateFieldEx...).Data(data).Update()
  1157. if err != nil {
  1158. return err
  1159. }
  1160. // 添加项目动态
  1161. dynamics := model.ProjBusinessDynamics{
  1162. BusId: business.Id,
  1163. OpnType: OpnToReserveApproval,
  1164. }
  1165. _, err = p.CreateProjBusinessDynamics(nil, dynamics, data)
  1166. if err != nil {
  1167. return err
  1168. }
  1169. return err
  1170. }
  1171. // 钉钉审批通知检查
  1172. func (p *businessService) checkDingTalkNotify(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) (*model.ProjBusiness, error) {
  1173. bizCode := strings.Split(flow.BizCode, ":")
  1174. if len(bizCode) != 2 {
  1175. return nil, fmt.Errorf("项目转储备审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  1176. }
  1177. nboCode := bizCode[0]
  1178. busId, err := strconv.Atoi(bizCode[1])
  1179. if err != nil {
  1180. return nil, fmt.Errorf("项目转储备审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  1181. }
  1182. if msg.ProcessType != "finish" && msg.ProcessType != "terminate" {
  1183. return nil, fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
  1184. }
  1185. if msg.Result != "agree" && msg.Result != "refuse" && msg.Result != "" {
  1186. return nil, fmt.Errorf("无法识别的 Result :%s", msg.Result)
  1187. }
  1188. fmt.Println(msg)
  1189. business, err := p.Dao.WherePri(busId).Where(p.Dao.C.NboCode, nboCode).One()
  1190. if err != nil {
  1191. return nil, err
  1192. }
  1193. if business == nil {
  1194. return nil, fmt.Errorf("项目不存在:%s Id: %d", flow.BizCode, flow.Id)
  1195. }
  1196. return business, nil
  1197. }