deliver_order.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. package work
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. basedao "dashoo.cn/micro/app/dao/base"
  7. contractdao "dashoo.cn/micro/app/dao/contract"
  8. custdao "dashoo.cn/micro/app/dao/cust"
  9. projdao "dashoo.cn/micro/app/dao/proj"
  10. workdao "dashoo.cn/micro/app/dao/work"
  11. contractmodel "dashoo.cn/micro/app/model/contract"
  12. work "dashoo.cn/micro/app/model/work"
  13. "dashoo.cn/micro/app/service"
  14. "dashoo.cn/opms_libary/micro_srv"
  15. "dashoo.cn/opms_libary/myerrors"
  16. "dashoo.cn/opms_libary/request"
  17. "github.com/gogf/gf/database/gdb"
  18. "github.com/gogf/gf/frame/g"
  19. "github.com/gogf/gf/os/gtime"
  20. "github.com/gogf/gf/util/gvalid"
  21. )
  22. type DeliverOrderService struct {
  23. Dao *workdao.DeliverOrderDao
  24. PlanDao *workdao.DeliverOrderImpPlanDao
  25. ProductDao *workdao.DeliverOrderProductDao
  26. ContractDao *contractdao.CtrContractDao
  27. ProjBusinessDao *projdao.ProjBusinessDao
  28. CustomerDao *custdao.CustCustomerDao
  29. CtrProductDao *contractdao.CtrContractProductDao
  30. BaseProductDao *basedao.BaseProductDao
  31. Tenant string
  32. userInfo request.UserInfo
  33. DataScope g.Map `json:"dataScope"`
  34. }
  35. func NewDeliverOrderService(ctx context.Context) (*DeliverOrderService, error) {
  36. tenant, err := micro_srv.GetTenant(ctx)
  37. if err != nil {
  38. err = myerrors.TipsError(fmt.Sprintf("获取租户码异常:%s", err.Error()))
  39. return nil, err
  40. }
  41. // 获取用户信息
  42. userInfo, err := micro_srv.GetUserInfo(ctx)
  43. if err != nil {
  44. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  45. }
  46. return &DeliverOrderService{
  47. Dao: workdao.NewDeliverOrderDao(tenant),
  48. PlanDao: workdao.NewDeliverOrderImpPlanDao(tenant),
  49. ContractDao: contractdao.NewCtrContractDao(tenant),
  50. ProjBusinessDao: projdao.NewProjBusinessDao(tenant),
  51. CustomerDao: custdao.NewCustCustomerDao(tenant),
  52. CtrProductDao: contractdao.NewCtrContractProductDao(tenant),
  53. ProductDao: workdao.NewDeliverOrderProductDao(tenant),
  54. BaseProductDao: basedao.NewBaseProductDao(tenant),
  55. Tenant: tenant,
  56. userInfo: userInfo,
  57. DataScope: userInfo.DataScope,
  58. }, nil
  59. }
  60. func (s DeliverOrderService) Get(ctx context.Context, id int) (*work.DeliverOrderGetRsp, error) {
  61. ent, err := s.Dao.Where("Id = ?", id).One()
  62. if err != nil {
  63. return nil, err
  64. }
  65. if ent == nil {
  66. return nil, myerrors.TipsError("工单不存在")
  67. }
  68. plan, err := s.PlanDao.Where("deliver_order_id = ?", id).All()
  69. if err != nil {
  70. return nil, err
  71. }
  72. if plan == nil {
  73. plan = []*work.DeliverOrderImpPlan{}
  74. }
  75. product, err := s.ProductDao.Where("deliver_order_id = ?", id).All()
  76. if err != nil {
  77. return nil, err
  78. }
  79. if product == nil {
  80. product = []*work.DeliverOrderProduct{}
  81. }
  82. return &work.DeliverOrderGetRsp{
  83. DeliverOrder: *ent,
  84. Plan: plan,
  85. Product: product,
  86. }, nil
  87. }
  88. func (s DeliverOrderService) List(ctx context.Context, req *work.DeliverOrderListReq) (int, []*work.DeliverOrder, error) {
  89. g.Log().Infof("DeliverOrderService List roles %v", s.userInfo.Roles)
  90. dao := s.Dao.DeliverOrderDao.As("a").LeftJoin("ctr_contract b", "a.contract_id=b.id").Unscoped().Where("a.deleted_time is null")
  91. // 销售只能看自己所属的,其他人只能看自己所属产品线的 (大区经理有可能同时具有销售角色,这里需要排除大区经理)
  92. if service.StringsContains(s.userInfo.Roles, "SalesEngineer") &&
  93. !service.StringsContains(s.userInfo.Roles, "RegionalManager") {
  94. dao = dao.Where("b.incharge_id = ?", s.userInfo.Id)
  95. // } else if service.StringsContains(s.userInfo.Roles, "ProjectDeliveryManager") {
  96. // dao = dao.Where("a.deliver_man_id = ?", s.userInfo.Id)
  97. } else {
  98. productCode, err := service.ColumnString(s.Dao.DB.Table("base_product_auth").Wheref("user_id = ?", s.userInfo.Id), "product_code")
  99. if err != nil {
  100. return 0, nil, err
  101. }
  102. g.Log().Infof("DeliverOrderService List product_code %v", productCode)
  103. dao = dao.Where("a.product in (?)", productCode)
  104. }
  105. if req.OrderCode != "" {
  106. dao = dao.Where("a.order_code = ?", req.OrderCode)
  107. }
  108. if req.OrderStatus != "" {
  109. dao = dao.Where("a.order_status = ?", req.OrderStatus)
  110. }
  111. if req.OrderType != "" {
  112. dao = dao.Where("a.order_type = ?", req.OrderType)
  113. }
  114. if req.CustId != 0 {
  115. dao = dao.Where("a.cust_id = ?", req.CustId)
  116. }
  117. if req.CustName != "" {
  118. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  119. dao = dao.Where("a.cust_name like ?", likestr)
  120. }
  121. if req.ProjectId != 0 {
  122. dao = dao.Where("a.project_id = ?", req.ProjectId)
  123. }
  124. if req.ProjectName != "" {
  125. likestr := fmt.Sprintf("%%%s%%", req.ProjectName)
  126. dao = dao.Where("a.project_name like ?", likestr)
  127. }
  128. if req.ContractId != 0 {
  129. dao = dao.Where("a.contract_id = ?", req.ContractId)
  130. }
  131. if req.ContractCode != "" {
  132. dao = dao.Where("a.contract_code = ?", req.ContractCode)
  133. }
  134. if req.ProjectManId != 0 {
  135. dao = dao.Where("a.project_man_id = ?", req.ProjectManId)
  136. }
  137. if req.ProjectManName != "" {
  138. likestr := fmt.Sprintf("%%%s%%", req.ProjectManName)
  139. dao = dao.Where("a.project_man_name like ?", likestr)
  140. }
  141. if req.DeliverManId != 0 {
  142. dao = dao.Where("a.deliver_man_id = ?", req.DeliverManId)
  143. }
  144. if req.DeliverManName != "" {
  145. likestr := fmt.Sprintf("%%%s%%", req.DeliverManName)
  146. dao = dao.Where("a.deliver_man_name like ?", likestr)
  147. }
  148. if req.Product != "" {
  149. dao = dao.Where("a.product = ?", req.Product)
  150. }
  151. total, err := dao.Count()
  152. if err != nil {
  153. return 0, nil, err
  154. }
  155. if req.PageNum != 0 {
  156. dao = dao.Page(req.GetPage())
  157. }
  158. orderby := "a.created_time desc"
  159. if req.OrderBy != "" {
  160. orderby = req.OrderBy
  161. }
  162. dao = dao.Order(orderby)
  163. ents := []*work.DeliverOrder{}
  164. err = dao.Fields("a.*").Structs(&ents)
  165. if err != nil && err != sql.ErrNoRows {
  166. return 0, nil, err
  167. }
  168. return total, ents, err
  169. }
  170. func (s DeliverOrderService) Add(ctx context.Context, req *work.DeliverOrderAddReq) ([]int, error) {
  171. validErr := gvalid.CheckStruct(ctx, req, nil)
  172. if validErr != nil {
  173. return nil, myerrors.TipsError(validErr.Current().Error())
  174. }
  175. var id = []int{}
  176. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  177. var err error
  178. id, err = DeliverOrderAdd(tx, req.ContractId, s.userInfo)
  179. return err
  180. })
  181. return id, txerr
  182. }
  183. func DeliverOrderAdd(tx *gdb.TX, contractId int, userInfo request.UserInfo) ([]int, error) {
  184. var c contractmodel.CtrContract
  185. err := tx.GetStruct(&c, "select * from ctr_contract where id = ?", contractId)
  186. if err == sql.ErrNoRows {
  187. return nil, myerrors.TipsError(fmt.Sprintf("合同:%d 不存在", contractId))
  188. }
  189. if err != nil {
  190. return nil, err
  191. }
  192. var product []*contractmodel.CtrContractProduct
  193. err = tx.GetStructs(&product, "select * from ctr_contract_product where contract_id = ?", contractId)
  194. if err == sql.ErrNoRows {
  195. return nil, myerrors.TipsError(fmt.Sprintf("合同产品为空: %d", contractId))
  196. }
  197. if err != nil {
  198. return nil, err
  199. }
  200. var deliverSoft *work.DeliverOrder
  201. var deliverHard *work.DeliverOrder
  202. deliverProductSoft := []work.DeliverOrderProduct{}
  203. deliverProductHard := []work.DeliverOrderProduct{}
  204. for _, p := range product {
  205. var orderType string
  206. if p.ProdClass == "10" || p.ProdClass == "20" || p.ProdClass == "30" {
  207. orderType = "10"
  208. }
  209. if p.ProdClass == "40" || p.ProdClass == "60" {
  210. orderType = "20"
  211. }
  212. o := work.DeliverOrder{
  213. OrderCode: fmt.Sprintf("%s%s", c.ContractCode, orderType),
  214. OrderStatus: "10",
  215. OrderType: orderType,
  216. CustId: c.CustId,
  217. CustName: c.CustName,
  218. ProjectId: c.NboId,
  219. ProjectName: c.NboName,
  220. ContractId: c.Id,
  221. ContractCode: c.ContractCode,
  222. ProjectManId: 0,
  223. ProjectManName: "",
  224. DeliverManId: 0,
  225. DeliverManName: "",
  226. Product: c.ProductLine,
  227. Remark: "",
  228. CreatedBy: int(userInfo.Id),
  229. CreatedName: userInfo.NickName,
  230. CreatedTime: gtime.Now(),
  231. UpdatedBy: int(userInfo.Id),
  232. UpdatedName: userInfo.NickName,
  233. UpdatedTime: gtime.Now(),
  234. }
  235. op := work.DeliverOrderProduct{
  236. DeliverOrderId: 0,
  237. ProductCode: p.ProdCode,
  238. ProductName: p.ProdName,
  239. ProductType: p.ProdClass,
  240. ProductNum: p.ProdNum,
  241. Remark: "",
  242. CreatedBy: int(userInfo.Id),
  243. CreatedName: userInfo.NickName,
  244. CreatedTime: gtime.Now(),
  245. UpdatedBy: int(userInfo.Id),
  246. UpdatedName: userInfo.NickName,
  247. UpdatedTime: gtime.Now(),
  248. }
  249. if orderType == "10" {
  250. deliverSoft = &o
  251. deliverProductSoft = append(deliverProductSoft, op)
  252. }
  253. if orderType == "20" {
  254. deliverHard = &o
  255. deliverProductHard = append(deliverProductHard, op)
  256. }
  257. }
  258. var id = []int{}
  259. if deliverHard != nil {
  260. oid, err := tx.InsertAndGetId("deliver_order", *deliverHard)
  261. if err != nil {
  262. return nil, err
  263. }
  264. err = DeliverOrderBindProduct(tx, int(oid), deliverProductHard)
  265. if err != nil {
  266. return nil, err
  267. }
  268. id = append(id, int(oid))
  269. }
  270. if deliverSoft != nil {
  271. oid, err := tx.InsertAndGetId("deliver_order", *deliverSoft)
  272. if err != nil {
  273. return nil, err
  274. }
  275. err = DeliverOrderBindProduct(tx, int(oid), deliverProductSoft)
  276. if err != nil {
  277. return nil, err
  278. }
  279. id = append(id, int(oid))
  280. }
  281. return id, nil
  282. }
  283. func DeliverOrderBindProduct(tx *gdb.TX, id int, product []work.DeliverOrderProduct) error {
  284. _, err := tx.Delete("deliver_order_product", "deliver_order_id = ?", id)
  285. if err != nil {
  286. return err
  287. }
  288. for i := range product {
  289. product[i].DeliverOrderId = id
  290. }
  291. if len(product) != 0 {
  292. _, err = tx.Insert("deliver_order_product", product)
  293. if err != nil {
  294. return err
  295. }
  296. }
  297. return nil
  298. }
  299. func (s DeliverOrderService) Update(ctx context.Context, req *work.DeliverOrderUpdateReq) error {
  300. validErr := gvalid.CheckStruct(ctx, req, nil)
  301. if validErr != nil {
  302. return myerrors.TipsError(validErr.Current().Error())
  303. }
  304. ent, err := s.Dao.Where("id = ?", req.Id).One()
  305. if err != nil {
  306. return err
  307. }
  308. if ent == nil {
  309. return myerrors.TipsError(fmt.Sprintf("工单不存在: %d", req.Id))
  310. }
  311. toupdate := map[string]interface{}{}
  312. if req.OrderStatus != "" {
  313. toupdate["order_status"] = req.OrderStatus
  314. }
  315. if req.ProjectManId != 0 {
  316. toupdate["project_man_id"] = req.ProjectManId
  317. }
  318. if req.ProjectManName != "" {
  319. toupdate["project_man_name"] = req.ProjectManName
  320. }
  321. if req.DeliverManId != 0 {
  322. toupdate["deliver_man_id"] = req.DeliverManId
  323. }
  324. if req.DeliverManName != "" {
  325. toupdate["deliver_man_name"] = req.DeliverManName
  326. }
  327. if len(toupdate) != 0 {
  328. toupdate["updated_by"] = int(s.userInfo.Id)
  329. toupdate["updated_name"] = s.userInfo.NickName
  330. toupdate["updated_time"] = gtime.Now()
  331. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  332. _, err = tx.Update("deliver_order", toupdate, "id = ?", req.Id)
  333. if err != nil {
  334. return err
  335. }
  336. return nil
  337. })
  338. if txerr != nil {
  339. return txerr
  340. }
  341. }
  342. return nil
  343. }
  344. func (s *DeliverOrderService) Finish(ctx context.Context, req *work.DeliverOrderFinishReq) error {
  345. ent, err := s.Dao.Where("id = ?", req.OrderId).One()
  346. if err != nil {
  347. return err
  348. }
  349. if ent == nil {
  350. return myerrors.TipsError("工单不存在")
  351. }
  352. // if ent.OrderStatus != "30" {
  353. // return myerrors.TipsError("当前工单不可完成")
  354. // }
  355. return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  356. _, err = tx.Update("deliver_order", map[string]interface{}{
  357. "order_status": "20",
  358. "finish_remark": req.FinishRemark,
  359. "finish_time": gtime.Now(),
  360. "finish_by": int(s.userInfo.Id),
  361. "finish_by_name": s.userInfo.NickName,
  362. "updated_by": int(s.userInfo.Id),
  363. "updated_name": s.userInfo.NickName,
  364. "updated_time": gtime.Now(),
  365. }, "id = ?", req.OrderId)
  366. return err
  367. })
  368. }
  369. func (s DeliverOrderService) Delete(ctx context.Context, id []int) error {
  370. if len(id) == 0 {
  371. return nil
  372. }
  373. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  374. return err
  375. }