ctr_contract.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/base64"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. basedao "dashoo.cn/micro/app/dao/base"
  14. dao "dashoo.cn/micro/app/dao/contract"
  15. custdao "dashoo.cn/micro/app/dao/cust"
  16. projdao "dashoo.cn/micro/app/dao/proj"
  17. workflowdao "dashoo.cn/micro/app/dao/workflow"
  18. model "dashoo.cn/micro/app/model/contract"
  19. proj "dashoo.cn/micro/app/model/proj"
  20. workflowModel "dashoo.cn/micro/app/model/workflow"
  21. "dashoo.cn/micro/app/service"
  22. baseService "dashoo.cn/micro/app/service/base"
  23. projsrv "dashoo.cn/micro/app/service/proj"
  24. worksrv "dashoo.cn/micro/app/service/work"
  25. workflowService "dashoo.cn/micro/app/service/workflow"
  26. "dashoo.cn/opms_libary/micro_srv"
  27. "dashoo.cn/opms_libary/multipart"
  28. "dashoo.cn/opms_libary/myerrors"
  29. "dashoo.cn/opms_libary/plugin/dingtalk"
  30. "dashoo.cn/opms_libary/plugin/dingtalk/message"
  31. "dashoo.cn/opms_libary/plugin/dingtalk/workflow"
  32. "dashoo.cn/opms_libary/request"
  33. "dashoo.cn/opms_libary/utils"
  34. "github.com/gogf/gf/database/gdb"
  35. "github.com/gogf/gf/frame/g"
  36. "github.com/gogf/gf/os/gcron"
  37. "github.com/gogf/gf/os/glog"
  38. "github.com/gogf/gf/os/gtime"
  39. "github.com/gogf/gf/util/gvalid"
  40. )
  41. type CtrContractService struct {
  42. Dao *dao.CtrContractDao
  43. ProjBusinessDao *projdao.ProjBusinessDao
  44. CustomerDao *custdao.CustCustomerDao
  45. CtrProductDao *dao.CtrContractProductDao
  46. ProductDao *basedao.BaseProductDao
  47. DynamicsDao *dao.CtrContractDynamicsDao
  48. WorkflowDao *workflowdao.PlatWorkflowDao
  49. AppendDao *dao.CtrContractAppendDao
  50. GoalDao *dao.CtrContractGoalDao
  51. Tenant string
  52. userInfo request.UserInfo
  53. DataScope g.Map `json:"dataScope"`
  54. }
  55. func NewCtrContractService(ctx context.Context) (*CtrContractService, error) {
  56. tenant, err := micro_srv.GetTenant(ctx)
  57. if err != nil {
  58. err = myerrors.TipsError(fmt.Sprintf("获取租户码异常:%s", err.Error()))
  59. return nil, err //fmt.Errorf("获取租户码异常:%s", err.Error())
  60. }
  61. // 获取用户信息
  62. userInfo, err := micro_srv.GetUserInfo(ctx)
  63. if err != nil {
  64. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  65. }
  66. return &CtrContractService{
  67. Dao: dao.NewCtrContractDao(tenant),
  68. ProjBusinessDao: projdao.NewProjBusinessDao(tenant),
  69. CustomerDao: custdao.NewCustCustomerDao(tenant),
  70. CtrProductDao: dao.NewCtrContractProductDao(tenant),
  71. ProductDao: basedao.NewBaseProductDao(tenant),
  72. DynamicsDao: dao.NewCtrContractDynamicsDao(tenant),
  73. WorkflowDao: workflowdao.NewPlatWorkflowDao(tenant),
  74. AppendDao: dao.NewCtrContractAppendDao(tenant),
  75. GoalDao: dao.NewCtrContractGoalDao(tenant),
  76. Tenant: tenant,
  77. userInfo: userInfo,
  78. DataScope: userInfo.DataScope,
  79. }, nil
  80. }
  81. func (s CtrContractService) Get(ctx context.Context, id int) (*model.CtrContractGetRsp, error) {
  82. ent, err := s.Dao.Where("Id = ?", id).One()
  83. if err != nil {
  84. return nil, err
  85. }
  86. if ent == nil {
  87. return nil, myerrors.TipsError("合同不存在")
  88. }
  89. product, err := s.CtrProductDao.Where("contract_id = ?", id).All()
  90. if err != nil {
  91. return nil, err
  92. }
  93. if product == nil {
  94. product = []*model.CtrContractProduct{}
  95. }
  96. return &model.CtrContractGetRsp{
  97. CtrContract: *ent,
  98. Product: product,
  99. }, nil
  100. }
  101. func (s CtrContractService) DynamicsList(ctx context.Context, req *model.CtrContractDynamicsListReq) (int, interface{}, error) {
  102. dao := &s.DynamicsDao.CtrContractDynamicsDao
  103. if req.SearchText != "" {
  104. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  105. dao = dao.Where("(opn_people LIKE ? || opn_content LIKE ?)", likestr, likestr)
  106. }
  107. if req.ContractId != 0 {
  108. dao = dao.Where("contract_id = ?", req.ContractId)
  109. }
  110. if req.OpnPeopleId != 0 {
  111. dao = dao.Where("opn_people_id = ?", req.OpnPeopleId)
  112. }
  113. if req.OpnPeople != "" {
  114. likestr := fmt.Sprintf("%%%s%%", req.OpnPeople)
  115. dao = dao.Where("opn_people like ?", likestr)
  116. }
  117. if req.OpnType != "" {
  118. dao = dao.Where("opn_type = ?", req.OpnType)
  119. }
  120. // if req.OpnContent != "" {
  121. // likestr := fmt.Sprintf("%%%s%%", req.OpnContent)
  122. // dao = dao.Where("opn_content like ?", likestr)
  123. // }
  124. if req.BeginTime != "" {
  125. dao = dao.Where("created_time > ?", req.BeginTime)
  126. }
  127. if req.EndTime != "" {
  128. dao = dao.Where("created_time < ?", req.EndTime)
  129. }
  130. total, err := dao.Count()
  131. if err != nil {
  132. return 0, nil, err
  133. }
  134. if req.PageNum != 0 {
  135. dao = dao.Page(req.GetPage())
  136. }
  137. orderby := "created_time desc"
  138. if req.OrderBy != "" {
  139. orderby = req.OrderBy
  140. }
  141. dao = dao.Order(orderby)
  142. ents := []*model.CtrContractDynamics{}
  143. err = dao.Structs(&ents)
  144. if err != nil && err != sql.ErrNoRows {
  145. return 0, nil, err
  146. }
  147. ret := map[string][]*model.CtrContractDynamics{}
  148. for _, ent := range ents {
  149. date := ent.OpnDate.Format("Y-m-d")
  150. ret[date] = append(ret[date], ent)
  151. }
  152. return total, ret, err
  153. }
  154. func (s CtrContractService) List(ctx context.Context, req *model.CtrContractListReq) (int, []*model.CtrContractListRsp, error) {
  155. ctx = context.WithValue(ctx, "contextService", s)
  156. dao := s.Dao.DataScope(ctx, "incharge_id").As("a")
  157. if req.SearchText != "" {
  158. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  159. dao = dao.Where("(a.contract_code LIKE ? || a.contract_name LIKE ? || a.cust_name LIKE ? || a.nbo_name LIKE ?)", likestr, likestr, likestr, likestr)
  160. }
  161. if req.ContractCode != "" {
  162. likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
  163. dao = dao.Where("a.contract_code like ?", likestr)
  164. }
  165. if req.ContractName != "" {
  166. likestr := fmt.Sprintf("%%%s%%", req.ContractName)
  167. dao = dao.Where("a.contract_name like ?", likestr)
  168. }
  169. if req.CustId != 0 {
  170. dao = dao.Where("a.cust_id = ?", req.CustId)
  171. }
  172. if req.CustName != "" {
  173. likestr := fmt.Sprintf("%%%s%%", req.CustName)
  174. dao = dao.Where("a.cust_name like ?", likestr)
  175. }
  176. if req.NboId != 0 {
  177. dao = dao.Where("a.nbo_id = ?", req.NboId)
  178. }
  179. if req.NboName != "" {
  180. likestr := fmt.Sprintf("%%%s%%", req.NboName)
  181. dao = dao.Where("a.nbo_name like ?", likestr)
  182. }
  183. if req.ApproStatus != "" {
  184. dao = dao.Where("a.appro_status = ?", req.ApproStatus)
  185. }
  186. if req.ContractType != "" {
  187. dao = dao.Where("a.contract_type = ?", req.ContractType)
  188. }
  189. // if req.ContractStartTime != nil {
  190. // dao = dao.Where("a.contract_start_time > ?", req.ContractStartTime)
  191. // }
  192. // if req.ContractEndTime != nil {
  193. // dao = dao.Where("a.contract_end_time < ?", req.ContractEndTime)
  194. // }
  195. if req.InchargeId != 0 {
  196. dao = dao.Where("a.incharge_id = ?", req.InchargeId)
  197. }
  198. if req.InchargeName != "" {
  199. likestr := fmt.Sprintf("%%%s%%", req.InchargeName)
  200. dao = dao.Where("a.incharge_name like ?", likestr)
  201. }
  202. if req.SignatoryId != 0 {
  203. dao = dao.Where("a.signatory_id = ?", req.SignatoryId)
  204. }
  205. if req.SignatoryName != "" {
  206. likestr := fmt.Sprintf("%%%s%%", req.SignatoryName)
  207. dao = dao.Where("a.signatory_name like ?", likestr)
  208. }
  209. if req.DistributorId != 0 {
  210. dao = dao.Where("a.distributor_id = ?", req.DistributorId)
  211. }
  212. if req.DistributorName != "" {
  213. likestr := fmt.Sprintf("%%%s%%", req.DistributorName)
  214. dao = dao.Where("a.distributor_name like ?", likestr)
  215. }
  216. if req.BeginTime != "" {
  217. dao = dao.Where("a.created_time > ?", req.BeginTime)
  218. }
  219. if req.EndTime != "" {
  220. dao = dao.Where("a.created_time < ?", req.EndTime)
  221. }
  222. if req.ContractEndTimeStart != "" {
  223. dao = dao.Where("a.contract_end_time >= ?", req.ContractEndTimeStart)
  224. }
  225. if req.ContractEndTimeEnd != "" {
  226. dao = dao.Where("a.contract_end_time <= ?", req.ContractEndTimeEnd)
  227. }
  228. if req.CustProvinceId != 0 {
  229. dao = dao.Where("a.cust_province_id = ?", req.CustProvinceId)
  230. }
  231. if req.CustCityId != 0 {
  232. dao = dao.Where("a.cust_city_id = ?", req.CustCityId)
  233. }
  234. total, err := dao.Count()
  235. if err != nil {
  236. return 0, nil, err
  237. }
  238. if req.PageNum != 0 {
  239. dao = dao.Page(req.GetPage())
  240. }
  241. orderby := "a.contract_sign_time desc"
  242. if req.OrderBy != "" {
  243. orderby = req.OrderBy
  244. }
  245. dao = dao.Order(orderby)
  246. ents := []*model.CtrContractListRsp{}
  247. err = dao.Structs(&ents)
  248. if err != nil && err != sql.ErrNoRows {
  249. return 0, nil, err
  250. }
  251. return total, ents, err
  252. }
  253. func (s CtrContractService) BindProduct(tx *gdb.TX, id int, code string, product []model.CtrAddProduct) error {
  254. var amount float64
  255. for _, p := range product {
  256. amount += (p.TranPrice * float64(p.ProdNum))
  257. }
  258. _, err := tx.Delete("ctr_contract_product", "contract_id = ?", id)
  259. if err != nil {
  260. return err
  261. }
  262. tocreate := []model.CtrContractProduct{}
  263. for _, p := range product {
  264. product, err := s.ProductDao.Where("id = ?", p.ProdId).One()
  265. if err != nil {
  266. return err
  267. }
  268. if product == nil {
  269. return myerrors.TipsError(fmt.Sprintf("产品: %d 不存在", p.ProdId))
  270. }
  271. tocreate = append(tocreate, model.CtrContractProduct{
  272. ContractId: id,
  273. ContractCode: code,
  274. ProdId: p.ProdId,
  275. ProdCode: product.ProdCode,
  276. ProdName: product.ProdName,
  277. ProdClass: product.ProdClass,
  278. ProdNum: p.ProdNum,
  279. MaintTerm: p.MaintTerm,
  280. SugSalesPrice: p.SugSalesPrice,
  281. TranPrice: p.TranPrice,
  282. ContractPrive: amount,
  283. Remark: p.Remark,
  284. CreatedBy: int(s.userInfo.Id),
  285. CreatedName: s.userInfo.NickName,
  286. CreatedTime: gtime.Now(),
  287. UpdatedBy: int(s.userInfo.Id),
  288. UpdatedName: s.userInfo.NickName,
  289. UpdatedTime: gtime.Now(),
  290. })
  291. }
  292. if len(tocreate) != 0 {
  293. _, err = tx.Insert("ctr_contract_product", tocreate)
  294. if err != nil {
  295. return err
  296. }
  297. }
  298. return nil
  299. }
  300. func (s CtrContractService) AddDynamicsByCurrentUser(tx *gdb.TX, contractId int, opnType string, content map[string]interface{}) error {
  301. contentByte, err := json.Marshal(content)
  302. if err != nil {
  303. return err
  304. }
  305. _, err = tx.InsertAndGetId("ctr_contract_dynamics", model.CtrContractDynamics{
  306. ContractId: contractId,
  307. OpnPeopleId: s.userInfo.Id,
  308. OpnPeople: s.userInfo.NickName,
  309. OpnDate: gtime.Now(),
  310. OpnType: opnType,
  311. OpnContent: string(contentByte),
  312. Remark: "",
  313. CreatedBy: s.userInfo.Id,
  314. CreatedName: s.userInfo.NickName,
  315. CreatedTime: gtime.Now(),
  316. UpdatedBy: s.userInfo.Id,
  317. UpdatedName: s.userInfo.NickName,
  318. UpdatedTime: gtime.Now(),
  319. })
  320. return err
  321. }
  322. func (s CtrContractService) Add(ctx context.Context, req *model.CtrContractAddReq) (int, error) {
  323. validErr := gvalid.CheckStruct(ctx, req, nil)
  324. if validErr != nil {
  325. return 0, myerrors.TipsError(validErr.Current().Error())
  326. }
  327. if len(req.Product) == 0 {
  328. return 0, myerrors.TipsError("产品不能为空")
  329. }
  330. for _, p := range req.Product {
  331. if p.ProdNum < 1 {
  332. return 0, myerrors.TipsError("产品数量必须大于 0")
  333. }
  334. }
  335. // c, err := s.Dao.Where("contract_name = ?", req.ContractName).One()
  336. // if err != nil {
  337. // return 0, err
  338. // }
  339. // if c != nil {
  340. // return 0, myerrors.TipsError(fmt.Sprintf("合同名称:%s 已存在", req.ContractName))
  341. // }
  342. nbo, err := s.ProjBusinessDao.Where("id = ?", req.NboId).One()
  343. if err != nil {
  344. return 0, err
  345. }
  346. if nbo == nil {
  347. return 0, myerrors.TipsError("项目不存在")
  348. }
  349. // c, err = s.Dao.Where("nbo_id = ?", req.NboId).One()
  350. // if err != nil {
  351. // return 0, err
  352. // }
  353. // if c != nil {
  354. // return 0, myerrors.TipsError("所选项目已添加合同")
  355. // }
  356. var sequence int
  357. if req.ContractType == "XS" { // 销售合同
  358. sequence, err = service.SequenceYearRest(s.Dao.DB, "contract_code_xs")
  359. if err != nil {
  360. return 0, err
  361. }
  362. } else if req.ContractType == "JS" { // 技术合同
  363. sequence, err = service.SequenceYearRest(s.Dao.DB, "contract_code_js")
  364. if err != nil {
  365. return 0, err
  366. }
  367. } else {
  368. return 0, myerrors.TipsError("不支持的合同类型")
  369. }
  370. if req.ContractCode == "" {
  371. req.ContractCode = fmt.Sprintf("DH%s%s-%03d", req.ContractType, time.Now().Format("0601"), sequence)
  372. }
  373. c, err := s.Dao.Where("contract_code = ?", req.ContractCode).One()
  374. if err != nil {
  375. return 0, err
  376. }
  377. if c != nil {
  378. return 0, myerrors.TipsError(fmt.Sprintf("合同编号:%s 已存在", req.ContractCode))
  379. }
  380. var contractAmount float64
  381. for _, p := range req.Product {
  382. contractAmount += (p.TranPrice * float64(p.ProdNum))
  383. }
  384. ctr := model.CtrContract{
  385. ContractCode: req.ContractCode,
  386. ContractName: req.ContractName,
  387. CustId: nbo.CustId,
  388. CustName: nbo.CustName,
  389. NboId: nbo.Id,
  390. NboName: nbo.NboName,
  391. IsBig: nbo.IsBig,
  392. ProductLine: nbo.ProductLine,
  393. CustProvinceId: nbo.CustProvinceId,
  394. CustProvince: nbo.CustProvince,
  395. CustCityId: nbo.CustCityId,
  396. CustCity: nbo.CustCity,
  397. ApproStatus: "10",
  398. ContractType: req.ContractType,
  399. ContractAmount: contractAmount,
  400. InvoiceAmount: 0,
  401. CollectedAmount: 0,
  402. ContractStartTime: req.ContractStartTime,
  403. ContractEndTime: req.ContractEndTime,
  404. ContractSignTime: req.ContractSignTime,
  405. InchargeId: req.InchargeId,
  406. InchargeName: req.InchargeName,
  407. SignatoryId: req.SignatoryId,
  408. SignatoryName: req.SignatoryName,
  409. SignatoryType: req.SignatoryType,
  410. SignatoryUnit: req.SignatoryUnit,
  411. EarnestMoney: req.EarnestMoney,
  412. CustSignatoryId: req.CustSignatoryId,
  413. CustSignatoryName: req.CustSignatoryName,
  414. DistributorId: req.DistributorId,
  415. DistributorName: req.DistributorName,
  416. Remark: req.Remark,
  417. ServiceFeeAgreement: req.ServiceFeeAgreement,
  418. CreatedBy: int(s.userInfo.Id),
  419. CreatedName: s.userInfo.NickName,
  420. CreatedTime: gtime.Now(),
  421. UpdatedBy: int(s.userInfo.Id),
  422. UpdatedName: s.userInfo.NickName,
  423. UpdatedTime: gtime.Now(),
  424. }
  425. var id int
  426. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  427. ctrid, err := tx.InsertAndGetId("ctr_contract", ctr)
  428. if err != nil {
  429. return err
  430. }
  431. err = s.BindProduct(tx, int(ctrid), ctr.ContractCode, req.Product)
  432. if err != nil {
  433. return err
  434. }
  435. err = s.AddDynamicsByCurrentUser(tx, int(ctrid), "创建合同", map[string]interface{}{})
  436. if err != nil {
  437. return err
  438. }
  439. _, err = tx.Update("proj_business", map[string]interface{}{
  440. "nbo_type": projsrv.StatusDeal,
  441. }, "id = ?", nbo.Id)
  442. if err != nil {
  443. return err
  444. }
  445. id = int(ctrid)
  446. return nil
  447. })
  448. return id, txerr
  449. }
  450. var ContractApplyProcessCode = "PROC-7057E20A-2066-4644-9B35-9331E4DA912C" // 创建合同
  451. func (s CtrContractService) Commit(ctx context.Context, req *model.CtrContractCommitReq) (int64, error) {
  452. validErr := gvalid.CheckStruct(ctx, req, nil)
  453. if validErr != nil {
  454. return 0, myerrors.TipsError(validErr.Current().Error())
  455. }
  456. if !(req.ContractModel == "大数模板" || req.ContractModel == "客户模板") {
  457. return 0, myerrors.TipsError("合同模板不合法")
  458. }
  459. if !(req.Terms == "接纳全部条款" || req.Terms == "不接纳全部条款") {
  460. return 0, myerrors.TipsError("条款情况不合法")
  461. }
  462. if req.PayTerms == "" {
  463. return 0, myerrors.TipsError("付款条件不能为空")
  464. }
  465. if len(req.File) == 0 {
  466. return 0, myerrors.TipsError("附件不能为空")
  467. }
  468. ent, err := s.Dao.Where("id = ?", req.Id).One()
  469. if err != nil {
  470. return 0, err
  471. }
  472. if ent == nil {
  473. return 0, myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.Id))
  474. }
  475. fileinfoByte, err := json.Marshal(req.File)
  476. if err != nil {
  477. return 0, err
  478. }
  479. workflowSrv, err := workflowService.NewFlowService(ctx)
  480. if err != nil {
  481. return 0, err
  482. }
  483. bizCode := strconv.Itoa(ent.Id)
  484. workflowId, err := workflowSrv.StartProcessInstance(bizCode, "30", "", &workflow.StartProcessInstanceRequest{
  485. ProcessCode: &ContractApplyProcessCode,
  486. FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
  487. {
  488. Id: utils.String("DDSelectField_ESX8H30W3VK0"),
  489. Name: utils.String("合同模板"),
  490. Value: utils.String(req.ContractModel),
  491. },
  492. {
  493. Id: utils.String("DDSelectField_13IQX96C2KAK0"),
  494. Name: utils.String("条款情况"),
  495. Value: utils.String(req.Terms),
  496. },
  497. {
  498. Id: utils.String("TextField_1A5SA7VOG5TS0"),
  499. Name: utils.String("合同编号"),
  500. Value: utils.String(ent.ContractCode),
  501. },
  502. {
  503. Id: utils.String("TextField_1EX61DPS3LA80"),
  504. Name: utils.String("客户名称"),
  505. Value: utils.String(ent.CustName),
  506. },
  507. {
  508. Id: utils.String("MoneyField_X1XV4KIR0GW0"),
  509. Name: utils.String("合同金额(元)"),
  510. Value: utils.String(strconv.FormatFloat(ent.ContractAmount, 'f', 2, 64)),
  511. },
  512. {
  513. Id: utils.String("TextareaField_1WZ5ILKBUVSW0"),
  514. Name: utils.String("付款条件"),
  515. Value: utils.String(req.PayTerms),
  516. },
  517. {
  518. Id: utils.String("DDAttachment_1051KJYC3MBK0"),
  519. Name: utils.String("附件"),
  520. // Details: productForm,
  521. Value: utils.String(string(fileinfoByte)),
  522. },
  523. },
  524. })
  525. if err != nil {
  526. return 0, err
  527. }
  528. _, err = s.Dao.Where("id = ?", ent.Id).Data(map[string]interface{}{
  529. "appro_status": 20,
  530. }).Update()
  531. return workflowId, err
  532. }
  533. // var spaceId = "21042518430"
  534. var spaceId = "21077726250"
  535. func (s CtrContractService) CommitWithFile(ctx context.Context, formData *multipart.Form) error {
  536. if s.userInfo.DingtalkUid == "" {
  537. return fmt.Errorf("该用户钉钉 uid 为空")
  538. }
  539. if len(formData.File) == 0 {
  540. return myerrors.TipsError("文件不能为空")
  541. }
  542. if _, ok := formData.File["file"]; !ok {
  543. return myerrors.TipsError("文件不能为空")
  544. }
  545. if formData.File["file"].FileName == "" {
  546. return fmt.Errorf("文件名称不能为空")
  547. }
  548. if formData.File["file"].File == nil {
  549. return fmt.Errorf("文件不能为空")
  550. }
  551. if formData.File["file"].File.Name() == "" {
  552. return fmt.Errorf("文件路径不能为空")
  553. }
  554. contractId, err := strconv.Atoi(formData.Value["contractId"])
  555. if err != nil {
  556. return fmt.Errorf("合同 Id 不合法 %s", formData.Value["contractId"])
  557. }
  558. //fmt.Println(args.File.Name(), args.FileName, args.FileSize, args.Meta)
  559. //fmt.Println(spaceId, s.userInfo.DingtalkId, args.FileName, args.File.Name())
  560. // resp, err := s.UploadFile("21042518430", "8xljy04PZiS9iPxp5PhDnUzQiEiE", "引物导入模板-000000.xlsx", "/Users/chengjian/Downloads/引物导入模板.xlsx")
  561. resp, err := dingtalk.Client.GetStorage().UploadFile(spaceId, s.userInfo.DingtalkId, formData.File["file"].FileName, formData.File["file"].File.Name())
  562. if err != nil {
  563. return fmt.Errorf("钉钉上传文件异常 %s", err.Error())
  564. }
  565. _, err = s.Commit(ctx, &model.CtrContractCommitReq{
  566. Id: contractId,
  567. ContractModel: formData.Value["contractModel"],
  568. Terms: formData.Value["terms"],
  569. PayTerms: formData.Value["payTerms"],
  570. File: []model.DingFileInfo{
  571. {
  572. SpaceId: resp.Dentry.SpaceId,
  573. FileId: resp.Dentry.Id,
  574. FileName: resp.Dentry.Name,
  575. FileSize: resp.Dentry.Size,
  576. FileType: resp.Dentry.Extension,
  577. },
  578. },
  579. })
  580. if err != nil {
  581. return err
  582. }
  583. // workflow, err := s.WorkflowDao.Where("id = ?", workflowId).One()
  584. // if err != nil {
  585. // return err
  586. // }
  587. // instance, err := dingtalk.Client.GetWorkflow().QueryProcessInstanceDetail(workflow.ProcessInstId)
  588. // if err != nil {
  589. // return fmt.Errorf("查询审批实例详情错误: %s", err.Error())
  590. // }
  591. // fmt.Println(workflow.ProcessInstId, instance.Result.ApproverUserIds)
  592. // approverUserIds := g.Config().GetStrings("dingtalk.approver-user-ids")
  593. // fmt.Println(approverUserIds)
  594. // for _, uid := range approverUserIds {
  595. // resp, err := dingtalk.Client.GetStorage().AddPermission(
  596. // dingtalk.Client.Context.CorpId, spaceId, uid, "DOWNLOADER", "USER")
  597. // if err != nil {
  598. // return fmt.Errorf("添加审核附件权限异常: %s", err.Error())
  599. // }
  600. // fmt.Println(uid, resp)
  601. // }
  602. appendSrv, err := NewCtrContractAppendService(ctx)
  603. if err != nil {
  604. return err
  605. }
  606. _, err = appendSrv.Add(ctx, &model.CtrContractAppendAddReq{
  607. ContractId: contractId,
  608. FileName: resp.Dentry.Name,
  609. FileType: resp.Dentry.Extension,
  610. FileUrl: strings.Join([]string{"dingtalk", resp.Dentry.SpaceId, resp.Dentry.Id}, ":"),
  611. Remark: "",
  612. })
  613. if err != nil {
  614. return err
  615. }
  616. return nil
  617. }
  618. func (s CtrContractService) CommitWithFileUrl(ctx context.Context, req *model.CtrContractCommitWithFileUrlReq) error {
  619. validErr := gvalid.CheckStruct(ctx, req, nil)
  620. if validErr != nil {
  621. return myerrors.TipsError(validErr.Current().Error())
  622. }
  623. fileinfoByte, err := baseService.UploadDingtalk(s.userInfo.DingtalkId, req.FileUrl, req.FileName)
  624. if err != nil {
  625. return err
  626. }
  627. var fileinfo = []model.DingFileInfo{}
  628. err = json.Unmarshal(fileinfoByte, &fileinfo)
  629. if err != nil {
  630. return err
  631. }
  632. _, err = s.Commit(ctx, &model.CtrContractCommitReq{
  633. Id: req.Id,
  634. ContractModel: req.ContractModel,
  635. Terms: req.Terms,
  636. PayTerms: req.PayTerms,
  637. File: fileinfo,
  638. })
  639. if err != nil {
  640. return err
  641. }
  642. appendSrv, err := NewCtrContractAppendService(ctx)
  643. if err != nil {
  644. return err
  645. }
  646. _, err = appendSrv.Add(ctx, &model.CtrContractAppendAddReq{
  647. ContractId: req.Id,
  648. FileName: fileinfo[0].FileName,
  649. FileType: fileinfo[0].FileType,
  650. FileUrl: strings.Join([]string{"dingtalk", fileinfo[0].SpaceId, fileinfo[0].FileId}, ":"),
  651. Remark: "",
  652. })
  653. if err != nil {
  654. return err
  655. }
  656. return nil
  657. }
  658. func (s CtrContractService) DownloadDingtalkFile(ctx context.Context, id int) (string, error) {
  659. ent, err := s.AppendDao.Where("id= ?", id).One()
  660. if err != nil {
  661. return "", err
  662. }
  663. if ent == nil {
  664. return "", myerrors.TipsError("附件不存在")
  665. }
  666. if !strings.HasPrefix(ent.FileUrl, "dingtalk") {
  667. return "", myerrors.TipsError("此附件不是钉钉附件")
  668. }
  669. fileInfo := strings.Split(ent.FileUrl, ":")
  670. if len(fileInfo) != 3 {
  671. return "", myerrors.TipsError("钉钉附件地址不合法")
  672. }
  673. spaceId := fileInfo[1]
  674. fileId := fileInfo[2]
  675. // res, err := dingtalk.Client.GetStorage().AddPermission(dingtalk.Client.Context.CorpId, spaceId, s.userInfo.DingtalkId, "EDITOR", "USER")
  676. // fmt.Println(res, err)
  677. // if err != nil {
  678. // return "", fmt.Errorf("设置权限异常 %s", err.Error())
  679. // }
  680. resp, err := dingtalk.Client.GetStorage().QueryFileDownloadInfo(spaceId, fileId, s.userInfo.DingtalkId)
  681. if err != nil {
  682. return "", myerrors.TipsError("获取文件下载信息异常")
  683. }
  684. fmt.Println(resp, err)
  685. req, err := http.NewRequest("GET", resp.HeaderSignatureInfo.ResourceUrls[0], nil)
  686. if err != nil {
  687. return "", fmt.Errorf("构建文件下载请求异常 %s", err.Error())
  688. }
  689. for k, v := range resp.HeaderSignatureInfo.Headers {
  690. req.Header.Add(k, v)
  691. }
  692. fileresp, err := http.DefaultClient.Do(req)
  693. if err != nil {
  694. return "", fmt.Errorf("文件下载异常 %s", err.Error())
  695. }
  696. data, err := io.ReadAll(fileresp.Body)
  697. if err != nil {
  698. return "", fmt.Errorf("读取下载内容异常 %s", err.Error())
  699. }
  700. return base64.StdEncoding.EncodeToString(data), nil
  701. }
  702. func ContractApplyApproval(ctx context.Context, flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  703. tenant, err := micro_srv.GetTenant(ctx)
  704. if err != nil {
  705. return fmt.Errorf("获取租户码异常:%s", err.Error())
  706. }
  707. contractDao := dao.NewCtrContractDao(tenant)
  708. contractId, err := strconv.Atoi(flow.BizCode)
  709. if err != nil {
  710. return fmt.Errorf("创建合同审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  711. }
  712. contract, err := contractDao.Where("id = ?", contractId).One()
  713. if err != nil {
  714. return err
  715. }
  716. if contract == nil {
  717. return fmt.Errorf("合同不存在:%s Id: %d", flow.BizCode, flow.Id)
  718. }
  719. if msg.ProcessType != "finish" && msg.ProcessType != "terminate" {
  720. return fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
  721. }
  722. if msg.Result != "agree" && msg.Result != "refuse" && msg.Result != "" {
  723. return fmt.Errorf("无法识别的 Result :%s", msg.Result)
  724. }
  725. var status string
  726. if msg.ProcessType == "terminate" {
  727. status = "50"
  728. } else {
  729. pass := msg.Result == "agree"
  730. if !pass {
  731. status = "40"
  732. } else {
  733. status = "30"
  734. }
  735. }
  736. _, err = contractDao.Where("id = ?", contractId).Data(map[string]interface{}{
  737. "appro_status": status,
  738. }).Update()
  739. if err != nil {
  740. return err
  741. }
  742. if status != "30" {
  743. return nil
  744. }
  745. return contractDao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  746. // 交付状态(0发起 10项目立项 15进行中 20 完成 30审批拒绝40关闭)
  747. _, err = worksrv.DeliverOrderAdd(tx, contractId, request.UserInfo{}, nil, "10")
  748. return err
  749. })
  750. }
  751. func (s CtrContractService) Update(ctx context.Context, req *model.CtrContractUpdateReq) error {
  752. validErr := gvalid.CheckStruct(ctx, req, nil)
  753. if validErr != nil {
  754. return myerrors.TipsError(validErr.Current().Error())
  755. }
  756. ent, err := s.Dao.Where("id = ?", req.Id).One()
  757. if err != nil {
  758. return err
  759. }
  760. if ent == nil {
  761. return myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.Id))
  762. }
  763. // if req.ContractCode != "" {
  764. // exist, err := s.Dao.Where("contract_code = ?", req.ContractCode).One()
  765. // if err != nil {
  766. // return err
  767. // }
  768. // if exist != nil && exist.Id != req.Id {
  769. // return myerrors.NewMsgError(nil, fmt.Sprintf("合同编号:%s 已存在", req.ContractCode))
  770. // }
  771. // }
  772. if req.ContractName != "" {
  773. exist, err := s.Dao.Where("contract_name = ?", req.ContractName).One()
  774. if err != nil {
  775. return err
  776. }
  777. if exist != nil && exist.Id != req.Id {
  778. return myerrors.TipsError(fmt.Sprintf("合同名称:%s 已存在", req.ContractName))
  779. }
  780. }
  781. var nbo *proj.ProjBusiness
  782. if req.NboId != 0 {
  783. nbo, err = s.ProjBusinessDao.Where("id = ?", req.NboId).One()
  784. if err != nil {
  785. return err
  786. }
  787. if nbo == nil {
  788. return myerrors.TipsError("项目不存在")
  789. }
  790. }
  791. toupdate := map[string]interface{}{}
  792. // if req.ContractCode != "" {
  793. // toupdate["contract_code"] = req.ContractCode
  794. // }
  795. if req.ContractName != "" {
  796. toupdate["contract_name"] = req.ContractName
  797. }
  798. if req.NboId != 0 {
  799. toupdate["cust_id"] = nbo.CustId
  800. toupdate["cust_name"] = nbo.CustName
  801. toupdate["nbo_id"] = nbo.Id
  802. toupdate["nbo_name"] = nbo.NboName
  803. toupdate["is_big"] = nbo.IsBig
  804. toupdate["product_line"] = nbo.ProductLine
  805. toupdate["cust_province_id"] = nbo.CustProvinceId
  806. toupdate["cust_province"] = nbo.CustProvince
  807. toupdate["cust_city_id"] = nbo.CustCityId
  808. toupdate["cust_city"] = nbo.CustCity
  809. }
  810. // if req.ApproStatus != "" {
  811. // toupdate["appro_status"] = req.ApproStatus
  812. // }
  813. if req.ContractType != "" {
  814. toupdate["contract_type"] = req.ContractType
  815. }
  816. // if req.ContractAmount != 0 {
  817. // toupdate["contract_amount"] = req.ContractAmount
  818. // }
  819. // if req.InvoiceAmount != 0 {
  820. // toupdate["invoice_amount"] = req.InvoiceAmount
  821. // }
  822. // if req.CollectedAmount != 0 {
  823. // toupdate["collected_amount"] = req.CollectedAmount
  824. // }
  825. if req.ContractStartTime != nil {
  826. toupdate["contract_start_time"] = req.ContractStartTime
  827. }
  828. if req.ContractEndTime != nil {
  829. toupdate["contract_end_time"] = req.ContractEndTime
  830. }
  831. if req.ContractSignTime != nil {
  832. toupdate["contract_sign_time"] = req.ContractSignTime
  833. }
  834. //if req.InchargeId != 0 {
  835. // toupdate["incharge_id"] = req.InchargeId
  836. //}
  837. //if req.InchargeName != "" {
  838. // toupdate["incharge_name"] = req.InchargeName
  839. //}
  840. if req.SignatoryId != 0 {
  841. toupdate["signatory_id"] = req.SignatoryId
  842. }
  843. if req.SignatoryName != "" {
  844. toupdate["signatory_name"] = req.SignatoryName
  845. }
  846. if req.SignatoryType != "" {
  847. toupdate["signatory_type"] = req.SignatoryType
  848. }
  849. if req.SignatoryUnit != "" {
  850. toupdate["signatory_unit"] = req.SignatoryUnit
  851. }
  852. if req.EarnestMoney != nil {
  853. toupdate["earnest_money"] = req.EarnestMoney
  854. }
  855. if req.CustSignatoryId != 0 {
  856. toupdate["cust_signatory_id"] = req.CustSignatoryId
  857. }
  858. if req.CustSignatoryName != "" {
  859. toupdate["cust_signatory_name"] = req.CustSignatoryName
  860. }
  861. if req.DistributorId != 0 {
  862. toupdate["distributor_id"] = req.DistributorId
  863. }
  864. if req.DistributorName != "" {
  865. toupdate["distributor_name"] = req.DistributorName
  866. }
  867. if req.Remark != nil {
  868. toupdate["remark"] = *req.Remark
  869. }
  870. if req.Product != nil {
  871. var contractAmount float64
  872. for _, p := range *req.Product {
  873. contractAmount += (p.TranPrice * float64(p.ProdNum))
  874. }
  875. toupdate["contract_amount"] = contractAmount
  876. }
  877. if len(toupdate) != 0 {
  878. toupdate["updated_by"] = int(s.userInfo.Id)
  879. toupdate["updated_name"] = s.userInfo.NickName
  880. toupdate["updated_time"] = gtime.Now()
  881. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  882. _, err = tx.Update("ctr_contract", toupdate, "id = ?", req.Id)
  883. if err != nil {
  884. return err
  885. }
  886. if req.Product != nil {
  887. err = s.BindProduct(tx, req.Id, ent.ContractCode, *req.Product)
  888. if err != nil {
  889. return err
  890. }
  891. }
  892. return nil
  893. })
  894. if txerr != nil {
  895. return txerr
  896. }
  897. }
  898. return nil
  899. }
  900. func (s CtrContractService) UpdateProduct(ctx context.Context, req *model.CtrContractUpdateProductReq) error {
  901. validErr := gvalid.CheckStruct(ctx, req, nil)
  902. if validErr != nil {
  903. return myerrors.TipsError(validErr.Current().Error())
  904. }
  905. ent, err := s.CtrProductDao.Where("id = ?", req.Id).One()
  906. if err != nil {
  907. return err
  908. }
  909. if ent == nil {
  910. return myerrors.TipsError(fmt.Sprintf("合同产品记录不存在: %d", req.Id))
  911. }
  912. toupdate := map[string]interface{}{}
  913. if req.MaintainPeriod != nil {
  914. toupdate["maintain_period"] = req.MaintainPeriod
  915. }
  916. if req.WarrantPeriod != nil {
  917. toupdate["warrant_period"] = req.WarrantPeriod
  918. }
  919. if req.MaintainStartTime != nil {
  920. toupdate["maintain_start_time"] = req.MaintainStartTime
  921. }
  922. if req.MaintainRemark != nil {
  923. toupdate["maintain_remark"] = req.MaintainRemark
  924. }
  925. if req.AcceptTime != nil {
  926. toupdate["accept_time"] = req.AcceptTime
  927. }
  928. if req.PurchaseCost != nil {
  929. toupdate["purchase_cost"] = req.PurchaseCost
  930. }
  931. if req.DevCost != nil {
  932. toupdate["dev_cost"] = req.DevCost
  933. }
  934. if req.MaintainCost != nil {
  935. toupdate["maintain_cost"] = req.MaintainCost
  936. }
  937. if req.DirectCost != nil {
  938. toupdate["direct_cost"] = req.DirectCost
  939. }
  940. if len(toupdate) != 0 {
  941. toupdate["updated_by"] = int(s.userInfo.Id)
  942. toupdate["updated_name"] = s.userInfo.NickName
  943. toupdate["updated_time"] = gtime.Now()
  944. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  945. _, err = tx.Update("ctr_contract_product", toupdate, "id = ?", req.Id)
  946. if err != nil {
  947. return err
  948. }
  949. err = s.AddDynamicsByCurrentUser(tx, ent.ContractId, "更新合同产品信息", toupdate)
  950. if err != nil {
  951. return err
  952. }
  953. return nil
  954. })
  955. if txerr != nil {
  956. return txerr
  957. }
  958. }
  959. return nil
  960. }
  961. func (s CtrContractService) Transfer(ctx context.Context, req *model.CtrContractTransferReq) error {
  962. if len(req.Id) == 0 {
  963. return nil
  964. }
  965. ents := map[int]*model.CtrContract{}
  966. for _, i := range req.Id {
  967. ent, err := s.Dao.Where("id = ?", i).One()
  968. if err != nil {
  969. return err
  970. }
  971. if ent == nil {
  972. return myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.Id))
  973. }
  974. ents[ent.Id] = ent
  975. }
  976. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  977. toupdate := map[string]interface{}{
  978. "incharge_id": req.InchargeId,
  979. "incharge_name": req.InchargeName,
  980. }
  981. _, err := tx.Update("ctr_contract", toupdate, "id in (?)", req.Id)
  982. if err != nil {
  983. return err
  984. }
  985. for _, ent := range ents {
  986. err = s.AddDynamicsByCurrentUser(tx, ent.Id, "转移合同", map[string]interface{}{
  987. "toInchargeId": req.InchargeId,
  988. "toInchargeName": req.InchargeName,
  989. "fromInchargeId": ent.InchargeId,
  990. "fromInchargeName": ent.InchargeName,
  991. "operatedId": s.userInfo.Id,
  992. "operatedName": s.userInfo.NickName,
  993. })
  994. if err != nil {
  995. return err
  996. }
  997. }
  998. return nil
  999. })
  1000. if txerr != nil {
  1001. return txerr
  1002. }
  1003. return nil
  1004. }
  1005. func (s CtrContractService) UpdateInvoiceAmount(tx *gdb.TX, id int) error {
  1006. ctr := model.CtrContract{}
  1007. err := tx.GetStruct(&ctr, "select * from ctr_contract where id = ?", id)
  1008. if err == sql.ErrNoRows {
  1009. return myerrors.TipsError(fmt.Sprintf("合同不存在 %d", id))
  1010. }
  1011. if err != nil {
  1012. return err
  1013. }
  1014. v, err := tx.GetValue("select sum(invoice_amount) from ctr_contract_invoice where contract_id=? and appro_status='30' and deleted_time is null", id)
  1015. if err != nil {
  1016. return err
  1017. }
  1018. amount := v.Float64()
  1019. _, err = tx.Update("ctr_contract",
  1020. map[string]interface{}{
  1021. "invoice_amount": amount,
  1022. }, "id = ?", id)
  1023. if err != nil {
  1024. return err
  1025. }
  1026. return nil
  1027. }
  1028. func (s CtrContractService) UpdateCollectedAmount(tx *gdb.TX, id int) error {
  1029. ctr := model.CtrContract{}
  1030. err := tx.GetStruct(&ctr, "select * from ctr_contract where id = ?", id)
  1031. if err == sql.ErrNoRows {
  1032. return myerrors.TipsError(fmt.Sprintf("合同不存在 %d", id))
  1033. }
  1034. if err != nil {
  1035. return err
  1036. }
  1037. v, err := tx.GetValue("select sum(collection_amount) from ctr_contract_collection where contract_id=? and appro_status='20' and deleted_time is null", id)
  1038. if err != nil {
  1039. return err
  1040. }
  1041. amount := v.Float64()
  1042. _, err = tx.Update("ctr_contract",
  1043. map[string]interface{}{
  1044. "collected_amount": amount,
  1045. }, "id = ?", id)
  1046. if err != nil {
  1047. return err
  1048. }
  1049. return nil
  1050. }
  1051. func (s CtrContractService) Delete(ctx context.Context, id []int) error {
  1052. if len(id) == 0 {
  1053. return nil
  1054. }
  1055. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  1056. return err
  1057. }
  1058. func init() {
  1059. tenant := g.Config().GetString("micro_srv.tenant")
  1060. if tenant == "" {
  1061. panic("定时任务租户码未设置,请前往配置")
  1062. }
  1063. contractDao := dao.NewCtrContractDao(tenant)
  1064. produceDao := dao.NewCtrContractProductDao(tenant)
  1065. job := func() {
  1066. alert := map[int]*[]model.CtrContractProduct{
  1067. 180: {},
  1068. 90: {},
  1069. 30: {},
  1070. 10: {},
  1071. // 7: {},
  1072. }
  1073. where := `DATE_FORMAT(DATE_ADD(maintain_start_time, INTERVAL ? day), "%Y-%m-%d") = DATE_FORMAT(NOW(), "%Y-%m-%d")`
  1074. for day, p := range alert {
  1075. err := produceDao.Where(where, day).Structs(p)
  1076. if err != nil {
  1077. glog.Error(err)
  1078. }
  1079. glog.Infof("运维期到期 %d 天提醒,产品个数 %d", day, len(*p))
  1080. }
  1081. for day, p := range alert {
  1082. for _, i := range *p {
  1083. ctr, err := contractDao.Where("id = ?", i.ContractId).One()
  1084. if err != nil {
  1085. glog.Error(err)
  1086. }
  1087. text := fmt.Sprintf("合同:%s-%s 中的产品:%s 距离运维到期还有 %d 天",
  1088. ctr.ContractCode, ctr.ContractName, i.ProdName, day)
  1089. msg := g.MapStrStr{
  1090. "msgTitle": "运维期到期提醒",
  1091. "msgContent": text,
  1092. "msgType": "20",
  1093. "recvUserIds": strconv.Itoa(ctr.InchargeId),
  1094. "msgStatus": "10",
  1095. "sendType": "10",
  1096. }
  1097. if err := service.CreateSystemMessage(msg); err != nil {
  1098. glog.Error("消息提醒异常:", err)
  1099. }
  1100. glog.Infof("%s", text)
  1101. }
  1102. }
  1103. }
  1104. // 每天凌晨2点执行
  1105. gcron.AddSingleton("0 0 2 * * *", job)
  1106. // job()
  1107. }