register.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. package contract
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-playground/validator/v10"
  8. "github.com/tealeg/xlsx"
  9. "hongze/fms_api/controller/resp"
  10. "hongze/fms_api/global"
  11. "hongze/fms_api/models/base"
  12. "hongze/fms_api/models/crm"
  13. "hongze/fms_api/models/fms"
  14. "hongze/fms_api/models/system"
  15. fmsService "hongze/fms_api/services/fms"
  16. "hongze/fms_api/utils"
  17. "net/http"
  18. "time"
  19. )
  20. // RegisterController 合同登记
  21. type RegisterController struct{}
  22. // List
  23. // @Title 合同登记列表
  24. // @Description 合同登记列表
  25. // @Param Keyword query string false "关键词"
  26. // @Param StartDate query string false "合同开始日期"
  27. // @Param EndDate query string false "合同结束日期"
  28. // @Param ServiceType query int false "套餐类型"
  29. // @Param ContractType query int false "合同类型"
  30. // @Param RegisterStatus query int false "登记状态"
  31. // @Success 200 {object} fms.ContractRegisterItem
  32. // @router /contract/register/list [get]
  33. func (rg *RegisterController) List(c *gin.Context) {
  34. var req fms.ContractRegisterListReq
  35. if e := c.BindQuery(&req); e != nil {
  36. err, ok := e.(validator.ValidationErrors)
  37. if !ok {
  38. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  39. return
  40. }
  41. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  42. return
  43. }
  44. cond := `1 = 1`
  45. pars := make([]interface{}, 0)
  46. if req.Keyword != "" {
  47. kw := "%" + req.Keyword + "%"
  48. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ?)`
  49. pars = append(pars, kw, kw, kw)
  50. }
  51. if req.StartDate != "" && req.EndDate != "" {
  52. cond += ` AND (create_time BETWEEN ? AND ?)`
  53. pars = append(pars, req.StartDate, req.EndDate)
  54. }
  55. if req.ContractType != 0 {
  56. cond += ` AND contract_type = ?`
  57. pars = append(pars, req.ContractType)
  58. }
  59. if req.RegisterStatus != 0 {
  60. cond += ` AND register_status = ?`
  61. pars = append(pars, req.RegisterStatus)
  62. }
  63. // 套餐筛选
  64. if req.ServiceType != 0 {
  65. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  66. if e != nil {
  67. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  68. return
  69. }
  70. if len(registerIds) > 0 {
  71. cond += ` AND contract_register_id IN ?`
  72. pars = append(pars, registerIds)
  73. } else {
  74. cond += ` AND 1 = 2`
  75. }
  76. }
  77. page := new(base.Page)
  78. page.SetPageSize(req.PageSize)
  79. page.SetCurrent(req.Current)
  80. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  81. total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
  82. if e != nil {
  83. resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
  84. return
  85. }
  86. registerIds := make([]int, 0)
  87. for i := range list {
  88. registerIds = append(registerIds, list[i].ContractRegisterId)
  89. }
  90. serviceMap := make(map[int]string, 0)
  91. invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  92. paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  93. if len(registerIds) > 0 {
  94. // 获取服务套餐
  95. servicesNameList, e := fms.GetContractRegisterServicesNameByRegisterIds(registerIds)
  96. if e != nil {
  97. resp.FailMsg("获取失败", "获取套餐拼接字符串失败, Err: "+e.Error(), c)
  98. return
  99. }
  100. for i := range servicesNameList {
  101. serviceMap[servicesNameList[i].ContractRegisterId] = servicesNameList[i].ServicesName
  102. }
  103. // 获取开票/到款列表
  104. invoiceCond := `contract_register_id IN ?`
  105. invoicePars := make([]interface{}, 0)
  106. invoicePars = append(invoicePars, registerIds)
  107. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  108. if e != nil {
  109. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  110. return
  111. }
  112. for i := range invoiceList {
  113. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  114. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  115. }
  116. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  117. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  118. }
  119. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  120. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  121. }
  122. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  123. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  124. }
  125. }
  126. }
  127. respList := make([]*fms.ContractRegisterList, 0)
  128. for i := range list {
  129. v := new(fms.ContractRegisterList)
  130. v.ContractRegisterItem = list[i]
  131. v.ServicesName = serviceMap[list[i].ContractRegisterId]
  132. v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
  133. v.PaymentList = paymentMap[list[i].ContractRegisterId]
  134. respList = append(respList, v)
  135. }
  136. page.SetTotal(total)
  137. baseData := new(base.BaseData)
  138. baseData.SetPage(page)
  139. baseData.SetList(respList)
  140. resp.OkData("获取成功", baseData, c)
  141. }
  142. // Add
  143. // @Title 新增合同登记
  144. // @Description 新增合同登记
  145. // @Param request body fms.ContractRegisterAddReq true "type json string"
  146. // @Success 200 string "操作成功"
  147. // @router /contract/register/add [post]
  148. func (rg *RegisterController) Add(c *gin.Context) {
  149. req := new(fms.ContractRegisterAddReq)
  150. err := c.ShouldBind(&req)
  151. if err != nil {
  152. errs, ok := err.(validator.ValidationErrors)
  153. if !ok {
  154. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  155. return
  156. }
  157. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  158. return
  159. }
  160. claims, _ := c.Get("adminInfo")
  161. adminInfo := claims.(*system.SysAdmin)
  162. // 日期校验
  163. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  164. if e != nil {
  165. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  166. return
  167. }
  168. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  169. if e != nil {
  170. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  171. return
  172. }
  173. signDate, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  174. if e != nil {
  175. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  176. return
  177. }
  178. // 是否存在相同合同编号的登记
  179. ob := new(fms.ContractRegister)
  180. existCond := `contract_code = ?`
  181. existPars := make([]interface{}, 0)
  182. existPars = append(existPars, req.ContractCode)
  183. exist, e := ob.FetchByCondition(existCond, existPars)
  184. if e != nil && e != utils.ErrNoRow {
  185. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  186. return
  187. }
  188. if exist != nil && exist.ContractRegisterId > 0 {
  189. resp.Fail("合同编号已存在", c)
  190. return
  191. }
  192. nowTime := time.Now().Local()
  193. ob.ContractCode = req.ContractCode
  194. ob.CrmContractId = req.CrmContractId
  195. ob.ContractSource = req.ContractSource
  196. ob.CompanyName = req.CompanyName
  197. ob.ProductId = req.ProductId
  198. ob.SellerId = req.SellerId
  199. ob.SellerName = req.SellerName
  200. ob.ContractType = req.ContractType
  201. ob.ContractAmount = req.ContractAmount
  202. ob.StartDate = startDate
  203. ob.EndDate = endDate
  204. ob.SignDate = signDate
  205. ob.AgreedPayTime = req.AgreedPayTime
  206. ob.ContractStatus = req.ContractStatus
  207. ob.RegisterStatus = fms.ContractRegisterStatusIng
  208. ob.Remark = req.Remark
  209. ob.Set()
  210. // 套餐信息
  211. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services)
  212. if e != nil {
  213. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  214. return
  215. }
  216. // 新增合同及套餐
  217. if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
  218. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  219. return
  220. }
  221. // 操作日志
  222. go func() {
  223. opData := ""
  224. opDataByte, e := json.Marshal(req)
  225. if e != nil {
  226. return
  227. }
  228. opData = string(opDataByte)
  229. logItem := new(fms.ContractRegisterLog)
  230. logItem.ContractRegisterId = ob.ContractRegisterId
  231. logItem.AdminId = int(adminInfo.AdminId)
  232. logItem.AdminName = adminInfo.AdminName
  233. logItem.OpData = opData
  234. logItem.OpType = fms.ContractRegisterOpTypeSave
  235. logItem.CreateTime = nowTime
  236. if e = logItem.Create(); e != nil {
  237. return
  238. }
  239. }()
  240. resp.Ok("操作成功", c)
  241. }
  242. // Edit
  243. // @Title 编辑合同登记
  244. // @Description 编辑合同登记
  245. // @Param request body fms.ContractRegisterEditReq true "type json string"
  246. // @Success 200 string "操作成功"
  247. // @router /contract/register/edit [post]
  248. func (rg *RegisterController) Edit(c *gin.Context) {
  249. req := new(fms.ContractRegisterEditReq)
  250. err := c.ShouldBind(&req)
  251. if err != nil {
  252. errs, ok := err.(validator.ValidationErrors)
  253. if !ok {
  254. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  255. return
  256. }
  257. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  258. return
  259. }
  260. claims, _ := c.Get("adminInfo")
  261. adminInfo := claims.(*system.SysAdmin)
  262. // 日期校验
  263. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  264. if e != nil {
  265. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  266. return
  267. }
  268. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  269. if e != nil {
  270. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  271. return
  272. }
  273. signDate, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  274. if e != nil {
  275. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  276. return
  277. }
  278. ob := new(fms.ContractRegister)
  279. item, e := ob.Fetch(req.ContractRegisterId)
  280. if e != nil {
  281. if e == utils.ErrNoRow {
  282. resp.Fail("登记记录不存在或已被删除", c)
  283. return
  284. }
  285. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  286. return
  287. }
  288. // 是否存在相同合同编号的登记
  289. existCond := `contract_code = ?`
  290. existPars := make([]interface{}, 0)
  291. existPars = append(existPars, req.ContractCode)
  292. exist, e := ob.FetchByCondition(existCond, existPars)
  293. if e != nil && e != utils.ErrNoRow {
  294. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  295. return
  296. }
  297. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  298. resp.Fail("合同编号已存在", c)
  299. return
  300. }
  301. nowTime := time.Now().Local()
  302. item.ContractCode = req.ContractCode
  303. item.CrmContractId = req.CrmContractId
  304. item.ContractSource = req.ContractSource
  305. item.CompanyName = req.CompanyName
  306. item.SellerId = req.SellerId
  307. item.SellerName = req.SellerName
  308. item.ContractType = req.ContractType
  309. item.ContractAmount = req.ContractAmount
  310. item.StartDate = startDate
  311. item.EndDate = endDate
  312. item.SignDate = signDate
  313. item.AgreedPayTime = req.AgreedPayTime
  314. item.ContractStatus = req.ContractStatus
  315. item.RegisterStatus = fms.ContractRegisterStatusIng
  316. item.Remark = req.Remark
  317. item.ModifyTime = nowTime
  318. updateCols := []string{
  319. "ContractCode", "CrmContractId", "ContractSource", "CompanyName", "SellerId", "SellerName",
  320. "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime", "ContractStatus",
  321. "RegisterStatus", "Remark", "ModifyTime",
  322. }
  323. // 套餐信息
  324. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services)
  325. if e != nil {
  326. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  327. return
  328. }
  329. // 更新合同及套餐
  330. if e = fms.UpdateContractRegisterAndServices(item, updateCols, serviceList); e != nil {
  331. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  332. return
  333. }
  334. // 校验金额-是否修改状态
  335. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  336. // 操作日志
  337. go func() {
  338. opData := ""
  339. opDataByte, e := json.Marshal(req)
  340. if e != nil {
  341. return
  342. }
  343. opData = string(opDataByte)
  344. logItem := new(fms.ContractRegisterLog)
  345. logItem.ContractRegisterId = item.ContractRegisterId
  346. logItem.AdminId = int(adminInfo.AdminId)
  347. logItem.AdminName = adminInfo.AdminName
  348. logItem.OpData = opData
  349. logItem.OpType = fms.ContractRegisterOpTypeSave
  350. logItem.CreateTime = nowTime
  351. if e = logItem.Create(); e != nil {
  352. return
  353. }
  354. }()
  355. resp.Ok("操作成功", c)
  356. }
  357. // Del
  358. // @Title 删除合同登记
  359. // @Description 删除合同登记
  360. // @Param request body fms.ContractRegisterDelReq true "type json string"
  361. // @Success 200 string "操作成功"
  362. // @router /contract/register/del [post]
  363. func (rg *RegisterController) Del(c *gin.Context) {
  364. req := new(fms.ContractRegisterDelReq)
  365. err := c.ShouldBind(&req)
  366. if err != nil {
  367. errs, ok := err.(validator.ValidationErrors)
  368. if !ok {
  369. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  370. return
  371. }
  372. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  373. return
  374. }
  375. claims, _ := c.Get("adminInfo")
  376. adminInfo := claims.(*system.SysAdmin)
  377. ob := new(fms.ContractRegister)
  378. item, e := ob.Fetch(req.ContractRegisterId)
  379. if e != nil {
  380. if e == utils.ErrNoRow {
  381. resp.Fail("合同登记不存在或已被删除", c)
  382. return
  383. }
  384. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  385. return
  386. }
  387. nowTime := time.Now().Local()
  388. item.IsDeleted = 1
  389. item.ModifyTime = nowTime
  390. updateCols := []string{"IsDeleted", "ModifyTime"}
  391. if e = item.Update(updateCols); e != nil {
  392. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  393. return
  394. }
  395. // 操作日志
  396. go func() {
  397. opData := ""
  398. opDataByte, e := json.Marshal(req)
  399. if e != nil {
  400. return
  401. }
  402. opData = string(opDataByte)
  403. logItem := new(fms.ContractRegisterLog)
  404. logItem.ContractRegisterId = req.ContractRegisterId
  405. logItem.AdminId = int(adminInfo.AdminId)
  406. logItem.AdminName = adminInfo.AdminName
  407. logItem.OpData = opData
  408. logItem.OpType = fms.ContractRegisterOpTypeDel
  409. logItem.CreateTime = nowTime
  410. if e = logItem.Create(); e != nil {
  411. return
  412. }
  413. }()
  414. resp.Ok("操作成功", c)
  415. }
  416. // Detail
  417. // @Title 合同登记详情
  418. // @Description 合同登记详情
  419. // @Param ContractRegisterId query int false "合同登记ID"
  420. // @Success 200 {object} fms.ContractRegisterDetail
  421. // @router /contract/register/detail [get]
  422. func (rg *RegisterController) Detail(c *gin.Context) {
  423. var req fms.ContractRegisterDetailReq
  424. if e := c.BindQuery(&req); e != nil {
  425. err, ok := e.(validator.ValidationErrors)
  426. if !ok {
  427. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  428. return
  429. }
  430. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  431. return
  432. }
  433. result := new(fms.ContractRegisterDetail)
  434. // 合同登记信息
  435. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  436. if e != nil {
  437. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  438. return
  439. }
  440. result.ContractRegisterItem = item
  441. // 套餐信息
  442. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  443. if e != nil {
  444. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  445. return
  446. }
  447. result.ServiceList = serviceList
  448. // 开票/到款信息
  449. invoiceCond := `contract_register_id = ?`
  450. invoicePars := make([]interface{}, 0)
  451. invoicePars = append(invoicePars, req.ContractRegisterId)
  452. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  453. if e != nil {
  454. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  455. return
  456. }
  457. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  458. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  459. for i := range invoiceList {
  460. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  461. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  462. continue
  463. }
  464. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  465. result.PaymentList = append(result.PaymentList, invoiceList[i])
  466. }
  467. }
  468. // 合同登记进度
  469. logCond := `contract_register_id = ?`
  470. logPars := make([]interface{}, 0)
  471. logPars = append(logPars, req.ContractRegisterId)
  472. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  473. if e != nil {
  474. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  475. return
  476. }
  477. result.Logs = logList
  478. resp.OkData("获取成功", result, c)
  479. }
  480. // UpdateStatus
  481. // @Title 修改合同状态
  482. // @Description 修改合同状态
  483. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  484. // @Success 200 string "操作成功"
  485. // @router /contract/register/update_status [post]
  486. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  487. req := new(fms.ContractRegisterUpdateStatusReq)
  488. err := c.ShouldBind(&req)
  489. if err != nil {
  490. errs, ok := err.(validator.ValidationErrors)
  491. if !ok {
  492. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  493. return
  494. }
  495. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  496. return
  497. }
  498. claims, _ := c.Get("adminInfo")
  499. adminInfo := claims.(*system.SysAdmin)
  500. ob := new(fms.ContractRegister)
  501. item, e := ob.Fetch(req.ContractRegisterId)
  502. if e != nil {
  503. if e == utils.ErrNoRow {
  504. resp.Fail("合同登记不存在或已被删除", c)
  505. return
  506. }
  507. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  508. return
  509. }
  510. nowTime := time.Now().Local()
  511. item.ContractStatus = req.ContractStatus
  512. item.ModifyTime = nowTime
  513. updateCols := []string{"ContractStatus", "ModifyTime"}
  514. if e = item.Update(updateCols); e != nil {
  515. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  516. return
  517. }
  518. // 校验金额-是否修改状态
  519. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  520. // 操作日志
  521. go func() {
  522. opData := ""
  523. opDataByte, e := json.Marshal(req)
  524. if e != nil {
  525. return
  526. }
  527. opData = string(opDataByte)
  528. logItem := new(fms.ContractRegisterLog)
  529. logItem.ContractRegisterId = req.ContractRegisterId
  530. logItem.AdminId = int(adminInfo.AdminId)
  531. logItem.AdminName = adminInfo.AdminName
  532. logItem.OpData = opData
  533. logItem.OpType = fms.ContractRegisterOpTypeStatus
  534. logItem.CreateTime = nowTime
  535. if e = logItem.Create(); e != nil {
  536. return
  537. }
  538. }()
  539. resp.Ok("操作成功", c)
  540. }
  541. // Invoice
  542. // @Title 开票/到款登记
  543. // @Description 开票/到款登记
  544. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  545. // @Success 200 string "操作成功"
  546. // @router /contract/register/invoice [post]
  547. func (rg *RegisterController) Invoice(c *gin.Context) {
  548. req := new(fms.ContractInvoiceSaveReq)
  549. err := c.ShouldBind(&req)
  550. if err != nil {
  551. errs, ok := err.(validator.ValidationErrors)
  552. if !ok {
  553. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  554. return
  555. }
  556. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  557. return
  558. }
  559. claims, _ := c.Get("adminInfo")
  560. adminInfo := claims.(*system.SysAdmin)
  561. newInvoice := make([]*fms.ContractInvoice, 0)
  562. if len(req.AmountList) > 0 {
  563. for i := range req.AmountList {
  564. if req.AmountList[i].Amount <= 0 {
  565. resp.Fail("登记金额有误", c)
  566. return
  567. }
  568. if req.AmountList[i].InvoiceDate == "" {
  569. resp.Fail("请选择日期", c)
  570. return
  571. }
  572. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  573. if e != nil {
  574. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  575. return
  576. }
  577. v := &fms.ContractInvoice{
  578. ContractRegisterId: req.ContractRegisterId,
  579. Amount: req.AmountList[i].Amount,
  580. InvoiceType: req.InvoiceType,
  581. InvoiceDate: t,
  582. AdminId: int(adminInfo.AdminId),
  583. AdminName: adminInfo.AdminName,
  584. }
  585. v.Set()
  586. newInvoice = append(newInvoice, v)
  587. }
  588. }
  589. // 删除并新增登记
  590. ob := new(fms.ContractInvoice)
  591. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, newInvoice); e != nil {
  592. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  593. return
  594. }
  595. // 校验金额-是否修改状态
  596. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  597. // 操作日志
  598. go func() {
  599. opData := ""
  600. opDataByte, e := json.Marshal(req)
  601. if e != nil {
  602. return
  603. }
  604. opData = string(opDataByte)
  605. opType := fms.ContractRegisterOpTypeInvoice
  606. if req.InvoiceType == fms.ContractInvoiceTypePay {
  607. opType = fms.ContractRegisterOpTypePayment
  608. }
  609. logItem := new(fms.ContractRegisterLog)
  610. logItem.ContractRegisterId = req.ContractRegisterId
  611. logItem.AdminId = int(adminInfo.AdminId)
  612. logItem.AdminName = adminInfo.AdminName
  613. logItem.OpData = opData
  614. logItem.OpType = opType
  615. logItem.CreateTime = time.Now().Local()
  616. if e = logItem.Create(); e != nil {
  617. return
  618. }
  619. }()
  620. resp.Ok("操作成功", c)
  621. }
  622. // 导出
  623. func (rg *RegisterController) Export(c *gin.Context) {
  624. // 获取小套餐品种
  625. cpCond := `product_id = ? AND permission_name <> ?`
  626. cpPars := make([]interface{}, 0)
  627. cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
  628. cp := new(crm.ChartPermission)
  629. permissionList, e := cp.List(cpCond, cpPars)
  630. if e != nil {
  631. resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
  632. return
  633. }
  634. permissionLen := len(permissionList)
  635. permissionNameIdMap := make(map[string]int)
  636. for i := range permissionList {
  637. permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  638. }
  639. // 获取套餐服务
  640. //serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
  641. //if e != nil {
  642. // resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
  643. // return
  644. //}
  645. // 获取列表数据
  646. cond := ``
  647. pars := make([]interface{}, 0)
  648. cr := new(fms.ContractRegister)
  649. list, e := cr.List(cond, pars)
  650. if e != nil {
  651. resp.FailData("获取合同列表失败", "Err:"+e.Error(), c)
  652. return
  653. }
  654. registerIds := make([]int, 0)
  655. for i := range list {
  656. registerIds = append(registerIds, list[i].ContractRegisterId)
  657. }
  658. // 套餐/开票/到款列表
  659. serviceMap := make(map[int][]*fms.ContractService)
  660. serviceChartPermissionsMap := make(map[int][]int)
  661. invoiceMap := make(map[int][]*fms.ContractInvoice)
  662. paymentMap := make(map[int][]*fms.ContractInvoice)
  663. maxInvoice := 0
  664. maxPayment := 0
  665. if len(registerIds) > 0 {
  666. // 获取套餐信息
  667. csCond := `contract_register_id IN ?`
  668. csPars := make([]interface{}, 0)
  669. csPars = append(csPars, registerIds)
  670. cs := new(fms.ContractService)
  671. serviceList, e := cs.List(csCond, csPars)
  672. if e != nil {
  673. resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
  674. return
  675. }
  676. for i := range serviceList {
  677. cid := serviceList[i].ContractRegisterId
  678. if serviceMap[cid] == nil {
  679. serviceMap[cid] = make([]*fms.ContractService, 0)
  680. }
  681. serviceMap[cid] = append(serviceMap[cid], serviceList[i])
  682. // 小套餐权限
  683. if serviceChartPermissionsMap[cid] == nil {
  684. serviceChartPermissionsMap[cid] = make([]int, 0)
  685. }
  686. if serviceList[i].ChartPermissionIds != "" {
  687. ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
  688. serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
  689. }
  690. }
  691. // 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
  692. ci := new(fms.ContractInvoice)
  693. invoiceList, e := ci.List(csCond, csPars)
  694. if e != nil {
  695. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  696. return
  697. }
  698. for k := range invoiceList {
  699. cid := invoiceList[k].ContractRegisterId
  700. if invoiceMap[cid] == nil {
  701. invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
  702. }
  703. if paymentMap[cid] == nil {
  704. paymentMap[cid] = make([]*fms.ContractInvoice, 0)
  705. }
  706. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
  707. invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
  708. continue
  709. }
  710. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
  711. paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
  712. continue
  713. }
  714. }
  715. // 取最大开票/到款数
  716. for j := range invoiceMap {
  717. if len(invoiceMap[j]) > maxInvoice {
  718. maxInvoice = len(invoiceMap[j])
  719. }
  720. }
  721. for p := range paymentMap {
  722. if len(paymentMap[p]) > maxPayment {
  723. maxPayment = len(paymentMap[p])
  724. }
  725. }
  726. }
  727. // 生成Excel文件
  728. //dir, err := os.Executable()
  729. //exPath := filepath.Dir(dir)
  730. //filePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  731. xlsxFile := xlsx.NewFile()
  732. //if err != nil {
  733. // resp.FailData("生成文件失败", "Err:"+err.Error(), c)
  734. // return
  735. //}
  736. style := xlsx.NewStyle()
  737. alignment := xlsx.Alignment{
  738. Horizontal: "center",
  739. Vertical: "center",
  740. WrapText: true,
  741. }
  742. style.Alignment = alignment
  743. style.ApplyAlignment = true
  744. sheet, err := xlsxFile.AddSheet("合同登记")
  745. if err != nil {
  746. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  747. return
  748. }
  749. //_ = sheet.SetColWidth(0, 0, 60)
  750. //_ = sheet.SetColWidth(4, 10, 60)
  751. // 1行表头
  752. titleRow := sheet.AddRow()
  753. titleRow.SetHeight(40)
  754. // 1行1列-右合并两格
  755. cell1 := titleRow.AddCell()
  756. cell1.HMerge = 2
  757. cell1.SetValue("FICC客户签约表 2022")
  758. cell1.SetStyle(style)
  759. // 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
  760. titleRow.AddCell().SetValue("")
  761. titleRow.AddCell().SetValue("")
  762. // 1行2列
  763. cell2 := titleRow.AddCell()
  764. cell2.SetValue("商品大套餐")
  765. cell2.SetStyle(style)
  766. // 1行3列-右合并小套餐数
  767. if permissionLen >= 1 {
  768. cell3 := titleRow.AddCell()
  769. cell3.HMerge = permissionLen - 1
  770. cell3.SetValue("商品小套餐")
  771. cell3.SetStyle(style)
  772. // 同上右增单元格小套餐数-1的空白单元格用于合并
  773. for i := 0; i < permissionLen-1; i++ {
  774. titleRow.AddCell().SetValue("")
  775. }
  776. }
  777. cell4 := titleRow.AddCell()
  778. cell4.SetValue("市场策略")
  779. cell4.SetStyle(style)
  780. cell5 := titleRow.AddCell()
  781. cell5.SetValue("财富管理")
  782. cell5.SetStyle(style)
  783. // 第二行表头
  784. titleRow2 := sheet.AddRow()
  785. titleRow2.SetHeight(60)
  786. row2Title := make([]string, 0)
  787. row2Title = append(row2Title, "客户名称", "续约-0\n新增-1", "销售", "商品大套餐")
  788. for i := range permissionList {
  789. row2Title = append(row2Title, permissionList[i].PermissionName)
  790. }
  791. row2Title = append(row2Title, "市场策略", "财富管理", "开始时间", "到期时间", "2022年合同金额", "约定付款时间", "签订日",
  792. "签订月", "合同状态", "合同编号", "备注")
  793. for i := range row2Title {
  794. v := titleRow2.AddCell()
  795. v.SetValue(row2Title[i])
  796. v.SetStyle(style)
  797. }
  798. // 第二行表头-开票/收款(动态添加)
  799. for i := 0; i < maxInvoice; i++ {
  800. n := i + 1
  801. c1 := titleRow2.AddCell()
  802. t1 := fmt.Sprintf("%s%d", "开票日", n)
  803. c1.SetValue(t1)
  804. c1.SetStyle(style)
  805. c2 := titleRow2.AddCell()
  806. t2 := fmt.Sprintf("%s%d", "开票金额", n)
  807. c2.SetValue(t2)
  808. c2.SetStyle(style)
  809. row2Title = append(row2Title, t1, t2)
  810. }
  811. for i := 0; i < maxPayment; i++ {
  812. n := i + 1
  813. c1 := titleRow2.AddCell()
  814. t1 := fmt.Sprintf("%s%d", "收款月", n)
  815. c1.SetValue(t1)
  816. c1.SetStyle(style)
  817. c2 := titleRow2.AddCell()
  818. t2 := fmt.Sprintf("%s%d", "收款金额", n)
  819. c2.SetValue(t2)
  820. c2.SetStyle(style)
  821. row2Title = append(row2Title, t1, t2)
  822. }
  823. // 此处取第二行标题NameKeyMap, 后面的动态匹配
  824. row2NameKeyMap := make(map[string]int)
  825. for i := range row2Title {
  826. row2NameKeyMap[row2Title[i]] = i
  827. }
  828. contractTMap := map[int]int{
  829. fms.ContractTypeNew: 1,
  830. fms.ContractTypeRenew: 0,
  831. }
  832. for _, v := range list {
  833. k := -1
  834. dataRow := sheet.AddRow()
  835. dataRow.SetHeight(20)
  836. k += 3
  837. dataRow.AddCell().SetString(v.CompanyName)
  838. dataRow.AddCell().SetString(fmt.Sprint(contractTMap[v.ContractType]))
  839. dataRow.AddCell().SetString(v.SellerName)
  840. // 大套餐
  841. k += 1
  842. col4Name := row2Title[k]
  843. svList := serviceMap[v.ContractRegisterId]
  844. col4 := ""
  845. if svList != nil && len(svList) > 0 {
  846. for isv := range svList {
  847. if svList[isv].Title == col4Name {
  848. col4 = "是"
  849. break
  850. }
  851. }
  852. }
  853. dataRow.AddCell().SetString(col4)
  854. // 小套餐
  855. serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
  856. for i := 0; i < permissionLen; i++ {
  857. k += 1
  858. colName := row2Title[k]
  859. chartPermissionId := permissionNameIdMap[colName]
  860. if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
  861. dataRow.AddCell().SetString("是")
  862. } else {
  863. dataRow.AddCell().SetString("")
  864. }
  865. }
  866. // 财富管理/市场策略(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
  867. k += 1
  868. col5Name := row2Title[k]
  869. col5 := ""
  870. if svList != nil && len(svList) > 0 {
  871. for isv := range svList {
  872. if svList[isv].Title == col5Name {
  873. col5 = "是"
  874. break
  875. }
  876. }
  877. }
  878. dataRow.AddCell().SetString(col5)
  879. k += 1
  880. col6Name := row2Title[k]
  881. col6 := ""
  882. if svList != nil && len(svList) > 0 {
  883. for isv := range svList {
  884. if svList[isv].Title == col6Name {
  885. col6 = "是"
  886. break
  887. }
  888. }
  889. }
  890. dataRow.AddCell().SetString(col6)
  891. // 其他信息
  892. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
  893. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate)) // 到期时间
  894. dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount)) // 2022年合同金额
  895. dataRow.AddCell().SetString(v.AgreedPayTime) // 约定付款时间
  896. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate)) // 签订日
  897. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01", v.SignDate)) // 签订月
  898. dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus]) // 合同状态
  899. dataRow.AddCell().SetString(v.ContractCode) // 合同编号
  900. dataRow.AddCell().SetString(v.Remark) // 备注
  901. // 开票/到款信息
  902. ivList := invoiceMap[v.ContractRegisterId]
  903. if ivList != nil && len(ivList) > 0 {
  904. for ia := range ivList {
  905. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", ivList[ia].InvoiceDate)) // 开票日
  906. dataRow.AddCell().SetString(fmt.Sprint(ivList[ia].Amount)) // 开票金额
  907. }
  908. }
  909. pyList := paymentMap[v.ContractRegisterId]
  910. if pyList != nil && len(pyList) > 0 {
  911. for ib := range pyList {
  912. dataRow.AddCell().SetString(utils.TimeTransferString("2006年01月", pyList[ib].InvoiceDate)) // 收款月
  913. dataRow.AddCell().SetString(fmt.Sprint(pyList[ib].Amount)) // 收款金额
  914. }
  915. }
  916. }
  917. // 输出文件
  918. var buffer bytes.Buffer
  919. _ = xlsxFile.Write(&buffer)
  920. content := bytes.NewReader(buffer.Bytes())
  921. //err = xlsxFile.Save(filePath)
  922. //if err != nil {
  923. // resp.FailData("保存文件失败", "Err:"+err.Error(), c)
  924. // return
  925. //}
  926. //defer func() {
  927. // _ = os.Remove(filePath)
  928. //}()
  929. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  930. fileName := "财务列表_" + randStr + ".xlsx"
  931. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  932. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  933. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  934. }