register.go 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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/shopspring/decimal"
  9. "github.com/tealeg/xlsx"
  10. "hongze/fms_api/controller/resp"
  11. "hongze/fms_api/global"
  12. "hongze/fms_api/models/base"
  13. "hongze/fms_api/models/crm"
  14. "hongze/fms_api/models/fms"
  15. "hongze/fms_api/models/system"
  16. "hongze/fms_api/services/alarm_msg"
  17. fmsService "hongze/fms_api/services/fms"
  18. "hongze/fms_api/utils"
  19. "net/http"
  20. "os"
  21. "path"
  22. "strconv"
  23. "strings"
  24. "time"
  25. )
  26. // RegisterController 合同登记
  27. type RegisterController struct{}
  28. // List
  29. // @Title 合同登记列表
  30. // @Description 合同登记列表
  31. // @Param Keyword query string false "关键词"
  32. // @Param StartDate query string false "合同开始日期"
  33. // @Param EndDate query string false "合同结束日期"
  34. // @Param ServiceType query int false "套餐类型"
  35. // @Param ContractType query int false "合同类型"
  36. // @Param RegisterStatus query int false "登记状态"
  37. // @Success 200 {object} fms.ContractRegisterItem
  38. // @router /contract/register/list [get]
  39. func (rg *RegisterController) List(c *gin.Context) {
  40. var req fms.ContractRegisterListReq
  41. if e := c.BindQuery(&req); e != nil {
  42. err, ok := e.(validator.ValidationErrors)
  43. if !ok {
  44. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  45. return
  46. }
  47. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  48. return
  49. }
  50. cond := `1 = 1`
  51. pars := make([]interface{}, 0)
  52. // 合同编号/客户姓名/销售/实际使用方
  53. if req.Keyword != "" {
  54. kw := "%" + req.Keyword + "%"
  55. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
  56. pars = append(pars, kw, kw, kw, kw)
  57. }
  58. if req.StartDate != "" && req.EndDate != "" {
  59. st := fmt.Sprint(req.StartDate, " 00:00:00")
  60. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  61. cond += ` AND (create_time BETWEEN ? AND ?)`
  62. pars = append(pars, st, ed)
  63. }
  64. if req.ContractType != 0 {
  65. cond += ` AND contract_type = ?`
  66. pars = append(pars, req.ContractType)
  67. }
  68. if req.RegisterStatus != 0 {
  69. cond += ` AND register_status = ?`
  70. pars = append(pars, req.RegisterStatus)
  71. }
  72. // 套餐筛选
  73. if req.ServiceType != 0 {
  74. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  75. if e != nil {
  76. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  77. return
  78. }
  79. if len(registerIds) > 0 {
  80. cond += ` AND contract_register_id IN ?`
  81. pars = append(pars, registerIds)
  82. } else {
  83. cond += ` AND 1 = 2`
  84. }
  85. }
  86. page := new(base.Page)
  87. page.SetPageSize(req.PageSize)
  88. page.SetCurrent(req.Current)
  89. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  90. total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
  91. if e != nil {
  92. resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
  93. return
  94. }
  95. registerIds := make([]int, 0)
  96. for i := range list {
  97. registerIds = append(registerIds, list[i].ContractRegisterId)
  98. }
  99. serviceMap := make(map[int]string, 0)
  100. invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  101. paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  102. if len(registerIds) > 0 {
  103. // 获取服务套餐
  104. servicesNameList, e := fms.GetContractRegisterServicesNameByRegisterIds(registerIds)
  105. if e != nil {
  106. resp.FailMsg("获取失败", "获取套餐拼接字符串失败, Err: "+e.Error(), c)
  107. return
  108. }
  109. for i := range servicesNameList {
  110. serviceMap[servicesNameList[i].ContractRegisterId] = servicesNameList[i].ServicesName
  111. }
  112. // 获取开票/到款列表
  113. invoiceCond := `contract_register_id IN ?`
  114. invoicePars := make([]interface{}, 0)
  115. invoicePars = append(invoicePars, registerIds)
  116. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  117. if e != nil {
  118. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  119. return
  120. }
  121. for i := range invoiceList {
  122. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  123. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  124. }
  125. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  126. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  127. }
  128. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  129. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  130. }
  131. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  132. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  133. }
  134. }
  135. }
  136. respList := make([]*fms.ContractRegisterList, 0)
  137. for i := range list {
  138. v := new(fms.ContractRegisterList)
  139. v.ContractRegisterItem = list[i]
  140. v.ServicesName = serviceMap[list[i].ContractRegisterId]
  141. v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
  142. v.PaymentList = paymentMap[list[i].ContractRegisterId]
  143. respList = append(respList, v)
  144. }
  145. page.SetTotal(total)
  146. baseData := new(base.BaseData)
  147. baseData.SetPage(page)
  148. baseData.SetList(respList)
  149. resp.OkData("获取成功", baseData, c)
  150. }
  151. // Add
  152. // @Title 新增合同登记
  153. // @Description 新增合同登记
  154. // @Param request body fms.ContractRegisterAddReq true "type json string"
  155. // @Success 200 string "操作成功"
  156. // @router /contract/register/add [post]
  157. func (rg *RegisterController) Add(c *gin.Context) {
  158. req := new(fms.ContractRegisterAddReq)
  159. err := c.ShouldBind(&req)
  160. if err != nil {
  161. errs, ok := err.(validator.ValidationErrors)
  162. if !ok {
  163. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  164. return
  165. }
  166. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  167. return
  168. }
  169. claims, _ := c.Get("adminInfo")
  170. adminInfo := claims.(*system.SysAdmin)
  171. // 日期校验
  172. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  173. if e != nil {
  174. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  175. return
  176. }
  177. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  178. if e != nil {
  179. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  180. return
  181. }
  182. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  183. if req.SignDate != "" {
  184. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  185. if e != nil {
  186. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  187. return
  188. }
  189. signDate = signDateTime
  190. }
  191. // 是否存在相同合同编号的登记
  192. ob := new(fms.ContractRegister)
  193. existCond := `contract_code = ?`
  194. existPars := make([]interface{}, 0)
  195. existPars = append(existPars, req.ContractCode)
  196. exist, e := ob.FetchByCondition(existCond, existPars)
  197. if e != nil && e != utils.ErrNoRow {
  198. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  199. return
  200. }
  201. if exist != nil && exist.ContractRegisterId > 0 {
  202. resp.Fail("合同编号已存在", c)
  203. return
  204. }
  205. nowTime := time.Now().Local()
  206. ob.ContractCode = req.ContractCode
  207. ob.RelateContractCode = req.RelateContractCode
  208. ob.CrmContractId = req.CrmContractId
  209. ob.ContractSource = req.ContractSource
  210. ob.CompanyName = req.CompanyName
  211. ob.ActualCompanyName = req.ActualCompanyName
  212. ob.ProductId = req.ProductId
  213. ob.SellerId = req.SellerId
  214. ob.SellerName = req.SellerName
  215. ob.ContractType = req.ContractType
  216. ob.ContractAmount = req.ContractAmount
  217. ob.StartDate = startDate
  218. ob.EndDate = endDate
  219. ob.SignDate = signDate
  220. ob.AgreedPayTime = req.AgreedPayTime
  221. ob.ContractStatus = req.ContractStatus
  222. ob.RegisterStatus = fms.ContractRegisterStatusIng
  223. ob.Remark = req.Remark
  224. ob.ServiceRemark = req.ServiceRemark
  225. ob.HasPayment = req.HasPayment
  226. ob.Set()
  227. // 存在代付的直接完成登记, 且不允许进行开票/到款登记
  228. if req.HasPayment == 1 {
  229. ob.RegisterStatus = fms.ContractRegisterStatusComplete
  230. }
  231. // 套餐信息
  232. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  233. if e != nil {
  234. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  235. return
  236. }
  237. // 新增合同及套餐
  238. if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
  239. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  240. return
  241. }
  242. // 操作日志
  243. go func() {
  244. opData := ""
  245. opDataByte, e := json.Marshal(req)
  246. if e != nil {
  247. return
  248. }
  249. opData = string(opDataByte)
  250. logItem := new(fms.ContractRegisterLog)
  251. logItem.ContractRegisterId = ob.ContractRegisterId
  252. logItem.AdminId = int(adminInfo.AdminId)
  253. logItem.AdminName = adminInfo.RealName
  254. logItem.OpData = opData
  255. logItem.OpType = fms.ContractRegisterOpTypeSave
  256. logItem.CreateTime = nowTime
  257. logItem.Remark = req.Remark
  258. if e = logItem.Create(); e != nil {
  259. return
  260. }
  261. }()
  262. resp.Ok("操作成功", c)
  263. }
  264. // Edit
  265. // @Title 编辑合同登记
  266. // @Description 编辑合同登记
  267. // @Param request body fms.ContractRegisterEditReq true "type json string"
  268. // @Success 200 string "操作成功"
  269. // @router /contract/register/edit [post]
  270. func (rg *RegisterController) Edit(c *gin.Context) {
  271. req := new(fms.ContractRegisterEditReq)
  272. err := c.ShouldBind(&req)
  273. if err != nil {
  274. errs, ok := err.(validator.ValidationErrors)
  275. if !ok {
  276. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  277. return
  278. }
  279. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  280. return
  281. }
  282. claims, _ := c.Get("adminInfo")
  283. adminInfo := claims.(*system.SysAdmin)
  284. // 日期校验
  285. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  286. if e != nil {
  287. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  288. return
  289. }
  290. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  291. if e != nil {
  292. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  293. return
  294. }
  295. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  296. if req.SignDate != "" {
  297. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  298. if e != nil {
  299. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  300. return
  301. }
  302. signDate = signDateTime
  303. }
  304. ob := new(fms.ContractRegister)
  305. item, e := ob.Fetch(req.ContractRegisterId)
  306. if e != nil {
  307. if e == utils.ErrNoRow {
  308. resp.Fail("登记记录不存在或已被删除", c)
  309. return
  310. }
  311. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  312. return
  313. }
  314. // 是否存在相同合同编号的登记
  315. existCond := `contract_code = ?`
  316. existPars := make([]interface{}, 0)
  317. existPars = append(existPars, req.ContractCode)
  318. exist, e := ob.FetchByCondition(existCond, existPars)
  319. if e != nil && e != utils.ErrNoRow {
  320. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  321. return
  322. }
  323. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  324. resp.Fail("合同编号已存在", c)
  325. return
  326. }
  327. originHasPayment := item.HasPayment
  328. updateCols := []string{
  329. "ContractCode", "RelateContractCode", "CrmContractId", "ContractSource", "CompanyName", "ActualCompanyName",
  330. "SellerId", "SellerName", "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime",
  331. "ContractStatus", "RegisterStatus", "Remark", "ServiceRemark", "HasPayment", "ModifyTime",
  332. }
  333. nowTime := time.Now().Local()
  334. item.ContractCode = req.ContractCode
  335. item.RelateContractCode = req.RelateContractCode
  336. item.CrmContractId = req.CrmContractId
  337. item.ContractSource = req.ContractSource
  338. item.CompanyName = req.CompanyName
  339. item.ActualCompanyName = req.ActualCompanyName
  340. item.SellerId = req.SellerId
  341. item.SellerName = req.SellerName
  342. item.ContractType = req.ContractType
  343. item.ContractAmount = req.ContractAmount
  344. item.StartDate = startDate
  345. item.EndDate = endDate
  346. item.SignDate = signDate
  347. item.AgreedPayTime = req.AgreedPayTime
  348. item.ContractStatus = req.ContractStatus
  349. item.RegisterStatus = fms.ContractRegisterStatusIng
  350. item.Remark = req.Remark
  351. item.ServiceRemark = req.ServiceRemark
  352. item.HasPayment = req.HasPayment
  353. item.ModifyTime = nowTime
  354. // 存在代付的直接完成登记, 且不允许进行开票/到款登记
  355. if req.HasPayment == 1 {
  356. item.RegisterStatus = fms.ContractRegisterStatusComplete
  357. updateCols = append(updateCols, "RegisterStatus")
  358. }
  359. // 套餐信息
  360. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  361. if e != nil {
  362. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  363. return
  364. }
  365. // 更新合同及套餐
  366. if e = fms.UpdateContractRegisterAndServices(item, updateCols, serviceList); e != nil {
  367. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  368. return
  369. }
  370. // 若从无代付修改为有代付, 则删除无代付期间新增的所有开票/到款登记(此情况并不经常出现, 但是可能会存在这种操作)
  371. if originHasPayment == 0 && req.HasPayment == 1 {
  372. go func() {
  373. if e = fms.DeleteContractInvoicesByRegisterId(item.ContractRegisterId); e != nil {
  374. alarm_msg.SendAlarmMsg("无代付修改为有代付, 删除开票到款记录失败, ErrMsg: "+err.Error(), 3)
  375. }
  376. }()
  377. }
  378. // 校验金额-是否修改状态
  379. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  380. // 操作日志
  381. go func() {
  382. opData := ""
  383. opDataByte, e := json.Marshal(req)
  384. if e != nil {
  385. return
  386. }
  387. opData = string(opDataByte)
  388. logItem := new(fms.ContractRegisterLog)
  389. logItem.ContractRegisterId = item.ContractRegisterId
  390. logItem.AdminId = int(adminInfo.AdminId)
  391. logItem.AdminName = adminInfo.RealName
  392. logItem.OpData = opData
  393. logItem.OpType = fms.ContractRegisterOpTypeEdit
  394. logItem.CreateTime = nowTime
  395. logItem.Remark = req.Remark
  396. if e = logItem.Create(); e != nil {
  397. return
  398. }
  399. }()
  400. resp.Ok("操作成功", c)
  401. }
  402. // Del
  403. // @Title 删除合同登记
  404. // @Description 删除合同登记
  405. // @Param request body fms.ContractRegisterDelReq true "type json string"
  406. // @Success 200 string "操作成功"
  407. // @router /contract/register/del [post]
  408. func (rg *RegisterController) Del(c *gin.Context) {
  409. req := new(fms.ContractRegisterDelReq)
  410. err := c.ShouldBind(&req)
  411. if err != nil {
  412. errs, ok := err.(validator.ValidationErrors)
  413. if !ok {
  414. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  415. return
  416. }
  417. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  418. return
  419. }
  420. claims, _ := c.Get("adminInfo")
  421. adminInfo := claims.(*system.SysAdmin)
  422. ob := new(fms.ContractRegister)
  423. item, e := ob.Fetch(req.ContractRegisterId)
  424. if e != nil {
  425. if e == utils.ErrNoRow {
  426. resp.Fail("合同登记不存在或已被删除", c)
  427. return
  428. }
  429. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  430. return
  431. }
  432. nowTime := time.Now().Local()
  433. item.IsDeleted = 1
  434. item.ModifyTime = nowTime
  435. updateCols := []string{"IsDeleted", "ModifyTime"}
  436. if e = item.Update(updateCols); e != nil {
  437. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  438. return
  439. }
  440. // 删除对应的开票到款登记
  441. go func() {
  442. if e = fms.DeleteContractInvoicesByRegisterId(item.ContractRegisterId); e != nil {
  443. alarm_msg.SendAlarmMsg("删除合同登记后, 删除开票到款记录失败, ErrMsg: "+err.Error(), 3)
  444. }
  445. }()
  446. // 操作日志
  447. go func() {
  448. opData := ""
  449. opDataByte, e := json.Marshal(req)
  450. if e != nil {
  451. return
  452. }
  453. opData = string(opDataByte)
  454. logItem := new(fms.ContractRegisterLog)
  455. logItem.ContractRegisterId = req.ContractRegisterId
  456. logItem.AdminId = int(adminInfo.AdminId)
  457. logItem.AdminName = adminInfo.RealName
  458. logItem.OpData = opData
  459. logItem.OpType = fms.ContractRegisterOpTypeDel
  460. logItem.CreateTime = nowTime
  461. if e = logItem.Create(); e != nil {
  462. return
  463. }
  464. }()
  465. resp.Ok("操作成功", c)
  466. }
  467. // Detail
  468. // @Title 合同登记详情
  469. // @Description 合同登记详情
  470. // @Param ContractRegisterId query int false "合同登记ID"
  471. // @Success 200 {object} fms.ContractRegisterDetail
  472. // @router /contract/register/detail [get]
  473. func (rg *RegisterController) Detail(c *gin.Context) {
  474. var req fms.ContractRegisterDetailReq
  475. if e := c.BindQuery(&req); e != nil {
  476. err, ok := e.(validator.ValidationErrors)
  477. if !ok {
  478. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  479. return
  480. }
  481. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  482. return
  483. }
  484. result := new(fms.ContractRegisterDetail)
  485. // 合同登记信息
  486. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  487. if e != nil {
  488. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  489. return
  490. }
  491. result.ContractRegisterItem = item
  492. // 套餐信息
  493. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  494. if e != nil {
  495. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  496. return
  497. }
  498. result.ServiceList = serviceList
  499. // 开票/到款信息
  500. invoiceCond := `contract_register_id = ?`
  501. invoicePars := make([]interface{}, 0)
  502. invoicePars = append(invoicePars, req.ContractRegisterId)
  503. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  504. if e != nil {
  505. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  506. return
  507. }
  508. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  509. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  510. for i := range invoiceList {
  511. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  512. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  513. continue
  514. }
  515. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  516. result.PaymentList = append(result.PaymentList, invoiceList[i])
  517. }
  518. }
  519. // 合同登记进度
  520. logCond := `contract_register_id = ?`
  521. logPars := make([]interface{}, 0)
  522. logPars = append(logPars, req.ContractRegisterId)
  523. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  524. if e != nil {
  525. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  526. return
  527. }
  528. result.Logs = logList
  529. resp.OkData("获取成功", result, c)
  530. }
  531. // UpdateStatus
  532. // @Title 修改合同状态
  533. // @Description 修改合同状态
  534. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  535. // @Success 200 string "操作成功"
  536. // @router /contract/register/update_status [post]
  537. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  538. req := new(fms.ContractRegisterUpdateStatusReq)
  539. err := c.ShouldBind(&req)
  540. if err != nil {
  541. errs, ok := err.(validator.ValidationErrors)
  542. if !ok {
  543. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  544. return
  545. }
  546. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  547. return
  548. }
  549. claims, _ := c.Get("adminInfo")
  550. adminInfo := claims.(*system.SysAdmin)
  551. ob := new(fms.ContractRegister)
  552. item, e := ob.Fetch(req.ContractRegisterId)
  553. if e != nil {
  554. if e == utils.ErrNoRow {
  555. resp.Fail("合同登记不存在或已被删除", c)
  556. return
  557. }
  558. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  559. return
  560. }
  561. nowTime := time.Now().Local()
  562. item.ContractStatus = req.ContractStatus
  563. item.ModifyTime = nowTime
  564. updateCols := []string{"ContractStatus", "ModifyTime"}
  565. if e = item.Update(updateCols); e != nil {
  566. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  567. return
  568. }
  569. // 校验金额-是否修改状态
  570. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  571. // 操作日志
  572. go func() {
  573. opData := ""
  574. opDataByte, e := json.Marshal(req)
  575. if e != nil {
  576. return
  577. }
  578. opData = string(opDataByte)
  579. logItem := new(fms.ContractRegisterLog)
  580. logItem.ContractRegisterId = req.ContractRegisterId
  581. logItem.AdminId = int(adminInfo.AdminId)
  582. logItem.AdminName = adminInfo.RealName
  583. logItem.OpData = opData
  584. logItem.OpType = fms.ContractRegisterOpTypeStatus
  585. logItem.CreateTime = nowTime
  586. if e = logItem.Create(); e != nil {
  587. return
  588. }
  589. }()
  590. resp.Ok("操作成功", c)
  591. }
  592. // Invoice
  593. // @Title 开票/到款登记
  594. // @Description 开票/到款登记
  595. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  596. // @Success 200 string "操作成功"
  597. // @router /contract/register/invoice [post]
  598. func (rg *RegisterController) Invoice(c *gin.Context) {
  599. req := new(fms.ContractInvoiceSaveReq)
  600. err := c.ShouldBind(&req)
  601. if err != nil {
  602. errs, ok := err.(validator.ValidationErrors)
  603. if !ok {
  604. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  605. return
  606. }
  607. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  608. return
  609. }
  610. claims, _ := c.Get("adminInfo")
  611. adminInfo := claims.(*system.SysAdmin)
  612. registerOB := new(fms.ContractRegister)
  613. item, e := registerOB.Fetch(req.ContractRegisterId)
  614. if e != nil {
  615. if e == utils.ErrNoRow {
  616. resp.Fail("合同登记不存在或已被删除", c)
  617. return
  618. }
  619. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  620. return
  621. }
  622. // 合同存在代付不允许开票/到款登记
  623. if item.HasPayment == 1 {
  624. resp.Fail("合同存在代付不允许添加开票/到款登记", c)
  625. return
  626. }
  627. noChangeInvoiceIds := make([]int, 0)
  628. newInvoice := make([]*fms.ContractInvoice, 0)
  629. if len(req.AmountList) > 0 {
  630. for i := range req.AmountList {
  631. if req.AmountList[i].Amount <= 0 {
  632. resp.Fail("登记金额有误", c)
  633. return
  634. }
  635. if req.AmountList[i].InvoiceDate == "" {
  636. resp.Fail("请选择日期", c)
  637. return
  638. }
  639. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  640. if e != nil {
  641. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  642. return
  643. }
  644. if req.AmountList[i].InvoiceId > 0 {
  645. noChangeInvoiceIds = append(noChangeInvoiceIds, req.AmountList[i].InvoiceId)
  646. } else {
  647. v := &fms.ContractInvoice{
  648. ContractRegisterId: req.ContractRegisterId,
  649. ContractCode: item.ContractCode,
  650. Amount: req.AmountList[i].Amount,
  651. InvoiceType: req.InvoiceType,
  652. InvoiceDate: t,
  653. AdminId: int(adminInfo.AdminId),
  654. AdminName: adminInfo.RealName,
  655. Remark: req.AmountList[i].Remark,
  656. }
  657. v.Set()
  658. newInvoice = append(newInvoice, v)
  659. }
  660. }
  661. }
  662. // 获取原有的登记信息
  663. invoiceCond := `contract_register_id = ? AND invoice_type = ?`
  664. invoicePars := make([]interface{}, 0)
  665. invoicePars = append(invoicePars, req.ContractRegisterId, req.InvoiceType)
  666. originInvoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  667. if e != nil {
  668. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  669. return
  670. }
  671. // 比对原有和现有的登记信息
  672. logList := make([]*fms.ContractRegisterLog, 0)
  673. opData := ""
  674. opDataByte, e := json.Marshal(req)
  675. if e != nil {
  676. return
  677. }
  678. opData = string(opDataByte)
  679. opType := fms.ContractRegisterOpTypeInvoice
  680. if req.InvoiceType == fms.ContractInvoiceTypePay {
  681. opType = fms.ContractRegisterOpTypePayment
  682. }
  683. nowTime := time.Now().Local()
  684. // 需要删除的记录
  685. deleteInvoiceIds := make([]int, 0)
  686. for i := range originInvoiceList {
  687. if !utils.InArrayByInt(noChangeInvoiceIds, originInvoiceList[i].ContractInvoiceId) {
  688. deleteInvoiceIds = append(deleteInvoiceIds, originInvoiceList[i].ContractInvoiceId)
  689. logList = append(logList, &fms.ContractRegisterLog{
  690. ContractRegisterId: req.ContractRegisterId,
  691. AdminId: int(adminInfo.AdminId),
  692. AdminName: adminInfo.RealName,
  693. OpData: opData,
  694. OpType: opType,
  695. CreateTime: nowTime,
  696. AmountRemark: fmt.Sprint("删除", fms.ContractInvoiceKeyNameMap[opType], "金额", originInvoiceList[i].Amount, "元"),
  697. })
  698. }
  699. }
  700. // 新增的记录
  701. if len(newInvoice) > 0 {
  702. newAmount := decimal.NewFromFloat(0).Round(2)
  703. for i := range newInvoice {
  704. a := decimal.NewFromFloat(newInvoice[i].Amount).Round(2)
  705. newAmount = newAmount.Add(a)
  706. }
  707. ia, _ := newAmount.Round(2).Float64()
  708. logList = append(logList, &fms.ContractRegisterLog{
  709. ContractRegisterId: req.ContractRegisterId,
  710. AdminId: int(adminInfo.AdminId),
  711. AdminName: adminInfo.RealName,
  712. OpData: opData,
  713. OpType: opType,
  714. CreateTime: nowTime,
  715. AmountRemark: fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
  716. })
  717. }
  718. // 删除并新增登记
  719. ob := new(fms.ContractInvoice)
  720. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, deleteInvoiceIds, newInvoice); e != nil {
  721. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  722. return
  723. }
  724. // 校验金额-是否修改状态
  725. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  726. // 操作日志
  727. go func() {
  728. logOB := new(fms.ContractRegisterLog)
  729. if e := logOB.AddInBatches(logList); e != nil {
  730. return
  731. }
  732. }()
  733. resp.Ok("操作成功", c)
  734. }
  735. // Export
  736. // @Title 合同登记-导出
  737. // @Description 合同登记-导出
  738. // @Param Keyword query string false "关键词"
  739. // @Param StartDate query string false "合同开始日期"
  740. // @Param EndDate query string false "合同结束日期"
  741. // @Param ServiceType query int false "套餐类型"
  742. // @Param ContractType query int false "合同类型"
  743. // @Param RegisterStatus query int false "登记状态"
  744. // @Success 200 string "操作成功"
  745. // @router /contract/register/export [get]
  746. func (rg *RegisterController) Export(c *gin.Context) {
  747. var req fms.ContractRegisterListReq
  748. if e := c.BindQuery(&req); e != nil {
  749. err, ok := e.(validator.ValidationErrors)
  750. if !ok {
  751. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  752. return
  753. }
  754. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  755. return
  756. }
  757. cond := `1 = 1`
  758. pars := make([]interface{}, 0)
  759. // 合同编号/客户姓名/销售/实际使用方
  760. if req.Keyword != "" {
  761. kw := "%" + req.Keyword + "%"
  762. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
  763. pars = append(pars, kw, kw, kw, kw)
  764. }
  765. if req.StartDate != "" && req.EndDate != "" {
  766. st := fmt.Sprint(req.StartDate, " 00:00:00")
  767. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  768. cond += ` AND (create_time BETWEEN ? AND ?)`
  769. pars = append(pars, st, ed)
  770. }
  771. if req.ContractType != 0 {
  772. cond += ` AND contract_type = ?`
  773. pars = append(pars, req.ContractType)
  774. }
  775. if req.RegisterStatus != 0 {
  776. cond += ` AND register_status = ?`
  777. pars = append(pars, req.RegisterStatus)
  778. }
  779. if req.ServiceType != 0 {
  780. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  781. if e != nil {
  782. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  783. return
  784. }
  785. if len(registerIds) > 0 {
  786. cond += ` AND contract_register_id IN ?`
  787. pars = append(pars, registerIds)
  788. } else {
  789. cond += ` AND 1 = 2`
  790. }
  791. }
  792. // 获取列表数据
  793. cr := new(fms.ContractRegister)
  794. list, e := cr.List(cond, pars)
  795. if e != nil {
  796. resp.FailData("获取合同列表失败", "Err:"+e.Error(), c)
  797. return
  798. }
  799. if len(list) == 0 {
  800. resp.Fail("无有效数据可导出", c)
  801. return
  802. }
  803. registerIds := make([]int, 0)
  804. for i := range list {
  805. registerIds = append(registerIds, list[i].ContractRegisterId)
  806. }
  807. // 获取小套餐品种
  808. cpCond := `product_id = ? AND permission_name <> ?`
  809. cpPars := make([]interface{}, 0)
  810. cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
  811. cp := new(crm.ChartPermission)
  812. permissionList, e := cp.List(cpCond, cpPars)
  813. if e != nil {
  814. resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
  815. return
  816. }
  817. permissionLen := len(permissionList)
  818. permissionNameIdMap := make(map[string]int)
  819. for i := range permissionList {
  820. permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  821. }
  822. // 获取套餐服务
  823. //serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
  824. //if e != nil {
  825. // resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
  826. // return
  827. //}
  828. // 套餐/开票/到款列表
  829. serviceMap := make(map[int][]*fms.ContractService)
  830. serviceChartPermissionsMap := make(map[int][]int)
  831. invoiceMap := make(map[int][]*fms.ContractInvoice)
  832. paymentMap := make(map[int][]*fms.ContractInvoice)
  833. maxInvoice := 0
  834. maxPayment := 0
  835. if len(registerIds) > 0 {
  836. // 获取套餐信息
  837. csCond := `contract_register_id IN ?`
  838. csPars := make([]interface{}, 0)
  839. csPars = append(csPars, registerIds)
  840. cs := new(fms.ContractService)
  841. serviceList, e := cs.List(csCond, csPars)
  842. if e != nil {
  843. resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
  844. return
  845. }
  846. for i := range serviceList {
  847. cid := serviceList[i].ContractRegisterId
  848. if serviceMap[cid] == nil {
  849. serviceMap[cid] = make([]*fms.ContractService, 0)
  850. }
  851. serviceMap[cid] = append(serviceMap[cid], serviceList[i])
  852. // 小套餐权限
  853. if serviceChartPermissionsMap[cid] == nil {
  854. serviceChartPermissionsMap[cid] = make([]int, 0)
  855. }
  856. if serviceList[i].ChartPermissionIds != "" {
  857. ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
  858. serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
  859. }
  860. }
  861. // 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
  862. ci := new(fms.ContractInvoice)
  863. invoiceList, e := ci.List(csCond, csPars, "")
  864. if e != nil {
  865. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  866. return
  867. }
  868. for k := range invoiceList {
  869. cid := invoiceList[k].ContractRegisterId
  870. if invoiceMap[cid] == nil {
  871. invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
  872. }
  873. if paymentMap[cid] == nil {
  874. paymentMap[cid] = make([]*fms.ContractInvoice, 0)
  875. }
  876. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
  877. invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
  878. continue
  879. }
  880. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
  881. paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
  882. continue
  883. }
  884. }
  885. // 取最大开票/到款数
  886. for j := range invoiceMap {
  887. if len(invoiceMap[j]) > maxInvoice {
  888. maxInvoice = len(invoiceMap[j])
  889. }
  890. }
  891. for p := range paymentMap {
  892. if len(paymentMap[p]) > maxPayment {
  893. maxPayment = len(paymentMap[p])
  894. }
  895. }
  896. }
  897. // 生成Excel文件
  898. //dir, err := os.Executable()
  899. //exPath := filepath.Dir(dir)
  900. //filePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  901. xlsxFile := xlsx.NewFile()
  902. //if err != nil {
  903. // resp.FailData("生成文件失败", "Err:"+err.Error(), c)
  904. // return
  905. //}
  906. style := xlsx.NewStyle()
  907. alignment := xlsx.Alignment{
  908. Horizontal: "center",
  909. Vertical: "center",
  910. WrapText: true,
  911. }
  912. style.Alignment = alignment
  913. style.ApplyAlignment = true
  914. sheet, err := xlsxFile.AddSheet("合同登记")
  915. if err != nil {
  916. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  917. return
  918. }
  919. //_ = sheet.SetColWidth(0, 0, 60)
  920. //_ = sheet.SetColWidth(4, 10, 60)
  921. // 1行表头
  922. titleRow := sheet.AddRow()
  923. titleRow.SetHeight(40)
  924. // 1行1列-右合并两格
  925. cell1 := titleRow.AddCell()
  926. cell1.HMerge = 2
  927. cell1.SetValue("FICC客户签约表 2022")
  928. cell1.SetStyle(style)
  929. // 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
  930. titleRow.AddCell().SetValue("")
  931. titleRow.AddCell().SetValue("")
  932. // 1行2列
  933. cell2 := titleRow.AddCell()
  934. cell2.SetValue("FICC大套餐")
  935. cell2.SetStyle(style)
  936. // 1行3列-右合并小套餐数
  937. if permissionLen >= 1 {
  938. cell3 := titleRow.AddCell()
  939. cell3.HMerge = permissionLen - 1
  940. cell3.SetValue("FICC小套餐")
  941. cell3.SetStyle(style)
  942. // 同上右增单元格小套餐数-1的空白单元格用于合并
  943. for i := 0; i < permissionLen-1; i++ {
  944. titleRow.AddCell().SetValue("")
  945. }
  946. }
  947. cell4 := titleRow.AddCell()
  948. cell4.SetValue("市场策略")
  949. cell4.SetStyle(style)
  950. cell5 := titleRow.AddCell()
  951. cell5.SetValue("财富管理")
  952. cell5.SetStyle(style)
  953. // 第二行表头
  954. titleRow2 := sheet.AddRow()
  955. titleRow2.SetHeight(60)
  956. row2Title := make([]string, 0)
  957. row2Title = append(row2Title, "客户名称", "续约-0\n新增-1", "销售", "FICC大套餐")
  958. for i := range permissionList {
  959. row2Title = append(row2Title, permissionList[i].PermissionName)
  960. }
  961. row2Title = append(row2Title, "市场策略", "财富管理", "套餐备注", "开始时间", "到期时间", "2022年合同金额", "约定付款时间", "签订日",
  962. "签订月", "合同状态", "合同编号", "备注")
  963. for i := range row2Title {
  964. v := titleRow2.AddCell()
  965. v.SetValue(row2Title[i])
  966. v.SetStyle(style)
  967. }
  968. // 第二行表头-开票/收款(动态添加)
  969. for i := 0; i < maxInvoice; i++ {
  970. n := i + 1
  971. c1 := titleRow2.AddCell()
  972. t1 := fmt.Sprintf("%s%d", "开票日", n)
  973. c1.SetValue(t1)
  974. c1.SetStyle(style)
  975. c2 := titleRow2.AddCell()
  976. t2 := fmt.Sprintf("%s%d", "开票金额", n)
  977. c2.SetValue(t2)
  978. c2.SetStyle(style)
  979. row2Title = append(row2Title, t1, t2)
  980. }
  981. for i := 0; i < maxPayment; i++ {
  982. n := i + 1
  983. c1 := titleRow2.AddCell()
  984. t1 := fmt.Sprintf("%s%d", "收款日", n)
  985. c1.SetValue(t1)
  986. c1.SetStyle(style)
  987. c2 := titleRow2.AddCell()
  988. t2 := fmt.Sprintf("%s%d", "收款金额", n)
  989. c2.SetValue(t2)
  990. c2.SetStyle(style)
  991. row2Title = append(row2Title, t1, t2)
  992. }
  993. // 此处取第二行标题NameKeyMap, 后面的动态匹配
  994. row2NameKeyMap := make(map[string]int)
  995. for i := range row2Title {
  996. row2NameKeyMap[row2Title[i]] = i
  997. }
  998. contractTMap := map[int]int{
  999. fms.ContractTypeNew: 1,
  1000. fms.ContractTypeRenew: 0,
  1001. fms.ContractTypeAgentPay: 2, // 代付合同
  1002. fms.ContractTypePlus: 3, // 补充协议
  1003. }
  1004. for _, v := range list {
  1005. k := -1
  1006. dataRow := sheet.AddRow()
  1007. dataRow.SetHeight(20)
  1008. k += 3
  1009. dataRow.AddCell().SetString(v.CompanyName)
  1010. dataRow.AddCell().SetString(fmt.Sprint(contractTMap[v.ContractType]))
  1011. dataRow.AddCell().SetString(v.SellerName)
  1012. // 大套餐
  1013. k += 1
  1014. col4Name := row2Title[k]
  1015. svList := serviceMap[v.ContractRegisterId]
  1016. col4 := ""
  1017. if svList != nil && len(svList) > 0 {
  1018. for isv := range svList {
  1019. if svList[isv].Title == col4Name {
  1020. col4 = "是"
  1021. break
  1022. }
  1023. }
  1024. }
  1025. dataRow.AddCell().SetString(col4)
  1026. // 小套餐
  1027. serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
  1028. for i := 0; i < permissionLen; i++ {
  1029. k += 1
  1030. colName := row2Title[k]
  1031. chartPermissionId := permissionNameIdMap[colName]
  1032. if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
  1033. dataRow.AddCell().SetString("是")
  1034. } else {
  1035. dataRow.AddCell().SetString("")
  1036. }
  1037. }
  1038. // 财富管理/市场策略(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
  1039. k += 1
  1040. col5Name := row2Title[k]
  1041. col5 := ""
  1042. if svList != nil && len(svList) > 0 {
  1043. for isv := range svList {
  1044. if svList[isv].Title == col5Name {
  1045. col5 = "是"
  1046. break
  1047. }
  1048. }
  1049. }
  1050. dataRow.AddCell().SetString(col5)
  1051. k += 1
  1052. col6Name := row2Title[k]
  1053. col6 := ""
  1054. if svList != nil && len(svList) > 0 {
  1055. for isv := range svList {
  1056. if svList[isv].Title == col6Name {
  1057. col6 = "是"
  1058. break
  1059. }
  1060. }
  1061. }
  1062. dataRow.AddCell().SetString(col6)
  1063. // 其他信息
  1064. dataRow.AddCell().SetString(v.ServiceRemark) // 套餐备注
  1065. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
  1066. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate)) // 到期时间
  1067. dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount)) // 2022年合同金额
  1068. dataRow.AddCell().SetString(v.AgreedPayTime) // 约定付款时间
  1069. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate)) // 签订日
  1070. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01", v.SignDate)) // 签订月
  1071. dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus]) // 合同状态
  1072. dataRow.AddCell().SetString(v.ContractCode) // 合同编号
  1073. dataRow.AddCell().SetString(v.Remark) // 备注
  1074. // 开票/到款信息
  1075. ivList := invoiceMap[v.ContractRegisterId]
  1076. ivListLen := len(ivList)
  1077. if ivList != nil && len(ivList) > 0 {
  1078. for ia := 0; ia < maxInvoice; ia++ {
  1079. if ia < ivListLen {
  1080. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", ivList[ia].InvoiceDate)) // 开票日
  1081. dataRow.AddCell().SetString(fmt.Sprint(ivList[ia].Amount)) // 开票金额
  1082. } else {
  1083. // 这里要把不够的填充为空
  1084. dataRow.AddCell().SetString("")
  1085. dataRow.AddCell().SetString("")
  1086. }
  1087. }
  1088. }
  1089. pyList := paymentMap[v.ContractRegisterId]
  1090. pyListLen := len(pyList)
  1091. if pyList != nil && pyListLen > 0 {
  1092. for ib := 0; ib < maxInvoice; ib++ {
  1093. if ib < pyListLen {
  1094. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", pyList[ib].InvoiceDate)) // 收款日
  1095. dataRow.AddCell().SetString(fmt.Sprint(pyList[ib].Amount)) // 收款金额
  1096. } else {
  1097. // 已经是最后的几列了其实可以不用填充空, 万一后面要加列此处还是填充上吧
  1098. dataRow.AddCell().SetString("")
  1099. dataRow.AddCell().SetString("")
  1100. }
  1101. }
  1102. }
  1103. }
  1104. // 输出文件
  1105. var buffer bytes.Buffer
  1106. _ = xlsxFile.Write(&buffer)
  1107. content := bytes.NewReader(buffer.Bytes())
  1108. //err = xlsxFile.Save(filePath)
  1109. //if err != nil {
  1110. // resp.FailData("保存文件失败", "Err:"+err.Error(), c)
  1111. // return
  1112. //}
  1113. //defer func() {
  1114. // _ = os.Remove(filePath)
  1115. //}()
  1116. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1117. fileName := "财务列表_" + randStr + ".xlsx"
  1118. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1119. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1120. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1121. }
  1122. // InvoiceList
  1123. // @Title 开票/到款列表
  1124. // @Description 开票/到款列表
  1125. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1126. // @Param ContractCode query string false "合同编号"
  1127. // @Param StartDate query string false "开始日期"
  1128. // @Param EndDate query string false "结束日期"
  1129. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1130. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1131. // @Success 200 {object} fms.ContractInvoiceItem
  1132. // @router /contract/register/invoice_list [get]
  1133. func (rg *RegisterController) InvoiceList(c *gin.Context) {
  1134. var req fms.ContractInvoiceListReq
  1135. if e := c.BindQuery(&req); e != nil {
  1136. err, ok := e.(validator.ValidationErrors)
  1137. if !ok {
  1138. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1139. return
  1140. }
  1141. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1142. return
  1143. }
  1144. cond := `invoice_type = ?`
  1145. pars := make([]interface{}, 0)
  1146. pars = append(pars, req.InvoiceType)
  1147. // 合同编号
  1148. if req.ContractCode != "" {
  1149. kw := fmt.Sprint("%", req.ContractCode, "%")
  1150. cond += ` AND contract_code LIKE ?`
  1151. pars = append(pars, kw)
  1152. }
  1153. if req.StartDate != "" && req.EndDate != "" {
  1154. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1155. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1156. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1157. pars = append(pars, st, ed)
  1158. }
  1159. if req.MinAmount > 0 {
  1160. cond += ` AND amount >= ?`
  1161. pars = append(pars, req.MinAmount)
  1162. }
  1163. if req.MaxAmount > 0 {
  1164. cond += ` AND amount <= ?`
  1165. pars = append(pars, req.MaxAmount)
  1166. }
  1167. page := new(base.Page)
  1168. page.SetPageSize(req.PageSize)
  1169. page.SetCurrent(req.Current)
  1170. page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
  1171. total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
  1172. if e != nil {
  1173. resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
  1174. return
  1175. }
  1176. page.SetTotal(total)
  1177. baseData := new(base.BaseData)
  1178. baseData.SetPage(page)
  1179. baseData.SetList(list)
  1180. resp.OkData("获取成功", baseData, c)
  1181. }
  1182. // InvoiceExport
  1183. // @Title 开票/到款列表-导出
  1184. // @Description 合同登记-导出
  1185. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1186. // @Param ContractCode query string false "合同编号"
  1187. // @Param StartDate query string false "开始日期"
  1188. // @Param EndDate query string false "结束日期"
  1189. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1190. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1191. // @Success 200 string "操作成功"
  1192. // @router /contract/register/invoice_export [get]
  1193. func (rg *RegisterController) InvoiceExport(c *gin.Context) {
  1194. var req fms.ContractInvoiceListReq
  1195. if e := c.BindQuery(&req); e != nil {
  1196. err, ok := e.(validator.ValidationErrors)
  1197. if !ok {
  1198. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1199. return
  1200. }
  1201. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1202. return
  1203. }
  1204. cond := `invoice_type = ?`
  1205. pars := make([]interface{}, 0)
  1206. pars = append(pars, req.InvoiceType)
  1207. // 合同编号
  1208. if req.ContractCode != "" {
  1209. kw := fmt.Sprint("%", req.ContractCode, "%")
  1210. cond += ` AND contract_code LIKE ?`
  1211. pars = append(pars, kw)
  1212. }
  1213. if req.StartDate != "" && req.EndDate != "" {
  1214. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1215. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1216. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1217. pars = append(pars, st, ed)
  1218. }
  1219. if req.MinAmount > 0 {
  1220. cond += ` AND amount >= ?`
  1221. pars = append(pars, req.MinAmount)
  1222. }
  1223. if req.MaxAmount > 0 {
  1224. cond += ` AND amount <= ?`
  1225. pars = append(pars, req.MaxAmount)
  1226. }
  1227. // 获取列表数据
  1228. cr := new(fms.ContractInvoice)
  1229. orderRule := `invoice_time DESC`
  1230. list, e := cr.List(cond, pars, orderRule)
  1231. if e != nil {
  1232. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  1233. return
  1234. }
  1235. if len(list) == 0 {
  1236. resp.Fail("无有效数据可导出", c)
  1237. return
  1238. }
  1239. // 生成Excel文件
  1240. xlsxFile := xlsx.NewFile()
  1241. style := xlsx.NewStyle()
  1242. alignment := xlsx.Alignment{
  1243. Horizontal: "center",
  1244. Vertical: "center",
  1245. WrapText: true,
  1246. }
  1247. style.Alignment = alignment
  1248. style.ApplyAlignment = true
  1249. sheet, err := xlsxFile.AddSheet("开票列表")
  1250. if err != nil {
  1251. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1252. return
  1253. }
  1254. // 表头
  1255. titleRow := sheet.AddRow()
  1256. titleRow.SetHeight(40)
  1257. cell1 := titleRow.AddCell()
  1258. cell1.SetValue("合同编号")
  1259. cell1.SetStyle(style)
  1260. cell2 := titleRow.AddCell()
  1261. cell2.SetValue("开票金额")
  1262. cell2.SetStyle(style)
  1263. cell3 := titleRow.AddCell()
  1264. cell3.SetValue("开票日期")
  1265. cell3.SetStyle(style)
  1266. for _, v := range list {
  1267. dataRow := sheet.AddRow()
  1268. dataRow.SetHeight(20)
  1269. dataRow.AddCell().SetString(v.ContractCode)
  1270. dataRow.AddCell().SetString(fmt.Sprint(v.Amount))
  1271. dataRow.AddCell().SetString(utils.TimeTransferString("2006-01-02", v.InvoiceDate))
  1272. }
  1273. // 输出文件
  1274. var buffer bytes.Buffer
  1275. _ = xlsxFile.Write(&buffer)
  1276. content := bytes.NewReader(buffer.Bytes())
  1277. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1278. fileName := "开票列表_" + randStr + ".xlsx"
  1279. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1280. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1281. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1282. }
  1283. // Import
  1284. // @Title 合同登记-导入
  1285. // @Description 合同登记-导入
  1286. // @Success 200 string "操作成功"
  1287. // @router /contract/register/import [post]
  1288. func (rg *RegisterController) Import(c *gin.Context) {
  1289. h, err := c.FormFile("File")
  1290. if err != nil {
  1291. resp.FailData("获取文件失败", "Err:"+err.Error(), c)
  1292. return
  1293. }
  1294. ext := path.Ext(h.Filename)
  1295. if ext != ".xlsx" && ext != ".xls" {
  1296. resp.Fail("请上传Excel文件", c)
  1297. return
  1298. }
  1299. uploadDir := "static/xls"
  1300. err = os.MkdirAll(uploadDir, 766)
  1301. if err != nil {
  1302. resp.FailData("存储目录创建失败", "Err:"+err.Error(), c)
  1303. return
  1304. }
  1305. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  1306. err = c.SaveUploadedFile(h, uploadPath)
  1307. if err != nil {
  1308. resp.FailData("保存本地文件失败", "Err:"+err.Error(), c)
  1309. return
  1310. }
  1311. defer func() {
  1312. _ = os.Remove(uploadPath)
  1313. }()
  1314. xlFile, err := xlsx.OpenFile(uploadPath)
  1315. if err != nil {
  1316. resp.FailData("打开文件失败", "Err:"+err.Error(), c)
  1317. return
  1318. }
  1319. // 获取所有已登记,根据合同编号去重
  1320. contractCodeArr := make([]string, 0)
  1321. registerOB := new(fms.ContractRegister)
  1322. registerCond := ``
  1323. registerPars := make([]interface{}, 0)
  1324. registerList, e := registerOB.List(registerCond, registerPars)
  1325. if e != nil {
  1326. resp.FailData("获取合同登记列表失败", "Err:"+e.Error(), c)
  1327. return
  1328. }
  1329. for i := range registerList {
  1330. contractCodeArr = append(contractCodeArr, registerList[i].ContractCode)
  1331. }
  1332. // 获取所有销售名称Map
  1333. sellerNameIdMap := make(map[string]int)
  1334. sellerCond := ``
  1335. sellerPars := make([]interface{}, 0)
  1336. sellerList, e := crm.GetCompanySellerByRoleCodes(sellerCond, sellerPars)
  1337. if e != nil {
  1338. resp.FailData("获取销售列表失败", "Err:"+e.Error(), c)
  1339. return
  1340. }
  1341. for i := range sellerList {
  1342. sellerNameIdMap[sellerList[i].RealName] = sellerList[i].AdminId
  1343. }
  1344. // 获取品种权限Map
  1345. chartPermissionNameIdMap := make(map[string]int)
  1346. cpCond := `product_id = ?`
  1347. cpPars := make([]interface{}, 0)
  1348. cpPars = append(cpPars, crm.CompanyProductFicc)
  1349. cp := new(crm.ChartPermission)
  1350. permissionList, e := cp.List(cpCond, cpPars)
  1351. if e != nil {
  1352. resp.FailData("获取权限列表失败", "Err:"+e.Error(), c)
  1353. return
  1354. }
  1355. for i := range permissionList {
  1356. chartPermissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  1357. }
  1358. // 获取所有套餐模板
  1359. serviceTempCond := ``
  1360. serviceTempPars := make([]interface{}, 0)
  1361. serviceTempOB := new(fms.ContractServiceTemplate)
  1362. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  1363. if e != nil {
  1364. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  1365. return
  1366. }
  1367. serviceTempNameMap := make(map[string]*fms.ContractServiceTemplate)
  1368. for i := range serviceTempList {
  1369. serviceTempNameMap[serviceTempList[i].Title] = serviceTempList[i]
  1370. }
  1371. titleMap := make(map[int]string)
  1372. newIds := make([]int, 0)
  1373. contractTypeArr := []string{"0", "1", "2", "3"}
  1374. for _, sheet := range xlFile.Sheets {
  1375. // 遍历行读取
  1376. maxRow := sheet.MaxRow
  1377. for i := 0; i < maxRow; i++ {
  1378. // 第二行开始读取表头
  1379. if i == 1 {
  1380. row := sheet.Row(i)
  1381. cells := row.Cells
  1382. for k, cell := range cells {
  1383. text := cell.String()
  1384. titleMap[k] = text
  1385. // 只检验前面部分表头
  1386. if k == 0 {
  1387. if text != "客户名称" {
  1388. resp.Fail("匹配失败, 请按模板导入", c)
  1389. return
  1390. }
  1391. }
  1392. if k == 1 {
  1393. if !strings.Contains(text, "续约") {
  1394. resp.Fail("匹配失败, 请按模板导入", c)
  1395. return
  1396. }
  1397. }
  1398. if k == 2 {
  1399. if text != "销售" {
  1400. resp.Fail("匹配失败, 请按模板导入", c)
  1401. return
  1402. }
  1403. }
  1404. if k == 3 {
  1405. if text != "FICC大套餐" {
  1406. resp.Fail("匹配失败, 请按模板导入", c)
  1407. return
  1408. }
  1409. }
  1410. }
  1411. }
  1412. // 数据行
  1413. if i >= 2 {
  1414. row := sheet.Row(i)
  1415. cells := row.Cells
  1416. // 登记信息
  1417. rowRegister := new(fms.ContractRegister)
  1418. // 套餐
  1419. rowServices := make([]*fms.ContractService, 0)
  1420. // 开票/到款
  1421. rowInvoices := make([]*fms.ContractInvoice, 0)
  1422. rowChartPermissionIdArr := make([]string, 0)
  1423. rowInvoice1 := new(fms.ContractInvoice)
  1424. rowInvoice2 := new(fms.ContractInvoice)
  1425. rowInvoice3 := new(fms.ContractInvoice)
  1426. rowInvoice4 := new(fms.ContractInvoice)
  1427. rowInvoice5 := new(fms.ContractInvoice)
  1428. rowInvoice6 := new(fms.ContractInvoice)
  1429. isSkip := false
  1430. for k, cell := range cells {
  1431. v := utils.TrimStr(cell.String())
  1432. //fmt.Printf("k: %d, v: %s\n", k, v)
  1433. // 客户名称
  1434. if k == 0 {
  1435. rowRegister.CompanyName = v
  1436. continue
  1437. }
  1438. // 续约/新增
  1439. if k == 1 {
  1440. if !utils.InArrayByStr(contractTypeArr, v) {
  1441. resp.Fail(fmt.Sprintf("第%d行合同类型有误, 请按模板导入", i+1), c)
  1442. return
  1443. }
  1444. if v == "0" {
  1445. rowRegister.ContractType = fms.ContractTypeRenew
  1446. }
  1447. if v == "1" {
  1448. rowRegister.ContractType = fms.ContractTypeNew
  1449. }
  1450. if v == "2" {
  1451. rowRegister.ContractType = fms.ContractTypeAgentPay
  1452. }
  1453. if v == "3" {
  1454. rowRegister.ContractType = fms.ContractTypePlus
  1455. }
  1456. continue
  1457. }
  1458. // 销售
  1459. if k == 2 {
  1460. rowRegister.SellerName = v
  1461. rowRegister.SellerId = sellerNameIdMap[rowRegister.SellerName]
  1462. continue
  1463. }
  1464. // FICC大套餐
  1465. if k == 3 {
  1466. if v == "是" {
  1467. tempItem := serviceTempNameMap[titleMap[k]]
  1468. if tempItem == nil {
  1469. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  1470. return
  1471. }
  1472. cs := &fms.ContractService{
  1473. ProductId: crm.CompanyProductFicc,
  1474. ServiceTemplateId: tempItem.ServiceTemplateId,
  1475. Title: tempItem.Title,
  1476. Value: tempItem.Value,
  1477. TableValue: tempItem.TableValue,
  1478. ChartPermissionId: tempItem.ChartPermissionId,
  1479. }
  1480. cs.Set()
  1481. rowServices = append(rowServices, cs)
  1482. }
  1483. continue
  1484. }
  1485. // FICC小套餐
  1486. if k >= 4 && k <= 23 {
  1487. // 小套餐权限
  1488. if v == "是" {
  1489. rowChartPermissionIdArr = append(rowChartPermissionIdArr, strconv.Itoa(chartPermissionNameIdMap[titleMap[k]]))
  1490. }
  1491. continue
  1492. }
  1493. // 市场策略/财富管理
  1494. if k == 24 || k == 25 {
  1495. if v == "是" {
  1496. tempItem := serviceTempNameMap[titleMap[k]]
  1497. if tempItem == nil {
  1498. resp.Fail(fmt.Sprintf("第%d行市场策略/财富管理套餐名称不匹配, 请按模板导入", i+1), c)
  1499. return
  1500. }
  1501. cs := &fms.ContractService{
  1502. ProductId: crm.CompanyProductFicc,
  1503. ServiceTemplateId: tempItem.ServiceTemplateId,
  1504. Title: tempItem.Title,
  1505. Value: tempItem.Value,
  1506. TableValue: tempItem.TableValue,
  1507. ChartPermissionId: tempItem.ChartPermissionId,
  1508. }
  1509. cs.Set()
  1510. rowServices = append(rowServices, cs)
  1511. }
  1512. continue
  1513. }
  1514. // 套餐备注
  1515. if k == 26 {
  1516. rowRegister.ServiceRemark = v
  1517. continue
  1518. }
  1519. // 开始时间/到期时间
  1520. if k == 27 {
  1521. // 转换失败可能是因为格式为Excel日期格式, 读取出来会是一串数字, 将其转换成日期字符串再处理
  1522. va := cell.Value
  1523. startDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1524. if e != nil {
  1525. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1526. startDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1527. if e != nil {
  1528. resp.Fail(fmt.Sprintf("第%d行开始时间格式有误, 请按模板导入", i+1), c)
  1529. return
  1530. }
  1531. }
  1532. rowRegister.StartDate = startDate
  1533. continue
  1534. }
  1535. if k == 28 {
  1536. va := cell.Value
  1537. endDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1538. if e != nil {
  1539. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1540. endDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1541. if e != nil {
  1542. resp.Fail(fmt.Sprintf("第%d行到期时间格式有误, 请按模板导入", i+1), c)
  1543. return
  1544. }
  1545. }
  1546. rowRegister.EndDate = endDate
  1547. continue
  1548. }
  1549. // 合同金额
  1550. if k == 29 {
  1551. amountStr := v
  1552. amount, e := strconv.ParseFloat(amountStr, 64)
  1553. if e != nil {
  1554. resp.Fail(fmt.Sprintf("第%d行合同金额有误, 请按模板导入", i+1), c)
  1555. return
  1556. }
  1557. rowRegister.ContractAmount = amount
  1558. continue
  1559. }
  1560. // 约定付款日期
  1561. if k == 30 {
  1562. rowRegister.AgreedPayTime = v
  1563. continue
  1564. }
  1565. // 签订日
  1566. // k == 32为签订月,可忽略
  1567. if k == 31 {
  1568. va := cell.Value
  1569. signDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1570. if e != nil {
  1571. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1572. signDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1573. if e != nil {
  1574. resp.Fail(fmt.Sprintf("第%d行签订日格式有误, 请按模板导入", i+1), c)
  1575. return
  1576. }
  1577. }
  1578. rowRegister.SignDate = signDate
  1579. continue
  1580. }
  1581. // 合同状态
  1582. if k == 33 {
  1583. rowRegister.ContractStatus = fms.ContractStatusNameKeyMap[v]
  1584. if rowRegister.ContractStatus == 0 {
  1585. resp.Fail(fmt.Sprintf("第%d行合同状态不匹配, 请按模板导入", i+1), c)
  1586. return
  1587. }
  1588. continue
  1589. }
  1590. // 合同编号
  1591. if k == 34 {
  1592. rowContractCode := v
  1593. if rowContractCode == "" {
  1594. resp.Fail(fmt.Sprintf("第%d行合同编号不可为空, 请按模板导入", i+1), c)
  1595. return
  1596. }
  1597. if utils.InArrayByStr(contractCodeArr, rowContractCode) {
  1598. // 此合同已登记, 跳过本行的读取
  1599. isSkip = true
  1600. break
  1601. }
  1602. rowRegister.ContractCode = rowContractCode
  1603. continue
  1604. }
  1605. // 备注
  1606. if k == 35 {
  1607. rowRegister.Remark = v
  1608. continue
  1609. }
  1610. // 开票日/开票金额
  1611. if k == 36 {
  1612. if v != "" {
  1613. va := cell.Value
  1614. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1615. if e != nil {
  1616. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1617. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1618. if e != nil {
  1619. resp.Fail(fmt.Sprintf("第%d行开票时间1格式有误, 请按模板导入", i+1), c)
  1620. return
  1621. }
  1622. }
  1623. rowInvoice1.InvoiceDate = invoiceDate
  1624. rowInvoice1.ContractCode = rowRegister.ContractCode
  1625. rowInvoice1.InvoiceType = fms.ContractInvoiceTypeMake
  1626. }
  1627. continue
  1628. }
  1629. if k == 37 && v != "" {
  1630. amountStr := v
  1631. amount, e := strconv.ParseFloat(amountStr, 64)
  1632. if e != nil {
  1633. resp.Fail(fmt.Sprintf("第%d行开票金额1有误, 请按模板导入", i+1), c)
  1634. return
  1635. }
  1636. rowInvoice1.Amount = amount
  1637. rowInvoices = append(rowInvoices, rowInvoice1)
  1638. continue
  1639. }
  1640. if k == 38 {
  1641. if v != "" {
  1642. va := cell.Value
  1643. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1644. if e != nil {
  1645. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1646. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1647. if e != nil {
  1648. resp.Fail(fmt.Sprintf("第%d行开票时间2格式有误, 请按模板导入", i+1), c)
  1649. return
  1650. }
  1651. }
  1652. rowInvoice2.InvoiceDate = invoiceDate
  1653. rowInvoice2.ContractCode = rowRegister.ContractCode
  1654. rowInvoice2.InvoiceType = fms.ContractInvoiceTypeMake
  1655. }
  1656. continue
  1657. }
  1658. if k == 39 && v != "" {
  1659. amountStr := v
  1660. amount, e := strconv.ParseFloat(amountStr, 64)
  1661. if e != nil {
  1662. resp.Fail(fmt.Sprintf("第%d行开票金额2有误, 请按模板导入", i+1), c)
  1663. return
  1664. }
  1665. rowInvoice2.Amount = amount
  1666. rowInvoices = append(rowInvoices, rowInvoice2)
  1667. continue
  1668. }
  1669. if k == 40 {
  1670. if v != "" {
  1671. va := cell.Value
  1672. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1673. if e != nil {
  1674. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1675. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1676. if e != nil {
  1677. resp.Fail(fmt.Sprintf("第%d行开票时间3格式有误, 请按模板导入", i+1), c)
  1678. return
  1679. }
  1680. }
  1681. rowInvoice3.InvoiceDate = invoiceDate
  1682. rowInvoice3.ContractCode = rowRegister.ContractCode
  1683. rowInvoice3.InvoiceType = fms.ContractInvoiceTypeMake
  1684. }
  1685. continue
  1686. }
  1687. if k == 41 && v != "" {
  1688. amountStr := v
  1689. amount, e := strconv.ParseFloat(amountStr, 64)
  1690. if e != nil {
  1691. resp.Fail(fmt.Sprintf("第%d行开票金额3有误, 请按模板导入", i+1), c)
  1692. return
  1693. }
  1694. rowInvoice3.Amount = amount
  1695. rowInvoices = append(rowInvoices, rowInvoice3)
  1696. continue
  1697. }
  1698. // 收款日/收款金额
  1699. if k == 42 {
  1700. if v != "" {
  1701. va := cell.Value
  1702. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1703. if e != nil {
  1704. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1705. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1706. if e != nil {
  1707. resp.Fail(fmt.Sprintf("第%d行收款时间1格式有误, 请按模板导入", i+1), c)
  1708. return
  1709. }
  1710. }
  1711. rowInvoice4.InvoiceDate = invoiceDate
  1712. rowInvoice4.ContractCode = rowRegister.ContractCode
  1713. rowInvoice4.InvoiceType = fms.ContractInvoiceTypePay
  1714. }
  1715. continue
  1716. }
  1717. if k == 43 && v != "" {
  1718. amountStr := v
  1719. amount, e := strconv.ParseFloat(amountStr, 64)
  1720. if e != nil {
  1721. resp.Fail(fmt.Sprintf("第%d行收款金额1有误, 请按模板导入", i+1), c)
  1722. return
  1723. }
  1724. rowInvoice4.Amount = amount
  1725. rowInvoices = append(rowInvoices, rowInvoice4)
  1726. continue
  1727. }
  1728. if k == 44 {
  1729. if v != "" {
  1730. va := cell.Value
  1731. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1732. if e != nil {
  1733. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1734. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1735. if e != nil {
  1736. resp.Fail(fmt.Sprintf("第%d行收款时间2格式有误, 请按模板导入", i+1), c)
  1737. return
  1738. }
  1739. }
  1740. rowInvoice5.InvoiceDate = invoiceDate
  1741. rowInvoice5.ContractCode = rowRegister.ContractCode
  1742. rowInvoice5.InvoiceType = fms.ContractInvoiceTypePay
  1743. }
  1744. continue
  1745. }
  1746. if k == 45 && v != "" {
  1747. amountStr := v
  1748. amount, e := strconv.ParseFloat(amountStr, 64)
  1749. if e != nil {
  1750. resp.Fail(fmt.Sprintf("第%d行收款金额2有误, 请按模板导入", i+1), c)
  1751. return
  1752. }
  1753. rowInvoice5.Amount = amount
  1754. rowInvoices = append(rowInvoices, rowInvoice5)
  1755. continue
  1756. }
  1757. if k == 46 {
  1758. if v != "" {
  1759. va := cell.Value
  1760. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1761. if e != nil {
  1762. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1763. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1764. if e != nil {
  1765. resp.Fail(fmt.Sprintf("第%d行收款时间3格式有误, 请按模板导入", i+1), c)
  1766. return
  1767. }
  1768. }
  1769. rowInvoice6.InvoiceDate = invoiceDate
  1770. rowInvoice6.ContractCode = rowRegister.ContractCode
  1771. rowInvoice6.InvoiceType = fms.ContractInvoiceTypePay
  1772. }
  1773. continue
  1774. }
  1775. if k == 47 && v != "" {
  1776. amountStr := v
  1777. amount, e := strconv.ParseFloat(amountStr, 64)
  1778. if e != nil {
  1779. resp.Fail(fmt.Sprintf("第%d行收款金额3有误, 请按模板导入", i+1), c)
  1780. return
  1781. }
  1782. rowInvoice6.Amount = amount
  1783. rowInvoices = append(rowInvoices, rowInvoice6)
  1784. continue
  1785. }
  1786. }
  1787. if isSkip {
  1788. continue
  1789. }
  1790. // 小套餐
  1791. if len(rowChartPermissionIdArr) > 0 {
  1792. // 说明有小套餐
  1793. tempItem := serviceTempNameMap["FICC小套餐"]
  1794. if tempItem == nil {
  1795. resp.Fail(fmt.Sprintf("第%d行小套餐名称不匹配, 请按模板导入", i+1), c)
  1796. return
  1797. }
  1798. rowChartPermissionIds := strings.Join(rowChartPermissionIdArr, ",")
  1799. cs := &fms.ContractService{
  1800. ProductId: crm.CompanyProductFicc,
  1801. ServiceTemplateId: tempItem.ServiceTemplateId,
  1802. Title: tempItem.Title,
  1803. Value: tempItem.Value,
  1804. TableValue: tempItem.TableValue,
  1805. ChartPermissionId: tempItem.ChartPermissionId,
  1806. ChartPermissionIds: rowChartPermissionIds,
  1807. }
  1808. cs.Set()
  1809. rowServices = append(rowServices, cs)
  1810. }
  1811. // 如果导入的最后一条合同编号为空并且合同编号之后的字段均为空
  1812. // excel这个包读行的时候不会再往后面没数据的地方读取, 所以此处需要重新判断一次
  1813. if rowRegister.ContractCode == "" {
  1814. resp.Fail(fmt.Sprintf("第%d行合同编号为空", i+1), c)
  1815. return
  1816. }
  1817. rowRegister.RegisterStatus = fms.ContractRegisterStatusIng
  1818. // 新增登记、套餐、开票到款信息
  1819. newId, e := fms.CreateImportContractRegister(rowRegister, rowServices, rowInvoices)
  1820. if e != nil {
  1821. resp.FailData(fmt.Sprintf("第%d行导入失败", i+1), "新增导入登记失败, Err: "+e.Error(), c)
  1822. return
  1823. }
  1824. newIds = append(newIds, newId)
  1825. }
  1826. }
  1827. }
  1828. // 更新登记状态
  1829. go func() {
  1830. for i := range newIds {
  1831. fmsService.CheckContractRegisterAmount(newIds[i])
  1832. }
  1833. }()
  1834. resp.Ok("操作成功", c)
  1835. }