| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- package work
- import (
- "context"
- "database/sql"
- "fmt"
- basedao "dashoo.cn/micro/app/dao/base"
- contractdao "dashoo.cn/micro/app/dao/contract"
- custdao "dashoo.cn/micro/app/dao/cust"
- projdao "dashoo.cn/micro/app/dao/proj"
- workdao "dashoo.cn/micro/app/dao/work"
- contractmodel "dashoo.cn/micro/app/model/contract"
- work "dashoo.cn/micro/app/model/work"
- "dashoo.cn/micro/app/service"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/request"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- )
- type DeliverOrderService struct {
- Dao *workdao.DeliverOrderDao
- PlanDao *workdao.DeliverOrderImpPlanDao
- ProductDao *workdao.DeliverOrderProductDao
- ContractDao *contractdao.CtrContractDao
- ProjBusinessDao *projdao.ProjBusinessDao
- CustomerDao *custdao.CustCustomerDao
- CtrProductDao *contractdao.CtrContractProductDao
- BaseProductDao *basedao.BaseProductDao
- Tenant string
- userInfo request.UserInfo
- DataScope g.Map `json:"dataScope"`
- }
- func NewDeliverOrderService(ctx context.Context) (*DeliverOrderService, error) {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- err = myerrors.TipsError(fmt.Sprintf("获取租户码异常:%s", err.Error()))
- return nil, err
- }
- // 获取用户信息
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- return &DeliverOrderService{
- Dao: workdao.NewDeliverOrderDao(tenant),
- PlanDao: workdao.NewDeliverOrderImpPlanDao(tenant),
- ContractDao: contractdao.NewCtrContractDao(tenant),
- ProjBusinessDao: projdao.NewProjBusinessDao(tenant),
- CustomerDao: custdao.NewCustCustomerDao(tenant),
- CtrProductDao: contractdao.NewCtrContractProductDao(tenant),
- ProductDao: workdao.NewDeliverOrderProductDao(tenant),
- BaseProductDao: basedao.NewBaseProductDao(tenant),
- Tenant: tenant,
- userInfo: userInfo,
- DataScope: userInfo.DataScope,
- }, nil
- }
- func (s DeliverOrderService) Get(ctx context.Context, id int) (*work.DeliverOrderGetRsp, error) {
- ent, err := s.Dao.Where("Id = ?", id).One()
- if err != nil {
- return nil, err
- }
- if ent == nil {
- return nil, myerrors.TipsError("工单不存在")
- }
- plan, err := s.PlanDao.Where("deliver_order_id = ?", id).All()
- if err != nil {
- return nil, err
- }
- if plan == nil {
- plan = []*work.DeliverOrderImpPlan{}
- }
- product, err := s.ProductDao.Where("deliver_order_id = ?", id).All()
- if err != nil {
- return nil, err
- }
- if product == nil {
- product = []*work.DeliverOrderProduct{}
- }
- return &work.DeliverOrderGetRsp{
- DeliverOrder: *ent,
- Plan: plan,
- Product: product,
- }, nil
- }
- func (s DeliverOrderService) List(ctx context.Context, req *work.DeliverOrderListReq) (int, []*work.DeliverOrder, error) {
- g.Log().Infof("DeliverOrderService List roles %v", s.userInfo.Roles)
- dao := s.Dao.DeliverOrderDao.As("a").LeftJoin("ctr_contract b", "a.contract_id=b.id").Unscoped().Where("a.deleted_time is null")
- // 销售只能看自己所属的,其他人只能看自己所属产品线的 (大区经理有可能同时具有销售角色,这里需要排除大区经理)
- if service.StringsContains(s.userInfo.Roles, "SalesEngineer") &&
- !service.StringsContains(s.userInfo.Roles, "RegionalManager") {
- dao = dao.Where("b.incharge_id = ?", s.userInfo.Id)
- // } else if service.StringsContains(s.userInfo.Roles, "ProjectDeliveryManager") {
- // dao = dao.Where("a.deliver_man_id = ?", s.userInfo.Id)
- } else {
- productCode, err := service.ColumnString(s.Dao.DB.Table("base_product_auth").Wheref("user_id = ?", s.userInfo.Id), "product_code")
- if err != nil {
- return 0, nil, err
- }
- g.Log().Infof("DeliverOrderService List product_code %v", productCode)
- dao = dao.Where("a.product in (?)", productCode)
- }
- if req.OrderCode != "" {
- dao = dao.Where("a.order_code = ?", req.OrderCode)
- }
- if req.OrderStatus != "" {
- dao = dao.Where("a.order_status = ?", req.OrderStatus)
- }
- if req.OrderType != "" {
- dao = dao.Where("a.order_type = ?", req.OrderType)
- }
- if req.CustId != 0 {
- dao = dao.Where("a.cust_id = ?", req.CustId)
- }
- if req.CustName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.CustName)
- dao = dao.Where("a.cust_name like ?", likestr)
- }
- if req.ProjectId != 0 {
- dao = dao.Where("a.project_id = ?", req.ProjectId)
- }
- if req.ProjectName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.ProjectName)
- dao = dao.Where("a.project_name like ?", likestr)
- }
- if req.ContractId != 0 {
- dao = dao.Where("a.contract_id = ?", req.ContractId)
- }
- if req.ContractCode != "" {
- dao = dao.Where("a.contract_code = ?", req.ContractCode)
- }
- if req.ProjectManId != 0 {
- dao = dao.Where("a.project_man_id = ?", req.ProjectManId)
- }
- if req.ProjectManName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.ProjectManName)
- dao = dao.Where("a.project_man_name like ?", likestr)
- }
- if req.DeliverManId != 0 {
- dao = dao.Where("a.deliver_man_id = ?", req.DeliverManId)
- }
- if req.DeliverManName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.DeliverManName)
- dao = dao.Where("a.deliver_man_name like ?", likestr)
- }
- if req.Product != "" {
- dao = dao.Where("a.product = ?", req.Product)
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.PageNum != 0 {
- dao = dao.Page(req.GetPage())
- }
- orderby := "a.created_time desc"
- if req.OrderBy != "" {
- orderby = req.OrderBy
- }
- dao = dao.Order(orderby)
- ents := []*work.DeliverOrder{}
- err = dao.Fields("a.*").Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- return total, ents, err
- }
- func (s DeliverOrderService) Add(ctx context.Context, req *work.DeliverOrderAddReq) ([]int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return nil, myerrors.TipsError(validErr.Current().Error())
- }
- var id = []int{}
- txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- var err error
- id, err = DeliverOrderAdd(tx, req.ContractId, s.userInfo)
- return err
- })
- return id, txerr
- }
- func DeliverOrderAdd(tx *gdb.TX, contractId int, userInfo request.UserInfo) ([]int, error) {
- var c contractmodel.CtrContract
- err := tx.GetStruct(&c, "select * from ctr_contract where id = ?", contractId)
- if err == sql.ErrNoRows {
- return nil, myerrors.TipsError(fmt.Sprintf("合同:%d 不存在", contractId))
- }
- if err != nil {
- return nil, err
- }
- var product []*contractmodel.CtrContractProduct
- err = tx.GetStructs(&product, "select * from ctr_contract_product where contract_id = ?", contractId)
- if err == sql.ErrNoRows {
- return nil, myerrors.TipsError(fmt.Sprintf("合同产品为空: %d", contractId))
- }
- if err != nil {
- return nil, err
- }
- var deliverSoft *work.DeliverOrder
- var deliverHard *work.DeliverOrder
- deliverProductSoft := []work.DeliverOrderProduct{}
- deliverProductHard := []work.DeliverOrderProduct{}
- for _, p := range product {
- var orderType string
- if p.ProdClass == "10" || p.ProdClass == "20" || p.ProdClass == "30" {
- orderType = "10"
- }
- if p.ProdClass == "40" || p.ProdClass == "60" {
- orderType = "20"
- }
- o := work.DeliverOrder{
- OrderCode: fmt.Sprintf("%s%s", c.ContractCode, orderType),
- OrderStatus: "10",
- OrderType: orderType,
- CustId: c.CustId,
- CustName: c.CustName,
- ProjectId: c.NboId,
- ProjectName: c.NboName,
- ContractId: c.Id,
- ContractCode: c.ContractCode,
- ProjectManId: 0,
- ProjectManName: "",
- DeliverManId: 0,
- DeliverManName: "",
- Product: c.ProductLine,
- Remark: "",
- CreatedBy: int(userInfo.Id),
- CreatedName: userInfo.NickName,
- CreatedTime: gtime.Now(),
- UpdatedBy: int(userInfo.Id),
- UpdatedName: userInfo.NickName,
- UpdatedTime: gtime.Now(),
- }
- op := work.DeliverOrderProduct{
- DeliverOrderId: 0,
- ProductCode: p.ProdCode,
- ProductName: p.ProdName,
- ProductType: p.ProdClass,
- ProductNum: p.ProdNum,
- Remark: "",
- CreatedBy: int(userInfo.Id),
- CreatedName: userInfo.NickName,
- CreatedTime: gtime.Now(),
- UpdatedBy: int(userInfo.Id),
- UpdatedName: userInfo.NickName,
- UpdatedTime: gtime.Now(),
- }
- if orderType == "10" {
- deliverSoft = &o
- deliverProductSoft = append(deliverProductSoft, op)
- }
- if orderType == "20" {
- deliverHard = &o
- deliverProductHard = append(deliverProductHard, op)
- }
- }
- var id = []int{}
- if deliverHard != nil {
- oid, err := tx.InsertAndGetId("deliver_order", *deliverHard)
- if err != nil {
- return nil, err
- }
- err = DeliverOrderBindProduct(tx, int(oid), deliverProductHard)
- if err != nil {
- return nil, err
- }
- id = append(id, int(oid))
- }
- if deliverSoft != nil {
- oid, err := tx.InsertAndGetId("deliver_order", *deliverSoft)
- if err != nil {
- return nil, err
- }
- err = DeliverOrderBindProduct(tx, int(oid), deliverProductSoft)
- if err != nil {
- return nil, err
- }
- id = append(id, int(oid))
- }
- return id, nil
- }
- func DeliverOrderBindProduct(tx *gdb.TX, id int, product []work.DeliverOrderProduct) error {
- _, err := tx.Delete("deliver_order_product", "deliver_order_id = ?", id)
- if err != nil {
- return err
- }
- for i := range product {
- product[i].DeliverOrderId = id
- }
- if len(product) != 0 {
- _, err = tx.Insert("deliver_order_product", product)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (s DeliverOrderService) Update(ctx context.Context, req *work.DeliverOrderUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- ent, err := s.Dao.Where("id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError(fmt.Sprintf("工单不存在: %d", req.Id))
- }
- toupdate := map[string]interface{}{}
- if req.OrderStatus != "" {
- toupdate["order_status"] = req.OrderStatus
- }
- if req.ProjectManId != 0 {
- toupdate["project_man_id"] = req.ProjectManId
- }
- if req.ProjectManName != "" {
- toupdate["project_man_name"] = req.ProjectManName
- }
- if req.DeliverManId != 0 {
- toupdate["deliver_man_id"] = req.DeliverManId
- }
- if req.DeliverManName != "" {
- toupdate["deliver_man_name"] = req.DeliverManName
- }
- if len(toupdate) != 0 {
- toupdate["updated_by"] = int(s.userInfo.Id)
- toupdate["updated_name"] = s.userInfo.NickName
- toupdate["updated_time"] = gtime.Now()
- txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- _, err = tx.Update("deliver_order", toupdate, "id = ?", req.Id)
- if err != nil {
- return err
- }
- return nil
- })
- if txerr != nil {
- return txerr
- }
- }
- return nil
- }
- func (s *DeliverOrderService) Finish(ctx context.Context, req *work.DeliverOrderFinishReq) error {
- ent, err := s.Dao.Where("id = ?", req.OrderId).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError("工单不存在")
- }
- // if ent.OrderStatus != "30" {
- // return myerrors.TipsError("当前工单不可完成")
- // }
- return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- _, err = tx.Update("deliver_order", map[string]interface{}{
- "order_status": "20",
- "finish_remark": req.FinishRemark,
- "finish_time": gtime.Now(),
- "finish_by": int(s.userInfo.Id),
- "finish_by_name": s.userInfo.NickName,
- "updated_by": int(s.userInfo.Id),
- "updated_name": s.userInfo.NickName,
- "updated_time": gtime.Now(),
- }, "id = ?", req.OrderId)
- return err
- })
- }
- func (s DeliverOrderService) Delete(ctx context.Context, id []int) error {
- if len(id) == 0 {
- return nil
- }
- _, err := s.Dao.Where("Id IN (?)", id).Delete()
- return err
- }
|