ctr_contract.go 34 KB

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