variety_plant.go 20 KB

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