mysteel_chemical.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "strings"
  12. "time"
  13. "github.com/rdlucklib/rdluck_tools/http"
  14. )
  15. // AddMysteelChemicalClassify 添加钢联化工分类
  16. func AddMysteelChemicalClassify(classifyName string, parentId, level, sysUserId int, sysUserName, lange string) (classifyInfo *data_manage.BaseFromMysteelChemicalClassify, err error, errMsg string) {
  17. // 校验分类名称相同的数量
  18. {
  19. var count int
  20. switch lange {
  21. case utils.EnLangVersion:
  22. count, err = data_manage.GetBaseFromMysteelChemicalClassifyEnCount(classifyName, parentId)
  23. default:
  24. count, err = data_manage.GetBaseFromMysteelChemicalClassifyCount(classifyName, parentId)
  25. }
  26. if err != nil {
  27. errMsg = `判断名称是否已存在失败`
  28. return
  29. }
  30. if count > 0 {
  31. errMsg = `分类名称:` + classifyName + ` 已存在,请重新输入`
  32. err = errors.New(errMsg)
  33. return
  34. }
  35. }
  36. //获取该层级下最大的排序数
  37. maxSort, err := data_manage.GetBaseFromMysteelChemicalClassifyMaxSort(parentId)
  38. classifyInfo = &data_manage.BaseFromMysteelChemicalClassify{
  39. //BaseFromMysteelChemicalClassifyId: 0,
  40. ClassifyName: classifyName,
  41. ClassifyNameEn: classifyName,
  42. ParentId: parentId,
  43. SysUserId: sysUserId,
  44. SysUserRealName: sysUserName,
  45. Sort: maxSort + 1,
  46. Level: level + 1,
  47. ModifyTime: time.Now(),
  48. CreateTime: time.Now(),
  49. }
  50. classifyId, err := data_manage.AddBaseFromMysteelChemicalClassify(classifyInfo)
  51. if err != nil {
  52. errMsg = "保存分类失败"
  53. }
  54. classifyInfo.BaseFromMysteelChemicalClassifyId = int(classifyId)
  55. return
  56. }
  57. // EditMysteelChemicalClassify 编辑钢联化工分类
  58. func EditMysteelChemicalClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
  59. item, err := data_manage.GetBaseFromMysteelChemicalClassifyById(classifyId)
  60. if err != nil {
  61. errMsg = `保存失败`
  62. return
  63. }
  64. // 操作权限校验
  65. button := GetMysteelChemicalOpButton(sysUser, item.SysUserId)
  66. if !button.OpButton {
  67. errMsg = "无权限操作"
  68. return
  69. }
  70. switch lang {
  71. case utils.EnLangVersion:
  72. // 名字相同,那么就直接返回
  73. if item.ClassifyNameEn == classifyName {
  74. return
  75. }
  76. // 判断名称是否已存在
  77. count, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyEnCount(classifyName, item.ParentId)
  78. if tmpErr != nil {
  79. err = tmpErr
  80. errMsg = "判断名称是否已存在失败"
  81. return
  82. }
  83. if count > 0 {
  84. errMsg = "分类名称已存在,请重新输入"
  85. err = errors.New(errMsg)
  86. return
  87. }
  88. // 修改数据
  89. err = data_manage.EditBaseFromMysteelChemicalClassifyEn(classifyId, classifyName)
  90. if err != nil {
  91. errMsg = "保存失败"
  92. }
  93. default:
  94. // 名字相同,那么就直接返回
  95. if item.ClassifyName == classifyName {
  96. return
  97. }
  98. // 判断名称是否已存在
  99. count, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyCount(classifyName, item.ParentId)
  100. if tmpErr != nil {
  101. err = tmpErr
  102. errMsg = "判断名称是否已存在失败"
  103. return
  104. }
  105. if count > 0 {
  106. errMsg = "分类名称已存在,请重新输入"
  107. err = errors.New(errMsg)
  108. return
  109. }
  110. // 修改数据
  111. err = data_manage.EditBaseFromMysteelChemicalClassify(classifyId, classifyName)
  112. if err != nil {
  113. errMsg = "保存失败"
  114. }
  115. }
  116. return
  117. }
  118. // MoveMysteelChemicalClassify 移动钢联化工分类
  119. func MoveMysteelChemicalClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int, sysUser *system.Admin) (err error, errMsg string) {
  120. //判断分类是否存在
  121. classifyInfo, err := data_manage.GetBaseFromMysteelChemicalClassifyById(classifyId)
  122. if err != nil {
  123. errMsg = "移动失败"
  124. err = errors.New("获取分类信息失败,Err:" + err.Error())
  125. return
  126. }
  127. // 操作权限校验
  128. button := GetMysteelChemicalOpButton(sysUser, classifyInfo.SysUserId)
  129. if !button.MoveButton {
  130. errMsg = "无权限操作"
  131. return
  132. }
  133. updateCol := make([]string, 0)
  134. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  135. if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  136. parentClassifyInfo, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyById(parentClassifyId)
  137. if tmpErr != nil {
  138. errMsg = "移动失败"
  139. err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
  140. return
  141. }
  142. classifyInfo.ParentId = parentClassifyInfo.BaseFromMysteelChemicalClassifyId
  143. classifyInfo.Level = parentClassifyInfo.Level + 1
  144. classifyInfo.ModifyTime = time.Now()
  145. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  146. }
  147. //如果有传入 上一个兄弟节点分类id
  148. if prevClassifyId > 0 {
  149. prevClassify, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyById(prevClassifyId)
  150. if tmpErr != nil {
  151. errMsg = "移动失败"
  152. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
  153. return
  154. }
  155. //如果是移动在两个兄弟节点之间
  156. if nextClassifyId > 0 {
  157. //下一个兄弟节点
  158. nextClassify, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyById(nextClassifyId)
  159. if tmpErr != nil {
  160. errMsg = "移动失败"
  161. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
  162. return
  163. }
  164. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  165. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == classifyInfo.Sort {
  166. //变更兄弟节点的排序
  167. updateSortStr := `sort + 2`
  168. _ = data_manage.UpdateBaseFromMysteelChemicalClassifySortByClassifyId(parentClassifyId, prevClassify.BaseFromMysteelChemicalClassifyId, prevClassify.Sort, updateSortStr)
  169. } else {
  170. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  171. if nextClassify.Sort-prevClassify.Sort == 1 {
  172. //变更兄弟节点的排序
  173. updateSortStr := `sort + 1`
  174. _ = data_manage.UpdateBaseFromMysteelChemicalClassifySortByClassifyId(parentClassifyId, 0, prevClassify.Sort, updateSortStr)
  175. }
  176. }
  177. }
  178. classifyInfo.Sort = prevClassify.Sort + 1
  179. classifyInfo.ModifyTime = time.Now()
  180. updateCol = append(updateCol, "Sort", "ModifyTime")
  181. } else {
  182. firstClassify, tmpErr := data_manage.GetFirstBaseFromMysteelChemicalClassify()
  183. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  184. errMsg = "移动失败"
  185. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  186. return
  187. }
  188. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  189. if firstClassify != nil && firstClassify.Sort == 0 {
  190. updateSortStr := ` sort + 1 `
  191. _ = data_manage.UpdateBaseFromMysteelChemicalClassifySortByClassifyId(parentClassifyId, firstClassify.BaseFromMysteelChemicalClassifyId-1, 0, updateSortStr)
  192. }
  193. classifyInfo.Sort = 0 //那就是排在第一位
  194. classifyInfo.ModifyTime = time.Now()
  195. updateCol = append(updateCol, "Sort", "ModifyTime")
  196. }
  197. //更新
  198. if len(updateCol) > 0 {
  199. err = classifyInfo.Update(updateCol)
  200. if err != nil {
  201. errMsg = "移动失败"
  202. err = errors.New("修改失败,Err:" + err.Error())
  203. return
  204. }
  205. }
  206. return
  207. }
  208. // DelMysteelChemicalClassify 删除钢联化工分类
  209. func DelMysteelChemicalClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) {
  210. //判断分类是否存在
  211. classifyInfo, err := data_manage.GetBaseFromMysteelChemicalClassifyById(classifyId)
  212. if err != nil {
  213. errMsg = "删除失败"
  214. err = errors.New("获取分类信息失败,Err:" + err.Error())
  215. return
  216. }
  217. // 操作权限校验
  218. button := GetMysteelChemicalOpButton(sysUser, classifyInfo.SysUserId)
  219. if !button.DeleteButton {
  220. errMsg = "无权限操作"
  221. return
  222. }
  223. // 需要删除的分类id集合
  224. classifyIdList := make([]int, 0)
  225. classifyIdList = append(classifyIdList, classifyId)
  226. mysteelChemicalIndexIdList := make([]int, 0)
  227. indexCodeList := make([]string, 0)
  228. // 查找所有的子集id
  229. if classifyInfo.ParentId <= 0 {
  230. childrenClassifyInfoList, tmpErr := data_manage.GetBaseFromMysteelChemicalClassifyByParentId(classifyId)
  231. if tmpErr != nil {
  232. errMsg = "删除失败"
  233. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  234. return
  235. }
  236. for _, v := range childrenClassifyInfoList {
  237. classifyIdList = append(classifyIdList, v.BaseFromMysteelChemicalClassifyId)
  238. }
  239. }
  240. // 获取分类下的所有指标
  241. baseFromMysteelChemicalIndexList, err := data_manage.GetBaseFromMysteelChemicalIndexListByClassifyIdList(classifyIdList)
  242. if err != nil {
  243. errMsg = "删除失败"
  244. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  245. return
  246. }
  247. for _, v := range baseFromMysteelChemicalIndexList {
  248. mysteelChemicalIndexIdList = append(mysteelChemicalIndexIdList, v.BaseFromMysteelChemicalIndexId)
  249. indexCodeList = append(indexCodeList, v.IndexCode)
  250. }
  251. // 获取已经加入到EDB指标库的钢联化工指标
  252. edbInfoList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexCodeList)
  253. if err != nil {
  254. errMsg = "删除失败"
  255. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  256. return
  257. }
  258. if len(edbInfoList) > 0 {
  259. errMsg = "该分类下存在指标加入到ETA图库,无法删除"
  260. return
  261. }
  262. // 删除对应的分类和指标
  263. err = data_manage.DeleteMysteelChemicalByClassifyId(classifyIdList, mysteelChemicalIndexIdList)
  264. if err != nil {
  265. return
  266. }
  267. // 删除指标服务的excel表
  268. go func(list []*data_manage.BaseFromMysteelChemicalIndex) {
  269. for _, v := range list {
  270. CurlDelMysteelChemicalData(v.IndexCode, v.UpdateWeek)
  271. }
  272. }(baseFromMysteelChemicalIndexList)
  273. return
  274. }
  275. // BatchAddMysteelChemicalIndex 批量添加钢联化工指标
  276. func BatchAddMysteelChemicalIndex(items []*data_manage.BaseFromMysteelChemicalIndex, lang string) (baseFromMysteelChemicalIndexs []*data_manage.BaseFromMysteelChemicalIndex, err error, errMsg string) {
  277. indexCodeList := make([]string, 0)
  278. for _, v := range items {
  279. indexCodeList = append(indexCodeList, v.IndexCode)
  280. }
  281. baseFromMysteelChemicalIndexs, err = data_manage.GetBaseFromMysteelChemicalIndexByCodeList(indexCodeList)
  282. if err != nil && err.Error() != utils.ErrNoRow() {
  283. errMsg = `获取数据失败`
  284. return
  285. }
  286. err = nil
  287. // 过滤已存在的指标
  288. filterMap := make(map[string]struct{})
  289. if len(baseFromMysteelChemicalIndexs) > 0 {
  290. existIndexCodeList := make([]string, 0)
  291. for _, v := range baseFromMysteelChemicalIndexs {
  292. existIndexCodeList = append(existIndexCodeList, v.IndexCode)
  293. filterMap[v.IndexCode] = struct{}{}
  294. }
  295. if lang == utils.EnLangVersion {
  296. errMsg = fmt.Sprintf(`Metric ID: %s already exists, please re-enter.`, strings.Join(existIndexCodeList, ","))
  297. } else {
  298. errMsg = fmt.Sprintf(`指标ID: %s 已存在,请重新输入`, strings.Join(existIndexCodeList, ","))
  299. }
  300. return
  301. }
  302. terminal := getEdbTerminal(utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  303. addItems := make([]*data_manage.BaseFromMysteelChemicalIndex, 0)
  304. preAddIndexCodeItems := make([]string, 0)
  305. for _, v := range items {
  306. if _, ok := filterMap[v.IndexCode]; ok {
  307. continue
  308. }
  309. v.TerminalCode = terminal.TerminalCode
  310. addItems = append(addItems, v)
  311. preAddIndexCodeItems = append(preAddIndexCodeItems, v.IndexCode)
  312. }
  313. _, err = data_manage.BatchAddBaseFromMysteelChemicalIndex(addItems)
  314. if err != nil {
  315. errMsg = "保存分类失败"
  316. return
  317. }
  318. baseFromMysteelChemicalIndexs, err = data_manage.GetBaseFromMysteelChemicalIndexByCodeList(preAddIndexCodeItems)
  319. if err != nil {
  320. errMsg = "获取数据失败"
  321. return
  322. }
  323. return
  324. }
  325. type HandleMysteelChemicalIndexReq struct {
  326. List []*data_manage.BaseFromMysteelChemicalIndex
  327. }
  328. func BatchAddMysteelChemicalData(items []*data_manage.BaseFromMysteelChemicalIndex, lang string) (resp *models.BaseResponse, err error) {
  329. url := utils.EDB_LIB_URL + "mysteel_chemical/handle/api/mysteel/index"
  330. req := new(HandleMysteelChemicalIndexReq)
  331. req.List = items
  332. postData, err := json.Marshal(req)
  333. if err != nil {
  334. return
  335. }
  336. body, err := HttpPost(url, string(postData), lang)
  337. if err != nil {
  338. return
  339. }
  340. if err = json.Unmarshal(body, &resp); err != nil {
  341. return
  342. }
  343. return
  344. }
  345. type MysteelChemicalIndexSource2EdbReq struct {
  346. EdbCode string
  347. EdbName string
  348. Frequency string
  349. Unit string
  350. ClassifyId int
  351. AdminId int
  352. AdminRealName string
  353. }
  354. // MysteelChemicalIndexSource2Edb 新增钢联化工数据源到指标库
  355. func MysteelChemicalIndexSource2Edb(req MysteelChemicalIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  356. if req.EdbCode == "" {
  357. err = fmt.Errorf("指标ID为空")
  358. return
  359. }
  360. defer func() {
  361. if err != nil {
  362. tips := fmt.Sprintf("MysteelChemicalIndexSource2Edb新增失败, Err: %s", err.Error())
  363. fmt.Println(tips)
  364. utils.FileLog.Info(tips)
  365. }
  366. }()
  367. source := utils.DATA_SOURCE_MYSTEEL_CHEMICAL
  368. // 是否新增过指标
  369. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  370. if e != nil && e.Error() != utils.ErrNoRow() {
  371. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  372. return
  373. }
  374. if exist != nil {
  375. skip = true
  376. return
  377. }
  378. // 开始结束时间
  379. var startDate, endDate string
  380. // 新增指标库
  381. edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
  382. if e != nil {
  383. errMsg = msg
  384. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  385. return
  386. }
  387. edb = edbInfo
  388. return
  389. }
  390. // AddMysteelChemicalIndex 添加钢联化工指标
  391. func AddMysteelChemicalIndex(classifyId int, indexCode, updateWeek, updateTimeStr string, sysUserId int, sysUserName, lang string) (baseFromMysteelChemicalIndex *data_manage.BaseFromMysteelChemicalIndex, err error, errMsg string) {
  392. baseFromMysteelChemicalIndex, err = data_manage.GetBaseFromMysteelChemicalIndexByCode(indexCode)
  393. if err != nil && err.Error() != utils.ErrNoRow() {
  394. errMsg = `获取数据失败`
  395. return
  396. }
  397. if baseFromMysteelChemicalIndex != nil {
  398. if lang == utils.EnLangVersion {
  399. errMsg = `Metric ID:` + indexCode + ` already exists, please re-enter.`
  400. } else {
  401. errMsg = `指标ID:` + indexCode + ` 已存在,请重新输入`
  402. }
  403. return
  404. }
  405. // 更新时间切割
  406. var updateTime1, updateTime2 string
  407. if updateTimeStr != `` {
  408. updateTimeList := strings.Split(updateTimeStr, ",")
  409. updateTime1 = updateTimeList[0]
  410. if len(updateTimeList) > 1 {
  411. updateTime2 = updateTimeList[1]
  412. }
  413. }
  414. edbTerminal := getEdbTerminal(utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  415. if edbTerminal == nil {
  416. errMsg = `钢联终端信息未配置`
  417. err = errors.New(errMsg)
  418. return
  419. }
  420. baseFromMysteelChemicalIndex = &data_manage.BaseFromMysteelChemicalIndex{
  421. //BaseFromMysteelChemicalIndexId: 0,
  422. BaseFromMysteelChemicalClassifyId: classifyId,
  423. IndexCode: indexCode,
  424. IndexName: "",
  425. Unit: "",
  426. Source: "",
  427. Frequency: "",
  428. StartDate: "",
  429. EndDate: "",
  430. Describe: "",
  431. UpdateWeek: updateWeek,
  432. UpdateTime: updateTime1,
  433. UpdateTime2: updateTime2,
  434. SysUserId: sysUserId,
  435. SysUserRealName: sysUserName,
  436. ModifyTime: time.Now(),
  437. CreateTime: time.Now(),
  438. TerminalCode: edbTerminal.TerminalCode, // 终端编码
  439. }
  440. _, err = data_manage.AddBaseFromMysteelChemicalIndex(baseFromMysteelChemicalIndex)
  441. if err != nil {
  442. errMsg = "保存分类失败"
  443. }
  444. return
  445. }
  446. // EditMysteelChemicalIndex 编辑钢联化工指标
  447. func EditMysteelChemicalIndex(indexId, classifyId int, updateWeek, updateTimeStr string, sysUser *system.Admin) (baseFromMysteelChemicalIndex *data_manage.BaseFromMysteelChemicalIndex, err error, errMsg string) {
  448. baseFromMysteelChemicalIndex, err = data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId)
  449. if err != nil {
  450. errMsg = `获取数据失败`
  451. if err.Error() == utils.ErrNoRow() {
  452. errMsg = `该指标未入库`
  453. err = nil
  454. }
  455. return
  456. }
  457. // 操作权限校验
  458. button := GetMysteelChemicalOpButton(sysUser, baseFromMysteelChemicalIndex.SysUserId)
  459. if !button.OpButton {
  460. errMsg = "无权限操作"
  461. return
  462. }
  463. oldUpdateWeek := baseFromMysteelChemicalIndex.UpdateWeek
  464. // 更新时间切割
  465. var updateTime1, updateTime2 string
  466. if updateTimeStr != `` {
  467. updateTimeList := strings.Split(updateTimeStr, ",")
  468. updateTime1 = updateTimeList[0]
  469. if len(updateTimeList) > 1 {
  470. updateTime2 = updateTimeList[1]
  471. }
  472. }
  473. baseFromMysteelChemicalIndex.BaseFromMysteelChemicalClassifyId = classifyId
  474. baseFromMysteelChemicalIndex.UpdateWeek = updateWeek
  475. baseFromMysteelChemicalIndex.UpdateTime = updateTime1
  476. baseFromMysteelChemicalIndex.UpdateTime2 = updateTime2
  477. baseFromMysteelChemicalIndex.ModifyTime = time.Now()
  478. err = baseFromMysteelChemicalIndex.Update([]string{"BaseFromMysteelChemicalClassifyId", "UpdateWeek", "UpdateTime", "UpdateTime2", "ModifyTime"})
  479. if err != nil {
  480. errMsg = "保存分类失败"
  481. }
  482. go func(oldUpdateWeek string, indexInfo *data_manage.BaseFromMysteelChemicalIndex) {
  483. // 先删除excel表
  484. CurlDelMysteelChemicalData(indexInfo.IndexCode, oldUpdateWeek)
  485. }(oldUpdateWeek, baseFromMysteelChemicalIndex)
  486. return
  487. }
  488. // DelMysteelChemical 删除钢联化工指标
  489. func DelMysteelChemical(indexId int, sysUser *system.Admin) (err error, errMsg string) {
  490. baseFromMysteelChemicalIndex, err := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId)
  491. if err != nil {
  492. errMsg = `获取数据失败`
  493. if err.Error() == utils.ErrNoRow() {
  494. errMsg = `该指标未入库`
  495. err = nil
  496. }
  497. return
  498. }
  499. // 操作权限校验
  500. button := GetMysteelChemicalOpButton(sysUser, baseFromMysteelChemicalIndex.SysUserId)
  501. if !button.DeleteButton {
  502. errMsg = "无权限操作"
  503. return
  504. }
  505. // 获取已经加入到EDB指标库的钢联化工指标
  506. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, baseFromMysteelChemicalIndex.IndexCode)
  507. if err != nil && err.Error() != utils.ErrNoRow() {
  508. errMsg = "删除失败"
  509. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  510. return
  511. }
  512. if edbInfo != nil {
  513. errMsg = "当前指标已被引用,不可删除"
  514. err = nil
  515. return
  516. }
  517. // 删除对应的分类和指标
  518. err = data_manage.DeleteMysteelChemical(indexId)
  519. if err != nil {
  520. return
  521. }
  522. // 删除指标服务的excel表
  523. go CurlDelMysteelChemicalData(baseFromMysteelChemicalIndex.IndexCode, baseFromMysteelChemicalIndex.UpdateWeek)
  524. return
  525. }
  526. // MoveMysteelChemical 移动钢联化工指标
  527. func MoveMysteelChemical(indexId, classifyId, prevIndexId, nextIndexId int, sysUser *system.Admin) (err error, errMsg string) {
  528. //分类信息
  529. baseFromMysteelChemicalIndex, err := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(indexId)
  530. if err != nil {
  531. errMsg = `获取数据失败`
  532. if err.Error() == utils.ErrNoRow() {
  533. errMsg = `该指标未入库`
  534. err = nil
  535. }
  536. return
  537. }
  538. // 操作权限校验
  539. button := GetMysteelChemicalOpButton(sysUser, baseFromMysteelChemicalIndex.SysUserId)
  540. if !button.MoveButton {
  541. errMsg = "无权限操作"
  542. return
  543. }
  544. //判断分类是否存在
  545. _, err = data_manage.GetBaseFromMysteelChemicalClassifyById(classifyId)
  546. if err != nil {
  547. errMsg = "移动失败"
  548. err = errors.New("获取分类信息失败,Err:" + err.Error())
  549. if err.Error() == utils.ErrNoRow() {
  550. errMsg = "找不到该分类"
  551. err = nil
  552. }
  553. return
  554. }
  555. //如果改变了分类,那么移动该图表数据
  556. if baseFromMysteelChemicalIndex.BaseFromMysteelChemicalClassifyId != classifyId {
  557. tmpErr := data_manage.MoveBaseFromMysteelChemicalIndex(indexId, classifyId)
  558. if err != nil {
  559. errMsg = "移动失败"
  560. err = errors.New("移动失败,Err:" + tmpErr.Error())
  561. return
  562. }
  563. }
  564. //移动排序
  565. updateCol := make([]string, 0)
  566. //如果有传入 上一个兄弟节点分类id
  567. if prevIndexId > 0 {
  568. prevIndex, tmpErr := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(prevIndexId)
  569. if err != nil {
  570. errMsg = "移动失败"
  571. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  572. return
  573. }
  574. //如果是移动在两个兄弟节点之间
  575. if nextIndexId > 0 {
  576. //下一个兄弟节点
  577. nextIndex, tmpErr := data_manage.GetBaseFromMysteelChemicalIndexByIndexId(nextIndexId)
  578. if err != nil {
  579. errMsg = "移动失败"
  580. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  581. return
  582. }
  583. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  584. if prevIndex.Sort == nextIndex.Sort || prevIndex.Sort == baseFromMysteelChemicalIndex.Sort {
  585. //变更兄弟节点的排序
  586. updateSortStr := `sort + 2`
  587. _ = data_manage.UpdateBaseFromMysteelChemicalIndexByClassifyId(prevIndex.BaseFromMysteelChemicalClassifyId, prevIndex.Sort, prevIndex.BaseFromMysteelChemicalIndexId, updateSortStr)
  588. } else {
  589. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  590. if nextIndex.Sort-prevIndex.Sort == 1 {
  591. //变更兄弟节点的排序
  592. updateSortStr := `sort + 1`
  593. _ = data_manage.UpdateBaseFromMysteelChemicalIndexByClassifyId(prevIndex.BaseFromMysteelChemicalClassifyId, prevIndex.Sort, prevIndex.BaseFromMysteelChemicalIndexId, updateSortStr)
  594. }
  595. }
  596. }
  597. baseFromMysteelChemicalIndex.Sort = prevIndex.Sort + 1
  598. baseFromMysteelChemicalIndex.ModifyTime = time.Now()
  599. updateCol = append(updateCol, "Sort", "ModifyTime")
  600. } else {
  601. firstClassify, tmpErr := data_manage.GetFirstBaseFromMysteelChemicalIndexByClassifyId(classifyId)
  602. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  603. errMsg = "移动失败"
  604. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  605. return
  606. }
  607. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  608. if firstClassify != nil && firstClassify.Sort == 0 {
  609. updateSortStr := ` sort + 1 `
  610. _ = data_manage.UpdateBaseFromMysteelChemicalIndexByClassifyId(firstClassify.BaseFromMysteelChemicalClassifyId, 0, firstClassify.BaseFromMysteelChemicalIndexId-1, updateSortStr)
  611. }
  612. baseFromMysteelChemicalIndex.Sort = 0 //那就是排在第一位
  613. baseFromMysteelChemicalIndex.ModifyTime = time.Now()
  614. updateCol = append(updateCol, "Sort", "ModifyTime")
  615. }
  616. //更新
  617. if len(updateCol) > 0 {
  618. err = baseFromMysteelChemicalIndex.Update(updateCol)
  619. if err != nil {
  620. errMsg = "移动失败"
  621. err = errors.New("修改失败,Err:" + err.Error())
  622. return
  623. }
  624. }
  625. if err != nil {
  626. errMsg = "移动失败"
  627. err = errors.New("修改失败,Err:" + err.Error())
  628. return
  629. }
  630. return
  631. }
  632. // GetMysteelChemicalOpButton 获取钢联化工的操作权限
  633. func GetMysteelChemicalOpButton(sysUser *system.Admin, belongUserId int) (button data_manage.BaseFromMysteelChemicalClassifyItemsButton) {
  634. //2、用户对于自己添加的分类,有权限编辑、移动和删除该分类;
  635. //3、ficc管理员和超管对所有分类有编辑、移动和删除权限;
  636. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.MysteelChemicalPermission == 1 {
  637. button.AddButton = true
  638. button.OpButton = true
  639. button.DeleteButton = true
  640. button.MoveButton = true
  641. }
  642. return
  643. }
  644. // CurlDelMysteelChemicalData 请求钢联服务器删除指标
  645. func CurlDelMysteelChemicalData(indexCode, updateWeek string) {
  646. postUrl := utils.MySteelPostUrl + `/api/index/delete`
  647. //type IndexAddReq struct {
  648. // IndexCode string `json:"IndexCode" binding:"required"` //指标编码
  649. // UpdateWeek string `json:"UpdateWeek"` //更新周期
  650. // RunMode string `description:"运行环境:debug:测试(默认),release:生产" json:"UpdateWeek"` //更新周期
  651. //}
  652. paramsMap := make(map[string]interface{})
  653. updateWeek = WeekMap[updateWeek]
  654. paramsMap["IndexCode"] = indexCode
  655. paramsMap["UpdateWeek"] = updateWeek
  656. paramsMap["RunMode"] = utils.RunMode
  657. paramJson, err := json.Marshal(paramsMap)
  658. if err != nil {
  659. fmt.Println("param json.Marshal Err:" + err.Error())
  660. err = errors.New("param json.Marshal Err:" + err.Error())
  661. return
  662. }
  663. utils.FileLog.Info(fmt.Sprintf("CurlDelMysteelChemicalData parms:%s", string(paramJson)))
  664. result, err := http.Post(postUrl, string(paramJson), "application/json")
  665. if err != nil {
  666. fmt.Println("post err:" + err.Error())
  667. err = errors.New("post Err:" + err.Error())
  668. return
  669. }
  670. utils.FileLog.Info("CurlDelMysteelChemicalData Result:%s", string(result))
  671. }
  672. // WeekMap 中英文日期转换
  673. var WeekMap = map[string]string{
  674. "": "",
  675. "周一": "monday",
  676. "周二": "tuesday",
  677. "周三": "wednesday",
  678. "周四": "thursday",
  679. "周五": "friday",
  680. "周六": "saturday",
  681. "周日": "sunday",
  682. }
  683. // CurlRefreshMysteelChemicalData 请求钢联服务器添加指标
  684. func CurlRefreshMysteelChemicalData(mergeFilePath string) {
  685. postUrl := utils.MySteelPostUrl + `/api/index/refresh`
  686. //type IndexAddReq struct {
  687. // IndexCode string `json:"IndexCode" binding:"required"` //指标编码
  688. // UpdateWeek string `json:"UpdateWeek"` //更新周期
  689. // RunMode string `description:"运行环境:debug:测试(默认),release:生产" json:"UpdateWeek"` //更新周期
  690. //}
  691. paramsMap := make(map[string]interface{})
  692. paramsMap["MergeFilePath"] = mergeFilePath
  693. paramJson, err := json.Marshal(paramsMap)
  694. if err != nil {
  695. fmt.Println("param json.Marshal Err:" + err.Error())
  696. err = errors.New("param json.Marshal Err:" + err.Error())
  697. return
  698. }
  699. utils.FileLog.Info(fmt.Sprintf("CurlRefreshMysteelChemicalData parms:%s", string(paramJson)))
  700. result, err := http.Post(postUrl, string(paramJson), "application/json")
  701. if err != nil {
  702. fmt.Println("post err:" + err.Error())
  703. err = errors.New("post Err:" + err.Error())
  704. return
  705. }
  706. utils.FileLog.Info("CurlRefreshMysteelChemicalData Result:%s", string(result))
  707. }
  708. // getEdbTerminal 根据指标来源获取可用指标终端信息
  709. func getEdbTerminal(source int) (edbTerminal *data_manage.EdbTerminal) {
  710. terminalNumList, err := data_manage.GetMysteelChemicalGroupTerminalNum()
  711. if err != nil {
  712. return
  713. }
  714. terminalNumMap := make(map[string]int)
  715. for _, v := range terminalNumList {
  716. terminalNumMap[v.TerminalCode] = v.Num
  717. }
  718. terminalList, err := data_manage.GetEdbTerminalListBySource(source)
  719. if err != nil {
  720. return
  721. }
  722. num := 0
  723. for _, v := range terminalList {
  724. tmpNum := terminalNumMap[v.TerminalCode]
  725. if edbTerminal == nil {
  726. edbTerminal = v
  727. num = tmpNum
  728. } else if tmpNum < num {
  729. edbTerminal = v
  730. num = tmpNum
  731. }
  732. }
  733. return
  734. }
  735. // RefreshMysteelChemicalData 根据手工指标code刷新手工指标数据
  736. func RefreshMysteelChemicalData(edbCode string) {
  737. var err error
  738. var errMsg string
  739. defer func() {
  740. if err != nil {
  741. go alarm_msg.SendAlarmMsg("根据钢联化工的code刷新指标数据失败提醒,Err"+err.Error(), 3)
  742. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"导入手工数据后,根据手工指标code刷新手工指标数据失败提醒", "errmsg:"+err.Error(), utils.EmailSendToUsers)
  743. }
  744. if errMsg != "" {
  745. go alarm_msg.SendAlarmMsg("根据钢联化工的code刷新指标数据失败提醒,errMsg"+errMsg, 3)
  746. }
  747. }()
  748. baseFromMysteelChemicalIndexs, err := data_manage.GetBaseFromMysteelChemicalIndexByCodeList([]string{edbCode})
  749. if err != nil && err.Error() != utils.ErrNoRow() {
  750. return
  751. }
  752. resp, err := BatchAddMysteelChemicalData(baseFromMysteelChemicalIndexs, "zh")
  753. if err != nil {
  754. return
  755. }
  756. if !resp.Success {
  757. errMsg = resp.ErrMsg
  758. }
  759. }
  760. // RefreshMysteelChemicalData 根据手工指标code刷新手工指标数据
  761. func HealthCheckMysteelChemicalApi() (isEnable bool, ErrMsg string, err error) {
  762. url := utils.EDB_LIB_URL + "mysteel_chemical/handle/mysteel/api/check"
  763. body, err := HttpPost(url, "", utils.ZhLangVersion)
  764. if err != nil {
  765. return
  766. }
  767. var resp models.BaseResponse
  768. if err = json.Unmarshal(body, &resp); err != nil {
  769. return
  770. }
  771. apiCheck, ok := resp.Data.(map[string]interface{})
  772. if ok {
  773. isEnable = apiCheck["IsEnable"].(bool)
  774. ErrMsg = apiCheck["ErrMsg"].(string)
  775. } else {
  776. err = errors.New("钢联api接口不可用")
  777. }
  778. return
  779. }