User.go 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Account"
  7. "Cold_Api/models/Company"
  8. "Cold_Api/models/Device"
  9. "Cold_Api/models/System"
  10. "Cold_Api/models/Warning"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. "github.com/beego/beego/v2/core/logs"
  15. beego "github.com/beego/beego/v2/server/web"
  16. uuid "github.com/satori/go.uuid"
  17. "github.com/shopspring/decimal"
  18. "github.com/xuri/excelize/v2"
  19. "math"
  20. "os"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. type UserController struct {
  26. beego.Controller
  27. Admin_r Account.Admin // 登陆的用户
  28. T_pid int // 公司id
  29. }
  30. func (c *UserController) Prepare() {
  31. GetCookie := c.Ctx.GetCookie("User_tokey")
  32. GetString := c.GetString("User_tokey")
  33. User_tokey := GetCookie
  34. if len(User_tokey) == 0 {
  35. User_tokey = GetString
  36. }
  37. if Account.Admin_r == nil {
  38. return
  39. }
  40. c.Admin_r = *Account.Admin_r
  41. T_pid := c.Admin_r.T_pid
  42. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  43. if EntryPid > 0 {
  44. T_pid = EntryPid
  45. }
  46. c.T_pid = T_pid
  47. }
  48. // 获取微信二维码 ( 扫描后 ,Nats 自动绑定在 Admin->T_wx )
  49. func (c *UserController) User_WxQRCode() {
  50. T_name := c.GetString("T_name")
  51. WxQRCode_str, _ := NatsServer.Wx_GenerateQRCode("-|" + lib.AesEncryptCBC(c.Admin_r.T_uuid+"/"+T_name, "0123456789012345") + "|- @宝智达冷链 微信公众号通知")
  52. WxQRCode_str = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + WxQRCode_str
  53. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: WxQRCode_str}
  54. c.ServeJSON()
  55. return
  56. }
  57. // 用户绑定的公司列表
  58. func (c *UserController) Company_Tree() {
  59. var r_jsons lib.R_JSONS
  60. T_name := c.GetString("T_name")
  61. r_jsons.Data = Account.Read_Company_Tree(c.Admin_r, T_name)
  62. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  63. c.ServeJSON()
  64. return
  65. }
  66. func (c *UserController) Company_Transport_List() {
  67. var r_jsons lib.R_JSONS
  68. T_name := c.GetString("T_name")
  69. r_jsons.Data = Account.Read_Transport_Company_List(T_name)
  70. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  71. c.ServeJSON()
  72. return
  73. }
  74. // 所有公司树
  75. func (c *UserController) Company_List() {
  76. var r_jsons lib.R_JSONS
  77. T_name := c.GetString("T_name")
  78. r_jsons.Data = Account.Read_Company_List(T_name)
  79. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  80. c.ServeJSON()
  81. return
  82. }
  83. func (c *UserController) Company_Get() {
  84. var r_jsons lib.R_JSONS
  85. Company_r, err := Account.Read_Company_ById(c.T_pid)
  86. if err != nil {
  87. logs.Warning(lib.FuncName(), "获取公司信息失败", err)
  88. }
  89. r_jsons.Data = Account.CompanyToCompany_R(Company_r)
  90. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  91. c.ServeJSON()
  92. return
  93. }
  94. func (c *UserController) Company_Add() {
  95. T_mid, _ := c.GetInt("T_mid")
  96. T_name := c.GetString("T_name")
  97. T_plan := c.GetString("T_plan")
  98. T_data := c.GetString("T_data")
  99. T_v3d := c.GetString("T_v3d")
  100. T_type, _ := c.GetInt("T_type")
  101. var_ := Account.Company{
  102. T_mid: T_mid,
  103. T_name: T_name,
  104. T_State: 1,
  105. T_plan: T_plan,
  106. T_data: T_data,
  107. T_v3d: T_v3d,
  108. T_warning: 1,
  109. T_type: T_type,
  110. }
  111. id, err := Account.Add_Company(var_)
  112. if err != nil {
  113. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  114. c.ServeJSON()
  115. return
  116. }
  117. if c.Admin_r.T_pids != "*" {
  118. T_pids := []int{int(id)}
  119. if err = Account.Add_Company_bind(c.Admin_r, T_pids); err != nil {
  120. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  121. c.ServeJSON()
  122. return
  123. }
  124. }
  125. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "添加", var_)
  126. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  127. c.ServeJSON()
  128. return
  129. }
  130. func (c *UserController) Company_Edit() {
  131. Id, Id_err := c.GetInt("Id")
  132. if Id_err != nil {
  133. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  134. c.ServeJSON()
  135. return
  136. }
  137. Company_r, err := Account.Read_Company_ById(Id)
  138. if err != nil {
  139. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  140. c.ServeJSON()
  141. return
  142. }
  143. T_name := c.GetString("T_name")
  144. T_type, _ := c.GetInt("T_type")
  145. if len(T_name) > 0 {
  146. Company_r.T_name = T_name
  147. }
  148. if T_type > 0 {
  149. Company_r.T_type = T_type
  150. }
  151. is := Account.Update_Company(Company_r, "T_name", "T_type")
  152. if !is {
  153. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  154. c.ServeJSON()
  155. return
  156. }
  157. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改", Company_r)
  158. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  159. c.ServeJSON()
  160. return
  161. }
  162. // 数字孪生
  163. func (c *UserController) Company_DigitalTwin() {
  164. Id, Id_err := c.GetInt("Id")
  165. if Id_err != nil {
  166. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  167. c.ServeJSON()
  168. return
  169. }
  170. Company_r, err := Account.Read_Company_ById(Id)
  171. if err != nil {
  172. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  173. c.ServeJSON()
  174. return
  175. }
  176. T_plan := c.GetString("T_plan")
  177. T_data := c.GetString("T_data")
  178. T_v3d := c.GetString("T_v3d")
  179. T_warning, _ := c.GetInt("T_warning")
  180. Company_r.T_plan = T_plan
  181. Company_r.T_data = T_data
  182. Company_r.T_v3d = T_v3d
  183. Company_r.T_warning = T_warning
  184. is := Account.Update_Company(Company_r, "T_plan", "T_data", "T_v3d", "T_warning")
  185. if !is {
  186. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  187. c.ServeJSON()
  188. return
  189. }
  190. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改数字孪生", Company_r)
  191. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  192. c.ServeJSON()
  193. return
  194. }
  195. // 充值
  196. func (c *UserController) Company_Pay() {
  197. Id, Id_err := c.GetInt("Id")
  198. if Id_err != nil {
  199. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  200. c.ServeJSON()
  201. return
  202. }
  203. Company_r, err := Account.Read_Company_ById(Id)
  204. if err != nil {
  205. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  206. c.ServeJSON()
  207. return
  208. }
  209. T_balance, _ := c.GetFloat("T_balance")
  210. T_money64, _ := decimal.NewFromFloat(float64(Company_r.T_money) + T_balance).Round(2).Float64()
  211. Company_r.T_money = float32(T_money64)
  212. is := Account.Update_Company(Company_r, "T_money")
  213. if !is {
  214. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  215. c.ServeJSON()
  216. return
  217. }
  218. // 添加充值记录
  219. bill := Company.CompanyBill{
  220. T_pid: Company_r.Id,
  221. T_type: "充值",
  222. T_project: "充值",
  223. T_bill: "充值",
  224. T_charging: float32(T_balance),
  225. T_balance: float32(T_money64),
  226. }
  227. _, err = Company.Add_CompanyBill(bill)
  228. if err != nil {
  229. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  230. c.ServeJSON()
  231. return
  232. }
  233. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "充值", Company_r)
  234. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  235. c.ServeJSON()
  236. return
  237. }
  238. // 报警统计
  239. func (c *UserController) Company_Warning() {
  240. Id, Id_err := c.GetInt("Id")
  241. if Id_err != nil {
  242. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  243. c.ServeJSON()
  244. return
  245. }
  246. Company_r, err := Account.Read_Company_ById(Id)
  247. if err != nil {
  248. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  249. c.ServeJSON()
  250. return
  251. }
  252. T_warning, _ := c.GetInt("T_warning")
  253. if T_warning > 0 {
  254. Company_r.T_warning = T_warning
  255. }
  256. is := Account.Update_Company(Company_r, "T_warning")
  257. if !is {
  258. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  259. c.ServeJSON()
  260. return
  261. }
  262. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改报警统计", Company_r)
  263. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. func (c *UserController) Company_Del() {
  268. Id, Id_err := c.GetInt("Id")
  269. if Id_err != nil {
  270. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  271. c.ServeJSON()
  272. return
  273. }
  274. Company_r, err := Account.Read_Company_ById(Id)
  275. if err != nil {
  276. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  277. c.ServeJSON()
  278. return
  279. }
  280. ids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  281. if cnt := Account.Read_Admin_Num_ByT_pid(ids); cnt > 0 {
  282. c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"}
  283. c.ServeJSON()
  284. return
  285. }
  286. o := orm.NewOrm()
  287. o.Begin()
  288. for _, id := range ids {
  289. if is := Account.Delete_Company(o, id); !is {
  290. o.Rollback()
  291. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  292. c.ServeJSON()
  293. return
  294. }
  295. if err = Account.Delete_Company_bind_T_pids(o, id); err != nil {
  296. o.Rollback()
  297. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  298. c.ServeJSON()
  299. return
  300. }
  301. }
  302. o.Commit()
  303. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "删除", Company_r)
  304. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  305. c.ServeJSON()
  306. return
  307. }
  308. // 进入公司
  309. func (c *UserController) Company_Entry() {
  310. GetCookie := c.Ctx.GetCookie("User_tokey")
  311. GetString := c.GetString("User_tokey")
  312. User_tokey := GetCookie
  313. if len(User_tokey) == 0 {
  314. User_tokey = GetString
  315. }
  316. T_pid, _ := c.GetInt("T_pid")
  317. // 操作pid
  318. company_r, err := Account.Read_Company_ById(T_pid)
  319. if err != nil {
  320. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  321. c.ServeJSON()
  322. return
  323. }
  324. visit := false
  325. if c.Admin_r.T_pid > 0 && strings.Contains(company_r.T_path, fmt.Sprintf("/%d/", c.Admin_r.T_pid)) {
  326. visit = true
  327. }
  328. if c.Admin_r.T_pid == 0 {
  329. if c.Admin_r.T_pids == "*" {
  330. visit = true
  331. } else {
  332. pids := lib.SplitStringToIntIds(c.Admin_r.T_pids, "P")
  333. if len(pids) > 0 {
  334. list := Account.ReadCompanyIds_T_pids(pids)
  335. for _, v := range list {
  336. if T_pid == v {
  337. visit = true
  338. break
  339. }
  340. }
  341. }
  342. }
  343. }
  344. if !visit {
  345. c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
  346. c.ServeJSON()
  347. return
  348. }
  349. Account.Redis_Tokey_T_pid_Set(User_tokey, T_pid)
  350. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "进入公司", T_pid)
  351. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  352. c.ServeJSON()
  353. return
  354. }
  355. // 记账扣费
  356. func (c *UserController) Company_Charging() {
  357. Id, Id_err := c.GetInt("Id")
  358. if Id_err != nil {
  359. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  360. c.ServeJSON()
  361. return
  362. }
  363. Company_r, err := Account.Read_Company_ById(Id)
  364. if err != nil {
  365. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  366. c.ServeJSON()
  367. return
  368. }
  369. T_Charging, _ := c.GetInt("T_Charging")
  370. Company_r.T_Charging = T_Charging
  371. is := Account.Update_Company(Company_r, "T_Charging")
  372. if !is {
  373. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  374. c.ServeJSON()
  375. return
  376. }
  377. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "记账扣费", Company_r)
  378. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  379. c.ServeJSON()
  380. return
  381. }
  382. // 检查 Tokey 是否过期
  383. // 用户管理
  384. func (c *UserController) User_List() {
  385. var r_jsons lib.R_JSONS
  386. page, _ := c.GetInt("page")
  387. if page < 1 {
  388. page = 1
  389. }
  390. page_z, _ := c.GetInt("page_z")
  391. if page_z < 1 {
  392. page_z = conf.Page_size
  393. }
  394. T_name := c.GetString("T_name")
  395. Account.Read_Power_All_Map()
  396. r_jsons.Data, r_jsons.Num = Account.Read_Admin_List(c.T_pid, T_name, page, page_z)
  397. r_jsons.Page = page
  398. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  399. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  400. c.ServeJSON()
  401. return
  402. }
  403. func (c *UserController) User_Get() {
  404. T_uuid := c.GetString("T_uuid")
  405. if len(T_uuid) < 8 {
  406. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  407. c.ServeJSON()
  408. return
  409. }
  410. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  411. if err != nil {
  412. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  413. c.ServeJSON()
  414. return
  415. }
  416. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_R(admin_r)}
  417. c.ServeJSON()
  418. return
  419. }
  420. func (c *UserController) User_Info() {
  421. type User_ struct {
  422. User Account.Admin_R
  423. //Power Account.Power // 权限
  424. //Menu map[string]Account.Menu_R // 菜单
  425. //Menu []Account.Menu // 菜单
  426. }
  427. Admin_r := User_{}
  428. Admin_r.User = Account.AdminToAdmin_R(c.Admin_r)
  429. //Admin_r.Power, _ = Account.Read_Power_ById(admin_r.T_power)
  430. //Admin_r.Menu = Account.Read_Menu_Tree(Power.T_menu, T_pid)
  431. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Admin_r}
  432. c.ServeJSON()
  433. return
  434. }
  435. func (c *UserController) User_Post() {
  436. admin_r := c.Admin_r
  437. T_name := c.GetString("T_name")
  438. T_pass := c.GetString("T_pass")
  439. T_phone := c.GetString("T_phone")
  440. T_mail := c.GetString("T_mail")
  441. T_wx := c.GetString("T_wx")
  442. if len(T_pass) < 8 && len(T_pass) > 0 {
  443. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  444. c.ServeJSON()
  445. return
  446. }
  447. if len(T_name) > 0 {
  448. admin_r.T_name = T_name
  449. }
  450. if len(T_pass) >= 8 {
  451. admin_r.T_pass = T_pass
  452. }
  453. if len(T_phone) > 0 {
  454. admin_r.T_phone = T_phone
  455. }
  456. if len(T_mail) > 0 {
  457. admin_r.T_mail = T_mail
  458. }
  459. if len(T_wx) > 0 {
  460. if T_wx == "-" {
  461. T_wx = ""
  462. }
  463. admin_r.T_wx = T_wx
  464. }
  465. is := Account.Update_Admin(admin_r, "T_name", "T_pass", "T_phone", "T_mail", "T_wx")
  466. if !is {
  467. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  468. c.ServeJSON()
  469. return
  470. }
  471. admin_r.T_pass = "******"
  472. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "修改个人信息", admin_r)
  473. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  474. c.ServeJSON()
  475. return
  476. }
  477. func (c *UserController) User_Add() {
  478. T_power, _ := c.GetInt("T_power")
  479. T_name := c.GetString("T_name")
  480. T_user := c.GetString("T_user")
  481. T_pass := c.GetString("T_pass")
  482. T_phone := c.GetString("T_phone")
  483. T_mail := c.GetString("T_mail")
  484. T_wx := c.GetString("T_wx")
  485. T_uuid := uuid.NewV4().String()
  486. var_ := Account.Admin{
  487. T_uuid: T_uuid,
  488. T_pid: c.T_pid,
  489. T_name: T_name,
  490. T_user: T_user,
  491. T_phone: T_phone,
  492. T_mail: T_mail,
  493. T_wx: T_wx,
  494. T_pass: T_pass,
  495. T_power: T_power,
  496. T_State: 1,
  497. }
  498. if len(T_pass) < 8 {
  499. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  500. c.ServeJSON()
  501. return
  502. }
  503. _, err := Account.Read_Admin_T_user(T_user)
  504. if err == nil {
  505. c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"}
  506. c.ServeJSON()
  507. return
  508. }
  509. _, err = Account.Add_Admin(var_)
  510. if err != nil {
  511. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  512. c.ServeJSON()
  513. return
  514. }
  515. var_.T_pass = "******"
  516. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "添加", var_)
  517. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  518. c.ServeJSON()
  519. return
  520. }
  521. func (c *UserController) User_Edit() {
  522. T_uuid := c.GetString("T_uuid")
  523. if len(T_uuid) < 8 {
  524. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  525. c.ServeJSON()
  526. return
  527. }
  528. r, err := Account.Read_Admin_ByUuid(T_uuid)
  529. if err != nil {
  530. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  531. c.ServeJSON()
  532. return
  533. }
  534. T_power, T_power_err := c.GetInt("T_power")
  535. T_name := c.GetString("T_name")
  536. T_pass := c.GetString("T_pass")
  537. T_phone := c.GetString("T_phone")
  538. T_mail := c.GetString("T_mail")
  539. if T_power_err == nil {
  540. r.T_power = T_power
  541. }
  542. if len(T_pass) < 8 && len(T_pass) > 0 {
  543. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  544. c.ServeJSON()
  545. return
  546. }
  547. if len(T_name) > 0 {
  548. r.T_name = T_name
  549. }
  550. if len(T_pass) >= 8 {
  551. r.T_pass = T_pass
  552. }
  553. if len(T_phone) > 0 {
  554. r.T_phone = T_phone
  555. }
  556. if len(T_mail) > 0 {
  557. r.T_mail = T_mail
  558. }
  559. is := Account.Update_Admin(r, "T_power", "T_pid", "T_name", "T_pass", "T_phone", "T_mail")
  560. if !is {
  561. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  562. c.ServeJSON()
  563. return
  564. }
  565. if len(T_name) > 0 {
  566. if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, T_name); !is {
  567. logs.Info("修改用户 => 修改用户关联报警通知失败", T_uuid, T_name)
  568. }
  569. }
  570. r.T_pass = "******"
  571. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "编辑", r)
  572. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  573. c.ServeJSON()
  574. return
  575. }
  576. func (c *UserController) User_Del() {
  577. T_uuid := c.GetString("T_uuid")
  578. if len(T_uuid) <= 0 {
  579. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  580. c.ServeJSON()
  581. return
  582. }
  583. admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid)
  584. if err != nil {
  585. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  586. c.ServeJSON()
  587. return
  588. }
  589. if is := Account.Delete_Admin(admin_r_lod); !is {
  590. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  591. c.ServeJSON()
  592. return
  593. }
  594. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is {
  595. logs.Info("删除用户 => 删除用户关联报警通知失败", T_uuid)
  596. }
  597. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "删除", T_uuid)
  598. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  599. c.ServeJSON()
  600. return
  601. }
  602. // 管理员
  603. func (c *UserController) User_Home() {
  604. // 报警标识
  605. //设备管理-设备报警 callthepolice
  606. //公司管理-设备报警 companyCallthepolice
  607. //T_warning_sign := c.GetString("T_warning_sign")
  608. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  609. if err != nil {
  610. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"}
  611. c.ServeJSON()
  612. return
  613. }
  614. ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid)
  615. var T_warning_sign string
  616. for _, v := range ret.MenuPermission {
  617. if v == Warning.CompanyCallthepolice {
  618. T_warning_sign = Warning.CompanyCallthepolice
  619. break
  620. }
  621. if v == Warning.Callthepolice {
  622. T_warning_sign = Warning.Callthepolice
  623. }
  624. }
  625. // 验证登录
  626. admin_r := c.Admin_r
  627. type NoticeRecord struct {
  628. Sms int // 短信
  629. Phone int // 电话
  630. }
  631. type Info struct {
  632. T_name string // 姓名
  633. T_pids string // 内部用户绑定的公司id
  634. T_pid int // 公司id
  635. T_Days int // 服务天数
  636. Admin []Account.Admin_ // 专属管理员
  637. WarningSign string // 报警标识
  638. WarningNum int64 // 报警数量
  639. UntreatedWarningNum int64 // 未处理报警数量
  640. NewsNum int64 // 消息数量
  641. WorkOrderNum int64 // 工单数量
  642. WarningRate float32 // 设备报警率
  643. UntreatedWorkOrderNum int64 // 未处理工单数量
  644. Balance float32 // 余额
  645. NoticeRecord NoticeRecord // 通知记录
  646. CompanyKey string
  647. CompanyName string
  648. }
  649. var info Info
  650. info.T_name = admin_r.T_name
  651. info.T_pid = admin_r.T_pid
  652. var Company_r Account.Company
  653. if c.T_pid > 0 {
  654. Company_r, err = Account.Read_Company_ById(c.T_pid)
  655. if err != nil {
  656. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  657. c.ServeJSON()
  658. return
  659. }
  660. info.CompanyKey = Company_r.T_key
  661. info.CompanyName = Company_r.T_name
  662. }
  663. if admin_r.T_pid > 0 {
  664. info.WarningSign = T_warning_sign
  665. bindSN, err := Account.Read_UserDevice_List(c.Admin_r)
  666. if err != nil {
  667. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  668. c.ServeJSON()
  669. return
  670. }
  671. power, _ := Account.Read_Power_ById(admin_r.T_power)
  672. company, _ := Account.Read_Company_ById(admin_r.T_pid)
  673. info.T_Days = lib.GetDiffDays(time.Now(), company.CreateTime)
  674. AdminList := Account.Read_Admin_List_T_pids(admin_r.T_pid)
  675. var Admin_ []Account.Admin_
  676. for _, v := range AdminList {
  677. Admin_ = append(Admin_, Account.Admin_RToAdmin_(v))
  678. }
  679. info.Admin = Admin_
  680. T_pids := []int{}
  681. // 拥有公司管理-设备报警权限 统计公司及其所有子公司的今日报警数量、未处理报警数量、我的消息数量
  682. if T_warning_sign == Warning.CompanyCallthepolice {
  683. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  684. info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 1, bindSN, power.T_warning, true)
  685. info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 3, bindSN, power.T_warning, false)
  686. info.NewsNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 1, bindSN, "T1003|T1002|", false)
  687. }
  688. // 拥有设备管理-设备报警权限 统计本公司今日报警数量、未处理报警数量、我的消息数量
  689. if T_warning_sign == Warning.Callthepolice {
  690. info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 1, bindSN, power.T_warning, true)
  691. info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 3, bindSN, power.T_warning, false)
  692. info.NewsNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 1, bindSN, "T1003|T1002|", false)
  693. }
  694. info.Balance = company.T_money
  695. info.NoticeRecord.Sms = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 1)
  696. info.NoticeRecord.Phone = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 2)
  697. }
  698. if admin_r.T_pid == 0 {
  699. if len(admin_r.T_pids) > 0 && admin_r.T_pids != "*" {
  700. info.T_pids = admin_r.T_pids
  701. info.WarningRate = Warning.Read_WarningRate_Yesterday(admin_r.T_uuid)
  702. }
  703. info.WorkOrderNum, info.UntreatedWorkOrderNum = NatsServer.Read_WorkOrderT_State_Count(admin_r.T_pids)
  704. }
  705. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: info}
  706. c.ServeJSON()
  707. return
  708. }
  709. // 内部用户管理
  710. func (c *UserController) Admin_List() {
  711. var r_jsons lib.R_JSONS
  712. page, _ := c.GetInt("page")
  713. if page < 1 {
  714. page = 1
  715. }
  716. page_z, _ := c.GetInt("page_z")
  717. if page_z < 1 {
  718. page_z = conf.Page_size
  719. }
  720. T_name := c.GetString("T_name")
  721. if c.Admin_r.T_pid > 0 {
  722. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
  723. c.ServeJSON()
  724. return
  725. }
  726. T_bind_company, _ := c.GetInt("T_bind_company") // 管理员绑定公司 0-全部 1-绑定公司
  727. Account.Read_Power_All_Map()
  728. userList, num := Account.Read_Admin_List(0, T_name, page, page_z)
  729. r_jsons.Data, r_jsons.Num = userList, num
  730. // 过滤已绑定公司列表
  731. if T_bind_company == 1 {
  732. var adminList []Account.Admin_R
  733. for _, v := range userList {
  734. if len(v.T_pids) == 0 || v.T_pids == "*" {
  735. continue
  736. }
  737. adminList = append(adminList, v)
  738. }
  739. r_jsons.Data = adminList
  740. }
  741. r_jsons.Page = page
  742. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  743. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  744. c.ServeJSON()
  745. return
  746. }
  747. func (c *UserController) Admin_Get() {
  748. T_uuid := c.GetString("T_uuid")
  749. if len(T_uuid) < 8 {
  750. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  751. c.ServeJSON()
  752. return
  753. }
  754. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  755. if err != nil {
  756. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  757. c.ServeJSON()
  758. return
  759. }
  760. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_R(admin_r)}
  761. c.ServeJSON()
  762. return
  763. }
  764. func (c *UserController) Admin_Add() {
  765. T_power, _ := c.GetInt("T_power")
  766. T_name := c.GetString("T_name")
  767. T_user := c.GetString("T_user")
  768. T_pass := c.GetString("T_pass")
  769. T_phone := c.GetString("T_phone")
  770. T_mail := c.GetString("T_mail")
  771. T_wx := c.GetString("T_wx")
  772. var_ := Account.Admin{
  773. T_uuid: uuid.NewV4().String(),
  774. T_pid: 0,
  775. T_name: T_name,
  776. T_user: T_user,
  777. T_phone: T_phone,
  778. T_mail: T_mail,
  779. T_wx: T_wx,
  780. T_pass: T_pass,
  781. T_power: T_power,
  782. T_State: 1,
  783. }
  784. if len(T_pass) < 8 {
  785. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  786. c.ServeJSON()
  787. return
  788. }
  789. _, err := Account.Read_Admin_T_user(T_user)
  790. if err == nil {
  791. c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"}
  792. c.ServeJSON()
  793. return
  794. }
  795. _, err = Account.Add_Admin(var_)
  796. if err != nil {
  797. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  798. c.ServeJSON()
  799. return
  800. }
  801. var_.T_pass = "******"
  802. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "添加", var_)
  803. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  804. c.ServeJSON()
  805. return
  806. }
  807. func (c *UserController) Admin_Edit() {
  808. T_uuid := c.GetString("T_uuid")
  809. if len(T_uuid) < 8 {
  810. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  811. c.ServeJSON()
  812. return
  813. }
  814. r, err := Account.Read_Admin_ByUuid(T_uuid)
  815. if err != nil {
  816. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  817. c.ServeJSON()
  818. return
  819. }
  820. T_power, T_power_err := c.GetInt("T_power")
  821. T_user := c.GetString("T_user")
  822. T_name := c.GetString("T_name")
  823. T_pass := c.GetString("T_pass")
  824. T_phone := c.GetString("T_phone")
  825. T_mail := c.GetString("T_mail")
  826. T_wx := c.GetString("T_wx")
  827. if T_power_err == nil {
  828. r.T_power = T_power
  829. }
  830. if len(T_pass) < 8 && len(T_pass) > 0 {
  831. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  832. c.ServeJSON()
  833. return
  834. }
  835. if len(T_user) > 0 {
  836. var admin Account.Admin
  837. admin, err = Account.Read_Admin_T_user(T_user)
  838. if err == nil && admin.T_uuid != T_uuid {
  839. c.Data["json"] = lib.JSONS{Code: 302, Msg: "用户名重复!"}
  840. c.ServeJSON()
  841. return
  842. }
  843. r.T_user = T_user
  844. }
  845. if len(T_name) > 0 {
  846. r.T_name = T_name
  847. }
  848. if len(T_pass) >= 8 {
  849. r.T_pass = T_pass
  850. }
  851. if len(T_phone) > 0 {
  852. r.T_phone = T_phone
  853. }
  854. if len(T_mail) > 0 {
  855. r.T_mail = T_mail
  856. }
  857. if len(T_wx) > 0 {
  858. r.T_wx = T_wx
  859. }
  860. is := Account.Update_Admin(r, "T_user", "T_power", "T_name", "T_pass", "T_phone", "T_mail", "T_wx")
  861. if !is {
  862. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  863. c.ServeJSON()
  864. return
  865. }
  866. if len(T_name) > 0 {
  867. if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, "内管-"+T_name); !is {
  868. logs.Info("修改内部用户 => 修改内部用户关联报警通知失败", T_uuid, T_name)
  869. }
  870. }
  871. r.T_pass = "********"
  872. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "修改", r)
  873. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  874. c.ServeJSON()
  875. return
  876. }
  877. func (c *UserController) Admin_Del() {
  878. T_uuid := c.GetString("T_uuid")
  879. if len(T_uuid) <= 0 {
  880. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  881. c.ServeJSON()
  882. return
  883. }
  884. admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid)
  885. if err != nil {
  886. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  887. c.ServeJSON()
  888. return
  889. }
  890. if admin_r_lod.Id == 1 {
  891. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权删除!"}
  892. c.ServeJSON()
  893. return
  894. }
  895. if is := Account.Delete_Admin(admin_r_lod); !is {
  896. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  897. c.ServeJSON()
  898. return
  899. }
  900. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is {
  901. logs.Info("删除内部用户 => 删除内部用户关联报警通知失败", T_uuid)
  902. }
  903. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "删除", T_uuid)
  904. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  905. c.ServeJSON()
  906. return
  907. }
  908. // 报警通知的用户列表
  909. func (c *UserController) User_Notice_List() {
  910. T_name := c.GetString("T_name")
  911. type R_JSONS struct {
  912. //必须的大写开头
  913. AdminList []Account.Admin_R
  914. UserList []Account.Admin_R
  915. }
  916. var AdminList, UserList []Account.Admin_R
  917. var r_jsons R_JSONS
  918. // 操作pid
  919. company_r, err := Account.Read_Company_ById(c.T_pid)
  920. if err != nil {
  921. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  922. c.ServeJSON()
  923. return
  924. }
  925. T_pids := []int{}
  926. for _, v := range strings.Split(strings.Trim(company_r.T_path, "/"), "/") {
  927. pid, _ := strconv.Atoi(v)
  928. T_pids = append(T_pids, pid)
  929. }
  930. // 绑定公司id的内部用户列表
  931. AdminList = Account.Read_Admin_List_T_pids(c.T_pid)
  932. // 绑定公司id的公司用户列表
  933. //r_jsons.UserList = Account.Read_Admin_List_T_pid(c.T_pid)
  934. // 绑定上级公司用户列表
  935. UserList = Account.Read_Parent_Admin_List(T_pids)
  936. if len(T_name) > 0 {
  937. for _, v := range AdminList {
  938. if strings.Contains(v.T_name, T_name) {
  939. r_jsons.AdminList = append(r_jsons.AdminList, v)
  940. }
  941. }
  942. for _, v := range UserList {
  943. if strings.Contains(v.T_name, T_name) {
  944. r_jsons.UserList = append(r_jsons.UserList, v)
  945. }
  946. }
  947. } else {
  948. r_jsons.AdminList = AdminList
  949. r_jsons.UserList = UserList
  950. }
  951. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  952. c.ServeJSON()
  953. return
  954. }
  955. // 管理员绑定公司
  956. func (c *UserController) AdminCompanyBind_List() {
  957. var r_jsons lib.R_JSONS
  958. T_uuid := c.GetString("T_uuid")
  959. if len(T_uuid) < 8 {
  960. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  961. c.ServeJSON()
  962. return
  963. }
  964. r, err := Account.Read_Admin_ByUuid(T_uuid)
  965. if err != nil {
  966. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  967. c.ServeJSON()
  968. return
  969. }
  970. r_jsons.Data = Account.Read_Company_List_ByT_pids(r.T_pids)
  971. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  972. c.ServeJSON()
  973. return
  974. }
  975. func (c *UserController) AdminCompanyBind_Add() {
  976. T_uuid := c.GetString("T_uuid")
  977. if len(T_uuid) < 8 {
  978. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  979. c.ServeJSON()
  980. return
  981. }
  982. T_pid, _ := c.GetInt("T_pid")
  983. if T_pid == 0 {
  984. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  985. c.ServeJSON()
  986. return
  987. }
  988. r, err := Account.Read_Admin_ByUuid(T_uuid)
  989. if err != nil {
  990. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  991. c.ServeJSON()
  992. return
  993. }
  994. if r.T_pids == "*" {
  995. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  996. c.ServeJSON()
  997. return
  998. }
  999. Company_r, err := Account.Read_Company_ById(T_pid)
  1000. if err != nil {
  1001. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1002. c.ServeJSON()
  1003. return
  1004. }
  1005. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1006. if err = Account.Add_Company_bind(r, T_pids); err != nil {
  1007. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  1008. c.ServeJSON()
  1009. return
  1010. }
  1011. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定添加", T_uuid+"-"+strconv.Itoa(T_pid))
  1012. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1013. c.ServeJSON()
  1014. return
  1015. }
  1016. func (c *UserController) AdminCompanyBind_Del() {
  1017. T_uuid := c.GetString("T_uuid")
  1018. if len(T_uuid) < 8 {
  1019. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1020. c.ServeJSON()
  1021. return
  1022. }
  1023. T_pid, _ := c.GetInt("T_pid")
  1024. if T_pid == 0 {
  1025. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1026. c.ServeJSON()
  1027. return
  1028. }
  1029. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1030. Company_r, err := Account.Read_Company_ById(T_pid)
  1031. if err != nil {
  1032. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1033. c.ServeJSON()
  1034. return
  1035. }
  1036. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1037. if len(T_pids) == 0 {
  1038. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1039. c.ServeJSON()
  1040. return
  1041. }
  1042. if err = Account.Delete_Company_bind(r, T_pids); err != nil {
  1043. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  1044. c.ServeJSON()
  1045. return
  1046. }
  1047. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid, T_pids); !is {
  1048. logs.Info("删除内部用户公司绑定 => 删除内部用户绑定的公司关联报警通知失败", T_uuid)
  1049. }
  1050. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定删除", T_uuid+"-"+strconv.Itoa(T_pid))
  1051. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1052. c.ServeJSON()
  1053. return
  1054. }
  1055. // 权限管理
  1056. func (c *UserController) Power_List() {
  1057. var r_jsons lib.R_JSONS
  1058. page, _ := c.GetInt("page")
  1059. if page < 1 {
  1060. page = 1
  1061. }
  1062. page_z, _ := c.GetInt("page_z")
  1063. if page_z < 1 {
  1064. page_z = conf.Page_size
  1065. }
  1066. Name := c.GetString("T_name")
  1067. r_jsons.Data, r_jsons.Num = Account.Read_Power_List(Name, page, page_z)
  1068. r_jsons.Page = page
  1069. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1070. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1071. c.ServeJSON()
  1072. return
  1073. }
  1074. // 添加用户 权限选择列表
  1075. func (c *UserController) Power_List_All() {
  1076. r, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1077. if err != nil {
  1078. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1079. c.ServeJSON()
  1080. return
  1081. }
  1082. var r_jsons lib.R_JSONS
  1083. r_jsons.Data = Account.Read_Power_List_ALL_User(r.T_select)
  1084. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1085. c.ServeJSON()
  1086. return
  1087. }
  1088. func (c *UserController) Power_Get() {
  1089. type Data struct {
  1090. Power Account.Power_
  1091. Menu []Account.Menu
  1092. Menu_checked []int
  1093. Select []Account.Power_R
  1094. Warning []Warning.WarningType_R
  1095. }
  1096. var data Data
  1097. id, id_err := c.GetInt("T_id")
  1098. if id_err != nil || id == 0 {
  1099. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1100. c.ServeJSON()
  1101. return
  1102. }
  1103. power, err := Account.Read_Power_ById(id)
  1104. if err != nil {
  1105. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1106. c.ServeJSON()
  1107. return
  1108. }
  1109. data.Power = Account.PowerToPower_(power)
  1110. data.Menu, data.Menu_checked = Account.Read_Menu_List_All()
  1111. if err != nil {
  1112. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"}
  1113. c.ServeJSON()
  1114. return
  1115. }
  1116. if power.T_menu != "*" {
  1117. data.Menu_checked = lib.SplitStringToIntIds(power.T_menu, "M")
  1118. }
  1119. if err != nil {
  1120. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"}
  1121. c.ServeJSON()
  1122. return
  1123. }
  1124. data.Select = Account.Read_Power_List_ALL_T_select(power.T_select)
  1125. if err != nil {
  1126. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_select Err!"}
  1127. c.ServeJSON()
  1128. return
  1129. }
  1130. data.Warning = Warning.Read_WarningType_All_Power_T_Warning(power.T_warning)
  1131. if err != nil {
  1132. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_warning Err!"}
  1133. c.ServeJSON()
  1134. return
  1135. }
  1136. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: data}
  1137. c.ServeJSON()
  1138. return
  1139. }
  1140. func (c *UserController) Power_Add() {
  1141. T_name := c.GetString("T_name")
  1142. T_select := c.GetString("T_select")
  1143. T_warning := c.GetString("T_warning")
  1144. T_menu := c.GetString("T_menu")
  1145. var_ := Account.Power{
  1146. T_name: T_name,
  1147. T_select: T_select,
  1148. T_warning: T_warning,
  1149. T_menu: T_menu,
  1150. T_State: 1,
  1151. }
  1152. _, err := Account.Add_Power(var_)
  1153. if err != nil {
  1154. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  1155. c.ServeJSON()
  1156. return
  1157. }
  1158. System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "添加", var_)
  1159. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1160. c.ServeJSON()
  1161. return
  1162. }
  1163. func (c *UserController) Power_Edit() {
  1164. Id, Id_err := c.GetInt("T_id")
  1165. if Id_err != nil || Id == 0 {
  1166. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1167. c.ServeJSON()
  1168. return
  1169. }
  1170. if Id == 1 {
  1171. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权修改!"}
  1172. c.ServeJSON()
  1173. return
  1174. }
  1175. r, err := Account.Read_Power_ById(Id)
  1176. if err != nil {
  1177. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1178. c.ServeJSON()
  1179. return
  1180. }
  1181. T_name := c.GetString("T_name")
  1182. T_select := c.GetString("T_select")
  1183. T_warning := c.GetString("T_warning")
  1184. T_menu := c.GetString("T_menu")
  1185. if len(T_name) > 0 {
  1186. r.T_name = T_name
  1187. }
  1188. if len(T_select) > 0 {
  1189. r.T_select = T_select
  1190. }
  1191. if len(T_warning) > 0 {
  1192. r.T_warning = T_warning
  1193. }
  1194. if len(T_menu) > 0 {
  1195. r.T_menu = T_menu
  1196. }
  1197. is := Account.Update_Power(r, "T_name", "T_select", "T_warning", "T_menu")
  1198. if !is {
  1199. c.Data["json"] = lib.JSONS{Code: 302, Msg: "修改失败!"}
  1200. c.ServeJSON()
  1201. return
  1202. }
  1203. System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "修改", r)
  1204. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1205. c.ServeJSON()
  1206. return
  1207. }
  1208. func (c *UserController) Power_Del() {
  1209. id, id_err := c.GetInt("T_id")
  1210. if id_err != nil || id == 0 {
  1211. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1212. c.ServeJSON()
  1213. return
  1214. }
  1215. r, err := Account.Read_Power_ById(id)
  1216. if err != nil {
  1217. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1218. c.ServeJSON()
  1219. return
  1220. }
  1221. if cnt := Account.Read_Admin_Num_ByT_power(id); cnt > 0 {
  1222. c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"}
  1223. c.ServeJSON()
  1224. return
  1225. }
  1226. is := Account.Delete_Power(r)
  1227. if !is {
  1228. c.Data["json"] = lib.JSONS{Code: 302, Msg: "删除失败!"}
  1229. c.ServeJSON()
  1230. return
  1231. }
  1232. System.Add_UserLogs(c.Admin_r.T_uuid, "权限管理", "删除", strconv.Itoa(id))
  1233. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1234. c.ServeJSON()
  1235. return
  1236. }
  1237. // 菜单列表
  1238. func (c *UserController) User_Menu_List() {
  1239. type R_JSONS struct {
  1240. //必须的大写开头
  1241. Data interface{}
  1242. Permission interface{}
  1243. }
  1244. var r_jsons R_JSONS
  1245. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1246. if err != nil {
  1247. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"}
  1248. c.ServeJSON()
  1249. return
  1250. }
  1251. //logs.Info("登录用户 菜单 pid 为:", c.T_pid)
  1252. ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid)
  1253. r_jsons.Data = ret.Menu
  1254. r_jsons.Permission = ret.Permission
  1255. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1256. c.ServeJSON()
  1257. return
  1258. }
  1259. // 权限菜单列表
  1260. func (c *UserController) Menu_List_All() {
  1261. var r_jsons lib.R_JSONS
  1262. r_jsons.Data, _ = Account.Read_Menu_List_All()
  1263. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1264. c.ServeJSON()
  1265. return
  1266. }
  1267. func (c *UserController) Menu_Excel() {
  1268. filename := fmt.Sprintf("冷链3.0菜单")
  1269. f := excelize.NewFile()
  1270. Style1, _ := f.NewStyle(
  1271. &excelize.Style{
  1272. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  1273. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1274. })
  1275. Style2, _ := f.NewStyle(
  1276. &excelize.Style{
  1277. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  1278. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1279. Border: []excelize.Border{
  1280. {Type: "left", Color: "000000", Style: 1},
  1281. {Type: "top", Color: "000000", Style: 1},
  1282. {Type: "bottom", Color: "000000", Style: 1},
  1283. {Type: "right", Color: "000000", Style: 1},
  1284. },
  1285. })
  1286. f.MergeCell("Sheet1", "A1", "G1")
  1287. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1288. f.SetCellValue("Sheet1", "A1", "Menu")
  1289. f.SetRowHeight("Sheet1", 1, 30)
  1290. f.MergeCell("Sheet1", "H1", "M1")
  1291. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1292. f.SetCellValue("Sheet1", "H1", "API")
  1293. f.SetRowHeight("Sheet1", 1, 30)
  1294. f.SetCellStyle("Sheet1", "A2", "G2", Style2)
  1295. f.SetRowHeight("Sheet1", 2, 25)
  1296. // 这里设置表头
  1297. f.SetCellValue("Sheet1", "A2", "ID")
  1298. f.SetCellValue("Sheet1", "B2", "t_mid")
  1299. f.SetCellValue("Sheet1", "C2", "t_name")
  1300. f.SetCellValue("Sheet1", "D2", "t_permission")
  1301. f.SetCellValue("Sheet1", "E2", "t_sort")
  1302. f.SetCellValue("Sheet1", "F2", "t_pid")
  1303. f.SetCellValue("Sheet1", "G2", "t_icon")
  1304. f.SetCellValue("Sheet1", "H2", "ID")
  1305. f.SetCellValue("Sheet1", "I2", "t__menu__id")
  1306. f.SetCellValue("Sheet1", "J2", "t_name")
  1307. f.SetCellValue("Sheet1", "K2", "t_uri")
  1308. f.SetCellValue("Sheet1", "L2", "t_method")
  1309. f.SetCellValue("Sheet1", "M2", "t_enable")
  1310. // 设置列宽
  1311. f.SetColWidth("Sheet1", "A", "M", 15)
  1312. line := 2
  1313. menu := Account.Read_Menu_All()
  1314. for _, m := range menu {
  1315. Menu_Call(f, m, line)
  1316. }
  1317. Style11, _ := f.NewStyle(
  1318. &excelize.Style{
  1319. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1320. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1321. Border: []excelize.Border{
  1322. {Type: "top", Color: "000000", Style: 1},
  1323. },
  1324. })
  1325. Style22, _ := f.NewStyle(
  1326. &excelize.Style{
  1327. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1328. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1329. Border: []excelize.Border{
  1330. {Type: "left", Color: "000000", Style: 1},
  1331. },
  1332. })
  1333. Style33, _ := f.NewStyle(
  1334. &excelize.Style{
  1335. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1336. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1337. Border: []excelize.Border{
  1338. {Type: "right", Color: "000000", Style: 1},
  1339. },
  1340. })
  1341. Style44, _ := f.NewStyle(
  1342. &excelize.Style{
  1343. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1344. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1345. Border: []excelize.Border{
  1346. {Type: "bottom", Color: "000000", Style: 1},
  1347. },
  1348. })
  1349. //// 循环写入数据
  1350. for _, v := range menu {
  1351. line1 := line
  1352. line = Menu_Call(f, v, line)
  1353. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line1), fmt.Sprintf("M%d", line1), Style11)
  1354. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("A%d", line), Style22)
  1355. f.SetCellStyle("Sheet1", fmt.Sprintf("M%d", line), fmt.Sprintf("M%d", line), Style33)
  1356. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("M%d", line), Style44)
  1357. }
  1358. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  1359. // 保存文件
  1360. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1361. logs.Error(err)
  1362. }
  1363. var url string
  1364. ////// 上传 OSS
  1365. //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1366. //if !is {
  1367. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1368. // c.ServeJSON()
  1369. // return
  1370. //}
  1371. ////删除目录
  1372. //err = os.Remove("ofile/" + timeStr + ".xlsx")
  1373. //if err != nil {
  1374. // logs.Error(err)
  1375. //}
  1376. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1377. c.ServeJSON()
  1378. return
  1379. }
  1380. func Menu_Call(f *excelize.File, v Account.Menu, line int) int {
  1381. line++
  1382. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.Id)
  1383. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_mid)
  1384. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_name)
  1385. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_permission)
  1386. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_sort)
  1387. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_pid)
  1388. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_icon)
  1389. apiList := Account.Read_API_List_ByMenuId(v.Id)
  1390. for _, api := range apiList {
  1391. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), api.Id)
  1392. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), api.T_Menu_Id)
  1393. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), api.T_name)
  1394. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), api.T_uri)
  1395. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), api.T_method)
  1396. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), api.T_enable)
  1397. line++
  1398. }
  1399. return line
  1400. }
  1401. // 流量池
  1402. func (c *UserController) Flow_Pool() {
  1403. var r_jsons lib.R_JSONS
  1404. r_jsons.Data, _ = lib.Yidong_group_data_margin()
  1405. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1406. c.ServeJSON()
  1407. return
  1408. }
  1409. // 账单下载
  1410. func (c *UserController) CompanyBill_Excel() {
  1411. T_month := c.GetString("T_month")
  1412. _, err := time.Parse("2006-01", T_month)
  1413. if err != nil {
  1414. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1415. c.ServeJSON()
  1416. return
  1417. }
  1418. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  1419. company, _ := Account.Read_Company_ById(c.Admin_r.T_pid)
  1420. if err != nil {
  1421. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1422. c.ServeJSON()
  1423. return
  1424. }
  1425. filename := fmt.Sprintf("%s%s年%s月账单", company.T_name, year, month)
  1426. f := excelize.NewFile()
  1427. Style1, _ := f.NewStyle(
  1428. &excelize.Style{
  1429. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  1430. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1431. })
  1432. Style2, _ := f.NewStyle(
  1433. &excelize.Style{
  1434. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  1435. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1436. Border: []excelize.Border{
  1437. {Type: "left", Color: "000000", Style: 1},
  1438. {Type: "top", Color: "000000", Style: 1},
  1439. {Type: "bottom", Color: "000000", Style: 1},
  1440. {Type: "right", Color: "000000", Style: 1},
  1441. },
  1442. })
  1443. f.MergeCell("Sheet1", "A1", "G1")
  1444. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1445. f.SetCellValue("Sheet1", "A1", filename)
  1446. f.SetRowHeight("Sheet1", 1, 30)
  1447. f.SetCellStyle("Sheet1", "A2", "G2", Style2)
  1448. f.SetRowHeight("Sheet1", 2, 25)
  1449. // 这里设置表头
  1450. f.SetCellValue("Sheet1", "A2", "编号")
  1451. f.SetCellValue("Sheet1", "B2", "项目")
  1452. f.SetCellValue("Sheet1", "C2", "说明")
  1453. f.SetCellValue("Sheet1", "D2", "扣费/充值")
  1454. f.SetCellValue("Sheet1", "E2", "金额(元)")
  1455. f.SetCellValue("Sheet1", "F2", "余额(元)")
  1456. f.SetCellValue("Sheet1", "G2", "时间")
  1457. // 设置列宽
  1458. f.SetColWidth("Sheet1", "A", "A", 10)
  1459. f.SetColWidth("Sheet1", "B", "B", 50)
  1460. f.SetColWidth("Sheet1", "C", "C", 15)
  1461. f.SetColWidth("Sheet1", "D", "D", 12)
  1462. f.SetColWidth("Sheet1", "E", "E", 15)
  1463. f.SetColWidth("Sheet1", "F", "F", 15)
  1464. f.SetColWidth("Sheet1", "G", "G", 20)
  1465. line := 2
  1466. CompanyBill_List, err := Company.Read_CompanyBill_List(c.Admin_r.T_pid, T_month)
  1467. if err != nil {
  1468. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1469. c.ServeJSON()
  1470. return
  1471. }
  1472. // 循环写入数据
  1473. for i, v := range CompanyBill_List {
  1474. line++
  1475. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  1476. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_project)
  1477. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_bill)
  1478. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_type)
  1479. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_charging)
  1480. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_balance)
  1481. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.CreateTime.Format("2006-01-02"))
  1482. }
  1483. Style4, _ := f.NewStyle(
  1484. &excelize.Style{
  1485. Font: &excelize.Font{Size: 12, Family: "宋体"},
  1486. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1487. Border: []excelize.Border{
  1488. {Type: "left", Color: "000000", Style: 1},
  1489. {Type: "top", Color: "000000", Style: 1},
  1490. {Type: "bottom", Color: "000000", Style: 1},
  1491. {Type: "right", Color: "000000", Style: 1},
  1492. },
  1493. })
  1494. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("G%d", line), Style4)
  1495. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  1496. // 保存文件
  1497. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1498. logs.Error(err)
  1499. }
  1500. var url string
  1501. //// 上传 OSS
  1502. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1503. if !is {
  1504. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1505. c.ServeJSON()
  1506. return
  1507. }
  1508. //删除目录
  1509. err = os.Remove("ofile/" + timeStr + ".xlsx")
  1510. if err != nil {
  1511. logs.Error(err)
  1512. }
  1513. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1514. c.ServeJSON()
  1515. return
  1516. }
  1517. // 获取微信支付二维码
  1518. func (c *UserController) WxPay_QRCode() {
  1519. if c.Admin_r.T_pid == 0 {
  1520. c.Data["json"] = lib.JSONS{Code: 202, Msg: "非公司用户不能充值"}
  1521. c.ServeJSON()
  1522. return
  1523. }
  1524. Total, _ := c.GetFloat("Total")
  1525. Title := "冷链3.0-" + c.Admin_r.T_user
  1526. res, err := lib.PayTransactionNative(Title, Total)
  1527. if err != nil {
  1528. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1529. c.ServeJSON()
  1530. return
  1531. }
  1532. if res.Code != 200 {
  1533. c.Data["json"] = lib.JSONS{Code: 202, Msg: res.Message}
  1534. c.ServeJSON()
  1535. return
  1536. }
  1537. var_ := Company.CompanyPayOrder{
  1538. T_pid: c.Admin_r.T_pid,
  1539. T_type: "微信",
  1540. T_title: Title,
  1541. T_total: Total,
  1542. T_order_no: res.OrderNo,
  1543. T_remark: c.Admin_r.T_uuid,
  1544. }
  1545. _, err = Company.Add_CompanyPayOrder(var_)
  1546. if err != nil {
  1547. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加订单失败!"}
  1548. c.ServeJSON()
  1549. return
  1550. }
  1551. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: res}
  1552. c.ServeJSON()
  1553. return
  1554. }
  1555. // 微信支付回调
  1556. func (c *UserController) WxPay_Notify() {
  1557. type RequestBody struct {
  1558. TradeNo string
  1559. Status string
  1560. }
  1561. type JSON struct {
  1562. Code int
  1563. Message string
  1564. }
  1565. buf := make([]byte, 1024)
  1566. n, _ := c.Ctx.Request.Body.Read(buf)
  1567. logs.Info("Body================ ", string(buf[0:n]))
  1568. //解密
  1569. adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey))
  1570. //adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey))
  1571. var body RequestBody
  1572. logs.Info("data================ ", string(adc_str))
  1573. err := json.Unmarshal(adc_str, &body)
  1574. if err != nil {
  1575. c.Data["json"] = JSON{Code: 202, Message: "json.Unmarshal is err:" + err.Error()}
  1576. c.ServeJSON()
  1577. }
  1578. payOrder, err := Company.Get_CompanyPayOrder_ByT_order_no(body.TradeNo)
  1579. if err != nil {
  1580. c.Data["json"] = JSON{Code: 202, Message: "查询失败!"}
  1581. c.ServeJSON()
  1582. return
  1583. }
  1584. //交易状态
  1585. //SUCCESS:支付成功
  1586. //REFUND:转入退款
  1587. //NOTPAY:未支付
  1588. //CLOSED:已关闭
  1589. //REVOKED:已撤销(付款码支付)
  1590. //USERPAYING:用户支付中(付款码支付)
  1591. //PAYERROR:支付失败(其他原因,如银行返回失败)
  1592. payOrder.T_status = body.Status
  1593. err = Company.Update_CompanyPayOrder(payOrder, "T_status")
  1594. if err != nil {
  1595. c.Data["json"] = JSON{Code: 202, Message: "更新状态失败!"}
  1596. c.ServeJSON()
  1597. return
  1598. }
  1599. // 添加充值记录
  1600. // 余额加上充值金额
  1601. if body.Status == "SUCCESS" {
  1602. Company_r, err := Account.Read_Company_ById(payOrder.T_pid)
  1603. if err != nil {
  1604. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1605. c.ServeJSON()
  1606. return
  1607. }
  1608. T_money64, _ := decimal.NewFromFloat(float64(Company_r.T_money) + payOrder.T_total).Round(2).Float64()
  1609. Company_r.T_money = float32(T_money64)
  1610. is := Account.Update_Company(Company_r, "T_money")
  1611. if !is {
  1612. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  1613. c.ServeJSON()
  1614. return
  1615. }
  1616. }
  1617. c.Data["json"] = JSON{Code: 200, Message: "成功"}
  1618. c.ServeJSON()
  1619. return
  1620. }
  1621. // 获取微信支付订单状态
  1622. func (c *UserController) WxPay_GetOrderState() {
  1623. OrderNo := c.GetString("OrderNo")
  1624. payOrder, err := Company.Get_CompanyPayOrder_ByT_order_no(OrderNo)
  1625. if err != nil {
  1626. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1627. c.ServeJSON()
  1628. return
  1629. }
  1630. c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功", Data: payOrder}
  1631. c.ServeJSON()
  1632. return
  1633. }
  1634. // 设备分类绑定
  1635. func (c *UserController) UserDeviceBind_List() {
  1636. var r_jsons lib.R_JSONS
  1637. page, _ := c.GetInt("page")
  1638. if page < 1 {
  1639. page = 1
  1640. }
  1641. page_z, _ := c.GetInt("page_z")
  1642. if page_z < 1 {
  1643. page_z = conf.Page_size
  1644. }
  1645. T_name := c.GetString("T_name")
  1646. T_uuid := c.GetString("T_uuid")
  1647. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1648. if err != nil {
  1649. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1650. c.ServeJSON()
  1651. return
  1652. }
  1653. bindSN, err := Account.Read_UserDevice_List(admin_r)
  1654. if err != nil {
  1655. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1656. c.ServeJSON()
  1657. return
  1658. }
  1659. if len(bindSN) == 0 {
  1660. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1661. c.ServeJSON()
  1662. return
  1663. }
  1664. r_jsons.Data, r_jsons.Num = Device.Read_Device_List(&c.Admin_r, bindSN, c.T_pid, T_name, "", "", -1, page, page_z)
  1665. r_jsons.Page = page
  1666. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1667. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1668. c.ServeJSON()
  1669. return
  1670. }
  1671. func (c *UserController) UserDeviceUnbind_List() {
  1672. var r_jsons lib.R_JSONS
  1673. page, _ := c.GetInt("page")
  1674. if page < 1 {
  1675. page = 1
  1676. }
  1677. page_z, _ := c.GetInt("page_z")
  1678. if page_z < 1 {
  1679. page_z = conf.Page_size
  1680. }
  1681. T_name := c.GetString("T_name")
  1682. T_uuid := c.GetString("T_uuid")
  1683. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1684. if err != nil {
  1685. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1686. c.ServeJSON()
  1687. return
  1688. }
  1689. bindSN, err := Account.Read_UserDevice_List(admin_r)
  1690. if err != nil {
  1691. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1692. c.ServeJSON()
  1693. return
  1694. }
  1695. r_jsons.Data, r_jsons.Num = Device.Read_User_Unbind_Device_List(&c.Admin_r, bindSN, c.T_pid, T_name, page, page_z)
  1696. r_jsons.Page = page
  1697. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1698. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1699. c.ServeJSON()
  1700. return
  1701. }
  1702. func (c *UserController) UserDeviceBind_Add() {
  1703. T_sn := c.GetString("T_sn")
  1704. T_uuid := c.GetString("T_uuid")
  1705. T_oneHey, _ := c.GetInt("T_oneKey") // 一键绑定
  1706. if T_oneHey == 1 {
  1707. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1708. if err != nil {
  1709. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1710. c.ServeJSON()
  1711. return
  1712. }
  1713. bindSN, err := Account.Read_UserDevice_List(admin_r)
  1714. if err != nil {
  1715. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1716. c.ServeJSON()
  1717. return
  1718. }
  1719. Unbind_Device_List, _ := Device.Read_User_Unbind_Device_List(&c.Admin_r, bindSN, c.T_pid, "", 0, 9999)
  1720. var UserDeviceList []Account.UserDevice
  1721. for _, r := range Unbind_Device_List {
  1722. UserDeviceList = append(UserDeviceList, Account.UserDevice{T_sn: r.T_sn, T_uuid: T_uuid})
  1723. }
  1724. if _, err = Account.Add_UserDevice_Multi(T_uuid, UserDeviceList); err != nil {
  1725. c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"}
  1726. c.ServeJSON()
  1727. return
  1728. }
  1729. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "一键绑定主机操作", T_uuid+"-"+T_sn)
  1730. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1731. c.ServeJSON()
  1732. return
  1733. }
  1734. if len(T_uuid) == 0 || len(T_sn) == 0 {
  1735. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1736. c.ServeJSON()
  1737. return
  1738. }
  1739. if len(T_uuid) < 8 {
  1740. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1741. c.ServeJSON()
  1742. return
  1743. }
  1744. device, err := Device.Read_Device_ByT_sn(T_sn)
  1745. if err != nil {
  1746. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1747. c.ServeJSON()
  1748. return
  1749. }
  1750. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1751. if err != nil {
  1752. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1753. c.ServeJSON()
  1754. return
  1755. }
  1756. if admin_r.T_pid != device.T_pid {
  1757. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1758. c.ServeJSON()
  1759. return
  1760. }
  1761. userdievice, err := Account.Read_UserDevice_ByT_uuid_T_sn(T_uuid, T_sn)
  1762. if userdievice.Id > 0 {
  1763. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复绑定!"}
  1764. c.ServeJSON()
  1765. return
  1766. }
  1767. if _, err = Account.Add_UserDevice(Account.UserDevice{T_sn: T_sn, T_uuid: T_uuid}); err != nil {
  1768. c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"}
  1769. c.ServeJSON()
  1770. return
  1771. }
  1772. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "绑定主机操作", T_uuid+"-"+T_sn)
  1773. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1774. c.ServeJSON()
  1775. return
  1776. }
  1777. func (c *UserController) UserDeviceBind_Del() {
  1778. T_sn := c.GetString("T_sn")
  1779. T_uuid := c.GetString("T_uuid")
  1780. if len(T_uuid) == 0 || len(T_sn) == 0 {
  1781. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1782. c.ServeJSON()
  1783. return
  1784. }
  1785. if len(T_uuid) < 8 {
  1786. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1787. c.ServeJSON()
  1788. return
  1789. }
  1790. device, err := Device.Read_Device_ByT_sn(T_sn)
  1791. if err != nil {
  1792. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  1793. c.ServeJSON()
  1794. return
  1795. }
  1796. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1797. if err != nil {
  1798. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1799. c.ServeJSON()
  1800. return
  1801. }
  1802. if admin_r.T_pid != device.T_pid {
  1803. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1804. c.ServeJSON()
  1805. return
  1806. }
  1807. userdievice, err := Account.Read_UserDevice_ByT_uuid_T_sn(T_uuid, T_sn)
  1808. if err != nil {
  1809. if err.Error() == orm.ErrNoRows.Error() {
  1810. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1811. c.ServeJSON()
  1812. return
  1813. }
  1814. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解绑失败!"}
  1815. c.ServeJSON()
  1816. return
  1817. }
  1818. if err = Account.Delete_UserDevice(Account.UserDevice{Id: userdievice.Id, T_sn: T_sn, T_uuid: T_uuid}); err != nil {
  1819. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除失败"}
  1820. c.ServeJSON()
  1821. return
  1822. }
  1823. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "解绑主机操作", T_uuid+"-"+T_sn)
  1824. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1825. c.ServeJSON()
  1826. return
  1827. }