ctr_contract.go 34 KB

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