business.go 33 KB

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