register.go 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  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. pageSize := req.PageSize
  1145. pageIndex := req.Current
  1146. if pageSize <= 0 {
  1147. pageSize = utils.PageSize20
  1148. }
  1149. if pageIndex <= 0 {
  1150. pageIndex = 1
  1151. }
  1152. cond := `invoice_type = ?`
  1153. pars := make([]interface{}, 0)
  1154. pars = append(pars, req.InvoiceType)
  1155. // 合同编号
  1156. if req.ContractCode != "" {
  1157. kw := fmt.Sprint("%", req.ContractCode, "%")
  1158. cond += ` AND contract_code LIKE ?`
  1159. pars = append(pars, kw)
  1160. }
  1161. if req.StartDate != "" && req.EndDate != "" {
  1162. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1163. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1164. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1165. pars = append(pars, st, ed)
  1166. }
  1167. if req.MinAmount > 0 {
  1168. cond += ` AND amount >= ?`
  1169. pars = append(pars, req.MinAmount)
  1170. }
  1171. if req.MaxAmount > 0 {
  1172. cond += ` AND amount <= ?`
  1173. pars = append(pars, req.MaxAmount)
  1174. }
  1175. page := new(base.Page)
  1176. page.SetPageSize(pageSize)
  1177. page.SetCurrent(pageIndex)
  1178. page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
  1179. total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
  1180. if e != nil {
  1181. resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
  1182. return
  1183. }
  1184. page.SetTotal(total)
  1185. // 金额合计
  1186. ob := new(fms.ContractInvoice)
  1187. amountTotal, e := ob.Sum("amount", cond, pars)
  1188. if e != nil {
  1189. resp.FailMsg("获取失败", "获取合同开票/到款列表合计金额失败, Err: "+e.Error(), c)
  1190. return
  1191. }
  1192. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotal), 64)
  1193. type RespData struct {
  1194. Page *base.Page `json:"page"`
  1195. List interface{} `json:"list"`
  1196. AmountTotal float64 `json:"amount_total"`
  1197. }
  1198. respData := &RespData{
  1199. Page: page,
  1200. List: list,
  1201. AmountTotal: amountTotal,
  1202. }
  1203. resp.OkData("获取成功", respData, c)
  1204. }
  1205. // InvoiceExport
  1206. // @Title 开票/到款列表-导出
  1207. // @Description 合同登记-导出
  1208. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1209. // @Param ContractCode query string false "合同编号"
  1210. // @Param StartDate query string false "开始日期"
  1211. // @Param EndDate query string false "结束日期"
  1212. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1213. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1214. // @Success 200 string "操作成功"
  1215. // @router /contract/register/invoice_export [get]
  1216. func (rg *RegisterController) InvoiceExport(c *gin.Context) {
  1217. var req fms.ContractInvoiceListReq
  1218. if e := c.BindQuery(&req); e != nil {
  1219. err, ok := e.(validator.ValidationErrors)
  1220. if !ok {
  1221. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1222. return
  1223. }
  1224. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1225. return
  1226. }
  1227. listName := "开票"
  1228. if req.InvoiceType == fms.ContractInvoiceTypePay {
  1229. listName = "到款"
  1230. }
  1231. cond := `invoice_type = ?`
  1232. pars := make([]interface{}, 0)
  1233. pars = append(pars, req.InvoiceType)
  1234. // 合同编号
  1235. if req.ContractCode != "" {
  1236. kw := fmt.Sprint("%", req.ContractCode, "%")
  1237. cond += ` AND contract_code LIKE ?`
  1238. pars = append(pars, kw)
  1239. }
  1240. if req.StartDate != "" && req.EndDate != "" {
  1241. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1242. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1243. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1244. pars = append(pars, st, ed)
  1245. }
  1246. if req.MinAmount > 0 {
  1247. cond += ` AND amount >= ?`
  1248. pars = append(pars, req.MinAmount)
  1249. }
  1250. if req.MaxAmount > 0 {
  1251. cond += ` AND amount <= ?`
  1252. pars = append(pars, req.MaxAmount)
  1253. }
  1254. // 获取列表数据
  1255. cr := new(fms.ContractInvoice)
  1256. orderRule := `invoice_time DESC`
  1257. list, e := cr.List(cond, pars, orderRule)
  1258. if e != nil {
  1259. resp.FailData(fmt.Sprintf("获取%s列表失败", listName), "Err:"+e.Error(), c)
  1260. return
  1261. }
  1262. if len(list) == 0 {
  1263. resp.Fail("无有效数据可导出", c)
  1264. return
  1265. }
  1266. // 生成Excel文件
  1267. xlsxFile := xlsx.NewFile()
  1268. style := xlsx.NewStyle()
  1269. alignment := xlsx.Alignment{
  1270. Horizontal: "center",
  1271. Vertical: "center",
  1272. WrapText: true,
  1273. }
  1274. style.Alignment = alignment
  1275. style.ApplyAlignment = true
  1276. sheet, err := xlsxFile.AddSheet(fmt.Sprintf("%s列表", listName))
  1277. if err != nil {
  1278. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1279. return
  1280. }
  1281. // 表头
  1282. titleRow := sheet.AddRow()
  1283. titleRow.SetHeight(40)
  1284. cell1 := titleRow.AddCell()
  1285. cell1.SetValue("合同编号")
  1286. cell1.SetStyle(style)
  1287. cell2 := titleRow.AddCell()
  1288. cell2.SetValue(fmt.Sprintf("%s金额", listName))
  1289. cell2.SetStyle(style)
  1290. cell3 := titleRow.AddCell()
  1291. cell3.SetValue(fmt.Sprintf("%s日期", listName))
  1292. cell3.SetStyle(style)
  1293. for _, v := range list {
  1294. dataRow := sheet.AddRow()
  1295. dataRow.SetHeight(20)
  1296. dataRow.AddCell().SetString(v.ContractCode)
  1297. dataRow.AddCell().SetString(fmt.Sprint(v.Amount))
  1298. dataRow.AddCell().SetString(utils.TimeTransferString("2006-01-02", v.InvoiceDate))
  1299. }
  1300. // 输出文件
  1301. var buffer bytes.Buffer
  1302. _ = xlsxFile.Write(&buffer)
  1303. content := bytes.NewReader(buffer.Bytes())
  1304. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1305. fileName := fmt.Sprintf("%s列表_%s.xlsx", listName, randStr)
  1306. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1307. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1308. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1309. }
  1310. // Import
  1311. // @Title 合同登记-导入
  1312. // @Description 合同登记-导入
  1313. // @Success 200 string "操作成功"
  1314. // @router /contract/register/import [post]
  1315. func (rg *RegisterController) Import(c *gin.Context) {
  1316. h, err := c.FormFile("File")
  1317. if err != nil {
  1318. resp.FailData("获取文件失败", "Err:"+err.Error(), c)
  1319. return
  1320. }
  1321. ext := path.Ext(h.Filename)
  1322. if ext != ".xlsx" && ext != ".xls" {
  1323. resp.Fail("请上传Excel文件", c)
  1324. return
  1325. }
  1326. uploadDir := "static/xls"
  1327. err = os.MkdirAll(uploadDir, 766)
  1328. if err != nil {
  1329. resp.FailData("存储目录创建失败", "Err:"+err.Error(), c)
  1330. return
  1331. }
  1332. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  1333. err = c.SaveUploadedFile(h, uploadPath)
  1334. if err != nil {
  1335. resp.FailData("保存本地文件失败", "Err:"+err.Error(), c)
  1336. return
  1337. }
  1338. defer func() {
  1339. _ = os.Remove(uploadPath)
  1340. }()
  1341. xlFile, err := xlsx.OpenFile(uploadPath)
  1342. if err != nil {
  1343. resp.FailData("打开文件失败", "Err:"+err.Error(), c)
  1344. return
  1345. }
  1346. // 获取所有已登记,根据合同编号去重
  1347. contractCodeArr := make([]string, 0)
  1348. registerOB := new(fms.ContractRegister)
  1349. registerCond := ``
  1350. registerPars := make([]interface{}, 0)
  1351. registerList, e := registerOB.List(registerCond, registerPars)
  1352. if e != nil {
  1353. resp.FailData("获取合同登记列表失败", "Err:"+e.Error(), c)
  1354. return
  1355. }
  1356. for i := range registerList {
  1357. contractCodeArr = append(contractCodeArr, registerList[i].ContractCode)
  1358. }
  1359. // 获取所有销售名称Map
  1360. sellerNameIdMap := make(map[string]int)
  1361. sellerCond := ``
  1362. sellerPars := make([]interface{}, 0)
  1363. sellerList, e := crm.GetCompanySellerByRoleCodes(sellerCond, sellerPars)
  1364. if e != nil {
  1365. resp.FailData("获取销售列表失败", "Err:"+e.Error(), c)
  1366. return
  1367. }
  1368. for i := range sellerList {
  1369. sellerNameIdMap[sellerList[i].RealName] = sellerList[i].AdminId
  1370. }
  1371. // 获取品种权限Map
  1372. chartPermissionNameIdMap := make(map[string]int)
  1373. cpCond := `product_id = ?`
  1374. cpPars := make([]interface{}, 0)
  1375. cpPars = append(cpPars, crm.CompanyProductFicc)
  1376. cp := new(crm.ChartPermission)
  1377. permissionList, e := cp.List(cpCond, cpPars)
  1378. if e != nil {
  1379. resp.FailData("获取权限列表失败", "Err:"+e.Error(), c)
  1380. return
  1381. }
  1382. for i := range permissionList {
  1383. chartPermissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  1384. }
  1385. // 获取所有套餐模板
  1386. serviceTempCond := ``
  1387. serviceTempPars := make([]interface{}, 0)
  1388. serviceTempOB := new(fms.ContractServiceTemplate)
  1389. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  1390. if e != nil {
  1391. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  1392. return
  1393. }
  1394. serviceTempNameMap := make(map[string]*fms.ContractServiceTemplate)
  1395. for i := range serviceTempList {
  1396. serviceTempNameMap[serviceTempList[i].Title] = serviceTempList[i]
  1397. }
  1398. titleMap := make(map[int]string)
  1399. newIds := make([]int, 0)
  1400. contractTypeArr := []string{"0", "1", "2", "3"}
  1401. contractTypeMap := map[string]int{
  1402. "0": fms.ContractTypeRenew,
  1403. "1": fms.ContractTypeNew,
  1404. "2": fms.ContractTypeAgentPay,
  1405. "3": fms.ContractTypePlus,
  1406. }
  1407. checkDate := time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local)
  1408. for _, sheet := range xlFile.Sheets {
  1409. // 遍历行读取
  1410. maxRow := sheet.MaxRow
  1411. for i := 0; i < maxRow; i++ {
  1412. // 第二行开始读取表头
  1413. if i == 1 {
  1414. row := sheet.Row(i)
  1415. cells := row.Cells
  1416. for k, cell := range cells {
  1417. text := cell.String()
  1418. titleMap[k] = text
  1419. // 只检验前面部分表头
  1420. if k == 0 {
  1421. if text != "客户名称" {
  1422. resp.Fail("匹配失败, 请按模板导入", c)
  1423. return
  1424. }
  1425. }
  1426. if k == 1 {
  1427. if !strings.Contains(text, "续约") {
  1428. resp.Fail("匹配失败, 请按模板导入", c)
  1429. return
  1430. }
  1431. }
  1432. if k == 2 {
  1433. if text != "销售" {
  1434. resp.Fail("匹配失败, 请按模板导入", c)
  1435. return
  1436. }
  1437. }
  1438. if k == 3 {
  1439. if text != "FICC大套餐" {
  1440. resp.Fail("匹配失败, 请按模板导入", c)
  1441. return
  1442. }
  1443. }
  1444. }
  1445. }
  1446. // 数据行
  1447. if i >= 2 {
  1448. row := sheet.Row(i)
  1449. cells := row.Cells
  1450. // 登记信息
  1451. rowRegister := new(fms.ContractRegister)
  1452. // 套餐
  1453. rowServices := make([]*fms.ContractService, 0)
  1454. // 开票/到款
  1455. rowInvoices := make([]*fms.ContractInvoice, 0)
  1456. rowChartPermissionIdArr := make([]string, 0)
  1457. rowInvoice1 := new(fms.ContractInvoice)
  1458. rowInvoice2 := new(fms.ContractInvoice)
  1459. rowInvoice3 := new(fms.ContractInvoice)
  1460. rowInvoice4 := new(fms.ContractInvoice)
  1461. rowInvoice5 := new(fms.ContractInvoice)
  1462. rowInvoice6 := new(fms.ContractInvoice)
  1463. isSkip := false
  1464. for k, cell := range cells {
  1465. v := utils.TrimStr(cell.String())
  1466. // 客户名称
  1467. if k == 0 {
  1468. if v == "" {
  1469. resp.Fail(fmt.Sprintf("第%d行客户名称不可为空, 请按模板导入", i+1), c)
  1470. return
  1471. }
  1472. rowRegister.CompanyName = v
  1473. continue
  1474. }
  1475. // 续约/新增
  1476. if k == 1 {
  1477. if !utils.InArrayByStr(contractTypeArr, v) {
  1478. resp.Fail(fmt.Sprintf("第%d行合同类型有误, 请按模板导入", i+1), c)
  1479. return
  1480. }
  1481. rowRegister.ContractType = contractTypeMap[v]
  1482. if rowRegister.ContractType == 0 {
  1483. resp.Fail(fmt.Sprintf("第%d行合同类型匹配有误, 请按模板导入", i+1), c)
  1484. return
  1485. }
  1486. continue
  1487. }
  1488. // 销售
  1489. if k == 2 {
  1490. if v == "" {
  1491. resp.Fail(fmt.Sprintf("第%d行销售名称不可为空, 请按模板导入", i+1), c)
  1492. return
  1493. }
  1494. rowRegister.SellerName = v
  1495. rowRegister.SellerId = sellerNameIdMap[rowRegister.SellerName]
  1496. if rowRegister.SellerId == 0 {
  1497. resp.Fail(fmt.Sprintf("第%d行销售名称与系统销售不匹配, 请核对名称后导入", i+1), c)
  1498. return
  1499. }
  1500. continue
  1501. }
  1502. // FICC大套餐
  1503. if k == 3 {
  1504. if v == "是" {
  1505. tempItem := serviceTempNameMap[titleMap[k]]
  1506. if tempItem == nil {
  1507. resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
  1508. return
  1509. }
  1510. cs := &fms.ContractService{
  1511. ProductId: crm.CompanyProductFicc,
  1512. ServiceTemplateId: tempItem.ServiceTemplateId,
  1513. Title: tempItem.Title,
  1514. Value: tempItem.Value,
  1515. TableValue: tempItem.TableValue,
  1516. ChartPermissionId: tempItem.ChartPermissionId,
  1517. }
  1518. cs.Set()
  1519. rowServices = append(rowServices, cs)
  1520. }
  1521. continue
  1522. }
  1523. // FICC小套餐
  1524. if k >= 4 && k <= 23 {
  1525. // 小套餐权限
  1526. if v == "是" {
  1527. rowChartPermissionIdArr = append(rowChartPermissionIdArr, strconv.Itoa(chartPermissionNameIdMap[titleMap[k]]))
  1528. }
  1529. continue
  1530. }
  1531. // 市场策略/财富管理
  1532. if k == 24 || k == 25 {
  1533. if v == "是" {
  1534. tempItem := serviceTempNameMap[titleMap[k]]
  1535. if tempItem == nil {
  1536. resp.Fail(fmt.Sprintf("第%d行市场策略/财富管理套餐名称不匹配, 请按模板导入", i+1), c)
  1537. return
  1538. }
  1539. cs := &fms.ContractService{
  1540. ProductId: crm.CompanyProductFicc,
  1541. ServiceTemplateId: tempItem.ServiceTemplateId,
  1542. Title: tempItem.Title,
  1543. Value: tempItem.Value,
  1544. TableValue: tempItem.TableValue,
  1545. ChartPermissionId: tempItem.ChartPermissionId,
  1546. }
  1547. cs.Set()
  1548. rowServices = append(rowServices, cs)
  1549. }
  1550. continue
  1551. }
  1552. // 套餐备注
  1553. if k == 26 {
  1554. rowRegister.ServiceRemark = v
  1555. continue
  1556. }
  1557. // 开始时间/到期时间
  1558. if k == 27 {
  1559. // 转换失败可能是因为格式为Excel日期格式, 读取出来会是一串数字, 将其转换成日期字符串再处理
  1560. va := cell.Value
  1561. if va == "" {
  1562. resp.Fail(fmt.Sprintf("第%d行开始时间不可为空, 请按模板导入", i+1), c)
  1563. return
  1564. }
  1565. startDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1566. if e != nil {
  1567. d := utils.ConvertToFormatDay(va, "2006/01/02")
  1568. startDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1569. if e != nil {
  1570. resp.Fail(fmt.Sprintf("第%d行开始时间格式转换有误, 请按模板导入", i+1), c)
  1571. return
  1572. }
  1573. }
  1574. // 转换后的日期小于1900-01-01表示当前生成的日期是有问题的
  1575. if startDate.Before(checkDate) {
  1576. resp.Fail(fmt.Sprintf("第%d行开始时间格式有误, 请按模板导入", i+1), c)
  1577. return
  1578. }
  1579. rowRegister.StartDate = startDate
  1580. continue
  1581. }
  1582. if k == 28 {
  1583. va := cell.Value
  1584. if va == "" {
  1585. resp.Fail(fmt.Sprintf("第%d行到期时间不可为空, 请按模板导入", i+1), c)
  1586. return
  1587. }
  1588. endDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1589. if e != nil {
  1590. d := utils.ConvertToFormatDay(va, "2006/01/02")
  1591. endDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1592. if e != nil {
  1593. resp.Fail(fmt.Sprintf("第%d行到期时间格式转换有误, 请按模板导入", i+1), c)
  1594. return
  1595. }
  1596. }
  1597. if endDate.Before(checkDate) {
  1598. resp.Fail(fmt.Sprintf("第%d行到期时间格式有误, 请按模板导入", i+1), c)
  1599. return
  1600. }
  1601. rowRegister.EndDate = endDate
  1602. continue
  1603. }
  1604. // 合同金额
  1605. if k == 29 {
  1606. amountStr := v
  1607. amount, e := strconv.ParseFloat(amountStr, 64)
  1608. if e != nil {
  1609. resp.Fail(fmt.Sprintf("第%d行合同金额有误, 请按模板导入", i+1), c)
  1610. return
  1611. }
  1612. rowRegister.ContractAmount = amount
  1613. continue
  1614. }
  1615. // 约定付款日期
  1616. if k == 30 {
  1617. rowRegister.AgreedPayTime = v
  1618. continue
  1619. }
  1620. // 签订日
  1621. // k == 32为签订月,可忽略
  1622. if k == 31 {
  1623. va := cell.Value
  1624. if va == "" {
  1625. continue
  1626. }
  1627. signDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1628. if e != nil {
  1629. d := utils.ConvertToFormatDay(va, "2006/01/02")
  1630. signDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1631. if e != nil {
  1632. resp.Fail(fmt.Sprintf("第%d行签订日格式转换有误, 请按模板导入", i+1), c)
  1633. return
  1634. }
  1635. }
  1636. if signDate.Before(checkDate) {
  1637. resp.Fail(fmt.Sprintf("第%d行签订日格式有误, 请按模板导入", i+1), c)
  1638. return
  1639. }
  1640. rowRegister.SignDate = signDate
  1641. continue
  1642. }
  1643. // 合同状态
  1644. if k == 33 {
  1645. rowRegister.ContractStatus = fms.ContractStatusNameKeyMap[v]
  1646. if rowRegister.ContractStatus == 0 {
  1647. resp.Fail(fmt.Sprintf("第%d行合同状态不匹配, 请按模板导入", i+1), c)
  1648. return
  1649. }
  1650. continue
  1651. }
  1652. // 合同编号
  1653. if k == 34 {
  1654. rowContractCode := v
  1655. if rowContractCode == "" {
  1656. resp.Fail(fmt.Sprintf("第%d行合同编号不可为空, 请按模板导入", i+1), c)
  1657. return
  1658. }
  1659. if utils.InArrayByStr(contractCodeArr, rowContractCode) {
  1660. // 此合同已登记, 跳过本行的读取
  1661. isSkip = true
  1662. break
  1663. }
  1664. rowRegister.ContractCode = rowContractCode
  1665. continue
  1666. }
  1667. // 备注
  1668. if k == 35 {
  1669. rowRegister.Remark = v
  1670. continue
  1671. }
  1672. // 开票日/开票金额
  1673. if k == 36 {
  1674. if v != "" {
  1675. va := cell.Value
  1676. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1677. if e != nil {
  1678. d := utils.ConvertToFormatDay(va, "2006/01/02")
  1679. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1680. if e != nil {
  1681. resp.Fail(fmt.Sprintf("第%d行开票时间1格式转换有误, 请按模板导入", i+1), c)
  1682. return
  1683. }
  1684. }
  1685. if invoiceDate.Before(checkDate) {
  1686. resp.Fail(fmt.Sprintf("第%d行开票时间1格式有误, 请按模板导入", i+1), c)
  1687. return
  1688. }
  1689. rowInvoice1.InvoiceDate = invoiceDate
  1690. rowInvoice1.ContractCode = rowRegister.ContractCode
  1691. rowInvoice1.InvoiceType = fms.ContractInvoiceTypeMake
  1692. }
  1693. continue
  1694. }
  1695. if k == 37 && v != "" {
  1696. amountStr := v
  1697. amount, e := strconv.ParseFloat(amountStr, 64)
  1698. if e != nil {
  1699. resp.Fail(fmt.Sprintf("第%d行开票金额1有误, 请按模板导入", i+1), c)
  1700. return
  1701. }
  1702. rowInvoice1.Amount = amount
  1703. rowInvoices = append(rowInvoices, rowInvoice1)
  1704. continue
  1705. }
  1706. if k == 38 {
  1707. if v != "" {
  1708. va := cell.Value
  1709. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1710. if e != nil {
  1711. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1712. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1713. if e != nil {
  1714. resp.Fail(fmt.Sprintf("第%d行开票时间2格式转换有误, 请按模板导入", i+1), c)
  1715. return
  1716. }
  1717. }
  1718. if invoiceDate.Before(checkDate) {
  1719. resp.Fail(fmt.Sprintf("第%d行开票时间2格式有误, 请按模板导入", i+1), c)
  1720. return
  1721. }
  1722. rowInvoice2.InvoiceDate = invoiceDate
  1723. rowInvoice2.ContractCode = rowRegister.ContractCode
  1724. rowInvoice2.InvoiceType = fms.ContractInvoiceTypeMake
  1725. }
  1726. continue
  1727. }
  1728. if k == 39 && v != "" {
  1729. amountStr := v
  1730. amount, e := strconv.ParseFloat(amountStr, 64)
  1731. if e != nil {
  1732. resp.Fail(fmt.Sprintf("第%d行开票金额2有误, 请按模板导入", i+1), c)
  1733. return
  1734. }
  1735. rowInvoice2.Amount = amount
  1736. rowInvoices = append(rowInvoices, rowInvoice2)
  1737. continue
  1738. }
  1739. if k == 40 {
  1740. if v != "" {
  1741. va := cell.Value
  1742. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1743. if e != nil {
  1744. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1745. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1746. if e != nil {
  1747. resp.Fail(fmt.Sprintf("第%d行开票时间3格式转换有误, 请按模板导入", i+1), c)
  1748. return
  1749. }
  1750. }
  1751. if invoiceDate.Before(checkDate) {
  1752. resp.Fail(fmt.Sprintf("第%d行开票时间3格式有误, 请按模板导入", i+1), c)
  1753. return
  1754. }
  1755. rowInvoice3.InvoiceDate = invoiceDate
  1756. rowInvoice3.ContractCode = rowRegister.ContractCode
  1757. rowInvoice3.InvoiceType = fms.ContractInvoiceTypeMake
  1758. }
  1759. continue
  1760. }
  1761. if k == 41 && v != "" {
  1762. amountStr := v
  1763. amount, e := strconv.ParseFloat(amountStr, 64)
  1764. if e != nil {
  1765. resp.Fail(fmt.Sprintf("第%d行开票金额3有误, 请按模板导入", i+1), c)
  1766. return
  1767. }
  1768. rowInvoice3.Amount = amount
  1769. rowInvoices = append(rowInvoices, rowInvoice3)
  1770. continue
  1771. }
  1772. // 收款日/收款金额
  1773. if k == 42 {
  1774. if v != "" {
  1775. va := cell.Value
  1776. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1777. if e != nil {
  1778. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1779. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1780. if e != nil {
  1781. resp.Fail(fmt.Sprintf("第%d行收款时间1格式转换有误, 请按模板导入", i+1), c)
  1782. return
  1783. }
  1784. }
  1785. if invoiceDate.Before(checkDate) {
  1786. resp.Fail(fmt.Sprintf("第%d行收款时间1格式有误, 请按模板导入", i+1), c)
  1787. return
  1788. }
  1789. rowInvoice4.InvoiceDate = invoiceDate
  1790. rowInvoice4.ContractCode = rowRegister.ContractCode
  1791. rowInvoice4.InvoiceType = fms.ContractInvoiceTypePay
  1792. }
  1793. continue
  1794. }
  1795. if k == 43 && v != "" {
  1796. amountStr := v
  1797. amount, e := strconv.ParseFloat(amountStr, 64)
  1798. if e != nil {
  1799. resp.Fail(fmt.Sprintf("第%d行收款金额1有误, 请按模板导入", i+1), c)
  1800. return
  1801. }
  1802. rowInvoice4.Amount = amount
  1803. rowInvoices = append(rowInvoices, rowInvoice4)
  1804. continue
  1805. }
  1806. if k == 44 {
  1807. if v != "" {
  1808. va := cell.Value
  1809. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1810. if e != nil {
  1811. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1812. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1813. if e != nil {
  1814. resp.Fail(fmt.Sprintf("第%d行收款时间2格式转换有误, 请按模板导入", i+1), c)
  1815. return
  1816. }
  1817. }
  1818. if invoiceDate.Before(checkDate) {
  1819. resp.Fail(fmt.Sprintf("第%d行收款时间2格式有误, 请按模板导入", i+1), c)
  1820. return
  1821. }
  1822. rowInvoice5.InvoiceDate = invoiceDate
  1823. rowInvoice5.ContractCode = rowRegister.ContractCode
  1824. rowInvoice5.InvoiceType = fms.ContractInvoiceTypePay
  1825. }
  1826. continue
  1827. }
  1828. if k == 45 && v != "" {
  1829. amountStr := v
  1830. amount, e := strconv.ParseFloat(amountStr, 64)
  1831. if e != nil {
  1832. resp.Fail(fmt.Sprintf("第%d行收款金额2有误, 请按模板导入", i+1), c)
  1833. return
  1834. }
  1835. rowInvoice5.Amount = amount
  1836. rowInvoices = append(rowInvoices, rowInvoice5)
  1837. continue
  1838. }
  1839. if k == 46 {
  1840. if v != "" {
  1841. va := cell.Value
  1842. invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
  1843. if e != nil {
  1844. d := utils.ConvertToFormatDay(va, "2006/01/02") //录入日期
  1845. invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
  1846. if e != nil {
  1847. resp.Fail(fmt.Sprintf("第%d行收款时间3格式转换有误, 请按模板导入", i+1), c)
  1848. return
  1849. }
  1850. }
  1851. if invoiceDate.Before(checkDate) {
  1852. resp.Fail(fmt.Sprintf("第%d行收款时间3格式有误, 请按模板导入", i+1), c)
  1853. return
  1854. }
  1855. rowInvoice6.InvoiceDate = invoiceDate
  1856. rowInvoice6.ContractCode = rowRegister.ContractCode
  1857. rowInvoice6.InvoiceType = fms.ContractInvoiceTypePay
  1858. }
  1859. continue
  1860. }
  1861. if k == 47 && v != "" {
  1862. amountStr := v
  1863. amount, e := strconv.ParseFloat(amountStr, 64)
  1864. if e != nil {
  1865. resp.Fail(fmt.Sprintf("第%d行收款金额3有误, 请按模板导入", i+1), c)
  1866. return
  1867. }
  1868. rowInvoice6.Amount = amount
  1869. rowInvoices = append(rowInvoices, rowInvoice6)
  1870. continue
  1871. }
  1872. }
  1873. if isSkip {
  1874. continue
  1875. }
  1876. // 小套餐
  1877. if len(rowChartPermissionIdArr) > 0 {
  1878. // 说明有小套餐
  1879. tempItem := serviceTempNameMap["FICC小套餐"]
  1880. if tempItem == nil {
  1881. resp.Fail(fmt.Sprintf("第%d行小套餐名称不匹配, 请按模板导入", i+1), c)
  1882. return
  1883. }
  1884. rowChartPermissionIds := strings.Join(rowChartPermissionIdArr, ",")
  1885. cs := &fms.ContractService{
  1886. ProductId: crm.CompanyProductFicc,
  1887. ServiceTemplateId: tempItem.ServiceTemplateId,
  1888. Title: tempItem.Title,
  1889. Value: tempItem.Value,
  1890. TableValue: tempItem.TableValue,
  1891. ChartPermissionId: tempItem.ChartPermissionId,
  1892. ChartPermissionIds: rowChartPermissionIds,
  1893. }
  1894. cs.Set()
  1895. rowServices = append(rowServices, cs)
  1896. }
  1897. // 如果导入的最后一条合同编号为空并且合同编号之后的字段均为空
  1898. // excel这个包读行的时候不会再往后面没数据的地方读取, 所以此处需要重新判断一次
  1899. if rowRegister.ContractCode == "" {
  1900. resp.Fail(fmt.Sprintf("第%d行合同编号为空", i+1), c)
  1901. return
  1902. }
  1903. rowRegister.RegisterStatus = fms.ContractRegisterStatusIng
  1904. // 新增登记、套餐、开票到款信息
  1905. newId, e := fms.CreateImportContractRegister(rowRegister, rowServices, rowInvoices)
  1906. if e != nil {
  1907. resp.FailData(fmt.Sprintf("第%d行导入失败", i+1), "新增导入登记失败, Err: "+e.Error(), c)
  1908. return
  1909. }
  1910. newIds = append(newIds, newId)
  1911. }
  1912. }
  1913. }
  1914. // 更新登记状态
  1915. go func() {
  1916. for i := range newIds {
  1917. fmsService.CheckContractRegisterAmount(newIds[i])
  1918. }
  1919. }()
  1920. resp.Ok("操作成功", c)
  1921. }