variety_plant.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. package supply_analysis
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage/request"
  6. "eta/eta_api/models/data_manage/response"
  7. "eta/eta_api/models/data_manage/supply_analysis"
  8. supply_analysisServ "eta/eta_api/services/data/supply_analysis"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "time"
  13. )
  14. // PlantList
  15. // @Title 装置列表
  16. // @Description 装置列表
  17. // @Param VarietyId query int true "品种id"
  18. // @Success 200 {object} response.VarietyListResp
  19. // @router /variety/plant/list [get]
  20. func (this *VarietyController) PlantList() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. varietyId, _ := this.GetInt("VarietyId")
  27. if varietyId <= 0 {
  28. br.Msg = "请选择品种"
  29. br.IsSendEmail = false
  30. return
  31. }
  32. // 获取品种详情
  33. varietyInfo, err := supply_analysis.GetVarietyById(varietyId)
  34. if err != nil {
  35. br.Msg = "查询品种失败"
  36. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  37. return
  38. }
  39. if varietyInfo == nil {
  40. br.Msg = "不存在该品种或该品种已被删除"
  41. br.ErrMsg = "不存在该品种或该品种已被删除"
  42. br.IsSendEmail = false
  43. return
  44. }
  45. var hasPermission bool //是否有操作权限
  46. // 是否有装置的操作权限
  47. hasPermission, err = supply_analysisServ.HasVarietyPlantPermission(this.SysUser, varietyId)
  48. if err != nil && err.Error() != utils.ErrNoRow() {
  49. br.Msg = "获取失败"
  50. br.ErrMsg = "查询是否有装置的操作权限失败;ERR:" + err.Error()
  51. return
  52. }
  53. var list []*supply_analysis.VarietyPlantItem
  54. list, err = supply_analysis.GetAllVarietyPlantByVarietyId(varietyId)
  55. if err != nil && err.Error() != utils.ErrNoRow() {
  56. br.Msg = "获取失败"
  57. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  58. return
  59. }
  60. for k, v := range list {
  61. button := supply_analysis.VarietyPlantButton{}
  62. if hasPermission {
  63. button.Edit = true
  64. button.Delete = true
  65. }
  66. v.Button = button
  67. maintenanceDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.MaintenanceDate, time.Local)
  68. if tmpErr != nil {
  69. list[k] = v
  70. continue
  71. }
  72. day := 0
  73. if v.IsStop == 0 {
  74. resumptionDate, _ := time.ParseInLocation(utils.FormatDate, v.ResumptionDate, time.Local)
  75. day = utils.GetTimeSubDay(maintenanceDate, resumptionDate)
  76. } else {
  77. // 停产的话,就截止到今天
  78. day = utils.GetTimeSubDay(maintenanceDate, time.Now())
  79. }
  80. v.Day = day + 1
  81. v.CapacityReduction, _ = decimal.NewFromFloat(v.AverageDailyCapacityReduction * float64(v.Day)).Round(4).Float64()
  82. list[k] = v
  83. }
  84. resp := response.VarietyPlantListResp{
  85. AddButton: hasPermission,
  86. List: list,
  87. }
  88. br.Ret = 200
  89. br.Success = true
  90. br.Msg = "获取成功"
  91. br.Data = resp
  92. }
  93. // AddPlant
  94. // @Title 新增品种装置
  95. // @Description 新增装置品种接口
  96. // @Param request body request.AddVarietyPlantReq true "type json string"
  97. // @Success 200 Ret=200 添加成功
  98. // @router /variety/plant/add [post]
  99. func (this *VarietyController) AddPlant() {
  100. br := new(models.BaseResponse).Init()
  101. defer func() {
  102. this.Data["json"] = br
  103. this.ServeJSON()
  104. }()
  105. var req request.AddVarietyPlantReq
  106. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  107. if err != nil {
  108. br.Msg = "参数解析异常!"
  109. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  110. return
  111. }
  112. //if req.FactoryName == "" {
  113. // br.Msg = "请输入工厂名称"
  114. // br.IsSendEmail = false
  115. // return
  116. //}
  117. factoryName := utils.TrimLRStr(req.FactoryName)
  118. //if req.PlantName == "" {
  119. // br.Msg = "请输入装置/产线名称"
  120. // br.IsSendEmail = false
  121. // return
  122. //}
  123. // 装置名称
  124. plantName := utils.TrimLRStr(req.PlantName)
  125. var hasPermission bool //是否有操作权限
  126. // 是否有装置的操作权限
  127. hasPermission, err = supply_analysisServ.HasVarietyPlantPermission(this.SysUser, req.VarietyId)
  128. if err != nil && err.Error() != utils.ErrNoRow() {
  129. br.Msg = "获取失败"
  130. br.ErrMsg = "查询是否有装置的操作权限失败;ERR:" + err.Error()
  131. return
  132. }
  133. if !hasPermission {
  134. br.Msg = "无操作权限"
  135. br.ErrMsg = "无操作权限"
  136. return
  137. }
  138. // 获取品种详情
  139. varietyInfo, err := supply_analysis.GetVarietyById(req.VarietyId)
  140. if err != nil {
  141. br.Msg = "查询品种失败"
  142. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  143. return
  144. }
  145. if varietyInfo == nil {
  146. br.Msg = "不存在该品种或该品种已被删除"
  147. br.ErrMsg = "不存在该品种或该品种已被删除"
  148. br.IsSendEmail = false
  149. return
  150. }
  151. // 检修日期 、 复产日期
  152. var maintenanceDate, resumptionDate time.Time
  153. if req.MaintenanceDate != `` {
  154. maintenanceDate, err = time.ParseInLocation(utils.FormatDate, req.MaintenanceDate, time.Local)
  155. if err != nil {
  156. br.Msg = "检修日期异常"
  157. br.ErrMsg = "检修日期异常,maintenanceDate:" + req.MaintenanceDate + ";ERR:" + err.Error()
  158. br.IsSendEmail = false
  159. return
  160. }
  161. }
  162. if req.ResumptionDate != `` {
  163. resumptionDate, err = time.ParseInLocation(utils.FormatDate, req.ResumptionDate, time.Local)
  164. if err != nil {
  165. br.Msg = "复产日期异常"
  166. br.ErrMsg = "复产日期异常,resumptionDate:" + req.ResumptionDate + ";ERR:" + err.Error()
  167. br.IsSendEmail = false
  168. return
  169. }
  170. }
  171. // 如果两个日期都填写了,那么就校验下吧
  172. if !resumptionDate.IsZero() && !maintenanceDate.IsZero() {
  173. if resumptionDate.Before(maintenanceDate) {
  174. br.Msg = "复产日期不得早于检修日期"
  175. br.IsSendEmail = false
  176. return
  177. }
  178. }
  179. sort := 0
  180. // 如果是上一个装置id不为空的情况,也就是向下插入(复制)
  181. if req.PrevVarietyPlantId > 0 {
  182. prevItem, err := supply_analysis.GetVarietyPlantById(req.PrevVarietyPlantId)
  183. if err != nil {
  184. br.Msg = "保存失败"
  185. br.ErrMsg = fmt.Sprint("查找上一个装置失败,上一个装置id:", req.PrevVarietyPlantId, ";ERR:"+err.Error())
  186. br.IsSendEmail = false
  187. return
  188. }
  189. sort = prevItem.Sort + 1
  190. updateSortStr := `sort + 1`
  191. err = supply_analysis.UpdateEdbInfoSortByPrevId(req.VarietyId, prevItem.VarietyPlantId, prevItem.Sort, updateSortStr)
  192. if err != nil {
  193. br.Msg = "保存失败"
  194. br.ErrMsg = fmt.Sprint("移动下面的其他装置失败;ERR:" + err.Error())
  195. br.IsSendEmail = false
  196. return
  197. }
  198. } else if req.NextVarietyPlantId > 0 {
  199. // 下一个装置id不为空的情况,也就是向上插入
  200. nextItem, err := supply_analysis.GetVarietyPlantById(req.NextVarietyPlantId)
  201. if err != nil {
  202. br.Msg = "保存失败"
  203. br.ErrMsg = fmt.Sprint("查找下一个装置失败,下一个装置id:", req.NextVarietyPlantId, ";ERR:"+err.Error())
  204. br.IsSendEmail = false
  205. return
  206. }
  207. sort = nextItem.Sort - 1
  208. updateSortStr := `sort - 1`
  209. err = supply_analysis.UpdateEdbInfoSortByNextId(req.VarietyId, nextItem.VarietyPlantId, nextItem.Sort, updateSortStr)
  210. if err != nil {
  211. br.Msg = "保存失败"
  212. br.ErrMsg = fmt.Sprint("移动上面的其他装置失败;ERR:" + err.Error())
  213. br.IsSendEmail = false
  214. return
  215. }
  216. }
  217. varietyPlant := &supply_analysis.VarietyPlant{
  218. //VarietyPlantId: 0,
  219. VarietyId: req.VarietyId,
  220. Province: utils.TrimLRStr(req.Province),
  221. City: utils.TrimLRStr(req.City),
  222. FactoryName: factoryName,
  223. PlantName: plantName,
  224. MaintenanceDate: maintenanceDate,
  225. ResumptionDate: resumptionDate,
  226. AnnualCapacity: req.AnnualCapacity,
  227. Coefficient: req.Coefficient,
  228. AverageDailyCapacityReduction: req.AverageDailyCapacityReduction,
  229. IsStop: req.IsStop,
  230. Sort: sort,
  231. SysUserId: this.SysUser.AdminId,
  232. SysUserRealName: this.SysUser.RealName,
  233. ModifyTime: time.Now(),
  234. CreateTime: time.Now(),
  235. }
  236. err = supply_analysis.AddVarietyPlant(varietyPlant)
  237. if err != nil {
  238. br.Msg = "添加装置失败"
  239. br.ErrMsg = "添加装置失败,Err:" + err.Error()
  240. return
  241. }
  242. // 变更品种的更新时间
  243. {
  244. varietyInfo.ModifyTime = time.Now()
  245. varietyInfo.LastUpdateSysUserId = this.SysUser.AdminId
  246. varietyInfo.LastUpdateSysUserRealName = this.SysUser.RealName
  247. go varietyInfo.Update([]string{"ModifyTime", "LastUpdateSysUserId", "LastUpdateSysUserRealName"})
  248. }
  249. br.Ret = 200
  250. br.Msg = "添加成功"
  251. br.IsAddLog = true
  252. br.Success = true
  253. }
  254. // EditPlant
  255. // @Title 编辑品种装置接口
  256. // @Description 编辑品种装置接口
  257. // @Param request body request.EditVarietyPlantReq true "type json string"
  258. // @Success 200 Ret=200 修改成功
  259. // @router /variety/plant/edit [post]
  260. func (this *VarietyController) EditPlant() {
  261. br := new(models.BaseResponse).Init()
  262. defer func() {
  263. this.Data["json"] = br
  264. this.ServeJSON()
  265. }()
  266. var req request.EditVarietyPlantReq
  267. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  268. if err != nil {
  269. br.Msg = "参数解析异常!"
  270. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  271. return
  272. }
  273. //if req.FactoryName == "" {
  274. // br.Msg = "请输入工厂名称"
  275. // br.IsSendEmail = false
  276. // return
  277. //}
  278. factoryName := utils.TrimLRStr(req.FactoryName)
  279. //if req.PlantName == "" {
  280. // br.Msg = "请输入装置/产线名称"
  281. // br.IsSendEmail = false
  282. // return
  283. //}
  284. // 装置名称
  285. plantName := utils.TrimLRStr(req.PlantName)
  286. // 获取品种装置详情
  287. varietyPlantInfo, err := supply_analysis.GetVarietyPlantById(req.VarietyPlantId)
  288. if err != nil {
  289. br.Msg = "查询品种失败"
  290. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  291. return
  292. }
  293. var hasPermission bool //是否有操作权限
  294. // 是否有装置的操作权限
  295. hasPermission, err = supply_analysisServ.HasVarietyPlantPermission(this.SysUser, varietyPlantInfo.VarietyId)
  296. if err != nil && err.Error() != utils.ErrNoRow() {
  297. br.Msg = "获取失败"
  298. br.ErrMsg = "查询是否有装置的操作权限失败;ERR:" + err.Error()
  299. return
  300. }
  301. if !hasPermission {
  302. br.Msg = "无操作权限"
  303. br.ErrMsg = "无操作权限"
  304. return
  305. }
  306. // 获取品种详情
  307. varietyInfo, err := supply_analysis.GetVarietyById(varietyPlantInfo.VarietyId)
  308. if err != nil {
  309. br.Msg = "查询品种失败"
  310. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  311. return
  312. }
  313. if varietyInfo == nil {
  314. br.Msg = "不存在该品种或该品种已被删除"
  315. br.ErrMsg = "不存在该品种或该品种已被删除"
  316. br.IsSendEmail = false
  317. return
  318. }
  319. // 检修日期 、 复产日期
  320. var maintenanceDate, resumptionDate time.Time
  321. if req.MaintenanceDate != `` {
  322. maintenanceDate, err = time.ParseInLocation(utils.FormatDate, req.MaintenanceDate, time.Local)
  323. if err != nil {
  324. br.Msg = "检修日期异常"
  325. br.ErrMsg = "检修日期异常,maintenanceDate:" + req.MaintenanceDate + ";ERR:" + err.Error()
  326. br.IsSendEmail = false
  327. return
  328. }
  329. }
  330. if req.ResumptionDate != `` {
  331. resumptionDate, err = time.ParseInLocation(utils.FormatDate, req.ResumptionDate, time.Local)
  332. if err != nil {
  333. br.Msg = "复产日期异常"
  334. br.ErrMsg = "复产日期异常,resumptionDate:" + req.ResumptionDate + ";ERR:" + err.Error()
  335. br.IsSendEmail = false
  336. return
  337. }
  338. }
  339. // 如果两个日期都填写了,那么就校验下吧
  340. if !resumptionDate.IsZero() && !maintenanceDate.IsZero() {
  341. if resumptionDate.Before(maintenanceDate) {
  342. br.Msg = "复产日期不得早于检修日期"
  343. br.IsSendEmail = false
  344. return
  345. }
  346. }
  347. // 编辑
  348. varietyPlantInfo.Province = utils.TrimLRStr(req.Province)
  349. varietyPlantInfo.City = utils.TrimLRStr(req.City)
  350. varietyPlantInfo.FactoryName = factoryName
  351. varietyPlantInfo.PlantName = plantName
  352. varietyPlantInfo.MaintenanceDate = maintenanceDate
  353. varietyPlantInfo.ResumptionDate = resumptionDate
  354. varietyPlantInfo.AnnualCapacity = req.AnnualCapacity
  355. varietyPlantInfo.Coefficient = req.Coefficient
  356. varietyPlantInfo.AverageDailyCapacityReduction = req.AverageDailyCapacityReduction
  357. varietyPlantInfo.IsStop = req.IsStop
  358. varietyPlantInfo.ModifyTime = time.Now()
  359. err = varietyPlantInfo.Update([]string{"Province", "City", "FactoryName", "PlantName", "MaintenanceDate", "ResumptionDate", "AnnualCapacity", "Coefficient", "AverageDailyCapacityReduction", "IsStop", "ModifyTime"})
  360. if err != nil {
  361. br.Msg = "修改失败"
  362. br.ErrMsg = "修改失败,Err:" + err.Error()
  363. return
  364. }
  365. // 变更品种的更新时间
  366. {
  367. varietyInfo.ModifyTime = time.Now()
  368. varietyInfo.LastUpdateSysUserId = this.SysUser.AdminId
  369. varietyInfo.LastUpdateSysUserRealName = this.SysUser.RealName
  370. go varietyInfo.Update([]string{"ModifyTime", "LastUpdateSysUserId", "LastUpdateSysUserRealName"})
  371. }
  372. br.Ret = 200
  373. br.Msg = "修改成功"
  374. br.IsAddLog = true
  375. br.Success = true
  376. }
  377. // DeletePlant
  378. // @Title 删除装置
  379. // @Description 删除装置接口
  380. // @Param request body request.DeleteVarietyPlantReq true "type json string"
  381. // @Success 200 Ret=200 删除成功
  382. // @router /variety/plant/delete [post]
  383. func (this *VarietyController) DeletePlant() {
  384. br := new(models.BaseResponse).Init()
  385. defer func() {
  386. this.Data["json"] = br
  387. this.ServeJSON()
  388. }()
  389. sysUser := this.SysUser
  390. if sysUser == nil {
  391. br.Msg = "请登录"
  392. br.ErrMsg = "请登录,SysUser Is Empty"
  393. br.Ret = 408
  394. return
  395. }
  396. var req request.DeleteVarietyPlantReq
  397. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  398. if err != nil {
  399. br.Msg = "参数解析异常!"
  400. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  401. return
  402. }
  403. if req.VarietyPlantId <= 0 {
  404. br.Msg = "请选择装置"
  405. br.IsSendEmail = false
  406. return
  407. }
  408. varietyPlantInfo, err := supply_analysis.GetVarietyPlantById(req.VarietyPlantId)
  409. if err != nil && err.Error() != utils.ErrNoRow() {
  410. br.Msg = "删除失败"
  411. br.ErrMsg = "查找品种装置失败,Err:" + err.Error()
  412. return
  413. }
  414. if varietyPlantInfo == nil {
  415. br.Msg = "该装置不存在或已删除"
  416. br.IsSendEmail = false
  417. return
  418. }
  419. // 获取品种详情
  420. varietyInfo, err := supply_analysis.GetVarietyById(varietyPlantInfo.VarietyId)
  421. if err != nil {
  422. br.Msg = "查询品种失败"
  423. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  424. return
  425. }
  426. if varietyInfo == nil {
  427. br.Msg = "不存在该品种或该品种已被删除"
  428. br.ErrMsg = "不存在该品种或该品种已被删除"
  429. br.IsSendEmail = false
  430. return
  431. }
  432. // 是否有装置的操作权限
  433. var hasPermission bool
  434. hasPermission, err = supply_analysisServ.HasVarietyPlantPermission(this.SysUser, varietyPlantInfo.VarietyId)
  435. if err != nil && err.Error() != utils.ErrNoRow() {
  436. br.Msg = "获取失败"
  437. br.ErrMsg = "查询是否有装置的操作权限失败;ERR:" + err.Error()
  438. return
  439. }
  440. if !hasPermission {
  441. br.Msg = "无操作权限"
  442. br.ErrMsg = "无操作权限"
  443. return
  444. }
  445. // 保留一条装置校验
  446. {
  447. total, err := supply_analysis.GetCountVarietyPlantByVarietyId(varietyInfo.VarietyId)
  448. if err != nil && err.Error() != utils.ErrNoRow() {
  449. br.Msg = "删除失败"
  450. br.ErrMsg = "查找品种装置总数量失败,Err:" + err.Error()
  451. return
  452. }
  453. if total <= 1 {
  454. br.Msg = "请保留至少一条装置"
  455. br.IsSendEmail = false
  456. return
  457. }
  458. }
  459. // 删除
  460. err = supply_analysis.DeleteVarietyPlantById(req.VarietyPlantId)
  461. if err != nil {
  462. br.Msg = "删除失败"
  463. br.ErrMsg = "删除装置失败,Err:" + err.Error()
  464. return
  465. }
  466. // 变更品种的更新时间
  467. {
  468. varietyInfo.ModifyTime = time.Now()
  469. varietyInfo.LastUpdateSysUserId = this.SysUser.AdminId
  470. varietyInfo.LastUpdateSysUserRealName = this.SysUser.RealName
  471. go varietyInfo.Update([]string{"ModifyTime", "LastUpdateSysUserId", "LastUpdateSysUserRealName"})
  472. }
  473. br.Ret = 200
  474. br.Msg = "删除成功"
  475. br.Success = true
  476. br.IsAddLog = true
  477. }
  478. // CopyPlant
  479. // @Title 从其他品种下复制装置
  480. // @Description 从其他品种下复制装置接口
  481. // @Param request body request.AddVarietyPlantReq true "type json string"
  482. // @Success 200 Ret=200 添加成功
  483. // @router /variety/plant/copy [post]
  484. func (this *VarietyController) CopyPlant() {
  485. br := new(models.BaseResponse).Init()
  486. defer func() {
  487. this.Data["json"] = br
  488. this.ServeJSON()
  489. }()
  490. var req request.CopyVarietyPlantReq
  491. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  492. if err != nil {
  493. br.Msg = "参数解析异常!"
  494. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  495. return
  496. }
  497. var hasPermission bool //是否有操作权限
  498. // 是否有装置的操作权限
  499. hasPermission, err = supply_analysisServ.HasVarietyPlantPermission(this.SysUser, req.VarietyId)
  500. if err != nil && err.Error() != utils.ErrNoRow() {
  501. br.Msg = "获取失败"
  502. br.ErrMsg = "查询是否有装置的操作权限失败;ERR:" + err.Error()
  503. return
  504. }
  505. if !hasPermission {
  506. br.Msg = "无操作权限"
  507. br.ErrMsg = "无操作权限"
  508. return
  509. }
  510. // 获取品种详情
  511. varietyInfo, err := supply_analysis.GetVarietyById(req.VarietyId)
  512. if err != nil {
  513. br.Msg = "查询品种失败"
  514. br.ErrMsg = "查询品种失败;ERR:" + err.Error()
  515. return
  516. }
  517. if varietyInfo == nil {
  518. br.Msg = "不存在该品种或该品种已被删除"
  519. br.ErrMsg = "不存在该品种或该品种已被删除"
  520. br.IsSendEmail = false
  521. return
  522. }
  523. if len(req.VarietyPlantIdList) <= 0 {
  524. br.Msg = "存量装置不允许必选"
  525. br.ErrMsg = "存量装置不允许必选"
  526. br.IsSendEmail = false
  527. return
  528. }
  529. list, err := supply_analysis.GetVarietyPlantByIdList(req.VarietyPlantIdList)
  530. if err != nil {
  531. br.Msg = "保存失败"
  532. br.ErrMsg = fmt.Sprint("查找需要复制的产品装置;ERR:" + err.Error())
  533. br.IsSendEmail = false
  534. return
  535. }
  536. num := len(list)
  537. varietyPlantMap := make(map[int]*supply_analysis.VarietyPlant)
  538. for _, v := range list {
  539. varietyPlantMap[v.VarietyPlantId] = v
  540. }
  541. prevSort := 0
  542. // 获取上级的品种
  543. if req.PrevVarietyPlantId > 0 {
  544. prevItem, err := supply_analysis.GetVarietyPlantById(req.PrevVarietyPlantId)
  545. if err != nil {
  546. br.Msg = "保存失败"
  547. br.ErrMsg = fmt.Sprint("查找上一个装置失败,上一个装置id:", req.PrevVarietyPlantId, ";ERR:"+err.Error())
  548. br.IsSendEmail = false
  549. return
  550. }
  551. updateSortStr := fmt.Sprintf(`sort + %d `, num)
  552. err = supply_analysis.UpdateEdbInfoSortByPrevId(req.VarietyId, prevItem.VarietyPlantId, prevItem.Sort, updateSortStr)
  553. if err != nil {
  554. br.Msg = "保存失败"
  555. br.ErrMsg = fmt.Sprint("移动下面的其他装置失败;ERR:" + err.Error())
  556. br.IsSendEmail = false
  557. return
  558. }
  559. prevSort = prevItem.Sort
  560. }
  561. i := 1
  562. addVarietyPlantList := make([]*supply_analysis.VarietyPlant, 0)
  563. for _, v := range req.VarietyPlantIdList {
  564. tmpVarietyPlant, ok := varietyPlantMap[v]
  565. if !ok {
  566. continue
  567. }
  568. addVarietyPlantList = append(addVarietyPlantList, &supply_analysis.VarietyPlant{
  569. VarietyId: req.VarietyId,
  570. Province: tmpVarietyPlant.Province,
  571. City: tmpVarietyPlant.City,
  572. FactoryName: tmpVarietyPlant.FactoryName,
  573. PlantName: tmpVarietyPlant.PlantName,
  574. MaintenanceDate: tmpVarietyPlant.MaintenanceDate,
  575. ResumptionDate: tmpVarietyPlant.ResumptionDate,
  576. AnnualCapacity: tmpVarietyPlant.AnnualCapacity,
  577. Coefficient: tmpVarietyPlant.Coefficient,
  578. AverageDailyCapacityReduction: tmpVarietyPlant.AverageDailyCapacityReduction,
  579. IsStop: tmpVarietyPlant.IsStop,
  580. Sort: prevSort + i,
  581. SysUserId: this.SysUser.AdminId,
  582. SysUserRealName: this.SysUser.RealName,
  583. ModifyTime: time.Now(),
  584. CreateTime: time.Now(),
  585. })
  586. i++
  587. }
  588. if len(addVarietyPlantList) > 0 {
  589. err = supply_analysis.MultiAddVarietyPlant(addVarietyPlantList)
  590. if err != nil {
  591. br.Msg = "添加装置失败"
  592. br.ErrMsg = "添加装置失败,Err:" + err.Error()
  593. return
  594. }
  595. }
  596. // 变更品种的更新时间
  597. {
  598. varietyInfo.ModifyTime = time.Now()
  599. varietyInfo.LastUpdateSysUserId = this.SysUser.AdminId
  600. varietyInfo.LastUpdateSysUserRealName = this.SysUser.RealName
  601. go varietyInfo.Update([]string{"ModifyTime", "LastUpdateSysUserId", "LastUpdateSysUserRealName"})
  602. }
  603. br.Ret = 200
  604. br.Msg = "添加成功"
  605. br.IsAddLog = true
  606. br.Success = true
  607. }