Project.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package Project
  2. import (
  3. "ERP_project/logs"
  4. "ERP_project/models/Account"
  5. "fmt"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. Delete = iota
  15. WaitAudit // 待审核
  16. HaveInHand // 进行中
  17. Completed // 已完成
  18. PayPerf // 已发绩效
  19. )
  20. // 项目管理
  21. type Project struct {
  22. Id int `orm:"column(ID);size(11);auto;pk"`
  23. T_uid string `orm:"index;size(32);null"` // 用户uuid
  24. T_dept int `orm:"index;size(8);null"` // 部门
  25. T_name string `orm:"size(256);null"` // 项目名称
  26. T_start_date string `orm:"size(256);null"` // 开始日期
  27. T_end_date string `orm:"size(256);null"` // 结束日期
  28. T_set_up_date string `orm:"size(256);null"` // 立项日期
  29. T_description string `orm:"type(text);null"` // 项目简介
  30. T_planning_cycle float32 `orm:"digits(12);decimals(2)"` // 计划完成周期
  31. T_reality_cycle float32 `orm:"digits(12);decimals(2)"` // 实际完成周期
  32. T_advance_day float32 `orm:"digits(12);decimals(2)"` // 提前完成天数
  33. T_bonus float32 `orm:"digits(12);decimals(2)"` // 奖励金额
  34. T_Perf float32 `orm:"digits(12);decimals(2)"` // 绩效总金额
  35. T_detail string `orm:"type(text);null"` // 明细
  36. T_remark string `orm:"type(text);null"` // 备注
  37. T_approver string `orm:"size(32);null"` // 审批人
  38. T_State int `orm:"size(2);default(2)"` // 0 删除 1 待审核 2 进行中 3 已完成 4 已发绩效
  39. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  40. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  41. }
  42. func (t *Project) TableName() string {
  43. return "project"
  44. }
  45. type ProjectDaoImpl struct {
  46. orm orm.Ormer
  47. }
  48. func NewProject(orm orm.Ormer) *ProjectDaoImpl {
  49. return &ProjectDaoImpl{orm: orm}
  50. }
  51. func init() {
  52. //注册模型
  53. orm2.Debug = true
  54. orm.RegisterModel(new(Project))
  55. }
  56. type Project_R struct {
  57. Id int
  58. T_uid string // 用户uuid
  59. T_user_name string // 用户名称
  60. T_name string // 项目名称
  61. T_start_date string // 开始日期
  62. T_end_date string // 结束日期
  63. T_set_up_date string // 立项日期
  64. T_description string // 项目简介
  65. T_planning_cycle float32 // 计划完成周期
  66. T_reality_cycle float32 // 实际完成周期
  67. T_advance_day float32 // 提前完成天数
  68. T_bonus float32 // 奖励金额
  69. T_Perf float32 // 绩效总金额
  70. T_remark string // 备注
  71. T_approver string // 审批人
  72. T_approver_name string // 审批人
  73. T_State int
  74. }
  75. type ProjectDetail_R struct {
  76. T_function string // 功能与指标
  77. T_planned_time float32 // 计划时间(工作日)
  78. T_finish string // 是否完成
  79. }
  80. type Project_Detail struct {
  81. Id int
  82. T_uid string // 用户uuid
  83. T_user_name string // 用户名称
  84. T_name string // 项目名称
  85. T_start_date string // 开始日期
  86. T_end_date string // 结束日期
  87. T_set_up_date string // 立项日期
  88. T_description string // 项目简介
  89. T_planning_cycle float32 // 计划完成周期
  90. T_reality_cycle float32 // 实际完成周期
  91. T_advance_day float32 // 提前完成天数
  92. T_bonus float32 // 奖励金额
  93. T_Perf float32 // 绩效总金额
  94. T_remark string // 备注
  95. T_approver string // 审批人
  96. T_approver_name string // 审批人
  97. T_State int
  98. T_Detail []ProjectDetail_R // 项目明细
  99. }
  100. type Project_S struct {
  101. Id int
  102. T_uid string // 用户uuid
  103. T_user_name string // 用户名称
  104. T_name string // 项目名称
  105. T_planning_cycle float32 // 开始日期
  106. T_end_date string // 结束日期
  107. T_set_up_date string // 立项日期
  108. T_Perf float32 // 绩效总金额
  109. T_State int
  110. }
  111. // ---------------- 特殊方法 -------------------
  112. func ProjectToProject_R(t Project) (r Project_R) {
  113. r.Id = t.Id
  114. r.T_uid = t.T_uid
  115. r.T_user_name = Account.Read_User_T_name_Get(t.T_uid)
  116. r.T_name = t.T_name
  117. r.T_start_date = t.T_start_date
  118. r.T_end_date = t.T_end_date
  119. r.T_set_up_date = t.T_set_up_date
  120. r.T_description = t.T_description
  121. r.T_planning_cycle = t.T_planning_cycle
  122. r.T_reality_cycle = t.T_reality_cycle
  123. r.T_advance_day = t.T_advance_day
  124. r.T_bonus = t.T_bonus
  125. r.T_Perf = t.T_Perf
  126. r.T_remark = t.T_remark
  127. r.T_approver = t.T_approver
  128. r.T_approver_name = Account.Read_User_T_name_Get(t.T_approver)
  129. r.T_State = t.T_State
  130. return r
  131. }
  132. func ProjectToProject_Detail(t Project) (r Project_Detail) {
  133. r.Id = t.Id
  134. r.T_uid = t.T_uid
  135. r.T_user_name = Account.Read_User_T_name_Get(t.T_uid)
  136. r.T_name = t.T_name
  137. r.T_start_date = t.T_start_date
  138. r.T_end_date = t.T_end_date
  139. r.T_set_up_date = t.T_set_up_date
  140. r.T_description = t.T_description
  141. r.T_planning_cycle = t.T_planning_cycle
  142. r.T_reality_cycle = t.T_reality_cycle
  143. r.T_advance_day = t.T_advance_day
  144. r.T_bonus = t.T_bonus
  145. r.T_Perf = t.T_Perf
  146. r.T_remark = t.T_remark
  147. r.T_approver = t.T_approver
  148. r.T_approver_name = Account.Read_User_T_name_Get(t.T_approver)
  149. r.T_State = t.T_State
  150. r.T_Detail = ProjectToProjectDetail(t.T_detail)
  151. return r
  152. }
  153. func ProjectToProjectDetail(T_detail string) (r []ProjectDetail_R) {
  154. if len(T_detail) == 0 {
  155. return
  156. }
  157. detailList := strings.Split(strings.Trim(T_detail, "|"), "|")
  158. for _, detail := range detailList {
  159. T_function := strings.Split(detail, ",")[0]
  160. T_planned_time := strings.Split(detail, ",")[1]
  161. T_finish := strings.Split(detail, ",")[2]
  162. r = append(r, ProjectDetail_R{
  163. T_function: T_function,
  164. T_planned_time: float32(lib.StringToFloat64TwoDecimal(T_planned_time)),
  165. T_finish: T_finish,
  166. })
  167. }
  168. return r
  169. }
  170. func ProjectToProject_S(t Project) (r Project_S) {
  171. r.Id = t.Id
  172. r.T_uid = t.T_uid
  173. r.T_user_name = Account.Read_User_T_name_Get(t.T_uid)
  174. r.T_name = t.T_name
  175. r.T_planning_cycle = t.T_planning_cycle
  176. r.T_end_date = t.T_end_date
  177. r.T_set_up_date = t.T_set_up_date
  178. r.T_Perf = t.T_Perf
  179. r.T_State = t.T_State
  180. return r
  181. }
  182. func (dao *ProjectDaoImpl) Read_Project_ById(id int) (r Project, err error) {
  183. o := orm.NewOrm()
  184. r = Project{Id: id}
  185. err = o.Read(&r)
  186. if err != nil {
  187. logs.Error(lib.FuncName(), err)
  188. return r, err
  189. }
  190. return r, err
  191. }
  192. // 添加
  193. func (dao *ProjectDaoImpl) Add_Project(m Project) (id int64, err error) {
  194. o := orm.NewOrm()
  195. id, err = o.Insert(&m)
  196. if err != nil {
  197. logs.Error(lib.FuncName(), err)
  198. return
  199. }
  200. return
  201. }
  202. // 修改
  203. func (dao *ProjectDaoImpl) Update_Project(m Project, cols ...string) (id int64, err error) {
  204. o := orm.NewOrm()
  205. id, err = o.Update(&m, cols...)
  206. if err != nil {
  207. logs.Error(lib.FuncName(), err)
  208. return
  209. }
  210. fmt.Println("Number of records updated in database:", id)
  211. return id, nil
  212. }
  213. // 删除
  214. func (dao *ProjectDaoImpl) Delete_Project(m Project) (id int64, err error) {
  215. o := orm.NewOrm()
  216. m.T_State = Delete
  217. id, err = o.Update(&m, "T_State")
  218. if err != nil {
  219. logs.Error(lib.FuncName(), err)
  220. return
  221. }
  222. fmt.Println("Number of records updated in database:", id)
  223. return id, nil
  224. }
  225. // 获取列表
  226. func (dao *ProjectDaoImpl) Read_Project_List(T_uid, T_approver string, page, page_z int) (r []Project_R, cnt int64) {
  227. o := orm.NewOrm()
  228. // 也可以直接使用 Model 结构体作为表名
  229. var map_r []Project
  230. qs := o.QueryTable(new(Project))
  231. var offset int64
  232. if page <= 1 {
  233. offset = 0
  234. } else {
  235. offset = int64((page - 1) * page_z)
  236. }
  237. cond := orm.NewCondition()
  238. cond = cond.And("T_State__gt", Delete)
  239. if len(T_uid) > 0 {
  240. cond = cond.And("T_uid", T_uid)
  241. }
  242. // 审批人
  243. if len(T_approver) > 0 {
  244. cond = cond.And("T_approver", T_approver)
  245. }
  246. var err error
  247. if page_z == 9999 {
  248. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  249. } else {
  250. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  251. }
  252. if err != nil {
  253. logs.Error(lib.FuncName(), err)
  254. return
  255. }
  256. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  257. if err != nil {
  258. logs.Error(lib.FuncName(), err)
  259. return
  260. }
  261. for _, v := range map_r {
  262. r = append(r, ProjectToProject_R(v))
  263. }
  264. return r, cnt
  265. }
  266. // 获取列表
  267. func (dao *ProjectDaoImpl) Read_Project_Finance_List(T_uuid string, page, page_z int) (r []Project_R, cnt int64) {
  268. o := orm.NewOrm()
  269. // 也可以直接使用 Model 结构体作为表名
  270. var map_r []Project
  271. qs := o.QueryTable(new(Project))
  272. var offset int64
  273. if page <= 1 {
  274. offset = 0
  275. } else {
  276. offset = int64((page - 1) * page_z)
  277. }
  278. cond := orm.NewCondition()
  279. state := []int{Completed, PayPerf}
  280. cond = cond.And("T_uid", T_uuid).And("T_State__in", state)
  281. var err error
  282. if page_z == 9999 {
  283. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  284. } else {
  285. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  286. }
  287. if err != nil {
  288. logs.Error(lib.FuncName(), err)
  289. return
  290. }
  291. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  292. if err != nil {
  293. logs.Error(lib.FuncName(), err)
  294. return
  295. }
  296. for _, v := range map_r {
  297. r = append(r, ProjectToProject_R(v))
  298. }
  299. return r, cnt
  300. }
  301. func (dao *ProjectDaoImpl) Read_T_uuid_List(T_approver string) (lists []string) {
  302. o := orm.NewOrm()
  303. var pl_lists orm2.ParamsList
  304. sql := "SELECT DISTINCT t_uid FROM project"
  305. if len(T_approver) > 0 {
  306. sql += " WHERE t_approver = '" + T_approver + "'"
  307. } else {
  308. sql += " WHERE t__state in ('" + strconv.Itoa(Completed) + "','" + strconv.Itoa(PayPerf) + "')"
  309. }
  310. sql += " LIMIT 0,1000"
  311. _, err := o.Raw(sql).ValuesFlat(&pl_lists)
  312. if err != nil {
  313. logs.Error(lib.FuncName(), err)
  314. return
  315. }
  316. for _, v := range pl_lists {
  317. lists = append(lists, v.(string))
  318. }
  319. return lists
  320. }