| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- package proj
- import (
- "context"
- projDao "dashoo.cn/micro/app/dao/proj"
- model "dashoo.cn/micro/app/model/proj"
- "dashoo.cn/micro/app/service"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/utils"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gconv"
- "github.com/shopspring/decimal"
- "strconv"
- "strings"
- )
- const (
- OpnCreate = "10" // 创建动态
- OpnUpdate = "20" // 更新动态
- OpnTransfer = "30" // 转移动态
- OpnRise = "40" // 升级动态
- OpnDrop = "50" // 降级动态
- OpnPrimacyContact = "60" // 设置首要联系人动态
- OpnStatus = "70" // 更新项目状态动态
- OpnAssociation = "80" // 关联联系人动态
- OpnDisassociation = "90" // 解除关联联系人动态
- )
- type businessService struct {
- *service.ContextService
- Dao *projDao.ProjBusinessDao
- }
- func NewBusinessService(ctx context.Context) (svc *businessService, err error) {
- svc = new(businessService)
- if svc.ContextService, err = svc.Init(ctx); err != nil {
- return nil, err
- }
- svc.Dao = projDao.NewProjBusinessDao(svc.Tenant)
- return svc, nil
- }
- func (p *businessService) GetList(req *model.ProjBusinessSearchReq) (total int, businessList []*model.ProjBusiness, err error) {
- db := p.Dao.M
- if req.NboName != "" {
- db = db.WhereLike(p.Dao.Columns.NboName, "%"+req.NboName+"%")
- }
- if req.CustName != "" {
- db = db.WhereLike(p.Dao.Columns.CustName, "%"+req.CustName+"%")
- }
- if req.SaleName != "" {
- db = db.WhereLike(p.Dao.Columns.SaleName, "%"+req.SaleName+"%")
- }
- if req.NboType != "" {
- db = db.Where(p.Dao.Columns.NboType, req.NboType)
- }
- total, err = db.Count()
- if err != nil {
- g.Log().Error(err)
- err = myerrors.DbError("获取总行数失败。")
- return
- }
- err = db.Page(req.PageNum, req.PageSize).Order("id asc").Scan(&businessList)
- return
- }
- func (p *businessService) GetEntityById(id int64) (business *model.ProjBusiness, err error) {
- err = p.Dao.Where(projDao.ProjBusiness.Columns.Id, id).Scan(&business)
- return
- }
- func (p *businessService) GetBusinessProduct(id int64) (productList []*model.ProjBusinessProduct, err error) {
- productDao := projDao.NewProjBusinessProductDao(p.Tenant)
- err = productDao.Where(productDao.ProjBusinessProductDao.Columns.BusId, id).Scan(&productList)
- return
- }
- func (p *businessService) GetBusinessDynamics(req *model.BusinessReq) (total int, result g.MapStrAny, err error) {
- result = make(g.MapStrAny, 0)
- dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).ProjBusinessDynamicsDao.Where(projDao.ProjBusinessDynamics.Columns.BusId, req.BusId)
- total, err = dynamicsDao.Count()
- if err != nil {
- g.Log().Error(err)
- return
- }
- dynamicsList, err := dynamicsDao.Page(req.GetPage()).Order("created_time desc").All()
- if err != nil || dynamicsList == nil {
- return
- }
- // 数据树格式转换
- opnDateFlag := gtime.New(dynamicsList[0].OpnDate).Format("Y-m-d")
- for k, v := range dynamicsList {
- opnDate := gtime.New(v.OpnDate).Format("Y-m-d")
- if opnDateFlag == opnDate && k != 0 {
- result[opnDate] = append(result[opnDate].(g.ListStrAny), g.Map{
- "opnPeople": v.OpnPeople,
- "opnDate": v.OpnDate,
- "opnType": v.OpnType,
- "remark": v.Remark,
- "opnContent": gconv.Map(v.OpnContent),
- })
- } else {
- temp := make(g.ListStrAny, 0)
- temp = append(temp, g.Map{
- "opnPeople": v.OpnPeople,
- "opnDate": v.OpnDate,
- "opnType": v.OpnType,
- "remark": v.Remark,
- "opnContent": gconv.Map(v.OpnContent),
- })
- result[opnDate] = temp
- }
- }
- return
- }
- func (p *businessService) GetBusinessDynamicsList(req *model.BusinessDynamicsReq) (total int, list []map[string]interface{}, err error) {
- dynamicsDao := projDao.NewProjBusinessDynamicsDao(p.Tenant).ProjBusinessDynamicsDao.Where(projDao.ProjBusinessDynamics.Columns.BusId, req.BusId)
- if req.OpnType != "" {
- dynamicsDao = dynamicsDao.Where(projDao.ProjBusinessDynamics.Columns.OpnType+" = ?", req.OpnType)
- }
- total, err = dynamicsDao.Count()
- if err != nil {
- g.Log().Error(err)
- return
- }
- dynamicsList, err := dynamicsDao.Page(req.GetPage()).Order("created_time desc").All()
- for _, v := range dynamicsList {
- val := gconv.Map(v)
- val["opnContent"] = gconv.Map(v.OpnContent)
- list = append(list, val)
- }
- return
- }
- func (p *businessService) Create(req *model.AddProjBusinessReq) (err error) {
- // 设置默认联系人
- contact := g.Map{
- projDao.ProjBusinessContact.Columns.ContactId: req.ContactId,
- }
- service.SetCreatedInfo(contact, p.GetCxtUserId(), p.GetCxtUserName())
- // 设置产品信息
- totalPrice, products, err := p.setProductInfo(0, req.Products)
- if err != nil {
- return err
- }
- // 初始化项目信息
- businessData := new(model.ProjBusiness)
- if err = gconv.Struct(req, businessData); err != nil {
- return
- }
- businessData.NboCode = "NBO" + strconv.Itoa(int(gtime.Timestamp()))
- businessData.NboStatus = "10"
- businessData.NboType = "C"
- businessData.ApproStatus = "10"
- businessData.EstTransPrice = totalPrice
- businessData.DeptId = p.GetCxtUserDeptId()
- service.SetCreatedInfo(businessData, p.GetCxtUserId(), p.GetCxtUserName())
- // 事务
- err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- // 添加项目
- lastId, err := p.Dao.TX(tx).InsertAndGetId(businessData)
- if err != nil {
- return err
- }
- // 创建了联系人
- contact[projDao.ProjBusinessContact.Columns.BusId] = lastId
- _, err = projDao.NewProjBusinessContactDao(p.Tenant).TX(tx).Insert(contact)
- if err != nil {
- return err
- }
- // 处理项目产品信息
- for _, v := range products {
- v.BusId = int(lastId)
- }
- // 添加项目产品
- _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Insert(products)
- if err != nil {
- return err
- }
- // 添加项目动态
- dynamics := model.ProjBusinessDynamics{
- BusId: int(lastId),
- OpnType: OpnCreate,
- Remark: businessData.Remark,
- }
- err = p.CreateProjBusinessDynamics(tx, dynamics, businessData)
- return err
- })
- return
- }
- // setProductInfo 设置产品信息
- func (p *businessService) setProductInfo(busId int, productInfo []model.BusinessProduct) (total float64, products []*model.ProjBusinessProduct, err error) {
- products = make([]*model.ProjBusinessProduct, len(productInfo))
- if err = gconv.Structs(productInfo, &products); err != nil {
- return 0, nil, err
- }
- var totalPrice decimal.Decimal
- for _, v := range products {
- v.Id = 0
- v.BusId = busId
- v.TotalPrice = decimal.NewFromFloat(v.ProdPrice).Mul(decimal.NewFromInt(int64(v.ProdNum))).InexactFloat64()
- totalPrice = totalPrice.Add(decimal.NewFromFloat(v.TotalPrice))
- service.SetCreatedInfo(v, p.GetCxtUserId(), p.GetCxtUserName())
- }
- return totalPrice.InexactFloat64(), products, nil
- }
- func (p *businessService) UpdateById(req *model.UpdateProjBusinessReq) error {
- record, err := p.Dao.WherePri(req.Id).Count()
- if err != nil {
- return err
- }
- if record == 0 {
- return myerrors.TipsError("项目不存在。")
- }
- // 设置产品信息
- totalPrice, products, err := p.setProductInfo(req.Id, req.Products)
- if err != nil {
- return err
- }
- // 设置默认联系人
- contact := g.Map{
- projDao.ProjBusinessContact.Columns.BusId: req.Id,
- projDao.ProjBusinessContact.Columns.ContactId: req.ContactId,
- }
- contactFlag, err := projDao.NewProjBusinessContactDao(p.Tenant).Where(contact).Count()
- if err != nil {
- return err
- }
- if contactFlag == 0 {
- service.SetCreatedInfo(contact, p.GetCxtUserId(), p.GetCxtUserName())
- }
- // 设置项目信息
- req.EstTransPrice = totalPrice
- businessData := gconv.Map(req)
- service.SetUpdatedInfo(businessData, p.GetCxtUserId(), p.GetCxtUserName())
- err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- // 更新项目
- _, err = p.Dao.TX(tx).FieldsEx(service.UpdateFieldEx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Update(businessData)
- if err != nil {
- return err
- }
- // 删除项目产品
- _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Where(projDao.ProjBusinessProduct.Columns.BusId, req.Id).Delete()
- if err != nil {
- return err
- }
- // 添加项目产品
- _, err = projDao.NewProjBusinessProductDao(p.Tenant).TX(tx).Insert(products)
- if err != nil {
- return err
- }
- // 关联联系人
- if contactFlag == 0 {
- _, err = projDao.NewProjBusinessContactDao(p.Tenant).TX(tx).Insert(contact)
- if err != nil {
- return err
- }
- }
- // 添加项目动态
- dynamics := model.ProjBusinessDynamics{
- BusId: req.Id,
- OpnType: OpnUpdate,
- Remark: req.Remark,
- }
- err = p.CreateProjBusinessDynamics(tx, dynamics, req)
- return err
- })
- return err
- }
- func (p *businessService) DeleteByIds(ids []int64) (err error) {
- _, err = p.Dao.WhereIn(projDao.ProjBusiness.Columns.Id, ids).Delete()
- return
- }
- // BusinessTransfer 项目转移
- func (p *businessService) BusinessTransfer(req *model.BusinessTransferReq) error {
- business, err := p.Dao.WherePri(req.Id).One()
- if err != nil {
- return err
- }
- if business == nil {
- return myerrors.TipsError("项目不存在。")
- }
- businessMap := g.Map{
- p.Dao.Columns.SaleId: req.UserId,
- p.Dao.Columns.SaleName: req.UserName,
- p.Dao.Columns.Remark: req.Remark,
- }
- service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
- opnContent := businessMap
- opnContent["origSaleId"] = business.SaleId
- opnContent["origSaleName"] = business.SaleName
- err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- // 更新项目
- _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Data(businessMap).Update()
- if err != nil {
- return err
- }
- // 添加项目动态
- dynamics := model.ProjBusinessDynamics{
- BusId: req.Id,
- OpnType: OpnTransfer,
- Remark: req.Remark,
- }
- err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
- return err
- })
- return err
- }
- // BusinessGradation 项目调级
- func (p *businessService) BusinessGradation(req *model.BusinessGradationReq) error {
- business, err := p.Dao.Where(projDao.ProjBusiness.Columns.Id, req.Id).One()
- if err != nil {
- return err
- }
- if business == nil {
- return myerrors.TipsError("项目不存在。")
- }
- if business.NboType == req.NboType {
- return myerrors.TipsError("同级无法进行调级。")
- }
- opnType := OpnRise
- // A < B return -1 项目降级
- if strings.Compare(business.NboType, req.NboType) < 0 {
- opnType = OpnDrop
- }
- businessMap := g.Map{
- p.Dao.Columns.NboType: req.NboType,
- }
- service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
- err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- // 更新项目调级
- _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Data(businessMap).Update()
- if err != nil {
- return err
- }
- // 添加项目动态
- dynamics := model.ProjBusinessDynamics{
- BusId: business.Id,
- OpnType: opnType,
- Remark: req.Remark,
- }
- err = p.CreateProjBusinessDynamics(tx, dynamics, g.Map{
- "origNboType": business.NboType,
- "nboType": req.NboType,
- })
- return err
- })
- return err
- }
- // SetPrimacyContact 项目设置首要联系人
- func (p *businessService) SetPrimacyContact(req *model.BusinessPrimacyContactReq) (err error) {
- business, err := p.Dao.Where(projDao.ProjBusiness.Columns.Id, req.Id).One()
- if err != nil {
- return err
- }
- if business == nil {
- return myerrors.TipsError("项目不存在。")
- }
- businessMap := g.Map{
- p.Dao.Columns.ContactId: req.ContactId,
- p.Dao.Columns.ContactName: req.ContactName,
- p.Dao.Columns.ContactPostion: req.ContactPostion,
- p.Dao.Columns.ContactTelephone: req.ContactTelephone,
- }
- service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
- opnContent := businessMap
- opnContent["origContactId"] = business.ContactId
- opnContent["origContactName"] = business.ContactName
- opnContent["origContactPostion"] = business.ContactPostion
- opnContent["origContactTelephone"] = business.ContactTelephone
- err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- // 更新项目
- _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Data(businessMap).Update()
- if err != nil {
- return err
- }
- // 添加项目动态
- dynamics := model.ProjBusinessDynamics{
- BusId: req.Id,
- OpnType: OpnPrimacyContact,
- Remark: req.Remark,
- }
- err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
- return err
- })
- return err
- }
- // UpdateBusinessStatus 更新项目状态
- func (p *businessService) UpdateBusinessStatus(req *model.UpdateBusinessStatusReq) error {
- business, err := p.Dao.WherePri(req.Id).One()
- if err != nil {
- return err
- }
- if business == nil {
- return myerrors.TipsError("项目不存在。")
- }
- businessMap := g.Map{
- p.Dao.Columns.NboStatus: req.NboStatus,
- p.Dao.Columns.Remark: req.Remark,
- }
- service.SetUpdatedInfo(businessMap, p.GetCxtUserId(), p.GetCxtUserName())
- opnContent := businessMap
- opnContent["origNboStatus"] = business.NboStatus
- err = p.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
- // 更新项目
- _, err = p.Dao.TX(tx).WherePri(projDao.ProjBusiness.Columns.Id, req.Id).Data(businessMap).Update()
- if err != nil {
- return err
- }
- // 添加项目动态
- dynamics := model.ProjBusinessDynamics{
- BusId: req.Id,
- OpnType: OpnStatus,
- Remark: req.Remark,
- }
- err = p.CreateProjBusinessDynamics(tx, dynamics, opnContent)
- return err
- })
- return err
- }
- // CreateProjBusinessDynamics 创建项目动态
- func (p *businessService) CreateProjBusinessDynamics(tx *gdb.TX, dynamics model.ProjBusinessDynamics, opnContent interface{}) error {
- if v, ok := opnContent.(g.Map); ok {
- opnContent = utils.MapKeySnakeCamelCase(v)
- }
- // 添加项目动态
- dynamics.OpnPeopleId = p.GetCxtUserId()
- dynamics.OpnPeople = p.GetCxtUserName()
- dynamics.OpnDate = gtime.Now()
- dynamics.OpnContent = gconv.String(opnContent)
- service.SetCreatedInfo(&dynamics, p.GetCxtUserId(), p.GetCxtUserName())
- _, err := projDao.NewProjBusinessDynamicsDao(p.Tenant).TX(tx).Insert(&dynamics)
- return err
- }
|