Basic.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. package controllers
  2. import (
  3. "ERP_storage/Nats/NatsServer"
  4. "ERP_storage/conf"
  5. "ERP_storage/models/Account"
  6. "ERP_storage/models/Basic"
  7. "ERP_storage/models/Percentage"
  8. "ERP_storage/models/Stock"
  9. "fmt"
  10. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  11. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  12. "github.com/beego/beego/v2/adapter/orm"
  13. beego "github.com/beego/beego/v2/server/web"
  14. "math"
  15. "strconv"
  16. )
  17. type BasicController struct {
  18. beego.Controller
  19. User userlibs.User
  20. }
  21. func (c *BasicController) Prepare() {
  22. c.User = *Account.User_r
  23. }
  24. func (c *BasicController) Depot_List() {
  25. // 分页参数 初始化
  26. page, _ := c.GetInt("page")
  27. if page < 1 {
  28. page = 1
  29. }
  30. page_z, _ := c.GetInt("page_z")
  31. if page_z < 1 {
  32. page_z = conf.Page_size
  33. }
  34. // 查询
  35. T_name := c.GetString("T_name")
  36. R_List, R_cnt := Basic.Read_Depot_List(T_name, page, page_z)
  37. var r_jsons lib.R_JSONS
  38. r_jsons.Num = R_cnt
  39. r_jsons.Data = R_List
  40. r_jsons.Page = page
  41. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  42. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  43. c.ServeJSON()
  44. return
  45. }
  46. func (c *BasicController) Depot_Add() {
  47. T_name := c.GetString("T_name")
  48. var_ := Basic.Depot{
  49. T_name: T_name,
  50. T_State: 1,
  51. }
  52. Id, err := Basic.Add_Depot(var_)
  53. if err != nil {
  54. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  55. c.ServeJSON()
  56. return
  57. }
  58. NatsServer.AddUserLogs(c.User.T_uuid, "仓库", "添加", var_)
  59. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  60. c.ServeJSON()
  61. return
  62. }
  63. func (c *BasicController) Depot_Edit() {
  64. T_id, _ := c.GetInt("T_id")
  65. T_name := c.GetString("T_name")
  66. Depot_r, err := Basic.Read_Depot_ById(T_id)
  67. if err != nil {
  68. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  69. c.ServeJSON()
  70. return
  71. }
  72. if len(T_name) > 0 {
  73. Depot_r.T_name = T_name
  74. }
  75. if err = Basic.Update_Depot(Depot_r, "T_name"); err != nil {
  76. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  77. c.ServeJSON()
  78. return
  79. }
  80. NatsServer.AddUserLogs(c.User.T_uuid, "仓库", "修改", Depot_r)
  81. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  82. c.ServeJSON()
  83. return
  84. }
  85. func (c *BasicController) Depot_Del() {
  86. T_id, _ := c.GetInt("T_id")
  87. Depot_r, err := Basic.Read_Depot_ById(T_id)
  88. if err != nil {
  89. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  90. c.ServeJSON()
  91. return
  92. }
  93. StockDao := Stock.NewStock(orm.NewOrm())
  94. count := StockDao.Read_Stock_Count_ByT_depot_id(Depot_r.Id)
  95. if count > 0 {
  96. c.Data["json"] = lib.JSONS{Code: 202, Msg: "该仓库库存不为空,禁止删除!"}
  97. c.ServeJSON()
  98. return
  99. }
  100. if err = Basic.Delete_Depot(Depot_r); err != nil {
  101. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  102. c.ServeJSON()
  103. return
  104. }
  105. NatsServer.AddUserLogs(c.User.T_uuid, "仓库", "删除", strconv.Itoa(T_id))
  106. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  107. c.ServeJSON()
  108. return
  109. }
  110. // 产品型号
  111. func (c *BasicController) ProductModel_List() {
  112. // 查询
  113. T_name := c.GetString("T_name")
  114. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Basic.Read_ProductModel_List(T_name)}
  115. c.ServeJSON()
  116. return
  117. }
  118. func (c *BasicController) ProductName_List() {
  119. // 查询
  120. T_name := c.GetString("T_name")
  121. T_class, _ := c.GetInt("T_class")
  122. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Basic.Read_ProductName_List(T_name, T_class)}
  123. c.ServeJSON()
  124. return
  125. }
  126. // 产品规格
  127. func (c *BasicController) ProductSpec_List() {
  128. // 查询
  129. T_name := c.GetString("T_name")
  130. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Basic.Read_ProductSpec_List(T_name)}
  131. c.ServeJSON()
  132. return
  133. }
  134. func (c *BasicController) ProductClass_List() {
  135. // 分页参数 初始化
  136. page, _ := c.GetInt("page")
  137. if page < 1 {
  138. page = 1
  139. }
  140. page_z, _ := c.GetInt("page_z")
  141. if page_z < 1 {
  142. page_z = conf.Page_size
  143. }
  144. // 查询
  145. T_name := c.GetString("T_name")
  146. R_List, R_cnt := Basic.Read_ProductClass_List(T_name, page, page_z)
  147. var r_jsons lib.R_JSONS
  148. r_jsons.Num = R_cnt
  149. r_jsons.Data = R_List
  150. r_jsons.Page = page
  151. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  152. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  153. c.ServeJSON()
  154. return
  155. }
  156. func (c *BasicController) ProductClass_Add() {
  157. T_name := c.GetString("T_name")
  158. var_ := Basic.ProductClass{
  159. T_name: T_name,
  160. T_State: 1,
  161. }
  162. Id, err := Basic.Add_ProductClass(var_)
  163. if err != nil {
  164. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  165. c.ServeJSON()
  166. return
  167. }
  168. NatsServer.AddUserLogs(c.User.T_uuid, "仓库", "添加", var_)
  169. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  170. c.ServeJSON()
  171. return
  172. }
  173. func (c *BasicController) ProductClass_Edit() {
  174. T_id, _ := c.GetInt("T_id")
  175. T_name := c.GetString("T_name")
  176. ProductClass_r, err := Basic.Read_ProductClass_ById(T_id)
  177. if err != nil {
  178. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  179. c.ServeJSON()
  180. return
  181. }
  182. if len(T_name) > 0 {
  183. ProductClass_r.T_name = T_name
  184. }
  185. if err = Basic.Update_ProductClass(ProductClass_r, "T_name"); err != nil {
  186. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  187. c.ServeJSON()
  188. return
  189. }
  190. NatsServer.AddUserLogs(c.User.T_uuid, "仓库", "修改", ProductClass_r)
  191. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  192. c.ServeJSON()
  193. return
  194. }
  195. func (c *BasicController) ProductClass_Del() {
  196. T_id, _ := c.GetInt("T_id")
  197. ProductClass_r, err := Basic.Read_ProductClass_ById(T_id)
  198. if err != nil {
  199. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  200. c.ServeJSON()
  201. return
  202. }
  203. if err = Basic.Delete_ProductClass(ProductClass_r); err != nil {
  204. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  205. c.ServeJSON()
  206. return
  207. }
  208. NatsServer.AddUserLogs(c.User.T_uuid, "仓库", "删除", strconv.Itoa(T_id))
  209. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  210. c.ServeJSON()
  211. return
  212. }
  213. func (c *BasicController) Product_List() {
  214. // 分页参数 初始化
  215. page, _ := c.GetInt("page")
  216. if page < 1 {
  217. page = 1
  218. }
  219. page_z, _ := c.GetInt("page_z")
  220. if page_z < 1 {
  221. page_z = conf.Page_size
  222. }
  223. // 查询
  224. T_name := c.GetString("T_name")
  225. T_model := c.GetString("T_model")
  226. T_class, _ := c.GetInt("T_class")
  227. T_VerifyContract, _ := c.GetInt("T_VerifyContract")
  228. R_List, R_cnt := Basic.Read_Product_List(T_name, T_model, T_class, T_VerifyContract, page, page_z)
  229. var r_jsons lib.R_JSONS
  230. r_jsons.Num = R_cnt
  231. r_jsons.Data = R_List
  232. r_jsons.Page = page
  233. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  234. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  235. c.ServeJSON()
  236. return
  237. }
  238. func (c *BasicController) Product_Add() {
  239. T_name := c.GetString("T_name")
  240. T_class, _ := c.GetInt("T_class")
  241. T_model := c.GetString("T_model")
  242. T_spec := c.GetString("T_spec")
  243. T_relation_sn, _ := c.GetInt("T_relation_sn")
  244. T_price, _ := c.GetFloat("T_price")
  245. T_img := c.GetString("T_img")
  246. T_remark := c.GetString("T_remark")
  247. if len(T_name) == 0 {
  248. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品名称不能为空!"}
  249. c.ServeJSON()
  250. return
  251. }
  252. product, err := Basic.Read_Product_ByT_name(T_name, T_model, T_spec)
  253. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  254. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  255. c.ServeJSON()
  256. return
  257. }
  258. if product.Id > 0 {
  259. c.Data["json"] = lib.JSONS{Code: 203, Msg: "该产品已存在"}
  260. c.ServeJSON()
  261. return
  262. }
  263. var_ := Basic.Product{
  264. T_name: T_name,
  265. T_class: T_class,
  266. T_model: T_model,
  267. T_spec: T_spec,
  268. T_price: float32(T_price),
  269. T_relation_sn: T_relation_sn,
  270. T_img: T_img,
  271. T_remark: T_remark,
  272. T_State: 1,
  273. }
  274. Id, err := Basic.Add_Product(var_)
  275. if err != nil {
  276. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  277. c.ServeJSON()
  278. return
  279. }
  280. NatsServer.AddUserLogs(c.User.T_uuid, "产品", "添加", var_)
  281. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  282. c.ServeJSON()
  283. return
  284. }
  285. func (c *BasicController) Product_Edit() {
  286. T_id, _ := c.GetInt("T_id")
  287. T_name := c.GetString("T_name")
  288. T_class, _ := c.GetInt("T_class")
  289. T_model := c.GetString("T_model")
  290. T_spec := c.GetString("T_spec")
  291. T_price, _ := c.GetFloat("T_price")
  292. T_relation_sn, _ := c.GetInt("T_relation_sn")
  293. T_img := c.GetString("T_img")
  294. T_remark := c.GetString("T_remark")
  295. Product_r, err := Basic.Read_Product_ById(T_id)
  296. if err != nil {
  297. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  298. c.ServeJSON()
  299. return
  300. }
  301. if len(T_name) > 0 {
  302. Product_r.T_name = T_name
  303. }
  304. if T_class > 0 {
  305. Product_r.T_class = T_class
  306. }
  307. if len(T_model) > 0 {
  308. Product_r.T_model = T_model
  309. }
  310. if len(T_spec) > 0 {
  311. Product_r.T_spec = T_spec
  312. }
  313. if T_relation_sn > 0 {
  314. Product_r.T_relation_sn = T_relation_sn
  315. }
  316. if len(T_img) > 0 {
  317. Product_r.T_img = T_img
  318. }
  319. if len(T_remark) > 0 {
  320. Product_r.T_remark = T_remark
  321. }
  322. if T_price > 0 {
  323. Product_r.T_price = float32(T_price)
  324. }
  325. if err = Basic.Update_Product(Product_r, "T_name", "T_class", "T_model", "T_spec", "T_price", "T_relation_sn", "T_img", "T_remark"); err != nil {
  326. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  327. c.ServeJSON()
  328. return
  329. }
  330. // 修改库存关联的产品信息
  331. StockDao := Stock.NewStock(orm.NewOrm())
  332. list, err := StockDao.Read_Stock_List_ByT_product_id(T_id)
  333. for _, v := range list {
  334. v.T_product_class = Product_r.T_class
  335. v.T_product_name = Product_r.T_name
  336. v.T_product_model = Product_r.T_model
  337. StockDao.Update_Stock(v, "T_product_class", "T_product_name", "T_product_model")
  338. }
  339. // 修改月度统计信息
  340. // 修改库存关联的产品信息
  341. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  342. stockMonthList, err := StockMonthDao.Read_StockMonth_ByT_product_id(T_id)
  343. for _, v := range stockMonthList {
  344. v.T_product_class = Product_r.T_class
  345. StockMonthDao.Update_StockMonth(v, "T_product_class")
  346. }
  347. NatsServer.AddUserLogs(c.User.T_uuid, "产品", "修改", Product_r)
  348. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  349. c.ServeJSON()
  350. return
  351. }
  352. func (c *BasicController) Product_Del() {
  353. T_id, _ := c.GetInt("T_id")
  354. Product_r, err := Basic.Read_Product_ById(T_id)
  355. if err != nil {
  356. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  357. c.ServeJSON()
  358. return
  359. }
  360. if err = Basic.Delete_Product(Product_r); err != nil {
  361. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  362. c.ServeJSON()
  363. return
  364. }
  365. NatsServer.AddUserLogs(c.User.T_uuid, "产品", "删除", strconv.Itoa(T_id))
  366. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  367. c.ServeJSON()
  368. return
  369. }
  370. // 修改产品排序
  371. func (c *BasicController) Product_Edit_Sort() {
  372. T_id, _ := c.GetInt("T_id")
  373. T_sort, _ := c.GetInt("T_sort")
  374. Product_r, err := Basic.Read_Product_ById(T_id)
  375. if err != nil {
  376. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  377. c.ServeJSON()
  378. return
  379. }
  380. Product_r.T_sort = T_sort
  381. if err = Basic.Update_Product(Product_r, "T_sort"); err != nil {
  382. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
  383. c.ServeJSON()
  384. return
  385. }
  386. NatsServer.AddUserLogs(c.User.T_uuid, "产品", "修改排序", Product_r)
  387. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  388. c.ServeJSON()
  389. return
  390. }
  391. // 验证项目
  392. func (c *BasicController) VerifyItem_List() {
  393. // 分页参数 初始化
  394. page, _ := c.GetInt("page")
  395. if page < 1 {
  396. page = 1
  397. }
  398. page_z, _ := c.GetInt("page_z")
  399. if page_z < 1 {
  400. page_z = conf.Page_size
  401. }
  402. // 查询
  403. T_type, _ := c.GetInt("T_type")
  404. T_name := c.GetString("T_name")
  405. R_List, R_cnt := Basic.Read_VerifyItem_List(T_type, T_name, page, page_z)
  406. var r_jsons lib.R_JSONS
  407. r_jsons.Num = R_cnt
  408. r_jsons.Data = R_List
  409. r_jsons.Page = page
  410. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  411. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  412. c.ServeJSON()
  413. return
  414. }
  415. func (c *BasicController) VerifyItem_Add() {
  416. T_name := c.GetString("T_name")
  417. T_type, _ := c.GetInt("T_type")
  418. T_price, _ := c.GetFloat("T_price")
  419. var_ := Basic.VerifyItem{
  420. T_name: T_name,
  421. T_type: T_type,
  422. T_price: float32(T_price),
  423. T_State: 1,
  424. }
  425. VerifyItem_r, err := Basic.Read_VerifyItem_ByT_nameT_type(T_name, T_type)
  426. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  427. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  428. c.ServeJSON()
  429. return
  430. }
  431. if VerifyItem_r.Id > 0 {
  432. c.Data["json"] = lib.JSONS{Code: 203, Msg: fmt.Sprintf("【%s】项目已存在", T_name)}
  433. c.ServeJSON()
  434. return
  435. }
  436. Id, err := Basic.Add_VerifyItem(var_)
  437. if err != nil {
  438. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  439. c.ServeJSON()
  440. return
  441. }
  442. NatsServer.AddUserLogs(c.User.T_uuid, "提成验证项目", "添加", var_)
  443. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  444. c.ServeJSON()
  445. return
  446. }
  447. func (c *BasicController) VerifyItem_Edit() {
  448. T_id, _ := c.GetInt("T_id")
  449. T_name := c.GetString("T_name")
  450. T_type, _ := c.GetInt("T_type")
  451. T_price, _ := c.GetFloat("T_price")
  452. VerifyItem_r, err := Basic.Read_VerifyItem_ById(T_id)
  453. if err != nil {
  454. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  455. c.ServeJSON()
  456. return
  457. }
  458. VerifyItem_r2, err := Basic.Read_VerifyItem_ByT_nameT_type(T_name, T_type)
  459. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  460. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  461. c.ServeJSON()
  462. return
  463. }
  464. if VerifyItem_r2.Id != VerifyItem_r.Id && VerifyItem_r2.Id > 0 {
  465. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】项目已存在", T_name)}
  466. c.ServeJSON()
  467. return
  468. }
  469. cnt, err := Percentage.Get_Percentage_Count_ByVerifyItemId(VerifyItem_r.Id)
  470. if VerifyItem_r2.Id != VerifyItem_r.Id && VerifyItem_r2.Id > 0 {
  471. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询提成项目统计失败"}
  472. c.ServeJSON()
  473. return
  474. }
  475. if VerifyItem_r.T_price != float32(T_price) && cnt > 0 {
  476. c.Data["json"] = lib.JSONS{Code: 202, Msg: "有未处理的提成项目,无法修改"}
  477. c.ServeJSON()
  478. return
  479. }
  480. if len(T_name) > 0 {
  481. VerifyItem_r.T_name = T_name
  482. }
  483. if T_type > 0 {
  484. VerifyItem_r.T_type = T_type
  485. }
  486. if T_price > 0 {
  487. VerifyItem_r.T_price = float32(T_price)
  488. }
  489. if err = Basic.Update_VerifyItem(VerifyItem_r, "T_name", "T_type", "T_price"); err != nil {
  490. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  491. c.ServeJSON()
  492. return
  493. }
  494. NatsServer.AddUserLogs(c.User.T_uuid, "提成验证项目", "修改", VerifyItem_r)
  495. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  496. c.ServeJSON()
  497. return
  498. }
  499. func (c *BasicController) VerifyItem_Del() {
  500. T_id, _ := c.GetInt("T_id")
  501. VerifyItem_r, err := Basic.Read_VerifyItem_ById(T_id)
  502. if err != nil {
  503. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  504. c.ServeJSON()
  505. return
  506. }
  507. if err = Basic.Delete_VerifyItem(VerifyItem_r); err != nil {
  508. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  509. c.ServeJSON()
  510. return
  511. }
  512. NatsServer.AddUserLogs(c.User.T_uuid, "提成验证项目", "删除", strconv.Itoa(T_id))
  513. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  514. c.ServeJSON()
  515. return
  516. }