ctr_contract.go 34 KB

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