oilcatalog.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. package oilsupplier
  2. import (
  3. "dashoo.cn/backend/api/business/oilsupplier/oilcatalog"
  4. "dashoo.cn/backend/api/business/organize"
  5. . "dashoo.cn/backend/api/controllers"
  6. "dashoo.cn/business2/permission"
  7. "dashoo.cn/utils"
  8. "encoding/json"
  9. "fmt"
  10. . "github.com/linxGnu/goseaweedfs"
  11. "github.com/tealeg/xlsx"
  12. "log"
  13. "os"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type OilCatalogController struct {
  19. BaseController
  20. }
  21. // @Title 获取列表
  22. // @Description get user by token
  23. // @Success 200 {object} []suppliercert.OilSupplierCert
  24. // @router /list [get]
  25. func (this *OilCatalogController) GetEntityList() {
  26. //获取分页信息
  27. page := this.GetPageInfoForm()
  28. where := " 1=1 "
  29. orderBy := "Id"
  30. asc := false
  31. Order := this.GetString("Order")
  32. Prop := this.GetString("Prop")
  33. if Order != "" && Prop != "" {
  34. orderBy = Prop
  35. if Order == "asc" {
  36. asc = true
  37. }
  38. }
  39. SubId := this.GetString("SubId")
  40. CompanyName := this.GetString("CompanyName")
  41. Business := this.GetString("Business")
  42. catalogType := this.GetString("CatalogType")
  43. Status := this.GetString("Status")
  44. CreateOn := this.GetString("CreateOn")
  45. Year := this.GetString("Year")
  46. if SubId != "" {
  47. where = where + " and SubId=" + SubId
  48. }
  49. if catalogType != "" {
  50. where = where + " and CatalogType=" + catalogType
  51. }
  52. if CompanyName != "" {
  53. where = where + " and CompanyName like '%" + CompanyName + "%' "
  54. }
  55. if Business != "" {
  56. where = where + " and Business like '%" + Business + "%' "
  57. }
  58. if Status != "" {
  59. where = where + " and Status = '" + Status + "'"
  60. }
  61. if Year != "" {
  62. where = where + " and CreateOn >= '" + Year + "-01-01 00:00:00' and CreateOn <='" + Year + "-12-31 00:00:00'"
  63. }
  64. if CreateOn != "" {
  65. dates := strings.Split(CreateOn, ",")
  66. if len(dates) == 2 {
  67. minDate := dates[0]
  68. maxDate := dates[1]
  69. where = where + " and ValidityTo>='" + minDate + "' and ValidityTo<='" + maxDate + "'"
  70. }
  71. }
  72. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  73. var list []oilcatalog.OilCatalog
  74. total := svc.GetPagingEntitiesWithOrderBytbl("", page.CurrentPage, page.Size, orderBy, asc, &list, where)
  75. var datainfo DataInfo
  76. datainfo.Items = list
  77. datainfo.CurrentItemCount = total
  78. datainfo.PageIndex = page.CurrentPage
  79. datainfo.ItemsPerPage = page.Size
  80. this.Data["json"] = &datainfo
  81. this.ServeJSON()
  82. }
  83. // @Title 添加
  84. // @Description 新增
  85. // @Success 200 {object} controllers.Request
  86. // @router /add [post]
  87. func (this *OilCatalogController) AddEntity() {
  88. var model oilcatalog.OilCatalog
  89. var jsonBlob = this.Ctx.Input.RequestBody
  90. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  91. json.Unmarshal(jsonBlob, &model)
  92. // model.Status = "0" // 由前端分不同页面(企管法规处2和普通用户2)传值创建
  93. model.CreateOn = time.Now()
  94. model.CreateBy = this.User.Realname
  95. model.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  96. _, err := svc.InsertEntityBytbl(OilCatalogName, &model)
  97. var errinfo ErrorDataInfo
  98. if err == nil {
  99. //新增
  100. errinfo.Message = "添加成功!"
  101. errinfo.Code = 0
  102. errinfo.Item = model.Id
  103. this.Data["json"] = &errinfo
  104. this.ServeJSON()
  105. } else {
  106. errinfo.Message = "添加失败!" + utils.AlertProcess(err.Error())
  107. errinfo.Code = -1
  108. this.Data["json"] = &errinfo
  109. this.ServeJSON()
  110. }
  111. }
  112. // @Title 删除单条信息
  113. // @Description
  114. // @Success 200 {object} ErrorInfo
  115. // @Failure 403 :id 为空
  116. // @router /delete/:Id [delete]
  117. func (this *OilCatalogController) DeleteEntity() {
  118. Id := this.Ctx.Input.Param(":Id")
  119. var errinfo ErrorInfo
  120. if Id == "" {
  121. errinfo.Message = "操作失败!请求信息不完整"
  122. errinfo.Code = -2
  123. this.Data["json"] = &errinfo
  124. this.ServeJSON()
  125. return
  126. }
  127. var model oilcatalog.OilCatalog
  128. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  129. err := svc.DeleteEntityById(Id, &model)
  130. if err == nil {
  131. errinfo.Message = "删除成功"
  132. errinfo.Code = 0
  133. this.Data["json"] = &errinfo
  134. this.ServeJSON()
  135. } else {
  136. errinfo.Message = "删除失败!" + utils.AlertProcess(err.Error())
  137. errinfo.Code = -1
  138. this.Data["json"] = &errinfo
  139. this.ServeJSON()
  140. }
  141. }
  142. // @Title 修改实体
  143. // @Description 修改实体
  144. // @Success 200 {object} controllers.Request
  145. // @router /update/:id [post]
  146. func (this *OilCatalogController) UpdateEntity() {
  147. id := this.Ctx.Input.Param(":id")
  148. var errinfo ErrorInfo
  149. if id == "" {
  150. errinfo.Message = "操作失败!请求信息不完整"
  151. errinfo.Code = -2
  152. this.Data["json"] = &errinfo
  153. this.ServeJSON()
  154. return
  155. }
  156. var model oilcatalog.OilCatalog
  157. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  158. var jsonBlob = this.Ctx.Input.RequestBody
  159. json.Unmarshal(jsonBlob, &model)
  160. model.ModifiedOn = time.Now()
  161. model.ModifiedBy = this.User.Realname
  162. model.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int()
  163. cols := []string{
  164. "RegCapital",
  165. "USCCode",
  166. "Address",
  167. "LegalPerson",
  168. "RecordScope",
  169. "IDCode",
  170. "DutyDept",
  171. "Dept",
  172. "CatalogType",
  173. "OrderNo",
  174. "CompanyName",
  175. "Business",
  176. "ValidityFrom",
  177. "ValidityTo",
  178. "Remark",
  179. }
  180. err := svc.UpdateEntityBytbl(OilCatalogName, id, &model, cols)
  181. if err == nil {
  182. errinfo.Message = "修改成功!"
  183. errinfo.Code = 0
  184. this.Data["json"] = &errinfo
  185. this.ServeJSON()
  186. } else {
  187. errinfo.Message = "修改失败!" + utils.AlertProcess(err.Error())
  188. errinfo.Code = -1
  189. this.Data["json"] = &errinfo
  190. this.ServeJSON()
  191. }
  192. }
  193. // @Title get 导出ex
  194. // @Description get SampleType by token
  195. // @Success 200 {object} sampletype.SampleType
  196. // @router /exportexcelall [get]
  197. func (this *OilCatalogController) ExportExcelAll() {
  198. CardTitle := this.GetString("CardTitle")
  199. CatalogType := this.GetString("CatalogType")
  200. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  201. var list []oilcatalog.OilCatalog
  202. where := "CatalogType=" + CatalogType
  203. svc.GetEntities(&list, where)
  204. fileTitle := "大港油田" + CardTitle + "目录"
  205. showColumnArr := "序号,企业名称,业务范围,有效期起,有效期止,备注"
  206. f := xlsx.NewFile()
  207. sheet, _ := f.AddSheet(fileTitle)
  208. cellName := strings.Split(showColumnArr, ",")
  209. row := sheet.AddRow()
  210. row.WriteSlice(&cellName, -1)
  211. for idx, item := range list {
  212. dataString := strconv.Itoa(idx+1) + "," + item.CompanyName + "," + svc.TrimSpaceForString(item.Business) + "," + utils.ToStr(item.ValidityFrom.Format("2006-01-02")) + "," +
  213. utils.ToStr(item.ValidityTo.Format("2006-01-02")) + "," + item.Remark
  214. cellName := strings.Split(dataString, ",")
  215. row := sheet.AddRow()
  216. row.WriteSlice(&cellName, -1)
  217. }
  218. for c, cl := 0, len(sheet.Cols); c < cl; c++ {
  219. sheet.Cols[c].Width = 20
  220. }
  221. dir := "static/file/excel/report/" + this.GetAccode()
  222. SaveDirectory(dir)
  223. path := dir + "/" + utils.TimeFormat(time.Now(), "20060102") + fileTitle + ".xlsx"
  224. f.Save(path)
  225. var sw *Seaweed
  226. var filer []string
  227. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  228. filer = []string{_filer}
  229. }
  230. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  231. _, _, fID, _ := sw.UploadFile(path, "", "")
  232. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  233. os.Remove(path)
  234. fmt.Println("==retDocWatermarkUrl==", retDocUrl)
  235. this.Data["json"] = retDocUrl
  236. this.ServeJSON()
  237. }
  238. // @Title get 导出ex --收入业务
  239. // @Description get SampleType by token
  240. // @Success 200 {object} sampletype.SampleType
  241. // @router /exportexcelincome [get]
  242. func (this *OilCatalogController) ExportExcelIncome() {
  243. //CardTitle := this.GetString("CardTitle")
  244. CatalogType := this.GetString("CatalogType")
  245. where := "CatalogType=" + CatalogType
  246. orderBy := "Id"
  247. asc := false
  248. Order := this.GetString("Order")
  249. Prop := this.GetString("Prop")
  250. if Order != "" && Prop != "" {
  251. orderBy = Prop
  252. if Order == "asc" {
  253. asc = true
  254. }
  255. }
  256. SubId := this.GetString("SubId")
  257. CompanyName := this.GetString("CompanyName")
  258. Business := this.GetString("Business")
  259. Status := this.GetString("Status")
  260. CreateOn := this.GetString("CreateOn")
  261. Year := this.GetString("Year")
  262. if SubId != "" {
  263. where = where + " and SubId=" + SubId
  264. }
  265. if CompanyName != "" {
  266. where = where + " and CompanyName like '%" + CompanyName + "%' "
  267. }
  268. if Business != "" {
  269. where = where + " and Business like '%" + Business + "%' "
  270. }
  271. if Status != "" {
  272. where = where + " and Status = '" + Status + "'"
  273. }
  274. if Year != "" {
  275. where = where + " and CreateOn >= '" + Year + "-01-01 00:00:00' and CreateOn <='" + Year + "-12-31 00:00:00'"
  276. }
  277. if CreateOn != "" {
  278. dates := strings.Split(CreateOn, ",")
  279. if len(dates) == 2 {
  280. minDate := dates[0]
  281. maxDate := dates[1]
  282. where = where + " and ValidityTo>='" + minDate + "' and ValidityTo<='" + maxDate + "'"
  283. }
  284. }
  285. var list []oilcatalog.OilCatalog
  286. sql := `select * from OilCatalog
  287. where ` + where + ` order by ` + orderBy
  288. if asc {
  289. sql = sql + " asc "
  290. } else {
  291. sql = sql + " desc "
  292. }
  293. utils.DBE.Sql(sql).Find(&list)
  294. fileTitle := "大港油田收入业务目录"
  295. showColumnArr := "企业名称,法人姓名,法人身份证号,企业注册地址,全国统一信用代码,注册资本,业务范围,申报单位,有效期,备注"
  296. f := xlsx.NewFile()
  297. sheet, _ := f.AddSheet(fileTitle)
  298. cellName := strings.Split(showColumnArr, ",")
  299. row := sheet.AddRow()
  300. row.WriteSlice(&cellName, -1)
  301. for _, item := range list {
  302. var dataArr []string
  303. dataArr = append(dataArr, item.CompanyName)
  304. dataArr = append(dataArr, item.LegalPerson)
  305. dataArr = append(dataArr, item.IDCode)
  306. dataArr = append(dataArr, item.Address)
  307. dataArr = append(dataArr, item.USCCode)
  308. dataArr = append(dataArr, strconv.FormatFloat(item.RegCapital, 'f', 2, 64))
  309. dataArr = append(dataArr, item.Business)
  310. dataArr = append(dataArr, item.Dept)
  311. dataArr = append(dataArr, item.ValidityTo.Format("2006-01-02 15:04:05"))
  312. dataArr = append(dataArr, item.Remark)
  313. row := sheet.AddRow()
  314. row.WriteSlice(&dataArr, -1)
  315. }
  316. for c, cl := 0, len(sheet.Cols); c < cl; c++ {
  317. sheet.Cols[c].Width = 20
  318. }
  319. dir := "static/file/excel/report/" + this.GetAccode()
  320. SaveDirectory(dir)
  321. path := dir + "/" + utils.TimeFormat(time.Now(), "20060102") + fileTitle + ".xlsx"
  322. f.Save(path)
  323. var sw *Seaweed
  324. var filer []string
  325. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  326. filer = []string{_filer}
  327. }
  328. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  329. _, _, fID, _ := sw.UploadFile(path, "", "")
  330. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  331. os.Remove(path)
  332. fmt.Println("==retDocWatermarkUrl==", retDocUrl)
  333. this.Data["json"] = retDocUrl
  334. this.ServeJSON()
  335. }
  336. // @Title
  337. // @Description
  338. // @Success 200 {object} controllers.Request
  339. // @router /isaccess [get]
  340. func (this *OilCatalogController) IsAccess() {
  341. roleId := this.GetString("RoleId")
  342. svcPerm := permission.GetPermissionService(utils.DBE)
  343. svc := organize.GetOrganizeService(utils.DBE)
  344. res := false
  345. if !svcPerm.IsAdmin(this.User.Id) {
  346. res = svc.UserInRoleById(this.User.Id, roleId)
  347. } else {
  348. res = true
  349. }
  350. this.Data["json"] = res
  351. this.ServeJSON()
  352. }
  353. // @Title get 导入excel
  354. // @Description get SampleType by token
  355. // @Success 200 {object} sampletype.SampleType
  356. // @router /importexcel [get]
  357. func (this *OilCatalogController) ImportExcel() {
  358. t := time.Now()
  359. url := this.GetString("ExcelUrl")
  360. CatalogType := this.GetString("CatalogType")
  361. var errorinfo ErrorInfo
  362. if url == "" {
  363. errorinfo.Code = -2
  364. errorinfo.Message = "导入失败,文件未上传成功"
  365. this.Data["json"] = &errorinfo
  366. this.ServeJSON()
  367. }
  368. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  369. log.Printf("url:==" + url) // http://60.30.245.229//upfile/dc1/2,063156edd288
  370. extranetIP := utils.Cfg.MustValue("server", "extranetIP")
  371. localIP := utils.Cfg.MustValue("server", "localIP")
  372. if strings.Index(url, extranetIP) >= 0 {
  373. url = strings.Replace(url, extranetIP, localIP, 1)
  374. }
  375. _dir := utils.Cfg.MustValue("file", "tmplateDir") + "xlsx"
  376. filename := strconv.Itoa(int(time.Now().Unix())) + ".xlsx"
  377. utils.DownloadFile(url, filename, _dir)
  378. filePath := utils.Cfg.MustValue("file", "tmplateDir") + "xlsx/" + filename
  379. xlFile, err := xlsx.OpenFile(filePath)
  380. //excelFileName := "F:/物资类项目与资质对照表-2017.xlsx"
  381. if err != nil {
  382. fmt.Printf("open failed: %s\n", err)
  383. }
  384. var sheet = xlFile.Sheets[0]
  385. errLineNo := ""
  386. for i := 1; i < len(sheet.Rows); i++ {
  387. cells := sheet.Rows[i].Cells
  388. if len(cells) != 0 {
  389. if cells[1].Value != "" {
  390. ValidityFrom, err1 := time.Parse("2006-01-02", cells[3].Value)
  391. ValidityTo, err2 := time.Parse("2006-01-02", cells[4].Value)
  392. CompanyName := cells[1].Value
  393. Business := cells[2].Value
  394. Remark := cells[5].Value
  395. if err1 != nil || err2 != nil {
  396. errLineNo = errLineNo + "," + strconv.Itoa(i)
  397. continue
  398. }
  399. var model oilcatalog.OilCatalog
  400. model.CatalogType, _ = strconv.Atoi(CatalogType)
  401. model.CompanyName = CompanyName
  402. model.Business = Business
  403. model.ValidityFrom = ValidityFrom
  404. model.ValidityTo = ValidityTo
  405. model.Remark = Remark
  406. where := "CatalogType=" + CatalogType + " and CompanyName='" + CompanyName + "'"
  407. has := svc.GetEntity(&model, where)
  408. if !has {
  409. svc.InsertEntity(&model)
  410. }
  411. }
  412. }
  413. }
  414. os.Remove(filePath)
  415. elapsed := time.Since(t)
  416. fmt.Println(elapsed)
  417. if len(errLineNo) > 0 {
  418. errorinfo.Code = -1
  419. errorinfo.Message = "导入完成!错误行号:" + errLineNo
  420. this.Data["json"] = &errorinfo
  421. this.ServeJSON()
  422. } else {
  423. errorinfo.Code = 0
  424. errorinfo.Message = "导入完成!"
  425. this.Data["json"] = &errorinfo
  426. this.ServeJSON()
  427. }
  428. }