business.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package proj
  2. import (
  3. "context"
  4. projDao "dashoo.cn/micro/app/dao/proj"
  5. model "dashoo.cn/micro/app/model/proj"
  6. "dashoo.cn/micro/app/service"
  7. "dashoo.cn/opms_libary/myerrors"
  8. "dashoo.cn/opms_libary/utils"
  9. "github.com/gogf/gf/database/gdb"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/os/gtime"
  12. "github.com/gogf/gf/util/gconv"
  13. "github.com/shopspring/decimal"
  14. "strconv"
  15. "strings"
  16. )
  17. const (
  18. OpnCreate = "10" // 创建动态
  19. OpnUpdate = "20" // 更新动态
  20. OpnTransfer = "30" // 转移动态
  21. OpnRise = "40" // 升级动态
  22. OpnDrop = "50" // 降级动态
  23. OpnPrimacyContact = "60" // 设置首要联系人动态
  24. OpnStatus = "70" // 更新项目状态动态
  25. OpnAssociation = "80" // 关联联系人动态
  26. OpnDisassociation = "90" // 解除关联联系人动态
  27. )
  28. type businessService struct {
  29. *service.ContextService
  30. Dao *projDao.ProjBusinessDao
  31. }
  32. func NewBusinessService(ctx context.Context) (svc *businessService, err error) {
  33. svc = new(businessService)
  34. if svc.ContextService, err = svc.Init(ctx); err != nil {
  35. return nil, err
  36. }
  37. svc.Dao = projDao.NewProjBusinessDao(svc.Tenant)
  38. return svc, nil
  39. }
  40. func (p *businessService) GetList(req *model.ProjBusinessSearchReq) (total int, businessList []*model.ProjBusiness, err error) {
  41. db := p.Dao.M
  42. if req.NboName != "" {
  43. db = db.WhereLike(p.Dao.Columns.NboName, "%"+req.NboName+"%")
  44. }
  45. if req.CustName != "" {
  46. db = db.WhereLike(p.Dao.Columns.CustName, "%"+req.CustName+"%")
  47. }
  48. if req.SaleName != "" {
  49. db = db.WhereLike(p.Dao.Columns.SaleName, "%"+req.SaleName+"%")
  50. }
  51. if req.NboType != "" {
  52. db = db.Where(p.Dao.Columns.NboType, req.NboType)
  53. }
  54. total, err = db.Count()
  55. if err != nil {
  56. g.Log().Error(err)
  57. err = myerrors.DbError("获取总行数失败。")
  58. return
  59. }
  60. err = db.Page(req.PageNum, req.PageSize).Order("id asc").Scan(&businessList)
  61. return
  62. }
  63. func (p *businessService) GetEntityById(id int64) (business *model.ProjBusiness, err error) {
  64. err = p.Dao.Where(projDao.ProjBusiness.Columns.Id, id).Scan(&business)
  65. return
  66. }
  67. func (p *businessService) GetBusinessProduct(id int64) (productList []*model.ProjBusinessProduct, err error) {
  68. productDao := projDao.NewProjBusinessProductDao(p.Tenant)
  69. err = productDao.Where(productDao.ProjBusinessProductDao.Columns.BusId, id).Scan(&productList)
  70. return
  71. }
  72. func (p *businessService) GetBusinessDynamics(req *model.BusinessReq) (total int, result g.MapStrAny, err error) {
  73. result = make(g.MapStrAny, 0)
  74. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).ProjBusinessDynamicsDao.Where(projDao.ProjBusinessDynamics.Columns.BusId, req.BusId)
  75. total, err = dynamicsDao.Count()
  76. if err != nil {
  77. g.Log().Error(err)
  78. return
  79. }
  80. dynamicsList, err := dynamicsDao.Page(req.GetPage()).Order("created_time desc").All()
  81. if err != nil || dynamicsList == nil {
  82. return
  83. }
  84. // 数据树格式转换
  85. opnDateFlag := gtime.New(dynamicsList[0].OpnDate).Format("Y-m-d")
  86. for k, v := range dynamicsList {
  87. opnDate := gtime.New(v.OpnDate).Format("Y-m-d")
  88. if opnDateFlag == opnDate && k != 0 {
  89. result[opnDate] = append(result[opnDate].(g.ListStrAny), g.Map{
  90. "opnPeople": v.OpnPeople,
  91. "opnDate": v.OpnDate,
  92. "opnType": v.OpnType,
  93. "remark": v.Remark,
  94. "opnContent": gconv.Map(v.OpnContent),
  95. })
  96. } else {
  97. temp := make(g.ListStrAny, 0)
  98. temp = append(temp, g.Map{
  99. "opnPeople": v.OpnPeople,
  100. "opnDate": v.OpnDate,
  101. "opnType": v.OpnType,
  102. "remark": v.Remark,
  103. "opnContent": gconv.Map(v.OpnContent),
  104. })
  105. result[opnDate] = temp
  106. }
  107. }
  108. return
  109. }
  110. func (p *businessService) GetBusinessDynamicsList(req *model.BusinessDynamicsReq) (total int, list []map[string]interface{}, err error) {
  111. dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).ProjBusinessDynamicsDao.Where(projDao.ProjBusinessDynamics.Columns.BusId, req.BusId)
  112. if req.OpnType != "" {
  113. dynamicsDao = dynamicsDao.Where(projDao.ProjBusinessDynamics.Columns.OpnType+" = ?", req.OpnType)
  114. }
  115. total, err = dynamicsDao.Count()
  116. if err != nil {
  117. g.Log().Error(err)
  118. return
  119. }
  120. dynamicsList, err := dynamicsDao.Page(req.GetPage()).Order("created_time desc").All()
  121. for _, v := range dynamicsList {
  122. val := gconv.Map(v)
  123. val["opnContent"] = gconv.Map(v.OpnContent)
  124. list = append(list, val)
  125. }
  126. return
  127. }
  128. func (p *businessService) Create(req *model.AddProjBusinessReq) (err error) {
  129. // 设置默认联系人
  130. contact := g.Map{
  131. projDao.ProjBusinessContact.Columns.ContactId: req.ContactId,
  132. }
  133. service.SetCreatedInfo(contact, p.GetCxtUserId(), p.GetCxtUserName())
  134. // 设置产品信息
  135. totalPrice, products, err := p.setProductInfo(0, req.Products)
  136. if err != nil {
  137. return err
  138. }
  139. // 初始化项目信息
  140. businessData := new(model.ProjBusiness)
  141. if err = gconv.Struct(req, businessData); err != nil {
  142. return
  143. }
  144. businessData.NboCode = "NBO" + strconv.Itoa(int(gtime.Timestamp()))
  145. businessData.NboStatus = "10"
  146. businessData.NboType = "C"
  147. businessData.ApproStatus = "10"
  148. businessData.EstTransPrice = totalPrice
  149. businessData.DeptId = p.GetCxtUserDeptId()
  150. service.SetCreatedInfo(businessData, p.GetCxtUserId(), p.GetCxtUserName())
  151. // 事务
  152. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  153. // 添加项目
  154. lastId, err := p.Dao.TX(tx).InsertAndGetId(businessData)
  155. if err != nil {
  156. return err
  157. }
  158. // 创建了联系人
  159. contact[projDao.ProjBusinessContact.Columns.BusId] = lastId
  160. _, err = projDao.NewProjBusinessContactDao(p.Tenant).TX(tx).Insert(contact)
  161. if err != nil {
  162. return err
  163. }
  164. // 处理项目产品信息
  165. for _, v := range products {
  166. v.BusId = int(lastId)
  167. }
  168. // 添加项目产品
  169. _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Insert(products)
  170. if err != nil {
  171. return err
  172. }
  173. // 添加项目动态
  174. dynamics := model.ProjBusinessDynamics{
  175. BusId: int(lastId),
  176. OpnType: OpnCreate,
  177. Remark: businessData.Remark,
  178. }
  179. err = p.CreateProjBusinessDynamics(tx, dynamics, businessData)
  180. return err
  181. })
  182. return
  183. }
  184. // setProductInfo 设置产品信息
  185. func (p *businessService) setProductInfo(busId int, productInfo []model.BusinessProduct) (total float64, products []*model.ProjBusinessProduct, err error) {
  186. products = make([]*model.ProjBusinessProduct, len(productInfo))
  187. if err = gconv.Structs(productInfo, &products); err != nil {
  188. return 0, nil, err
  189. }
  190. var totalPrice decimal.Decimal
  191. for _, v := range products {
  192. v.Id = 0
  193. v.BusId = busId
  194. v.TotalPrice = decimal.NewFromFloat(v.ProdPrice).Mul(decimal.NewFromInt(int64(v.ProdNum))).InexactFloat64()
  195. totalPrice = totalPrice.Add(decimal.NewFromFloat(v.TotalPrice))
  196. service.SetCreatedInfo(v, p.GetCxtUserId(), p.GetCxtUserName())
  197. }
  198. return totalPrice.InexactFloat64(), products, nil
  199. }
  200. func (p *businessService) UpdateById(req *model.UpdateProjBusinessReq) error {
  201. record, err := p.Dao.WherePri(req.Id).Count()
  202. if err != nil {
  203. return err
  204. }
  205. if record == 0 {
  206. return myerrors.TipsError("项目不存在。")
  207. }
  208. // 设置产品信息
  209. totalPrice, products, err := p.setProductInfo(req.Id, req.Products)
  210. if err != nil {
  211. return err
  212. }
  213. // 设置默认联系人
  214. contact := g.Map{
  215. projDao.ProjBusinessContact.Columns.BusId: req.Id,
  216. projDao.ProjBusinessContact.Columns.ContactId: req.ContactId,
  217. }
  218. contactFlag, err := projDao.NewProjBusinessContactDao(p.Tenant).Where(contact).Count()
  219. if err != nil {
  220. return err
  221. }
  222. if contactFlag == 0 {
  223. service.SetCreatedInfo(contact, p.GetCxtUserId(), p.GetCxtUserName())
  224. }
  225. // 设置项目信息
  226. req.EstTransPrice = totalPrice
  227. businessData := gconv.Map(req)
  228. service.SetUpdatedInfo(businessData, p.GetCxtUserId(), p.GetCxtUserName())
  229. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  230. // 更新项目
  231. _, err = p.Dao.TX(tx).FieldsEx(service.UpdateFieldEx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Update(businessData)
  232. if err != nil {
  233. return err
  234. }
  235. // 删除项目产品
  236. _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Where(projDao.ProjBusinessProduct.Columns.BusId, req.Id).Delete()
  237. if err != nil {
  238. return err
  239. }
  240. // 添加项目产品
  241. _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Insert(products)
  242. if err != nil {
  243. return err
  244. }
  245. // 关联联系人
  246. if contactFlag == 0 {
  247. _, err = projDao.NewProjBusinessContactDao(p.Tenant).TX(tx).Insert(contact)
  248. if err != nil {
  249. return err
  250. }
  251. }
  252. // 添加项目动态
  253. dynamics := model.ProjBusinessDynamics{
  254. BusId: req.Id,
  255. OpnType: OpnUpdate,
  256. Remark: req.Remark,
  257. }
  258. err = p.CreateProjBusinessDynamics(tx, dynamics, req)
  259. return err
  260. })
  261. return err
  262. }
  263. func (p *businessService) DeleteByIds(ids []int64) (err error) {
  264. _, err = p.Dao.WhereIn(projDao.ProjBusiness.Columns.Id, ids).Delete()
  265. return
  266. }
  267. // BusinessTransfer 项目转移
  268. func (p *businessService) BusinessTransfer(req *model.BusinessTransferReq) error {
  269. business, err := p.Dao.WherePri(req.Id).One()
  270. if err != nil {
  271. return err
  272. }
  273. if business == nil {
  274. return myerrors.TipsError("项目不存在。")
  275. }
  276. businessMap := g.Map{
  277. p.Dao.Columns.SaleId: req.UserId,
  278. p.Dao.Columns.SaleName: req.UserName,
  279. p.Dao.Columns.Remark: req.Remark,
  280. }
  281. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  282. opnContent := businessMap
  283. opnContent["origSaleId"] = business.SaleId
  284. opnContent["origSaleName"] = business.SaleName
  285. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  286. // 更新项目
  287. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Data(businessMap).Update()
  288. if err != nil {
  289. return err
  290. }
  291. // 添加项目动态
  292. dynamics := model.ProjBusinessDynamics{
  293. BusId: req.Id,
  294. OpnType: OpnTransfer,
  295. Remark: req.Remark,
  296. }
  297. err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  298. return err
  299. })
  300. return err
  301. }
  302. // BusinessGradation 项目调级
  303. func (p *businessService) BusinessGradation(req *model.BusinessGradationReq) error {
  304. business, err := p.Dao.Where(projDao.ProjBusiness.Columns.Id, req.Id).One()
  305. if err != nil {
  306. return err
  307. }
  308. if business == nil {
  309. return myerrors.TipsError("项目不存在。")
  310. }
  311. if business.NboType == req.NboType {
  312. return myerrors.TipsError("同级无法进行调级。")
  313. }
  314. opnType := OpnRise
  315. // A < B return -1 项目降级
  316. if strings.Compare(business.NboType, req.NboType) < 0 {
  317. opnType = OpnDrop
  318. }
  319. businessMap := g.Map{
  320. p.Dao.Columns.NboType: req.NboType,
  321. }
  322. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  323. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  324. // 更新项目调级
  325. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Data(businessMap).Update()
  326. if err != nil {
  327. return err
  328. }
  329. // 添加项目动态
  330. dynamics := model.ProjBusinessDynamics{
  331. BusId: business.Id,
  332. OpnType: opnType,
  333. Remark: req.Remark,
  334. }
  335. err = p.CreateProjBusinessDynamics(tx, dynamics, g.Map{
  336. "origNboType": business.NboType,
  337. "nboType": req.NboType,
  338. })
  339. return err
  340. })
  341. return err
  342. }
  343. // SetPrimacyContact 项目设置首要联系人
  344. func (p *businessService) SetPrimacyContact(req *model.BusinessPrimacyContactReq) (err error) {
  345. business, err := p.Dao.Where(projDao.ProjBusiness.Columns.Id, req.Id).One()
  346. if err != nil {
  347. return err
  348. }
  349. if business == nil {
  350. return myerrors.TipsError("项目不存在。")
  351. }
  352. businessMap := g.Map{
  353. p.Dao.Columns.ContactId: req.ContactId,
  354. p.Dao.Columns.ContactName: req.ContactName,
  355. p.Dao.Columns.ContactPostion: req.ContactPostion,
  356. p.Dao.Columns.ContactTelephone: req.ContactTelephone,
  357. }
  358. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  359. opnContent := businessMap
  360. opnContent["origContactId"] = business.ContactId
  361. opnContent["origContactName"] = business.ContactName
  362. opnContent["origContactPostion"] = business.ContactPostion
  363. opnContent["origContactTelephone"] = business.ContactTelephone
  364. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  365. // 更新项目
  366. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Data(businessMap).Update()
  367. if err != nil {
  368. return err
  369. }
  370. // 添加项目动态
  371. dynamics := model.ProjBusinessDynamics{
  372. BusId: req.Id,
  373. OpnType: OpnPrimacyContact,
  374. Remark: req.Remark,
  375. }
  376. err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  377. return err
  378. })
  379. return err
  380. }
  381. // UpdateBusinessStatus 更新项目状态
  382. func (p *businessService) UpdateBusinessStatus(req *model.UpdateBusinessStatusReq) error {
  383. business, err := p.Dao.WherePri(req.Id).One()
  384. if err != nil {
  385. return err
  386. }
  387. if business == nil {
  388. return myerrors.TipsError("项目不存在。")
  389. }
  390. businessMap := g.Map{
  391. p.Dao.Columns.NboStatus: req.NboStatus,
  392. p.Dao.Columns.Remark: req.Remark,
  393. }
  394. service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
  395. opnContent := businessMap
  396. opnContent["origNboStatus"] = business.NboStatus
  397. err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  398. // 更新项目
  399. _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Data(businessMap).Update()
  400. if err != nil {
  401. return err
  402. }
  403. // 添加项目动态
  404. dynamics := model.ProjBusinessDynamics{
  405. BusId: req.Id,
  406. OpnType: OpnStatus,
  407. Remark: req.Remark,
  408. }
  409. err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
  410. return err
  411. })
  412. return err
  413. }
  414. // CreateProjBusinessDynamics 创建项目动态
  415. func (p *businessService) CreateProjBusinessDynamics(tx *gdb.TX, dynamics model.ProjBusinessDynamics, opnContent interface{}) error {
  416. if v, ok := opnContent.(g.Map); ok {
  417. opnContent = utils.MapKeySnakeCamelCase(v)
  418. }
  419. // 添加项目动态
  420. dynamics.OpnPeopleId = p.GetCxtUserId()
  421. dynamics.OpnPeople = p.GetCxtUserName()
  422. dynamics.OpnDate = gtime.Now()
  423. dynamics.OpnContent = gconv.String(opnContent)
  424. service.SetCreatedInfo(&dynamics, p.GetCxtUserId(), p.GetCxtUserName())
  425. _, err := projDao.NewProjBusinessDynamicsDao(p.Tenant).TX(tx).Insert(&dynamics)
  426. return err
  427. }