User.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/logs"
  7. "Cold_Api/models/Account"
  8. "Cold_Api/models/Company"
  9. "Cold_Api/models/System"
  10. "Cold_Api/models/Warning"
  11. "fmt"
  12. "github.com/beego/beego/v2/adapter/orm"
  13. beego "github.com/beego/beego/v2/server/web"
  14. uuid "github.com/satori/go.uuid"
  15. "github.com/shopspring/decimal"
  16. "github.com/xuri/excelize/v2"
  17. "math"
  18. "os"
  19. "strconv"
  20. "strings"
  21. "time"
  22. )
  23. type UserController struct {
  24. beego.Controller
  25. Admin_r Account.Admin // 登陆的用户
  26. T_pid int // 公司id
  27. }
  28. func (c *UserController) Prepare() {
  29. GetCookie := c.Ctx.GetCookie("User_tokey")
  30. GetString := c.GetString("User_tokey")
  31. User_tokey := GetCookie
  32. if len(User_tokey) == 0 {
  33. User_tokey = GetString
  34. }
  35. c.Admin_r = *Account.Admin_r
  36. T_pid := c.Admin_r.T_pid
  37. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  38. if EntryPid > 0 {
  39. T_pid = EntryPid
  40. }
  41. c.T_pid = T_pid
  42. }
  43. // 获取微信二维码 ( 扫描后 ,Nats 自动绑定在 Admin->T_wx )
  44. func (c *UserController) User_WxQRCode() {
  45. T_name := c.GetString("T_name")
  46. WxQRCode_str, _ := NatsServer.Wx_GenerateQRCode("-|" + lib.AesEncryptCBC(c.Admin_r.T_uuid+"/"+T_name, "0123456789012345") + "|- @宝智达冷链 微信公众号通知")
  47. WxQRCode_str = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + WxQRCode_str
  48. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: WxQRCode_str}
  49. c.ServeJSON()
  50. return
  51. }
  52. // 公司管理
  53. func (c *UserController) Company_Tree() {
  54. var r_jsons lib.R_JSONS
  55. T_name := c.GetString("T_name")
  56. r_jsons.Data = Account.Read_Company_Tree(c.Admin_r, T_name)
  57. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  58. c.ServeJSON()
  59. return
  60. }
  61. // 所有公司树
  62. func (c *UserController) Company_List() {
  63. var r_jsons lib.R_JSONS
  64. T_name := c.GetString("T_name")
  65. r_jsons.Data = Account.Read_Company_List(T_name)
  66. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  67. c.ServeJSON()
  68. return
  69. }
  70. func (c *UserController) Company_Get() {
  71. var r_jsons lib.R_JSONS
  72. Company_r, err := Account.Read_Company_ById(c.T_pid)
  73. if err != nil {
  74. logs.Warning(lib.FuncName(), "获取公司信息失败", err)
  75. }
  76. r_jsons.Data = Account.CompanyToCompany_R(Company_r)
  77. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  78. c.ServeJSON()
  79. return
  80. }
  81. func (c *UserController) Company_Add() {
  82. T_mid, _ := c.GetInt("T_mid")
  83. T_name := c.GetString("T_name")
  84. T_plan := c.GetString("T_plan")
  85. T_data := c.GetString("T_data")
  86. T_v3d := c.GetString("T_v3d")
  87. var_ := Account.Company{
  88. T_mid: T_mid,
  89. T_name: T_name,
  90. T_State: 1,
  91. T_plan: T_plan,
  92. T_data: T_data,
  93. T_v3d: T_v3d,
  94. T_warning: 1,
  95. }
  96. id, err := Account.Add_Company(var_)
  97. if err != nil {
  98. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  99. c.ServeJSON()
  100. return
  101. }
  102. if c.Admin_r.T_pids != "*" {
  103. T_pids := []int{int(id)}
  104. if err = Account.Add_Company_bind(c.Admin_r, T_pids); err != nil {
  105. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  106. c.ServeJSON()
  107. return
  108. }
  109. }
  110. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "添加", var_)
  111. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  112. c.ServeJSON()
  113. return
  114. }
  115. func (c *UserController) Company_Edit() {
  116. Id, Id_err := c.GetInt("Id")
  117. if Id_err != nil {
  118. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  119. c.ServeJSON()
  120. return
  121. }
  122. Company_r, err := Account.Read_Company_ById(Id)
  123. if err != nil {
  124. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  125. c.ServeJSON()
  126. return
  127. }
  128. T_name := c.GetString("T_name")
  129. if len(T_name) > 0 {
  130. Company_r.T_name = T_name
  131. }
  132. is := Account.Update_Company(Company_r, "T_name")
  133. if !is {
  134. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  135. c.ServeJSON()
  136. return
  137. }
  138. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改", Company_r)
  139. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  140. c.ServeJSON()
  141. return
  142. }
  143. // 数字孪生
  144. func (c *UserController) Company_DigitalTwin() {
  145. Id, Id_err := c.GetInt("Id")
  146. if Id_err != nil {
  147. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  148. c.ServeJSON()
  149. return
  150. }
  151. Company_r, err := Account.Read_Company_ById(Id)
  152. if err != nil {
  153. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  154. c.ServeJSON()
  155. return
  156. }
  157. T_plan := c.GetString("T_plan")
  158. T_data := c.GetString("T_data")
  159. T_v3d := c.GetString("T_v3d")
  160. T_warning, _ := c.GetInt("T_warning")
  161. if len(T_plan) > 0 {
  162. Company_r.T_plan = T_plan
  163. }
  164. if len(T_data) > 0 {
  165. Company_r.T_data = T_data
  166. }
  167. if len(T_v3d) > 0 {
  168. Company_r.T_v3d = T_v3d
  169. }
  170. if T_warning > 0 {
  171. Company_r.T_warning = T_warning
  172. }
  173. is := Account.Update_Company(Company_r, "T_plan", "T_data", "T_v3d")
  174. if !is {
  175. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  176. c.ServeJSON()
  177. return
  178. }
  179. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改数字孪生", Company_r)
  180. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  181. c.ServeJSON()
  182. return
  183. }
  184. // 充值
  185. func (c *UserController) Company_Pay() {
  186. Id, Id_err := c.GetInt("Id")
  187. if Id_err != nil {
  188. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  189. c.ServeJSON()
  190. return
  191. }
  192. Company_r, err := Account.Read_Company_ById(Id)
  193. if err != nil {
  194. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  195. c.ServeJSON()
  196. return
  197. }
  198. T_balance, _ := c.GetFloat("T_balance")
  199. T_money64, _ := decimal.NewFromFloat(float64(Company_r.T_money) + T_balance).Round(2).Float64()
  200. Company_r.T_money = float32(T_money64)
  201. is := Account.Update_Company(Company_r, "T_money")
  202. if !is {
  203. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  204. c.ServeJSON()
  205. return
  206. }
  207. // 添加充值记录
  208. bill := Company.CompanyBill{
  209. T_pid: Company_r.Id,
  210. T_type: "充值",
  211. T_project: "充值",
  212. T_bill: "充值",
  213. T_charging: float32(T_balance),
  214. T_balance: float32(T_money64),
  215. }
  216. _, err = Company.Add_CompanyBill(bill)
  217. if err != nil {
  218. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  219. c.ServeJSON()
  220. return
  221. }
  222. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "充值", Company_r)
  223. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  224. c.ServeJSON()
  225. return
  226. }
  227. // 报警统计
  228. func (c *UserController) Company_Warning() {
  229. Id, Id_err := c.GetInt("Id")
  230. if Id_err != nil {
  231. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  232. c.ServeJSON()
  233. return
  234. }
  235. Company_r, err := Account.Read_Company_ById(Id)
  236. if err != nil {
  237. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  238. c.ServeJSON()
  239. return
  240. }
  241. T_warning, _ := c.GetInt("T_warning")
  242. if T_warning > 0 {
  243. Company_r.T_warning = T_warning
  244. }
  245. is := Account.Update_Company(Company_r, "T_warning")
  246. if !is {
  247. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  248. c.ServeJSON()
  249. return
  250. }
  251. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改报警统计", Company_r)
  252. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  253. c.ServeJSON()
  254. return
  255. }
  256. func (c *UserController) Company_Del() {
  257. Id, Id_err := c.GetInt("Id")
  258. if Id_err != nil {
  259. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  260. c.ServeJSON()
  261. return
  262. }
  263. Company_r, err := Account.Read_Company_ById(Id)
  264. if err != nil {
  265. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  266. c.ServeJSON()
  267. return
  268. }
  269. ids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  270. if cnt := Account.Read_Admin_Num_ByT_pid(ids); cnt > 0 {
  271. c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"}
  272. c.ServeJSON()
  273. return
  274. }
  275. o := orm.NewOrm()
  276. o.Begin()
  277. for _, id := range ids {
  278. if is := Account.Delete_Company(o, id); !is {
  279. o.Rollback()
  280. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  281. c.ServeJSON()
  282. return
  283. }
  284. if err = Account.Delete_Company_bind_T_pids(o, id); err != nil {
  285. o.Rollback()
  286. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  287. c.ServeJSON()
  288. return
  289. }
  290. }
  291. o.Commit()
  292. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "删除", Company_r)
  293. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  294. c.ServeJSON()
  295. return
  296. }
  297. // 进入公司
  298. func (c *UserController) Company_Entry() {
  299. GetCookie := c.Ctx.GetCookie("User_tokey")
  300. GetString := c.GetString("User_tokey")
  301. User_tokey := GetCookie
  302. if len(User_tokey) == 0 {
  303. User_tokey = GetString
  304. }
  305. T_pid, _ := c.GetInt("T_pid")
  306. // 操作pid
  307. company_r, err := Account.Read_Company_ById(T_pid)
  308. if err != nil {
  309. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  310. c.ServeJSON()
  311. return
  312. }
  313. visit := false
  314. if c.Admin_r.T_pid > 0 && strings.Contains(company_r.T_path, fmt.Sprintf("/%d/", c.Admin_r.T_pid)) {
  315. visit = true
  316. }
  317. if c.Admin_r.T_pid == 0 {
  318. if c.Admin_r.T_pids == "*" {
  319. visit = true
  320. } else {
  321. pids := lib.SplitStringToIntIds(c.Admin_r.T_pids, "P")
  322. if len(pids) > 0 {
  323. list := Account.ReadCompanyIds_T_pids(pids)
  324. for _, v := range list {
  325. if T_pid == v {
  326. visit = true
  327. break
  328. }
  329. }
  330. }
  331. }
  332. }
  333. if !visit {
  334. c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
  335. c.ServeJSON()
  336. return
  337. }
  338. Account.Redis_Tokey_T_pid_Set(User_tokey, T_pid)
  339. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "进入公司", T_pid)
  340. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  341. c.ServeJSON()
  342. return
  343. }
  344. // 检查 Tokey 是否过期
  345. // 用户管理
  346. func (c *UserController) User_List() {
  347. var r_jsons lib.R_JSONS
  348. page, _ := c.GetInt("page")
  349. if page < 1 {
  350. page = 1
  351. }
  352. page_z, _ := c.GetInt("page_z")
  353. if page_z < 1 {
  354. page_z = conf.Page_size
  355. }
  356. T_name := c.GetString("T_name")
  357. Account.Read_Power_All_Map()
  358. r_jsons.Data, r_jsons.Num = Account.Read_Admin_List(c.T_pid, T_name, page, page_z)
  359. r_jsons.Page = page
  360. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  361. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  362. c.ServeJSON()
  363. return
  364. }
  365. func (c *UserController) User_Get() {
  366. T_uuid := c.GetString("T_uuid")
  367. if len(T_uuid) < 8 {
  368. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  369. c.ServeJSON()
  370. return
  371. }
  372. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  373. if err != nil {
  374. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  375. c.ServeJSON()
  376. return
  377. }
  378. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_R(admin_r)}
  379. c.ServeJSON()
  380. return
  381. }
  382. func (c *UserController) User_Info() {
  383. type User_ struct {
  384. User Account.Admin_R
  385. //Power Account.Power // 权限
  386. //Menu map[string]Account.Menu_R // 菜单
  387. //Menu []Account.Menu // 菜单
  388. }
  389. Admin_r := User_{}
  390. Admin_r.User = Account.AdminToAdmin_R(c.Admin_r)
  391. //Admin_r.Power, _ = Account.Read_Power_ById(admin_r.T_power)
  392. //Admin_r.Menu = Account.Read_Menu_Tree(Power.T_menu, T_pid)
  393. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Admin_r}
  394. c.ServeJSON()
  395. return
  396. }
  397. func (c *UserController) User_Post() {
  398. admin_r := c.Admin_r
  399. T_name := c.GetString("T_name")
  400. T_pass := c.GetString("T_pass")
  401. T_phone := c.GetString("T_phone")
  402. T_mail := c.GetString("T_mail")
  403. T_wx := c.GetString("T_wx")
  404. if len(T_pass) < 8 && len(T_pass) > 0 {
  405. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  406. c.ServeJSON()
  407. return
  408. }
  409. if len(T_name) > 0 {
  410. admin_r.T_name = T_name
  411. }
  412. if len(T_pass) >= 8 {
  413. admin_r.T_pass = T_pass
  414. }
  415. if len(T_phone) > 0 {
  416. admin_r.T_phone = T_phone
  417. }
  418. if len(T_mail) > 0 {
  419. admin_r.T_mail = T_mail
  420. }
  421. if len(T_wx) > 0 {
  422. if T_wx == "-" {
  423. T_wx = ""
  424. }
  425. admin_r.T_wx = T_wx
  426. }
  427. is := Account.Update_Admin(admin_r, "T_name", "T_pass", "T_phone", "T_mail", "T_wx")
  428. if !is {
  429. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  430. c.ServeJSON()
  431. return
  432. }
  433. admin_r.T_pass = "******"
  434. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "修改个人信息", admin_r)
  435. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  436. c.ServeJSON()
  437. return
  438. }
  439. func (c *UserController) User_Add() {
  440. T_power, _ := c.GetInt("T_power")
  441. T_name := c.GetString("T_name")
  442. T_user := c.GetString("T_user")
  443. T_pass := c.GetString("T_pass")
  444. T_phone := c.GetString("T_phone")
  445. T_mail := c.GetString("T_mail")
  446. T_wx := c.GetString("T_wx")
  447. T_uuid := uuid.NewV4().String()
  448. var_ := Account.Admin{
  449. T_uuid: T_uuid,
  450. T_pid: c.T_pid,
  451. T_name: T_name,
  452. T_user: T_user,
  453. T_phone: T_phone,
  454. T_mail: T_mail,
  455. T_wx: T_wx,
  456. T_pass: T_pass,
  457. T_power: T_power,
  458. T_State: 1,
  459. }
  460. if len(T_pass) < 8 {
  461. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  462. c.ServeJSON()
  463. return
  464. }
  465. _, err := Account.Read_Admin_T_user(T_user)
  466. if err == nil {
  467. c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"}
  468. c.ServeJSON()
  469. return
  470. }
  471. _, err = Account.Add_Admin(var_)
  472. if err != nil {
  473. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  474. c.ServeJSON()
  475. return
  476. }
  477. var_.T_pass = "******"
  478. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "添加", var_)
  479. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  480. c.ServeJSON()
  481. return
  482. }
  483. func (c *UserController) User_Edit() {
  484. T_uuid := c.GetString("T_uuid")
  485. if len(T_uuid) < 8 {
  486. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  487. c.ServeJSON()
  488. return
  489. }
  490. r, err := Account.Read_Admin_ByUuid(T_uuid)
  491. if err != nil {
  492. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  493. c.ServeJSON()
  494. return
  495. }
  496. T_power, T_power_err := c.GetInt("T_power")
  497. T_name := c.GetString("T_name")
  498. T_pass := c.GetString("T_pass")
  499. T_phone := c.GetString("T_phone")
  500. T_mail := c.GetString("T_mail")
  501. if T_power_err == nil {
  502. r.T_power = T_power
  503. }
  504. if len(T_pass) < 8 && len(T_pass) > 0 {
  505. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  506. c.ServeJSON()
  507. return
  508. }
  509. if len(T_name) > 0 {
  510. r.T_name = T_name
  511. }
  512. if len(T_pass) >= 8 {
  513. r.T_pass = T_pass
  514. }
  515. if len(T_phone) > 0 {
  516. r.T_phone = T_phone
  517. }
  518. if len(T_mail) > 0 {
  519. r.T_mail = T_mail
  520. }
  521. is := Account.Update_Admin(r, "T_power", "T_pid", "T_name", "T_pass", "T_phone", "T_mail")
  522. if !is {
  523. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  524. c.ServeJSON()
  525. return
  526. }
  527. if len(T_name) > 0 {
  528. if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, T_name); !is {
  529. logs.Println("修改用户 => 修改用户关联报警通知失败", T_uuid, T_name)
  530. }
  531. }
  532. r.T_pass = "******"
  533. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "编辑", r)
  534. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  535. c.ServeJSON()
  536. return
  537. }
  538. func (c *UserController) User_Del() {
  539. T_uuid := c.GetString("T_uuid")
  540. if len(T_uuid) <= 0 {
  541. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  542. c.ServeJSON()
  543. return
  544. }
  545. admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid)
  546. if err != nil {
  547. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  548. c.ServeJSON()
  549. return
  550. }
  551. if is := Account.Delete_Admin(admin_r_lod); !is {
  552. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  553. c.ServeJSON()
  554. return
  555. }
  556. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is {
  557. logs.Println("删除用户 => 删除用户关联报警通知失败", T_uuid)
  558. }
  559. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "删除", T_uuid)
  560. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  561. c.ServeJSON()
  562. return
  563. }
  564. // 管理员
  565. func (c *UserController) User_Home() {
  566. // 验证登录
  567. admin_r := c.Admin_r
  568. type NoticeRecord struct {
  569. Sms int // 短信
  570. Phone int // 电话
  571. }
  572. type Info struct {
  573. T_name string // 姓名
  574. T_pids string // 内部用户绑定的公司id
  575. T_pid int // 公司id
  576. T_Days int // 服务天输
  577. Admin []Account.Admin_ // 专属管理员
  578. WarningNum int64 // 报警数量
  579. UntreatedWarningNum int64 // 未处理报警数量
  580. WorkOrderNum int64 // 工单数量
  581. WarningRate float32 // 设备报警率
  582. UntreatedWorkOrderNum int64 // 未处理工单数量
  583. Balance float32 // 余额
  584. NoticeRecord NoticeRecord // 通知记录
  585. }
  586. var info Info
  587. info.T_name = admin_r.T_name
  588. info.T_pid = admin_r.T_pid
  589. if admin_r.T_pid > 0 {
  590. power, _ := Account.Read_Power_ById(admin_r.T_power)
  591. company, _ := Account.Read_Company_ById(admin_r.T_pid)
  592. info.T_Days = lib.GetDiffDays(time.Now(), company.CreateTime)
  593. AdminList := Account.Read_Admin_List_T_pids(admin_r.T_pid)
  594. var Admin_ []Account.Admin_
  595. for _, v := range AdminList {
  596. Admin_ = append(Admin_, Account.Admin_RToAdmin_(v))
  597. }
  598. info.Admin = Admin_
  599. info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, 1, power.T_warning, true)
  600. info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, 3, power.T_warning, false)
  601. info.Balance = company.T_money
  602. info.NoticeRecord.Sms = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 1)
  603. info.NoticeRecord.Phone = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 2)
  604. }
  605. if admin_r.T_pid == 0 {
  606. if len(admin_r.T_pids) > 0 && admin_r.T_pids != "*" {
  607. info.T_pids = admin_r.T_pids
  608. info.WarningRate = Warning.Read_WarningRate_Yesterday(admin_r.T_uuid)
  609. }
  610. info.WorkOrderNum, info.UntreatedWorkOrderNum = NatsServer.Read_WorkOrderT_State_Count(admin_r.T_pids)
  611. }
  612. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: info}
  613. c.ServeJSON()
  614. return
  615. }
  616. // 内部用户管理
  617. func (c *UserController) Admin_List() {
  618. var r_jsons lib.R_JSONS
  619. page, _ := c.GetInt("page")
  620. if page < 1 {
  621. page = 1
  622. }
  623. page_z, _ := c.GetInt("page_z")
  624. if page_z < 1 {
  625. page_z = conf.Page_size
  626. }
  627. T_name := c.GetString("T_name")
  628. if c.Admin_r.T_pid > 0 {
  629. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
  630. c.ServeJSON()
  631. return
  632. }
  633. T_bind_company, _ := c.GetInt("T_bind_company") // 管理员绑定公司 0-全部 1-绑定公司
  634. Account.Read_Power_All_Map()
  635. userList, num := Account.Read_Admin_List(0, T_name, page, page_z)
  636. r_jsons.Data, r_jsons.Num = userList, num
  637. // 过滤已绑定公司列表
  638. if T_bind_company == 1 {
  639. var adminList []Account.Admin_R
  640. for _, v := range userList {
  641. if len(v.T_pids) == 0 || v.T_pids == "*" {
  642. continue
  643. }
  644. adminList = append(adminList, v)
  645. }
  646. r_jsons.Data = adminList
  647. }
  648. r_jsons.Page = page
  649. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  650. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  651. c.ServeJSON()
  652. return
  653. }
  654. func (c *UserController) Admin_Get() {
  655. T_uuid := c.GetString("T_uuid")
  656. if len(T_uuid) < 8 {
  657. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  658. c.ServeJSON()
  659. return
  660. }
  661. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  662. if err != nil {
  663. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  664. c.ServeJSON()
  665. return
  666. }
  667. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_R(admin_r)}
  668. c.ServeJSON()
  669. return
  670. }
  671. func (c *UserController) Admin_Add() {
  672. T_power, _ := c.GetInt("T_power")
  673. T_name := c.GetString("T_name")
  674. T_user := c.GetString("T_user")
  675. T_pass := c.GetString("T_pass")
  676. T_phone := c.GetString("T_phone")
  677. T_mail := c.GetString("T_mail")
  678. T_wx := c.GetString("T_wx")
  679. var_ := Account.Admin{
  680. T_uuid: uuid.NewV4().String(),
  681. T_pid: 0,
  682. T_name: T_name,
  683. T_user: T_user,
  684. T_phone: T_phone,
  685. T_mail: T_mail,
  686. T_wx: T_wx,
  687. T_pass: T_pass,
  688. T_power: T_power,
  689. T_State: 1,
  690. }
  691. if len(T_pass) < 8 {
  692. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  693. c.ServeJSON()
  694. return
  695. }
  696. _, err := Account.Read_Admin_T_user(T_user)
  697. if err == nil {
  698. c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"}
  699. c.ServeJSON()
  700. return
  701. }
  702. _, err = Account.Add_Admin(var_)
  703. if err != nil {
  704. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  705. c.ServeJSON()
  706. return
  707. }
  708. var_.T_pass = "******"
  709. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "添加", var_)
  710. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  711. c.ServeJSON()
  712. return
  713. }
  714. func (c *UserController) Admin_Edit() {
  715. T_uuid := c.GetString("T_uuid")
  716. if len(T_uuid) < 8 {
  717. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  718. c.ServeJSON()
  719. return
  720. }
  721. r, err := Account.Read_Admin_ByUuid(T_uuid)
  722. if err != nil {
  723. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  724. c.ServeJSON()
  725. return
  726. }
  727. T_power, T_power_err := c.GetInt("T_power")
  728. T_name := c.GetString("T_name")
  729. T_pass := c.GetString("T_pass")
  730. T_phone := c.GetString("T_phone")
  731. T_mail := c.GetString("T_mail")
  732. T_wx := c.GetString("T_wx")
  733. if T_power_err == nil {
  734. r.T_power = T_power
  735. }
  736. if len(T_pass) < 8 && len(T_pass) > 0 {
  737. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  738. c.ServeJSON()
  739. return
  740. }
  741. if len(T_name) > 0 {
  742. r.T_name = T_name
  743. }
  744. if len(T_pass) >= 8 {
  745. r.T_pass = T_pass
  746. }
  747. if len(T_phone) > 0 {
  748. r.T_phone = T_phone
  749. }
  750. if len(T_mail) > 0 {
  751. r.T_mail = T_mail
  752. }
  753. if len(T_wx) > 0 {
  754. r.T_wx = T_wx
  755. }
  756. is := Account.Update_Admin(r, "T_power", "T_name", "T_pass", "T_phone", "T_mail", "T_wx")
  757. if !is {
  758. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  759. c.ServeJSON()
  760. return
  761. }
  762. if len(T_name) > 0 {
  763. if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, "内管-"+T_name); !is {
  764. logs.Println("修改内部用户 => 修改内部用户关联报警通知失败", T_uuid, T_name)
  765. }
  766. }
  767. r.T_pass = "********"
  768. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "修改", r)
  769. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  770. c.ServeJSON()
  771. return
  772. }
  773. func (c *UserController) Admin_Del() {
  774. T_uuid := c.GetString("T_uuid")
  775. if len(T_uuid) <= 0 {
  776. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  777. c.ServeJSON()
  778. return
  779. }
  780. admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid)
  781. if err != nil {
  782. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  783. c.ServeJSON()
  784. return
  785. }
  786. if admin_r_lod.Id == 1 {
  787. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权删除!"}
  788. c.ServeJSON()
  789. return
  790. }
  791. if is := Account.Delete_Admin(admin_r_lod); !is {
  792. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  793. c.ServeJSON()
  794. return
  795. }
  796. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is {
  797. logs.Println("删除内部用户 => 删除内部用户关联报警通知失败", T_uuid)
  798. }
  799. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "删除", T_uuid)
  800. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  801. c.ServeJSON()
  802. return
  803. }
  804. // 报警通知的用户列表
  805. func (c *UserController) User_Notice_List() {
  806. type R_JSONS struct {
  807. //必须的大写开头
  808. AdminList []Account.Admin_R
  809. UserList []Account.Admin_R
  810. }
  811. var r_jsons R_JSONS
  812. // 绑定公司id的内部用户列表
  813. r_jsons.AdminList = Account.Read_Admin_List_T_pids(c.T_pid)
  814. // 绑定公司id的公司用户列表
  815. r_jsons.UserList = Account.Read_Admin_List_T_pid(c.T_pid)
  816. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  817. c.ServeJSON()
  818. return
  819. }
  820. // 管理员绑定公司
  821. func (c *UserController) AdminCompanyBind_List() {
  822. var r_jsons lib.R_JSONS
  823. T_uuid := c.GetString("T_uuid")
  824. if len(T_uuid) < 8 {
  825. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  826. c.ServeJSON()
  827. return
  828. }
  829. r, err := Account.Read_Admin_ByUuid(T_uuid)
  830. if err != nil {
  831. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  832. c.ServeJSON()
  833. return
  834. }
  835. r_jsons.Data = Account.Read_Company_List_ByT_pids(r.T_pids)
  836. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  837. c.ServeJSON()
  838. return
  839. }
  840. func (c *UserController) AdminCompanyBind_Add() {
  841. T_uuid := c.GetString("T_uuid")
  842. if len(T_uuid) < 8 {
  843. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  844. c.ServeJSON()
  845. return
  846. }
  847. T_pid, _ := c.GetInt("T_pid")
  848. if T_pid == 0 {
  849. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  850. c.ServeJSON()
  851. return
  852. }
  853. r, err := Account.Read_Admin_ByUuid(T_uuid)
  854. if err != nil {
  855. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  856. c.ServeJSON()
  857. return
  858. }
  859. if r.T_pids == "*" {
  860. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  861. c.ServeJSON()
  862. return
  863. }
  864. Company_r, err := Account.Read_Company_ById(T_pid)
  865. if err != nil {
  866. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  867. c.ServeJSON()
  868. return
  869. }
  870. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  871. if err = Account.Add_Company_bind(r, T_pids); err != nil {
  872. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  873. c.ServeJSON()
  874. return
  875. }
  876. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定添加", T_uuid+"-"+strconv.Itoa(T_pid))
  877. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  878. c.ServeJSON()
  879. return
  880. }
  881. func (c *UserController) AdminCompanyBind_Del() {
  882. T_uuid := c.GetString("T_uuid")
  883. if len(T_uuid) < 8 {
  884. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  885. c.ServeJSON()
  886. return
  887. }
  888. T_pid, _ := c.GetInt("T_pid")
  889. if T_pid == 0 {
  890. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  891. c.ServeJSON()
  892. return
  893. }
  894. r, err := Account.Read_Admin_ByUuid(T_uuid)
  895. Company_r, err := Account.Read_Company_ById(T_pid)
  896. if err != nil {
  897. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  898. c.ServeJSON()
  899. return
  900. }
  901. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  902. if len(T_pids) == 0 {
  903. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  904. c.ServeJSON()
  905. return
  906. }
  907. if err = Account.Delete_Company_bind(r, T_pids); err != nil {
  908. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  909. c.ServeJSON()
  910. return
  911. }
  912. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid, T_pids); !is {
  913. logs.Println("删除内部用户公司绑定 => 删除内部用户绑定的公司关联报警通知失败", T_uuid)
  914. }
  915. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定删除", T_uuid+"-"+strconv.Itoa(T_pid))
  916. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  917. c.ServeJSON()
  918. return
  919. }
  920. // 权限管理
  921. func (c *UserController) Power_List() {
  922. var r_jsons lib.R_JSONS
  923. page, _ := c.GetInt("page")
  924. if page < 1 {
  925. page = 1
  926. }
  927. page_z, _ := c.GetInt("page_z")
  928. if page_z < 1 {
  929. page_z = conf.Page_size
  930. }
  931. Name := c.GetString("T_name")
  932. r_jsons.Data, r_jsons.Num = Account.Read_Power_List(Name, page, page_z)
  933. r_jsons.Page = page
  934. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  935. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  936. c.ServeJSON()
  937. return
  938. }
  939. // 添加用户 权限选择列表
  940. func (c *UserController) Power_List_All() {
  941. r, err := Account.Read_Power_ById(c.Admin_r.T_power)
  942. if err != nil {
  943. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  944. c.ServeJSON()
  945. return
  946. }
  947. var r_jsons lib.R_JSONS
  948. r_jsons.Data = Account.Read_Power_List_ALL_User(r.T_select)
  949. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  950. c.ServeJSON()
  951. return
  952. }
  953. func (c *UserController) Power_Get() {
  954. type Data struct {
  955. Power Account.Power_
  956. Menu []Account.Menu
  957. Menu_checked []int
  958. Select []Account.Power_R
  959. Warning []Warning.WarningType_R
  960. }
  961. var data Data
  962. id, id_err := c.GetInt("T_id")
  963. if id_err != nil || id == 0 {
  964. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  965. c.ServeJSON()
  966. return
  967. }
  968. power, err := Account.Read_Power_ById(id)
  969. if err != nil {
  970. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  971. c.ServeJSON()
  972. return
  973. }
  974. data.Power = Account.PowerToPower_(power)
  975. data.Menu, data.Menu_checked = Account.Read_Menu_List_All()
  976. if err != nil {
  977. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"}
  978. c.ServeJSON()
  979. return
  980. }
  981. if power.T_menu != "*" {
  982. data.Menu_checked = lib.SplitStringToIntIds(power.T_menu, "M")
  983. }
  984. if err != nil {
  985. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"}
  986. c.ServeJSON()
  987. return
  988. }
  989. data.Select = Account.Read_Power_List_ALL_T_select(power.T_select)
  990. if err != nil {
  991. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_select Err!"}
  992. c.ServeJSON()
  993. return
  994. }
  995. data.Warning = Warning.Read_WarningType_All_Power_T_Warning(power.T_warning)
  996. if err != nil {
  997. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_warning Err!"}
  998. c.ServeJSON()
  999. return
  1000. }
  1001. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: data}
  1002. c.ServeJSON()
  1003. return
  1004. }
  1005. func (c *UserController) Power_Add() {
  1006. T_name := c.GetString("T_name")
  1007. T_select := c.GetString("T_select")
  1008. T_warning := c.GetString("T_warning")
  1009. T_menu := c.GetString("T_menu")
  1010. var_ := Account.Power{
  1011. T_name: T_name,
  1012. T_select: T_select,
  1013. T_warning: T_warning,
  1014. T_menu: T_menu,
  1015. T_State: 1,
  1016. }
  1017. _, err := Account.Add_Power(var_)
  1018. if err != nil {
  1019. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  1020. c.ServeJSON()
  1021. return
  1022. }
  1023. System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "添加", var_)
  1024. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1025. c.ServeJSON()
  1026. return
  1027. }
  1028. func (c *UserController) Power_Edit() {
  1029. Id, Id_err := c.GetInt("T_id")
  1030. if Id_err != nil || Id == 0 {
  1031. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1032. c.ServeJSON()
  1033. return
  1034. }
  1035. if Id == 1 {
  1036. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权修改!"}
  1037. c.ServeJSON()
  1038. return
  1039. }
  1040. r, err := Account.Read_Power_ById(Id)
  1041. if err != nil {
  1042. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1043. c.ServeJSON()
  1044. return
  1045. }
  1046. T_name := c.GetString("T_name")
  1047. T_select := c.GetString("T_select")
  1048. T_warning := c.GetString("T_warning")
  1049. T_menu := c.GetString("T_menu")
  1050. if len(T_name) > 0 {
  1051. r.T_name = T_name
  1052. }
  1053. if len(T_select) > 0 {
  1054. r.T_select = T_select
  1055. }
  1056. if len(T_warning) > 0 {
  1057. r.T_warning = T_warning
  1058. }
  1059. if len(T_menu) > 0 {
  1060. r.T_menu = T_menu
  1061. }
  1062. is := Account.Update_Power(r, "T_name", "T_select", "T_warning", "T_menu")
  1063. if !is {
  1064. c.Data["json"] = lib.JSONS{Code: 302, Msg: "修改失败!"}
  1065. c.ServeJSON()
  1066. return
  1067. }
  1068. System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "修改", r)
  1069. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1070. c.ServeJSON()
  1071. return
  1072. }
  1073. func (c *UserController) Power_Del() {
  1074. id, id_err := c.GetInt("T_id")
  1075. if id_err != nil || id == 0 {
  1076. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1077. c.ServeJSON()
  1078. return
  1079. }
  1080. r, err := Account.Read_Power_ById(id)
  1081. if err != nil {
  1082. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1083. c.ServeJSON()
  1084. return
  1085. }
  1086. if cnt := Account.Read_Admin_Num_ByT_power(id); cnt > 0 {
  1087. c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"}
  1088. c.ServeJSON()
  1089. return
  1090. }
  1091. is := Account.Delete_Power(r)
  1092. if !is {
  1093. c.Data["json"] = lib.JSONS{Code: 302, Msg: "删除失败!"}
  1094. c.ServeJSON()
  1095. return
  1096. }
  1097. System.Add_UserLogs(c.Admin_r.T_uuid, "权限管理", "删除", strconv.Itoa(id))
  1098. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1099. c.ServeJSON()
  1100. return
  1101. }
  1102. // 菜单列表
  1103. func (c *UserController) User_Menu_List() {
  1104. type R_JSONS struct {
  1105. //必须的大写开头
  1106. Data interface{}
  1107. Permission interface{}
  1108. }
  1109. var r_jsons R_JSONS
  1110. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1111. if err != nil {
  1112. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"}
  1113. c.ServeJSON()
  1114. return
  1115. }
  1116. logs.Println("登录用户 菜单 pid 为:", c.T_pid)
  1117. ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid)
  1118. r_jsons.Data = ret.Menu
  1119. r_jsons.Permission = ret.Permission
  1120. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1121. c.ServeJSON()
  1122. return
  1123. }
  1124. // 权限菜单列表
  1125. func (c *UserController) Menu_List_All() {
  1126. var r_jsons lib.R_JSONS
  1127. r_jsons.Data, _ = Account.Read_Menu_List_All()
  1128. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1129. c.ServeJSON()
  1130. return
  1131. }
  1132. func (c *UserController) Menu_Excel() {
  1133. filename := fmt.Sprintf("冷链3.0菜单")
  1134. f := excelize.NewFile()
  1135. Style1, _ := f.NewStyle(
  1136. &excelize.Style{
  1137. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  1138. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1139. })
  1140. Style2, _ := f.NewStyle(
  1141. &excelize.Style{
  1142. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  1143. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1144. Border: []excelize.Border{
  1145. {Type: "left", Color: "000000", Style: 1},
  1146. {Type: "top", Color: "000000", Style: 1},
  1147. {Type: "bottom", Color: "000000", Style: 1},
  1148. {Type: "right", Color: "000000", Style: 1},
  1149. },
  1150. })
  1151. f.MergeCell("Sheet1", "A1", "G1")
  1152. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1153. f.SetCellValue("Sheet1", "A1", "Menu")
  1154. f.SetRowHeight("Sheet1", 1, 30)
  1155. f.MergeCell("Sheet1", "H1", "M1")
  1156. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1157. f.SetCellValue("Sheet1", "H1", "API")
  1158. f.SetRowHeight("Sheet1", 1, 30)
  1159. f.SetCellStyle("Sheet1", "A2", "G2", Style2)
  1160. f.SetRowHeight("Sheet1", 2, 25)
  1161. // 这里设置表头
  1162. f.SetCellValue("Sheet1", "A2", "ID")
  1163. f.SetCellValue("Sheet1", "B2", "t_mid")
  1164. f.SetCellValue("Sheet1", "C2", "t_name")
  1165. f.SetCellValue("Sheet1", "D2", "t_permission")
  1166. f.SetCellValue("Sheet1", "E2", "t_sort")
  1167. f.SetCellValue("Sheet1", "F2", "t_pid")
  1168. f.SetCellValue("Sheet1", "G2", "t_icon")
  1169. f.SetCellValue("Sheet1", "H2", "ID")
  1170. f.SetCellValue("Sheet1", "I2", "t__menu__id")
  1171. f.SetCellValue("Sheet1", "J2", "t_name")
  1172. f.SetCellValue("Sheet1", "K2", "t_uri")
  1173. f.SetCellValue("Sheet1", "L2", "t_method")
  1174. f.SetCellValue("Sheet1", "M2", "t_enable")
  1175. // 设置列宽
  1176. f.SetColWidth("Sheet1", "A", "M", 15)
  1177. line := 2
  1178. menu := Account.Read_Menu_All()
  1179. for _, m := range menu {
  1180. Menu_Call(f, m, line)
  1181. }
  1182. Style11, _ := f.NewStyle(
  1183. &excelize.Style{
  1184. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1185. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1186. Border: []excelize.Border{
  1187. {Type: "top", Color: "000000", Style: 1},
  1188. },
  1189. })
  1190. Style22, _ := f.NewStyle(
  1191. &excelize.Style{
  1192. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1193. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1194. Border: []excelize.Border{
  1195. {Type: "left", Color: "000000", Style: 1},
  1196. },
  1197. })
  1198. Style33, _ := f.NewStyle(
  1199. &excelize.Style{
  1200. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1201. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1202. Border: []excelize.Border{
  1203. {Type: "right", Color: "000000", Style: 1},
  1204. },
  1205. })
  1206. Style44, _ := f.NewStyle(
  1207. &excelize.Style{
  1208. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1209. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1210. Border: []excelize.Border{
  1211. {Type: "bottom", Color: "000000", Style: 1},
  1212. },
  1213. })
  1214. //// 循环写入数据
  1215. for _, v := range menu {
  1216. line1 := line
  1217. line = Menu_Call(f, v, line)
  1218. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line1), fmt.Sprintf("M%d", line1), Style11)
  1219. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("A%d", line), Style22)
  1220. f.SetCellStyle("Sheet1", fmt.Sprintf("M%d", line), fmt.Sprintf("M%d", line), Style33)
  1221. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("M%d", line), Style44)
  1222. }
  1223. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  1224. // 保存文件
  1225. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1226. fmt.Println(err)
  1227. }
  1228. var url string
  1229. ////// 上传 OSS
  1230. //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1231. //if !is {
  1232. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1233. // c.ServeJSON()
  1234. // return
  1235. //}
  1236. ////删除目录
  1237. //err = os.Remove("ofile/" + timeStr + ".xlsx")
  1238. //if err != nil {
  1239. // fmt.Println(err)
  1240. //}
  1241. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1242. c.ServeJSON()
  1243. return
  1244. }
  1245. func Menu_Call(f *excelize.File, v Account.Menu, line int) int {
  1246. line++
  1247. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.Id)
  1248. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_mid)
  1249. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_name)
  1250. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_permission)
  1251. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_sort)
  1252. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_pid)
  1253. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_icon)
  1254. apiList := Account.Read_API_List_ByMenuId(v.Id)
  1255. for _, api := range apiList {
  1256. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), api.Id)
  1257. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), api.T_Menu_Id)
  1258. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), api.T_name)
  1259. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), api.T_uri)
  1260. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), api.T_method)
  1261. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), api.T_enable)
  1262. line++
  1263. }
  1264. return line
  1265. }
  1266. // 流量池
  1267. func (c *UserController) Flow_Pool() {
  1268. var r_jsons lib.R_JSONS
  1269. r_jsons.Data, _ = lib.Yidong_group_data_margin()
  1270. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1271. c.ServeJSON()
  1272. return
  1273. }
  1274. // 账单下载
  1275. func (c *UserController) CompanyBill_Excel() {
  1276. T_month := c.GetString("T_month")
  1277. _, err := time.Parse("2006-01", T_month)
  1278. if err != nil {
  1279. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1280. c.ServeJSON()
  1281. return
  1282. }
  1283. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  1284. company, _ := Account.Read_Company_ById(c.Admin_r.T_pid)
  1285. if err != nil {
  1286. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1287. c.ServeJSON()
  1288. return
  1289. }
  1290. filename := fmt.Sprintf("%s%s年%s月账单", company.T_name, year, month)
  1291. f := excelize.NewFile()
  1292. Style1, _ := f.NewStyle(
  1293. &excelize.Style{
  1294. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  1295. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1296. })
  1297. Style2, _ := f.NewStyle(
  1298. &excelize.Style{
  1299. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  1300. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1301. Border: []excelize.Border{
  1302. {Type: "left", Color: "000000", Style: 1},
  1303. {Type: "top", Color: "000000", Style: 1},
  1304. {Type: "bottom", Color: "000000", Style: 1},
  1305. {Type: "right", Color: "000000", Style: 1},
  1306. },
  1307. })
  1308. f.MergeCell("Sheet1", "A1", "G1")
  1309. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1310. f.SetCellValue("Sheet1", "A1", filename)
  1311. f.SetRowHeight("Sheet1", 1, 30)
  1312. f.SetCellStyle("Sheet1", "A2", "G2", Style2)
  1313. f.SetRowHeight("Sheet1", 2, 25)
  1314. // 这里设置表头
  1315. f.SetCellValue("Sheet1", "A2", "编号")
  1316. f.SetCellValue("Sheet1", "B2", "项目")
  1317. f.SetCellValue("Sheet1", "C2", "说明")
  1318. f.SetCellValue("Sheet1", "D2", "扣费/充值")
  1319. f.SetCellValue("Sheet1", "E2", "金额(元)")
  1320. f.SetCellValue("Sheet1", "F2", "余额(元)")
  1321. f.SetCellValue("Sheet1", "G2", "时间")
  1322. // 设置列宽
  1323. f.SetColWidth("Sheet1", "A", "A", 10)
  1324. f.SetColWidth("Sheet1", "B", "B", 50)
  1325. f.SetColWidth("Sheet1", "C", "C", 15)
  1326. f.SetColWidth("Sheet1", "D", "D", 12)
  1327. f.SetColWidth("Sheet1", "E", "E", 15)
  1328. f.SetColWidth("Sheet1", "F", "F", 15)
  1329. f.SetColWidth("Sheet1", "G", "G", 20)
  1330. line := 2
  1331. CompanyBill_List, err := Company.Read_CompanyBill_List(c.Admin_r.T_pid, T_month)
  1332. if err != nil {
  1333. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1334. c.ServeJSON()
  1335. return
  1336. }
  1337. // 循环写入数据
  1338. for i, v := range CompanyBill_List {
  1339. line++
  1340. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  1341. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_project)
  1342. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_bill)
  1343. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_type)
  1344. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_charging)
  1345. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_balance)
  1346. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.CreateTime.Format("2006-01-02"))
  1347. }
  1348. Style4, _ := f.NewStyle(
  1349. &excelize.Style{
  1350. Font: &excelize.Font{Size: 12, Family: "宋体"},
  1351. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1352. Border: []excelize.Border{
  1353. {Type: "left", Color: "000000", Style: 1},
  1354. {Type: "top", Color: "000000", Style: 1},
  1355. {Type: "bottom", Color: "000000", Style: 1},
  1356. {Type: "right", Color: "000000", Style: 1},
  1357. },
  1358. })
  1359. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("G%d", line), Style4)
  1360. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  1361. // 保存文件
  1362. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1363. fmt.Println(err)
  1364. }
  1365. var url string
  1366. //// 上传 OSS
  1367. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1368. if !is {
  1369. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1370. c.ServeJSON()
  1371. return
  1372. }
  1373. //删除目录
  1374. err = os.Remove("ofile/" + timeStr + ".xlsx")
  1375. if err != nil {
  1376. fmt.Println(err)
  1377. }
  1378. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1379. c.ServeJSON()
  1380. return
  1381. }