oilcatalog.go 11 KB

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