plan_item.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package plan
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "zhiyuan/models"
  8. "zhiyuan/pkg/db"
  9. adminParam "zhiyuan/pkg/param/admin"
  10. "zhiyuan/services/admin"
  11. "zhiyuan/services/dept"
  12. "github.com/gin-gonic/gin"
  13. )
  14. type PlanItem struct {
  15. ID int64 `json:"id" prop:"add:false"`
  16. AdminId int64 `json:"adminId" label:"人员ID" type:"int" prop:"add:false" search:"="`
  17. Time int64 `json:"time" label:"时间" type:"int" prop:"add" search:"="`
  18. BuildingId int64 `json:"buildingId" label:"楼盘ID" type:"int" prop:"add" search:"="`
  19. TypeId int64 `json:"typeId" label:"类型ID" type:"int" prop:"add edit" search:"="`
  20. Types string `json:"types" label:"动作" type:"string" prop:"add edit" search:"like"`
  21. Num int64 `json:"num" label:"数量" type:"int" prop:"add edit"`
  22. TypeName string `json:"type_name" prop:"add:false select:plantype.name"`
  23. BuildingName string `json:"building_name" prop:"add:false select:building.name"`
  24. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  25. CreatedAt int64 `json:"created_at" prop:"add:false"`
  26. UpdatedAt int64 `json:"updated_at" prop:"add:false select:false"`
  27. db.BaseModel
  28. }
  29. func (PlanItem) TableName() string {
  30. return "zy_plan_item"
  31. }
  32. func (model PlanItem) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  33. s.Where = append(s.Where, fmt.Sprintf("(%s = %s)", fmt.Sprintf("`%s`.`adminId`", model.TableName()), s.Param(c.GetInt("adminID"))))
  34. return true
  35. }
  36. func (PlanItem) OnePrivilege(c *gin.Context, id int64) bool {
  37. return true
  38. }
  39. func (PlanItem) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  40. data["adminId"] = c.GetInt("adminID")
  41. return nil
  42. }
  43. func (PlanItem) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
  44. return nil
  45. }
  46. func (PlanItem) DelPrivilege(c *gin.Context, id int64) error {
  47. return nil
  48. }
  49. func (PlanItem) Page() bool {
  50. return false
  51. }
  52. func (PlanItem) Count() bool {
  53. return true
  54. }
  55. func (model PlanItem) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  56. return []db.JoinModel{
  57. {
  58. Model: PlanType{},
  59. As: "plantype",
  60. On: []string{"`plantype`.`id` = " + model.TableName() + ".`typeId`"},
  61. },
  62. {
  63. Model: Building{},
  64. As: "building",
  65. On: []string{"`building`.`id` = " + model.TableName() + ".`buildingId`"},
  66. },
  67. }
  68. }
  69. type PlanItemStatist struct {
  70. AdminName string `json:"admin_name" prop:"select:admin.username"`
  71. TypesName string `json:"types_name" label:"具体动作" prop:"select:GROUP_CONCAT(types.name)"`
  72. MonNum int64 `json:"mon_num" label:"周一数量" prop:"select:if(isnull(monplanfinish.num),0,monplanfinish.num)"`
  73. TueNum int64 `json:"tue_num" label:"周二数量" prop:"select:if(isnull(tueplanfinish.num),0,tueplanfinish.num)"`
  74. WedNum int64 `json:"wed_num" label:"周三数量" prop:"select:if(isnull(wedplanfinish.num),0,wedplanfinish.num)"`
  75. ThuNum int64 `json:"thu_num" label:"周四数量" prop:"select:if(isnull(thuplanfinish.num),0,thuplanfinish.num)"`
  76. FriNum int64 `json:"fri_num" label:"周五数量" prop:"select:if(isnull(friplanfinish.num),0,friplanfinish.num)"`
  77. SatNum int64 `json:"sat_num" label:"周六数量" prop:"select:if(isnull(satplanfinish.num),0,satplanfinish.num)"`
  78. SunNum int64 `json:"sun_num" label:"周日数量" prop:"select:if(isnull(sunplanfinish.num),0,sunplanfinish.num)"`
  79. FinishNum int64 `json:"finish_num" label:"完成量" prop:"select:if(isnull(monplanfinish.num),0,monplanfinish.num)+if(isnull(tueplanfinish.num),0,tueplanfinish.num)+if(isnull(wedplanfinish.num),0,wedplanfinish.num)+if(isnull(thuplanfinish.num),0,thuplanfinish.num)+if(isnull(friplanfinish.num),0,friplanfinish.num)+if(isnull(satplanfinish.num),0,satplanfinish.num)+if(isnull(sunplanfinish.num),0,sunplanfinish.num)"`
  80. State int64 `json:"state" label:"状态" type:"int" prop:"select:if(if(isnull(monplanfinish.num),0,monplanfinish.num)+if(isnull(tueplanfinish.num),0,tueplanfinish.num)+if(isnull(wedplanfinish.num),0,wedplanfinish.num)+if(isnull(thuplanfinish.num),0,thuplanfinish.num)+if(isnull(friplanfinish.num),0,friplanfinish.num)+if(isnull(satplanfinish.num),0,satplanfinish.num)+if(isnull(sunplanfinish.num),0,sunplanfinish.num)>=zy_plan_item.num,1,0)" search:"="`
  81. PlanItem
  82. }
  83. func (model PlanItemStatist) GroupBy() string {
  84. return fmt.Sprintf("`%s`.`id`", model.TableName())
  85. }
  86. func (model PlanItemStatist) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  87. if !admin.IsSuperAdmin(c.GetInt("adminID")) {
  88. var adminInfo *models.Admin
  89. admin.GetInfoByID(c.GetInt("adminID"), nil, &adminInfo)
  90. if adminInfo == nil {
  91. return false
  92. }
  93. adminRole := false
  94. roleSlice := strings.Split(adminInfo.RoleIds, ",")
  95. for _, v := range roleSlice {
  96. if role, err := strconv.Atoi(v); err == nil && (role == adminParam.AdminRoleId) {
  97. adminRole = true
  98. break
  99. }
  100. }
  101. if !adminRole {
  102. s.Where = append(s.Where, fmt.Sprintf("`admin`.`dept_id` IN %s", s.Param(dept.GetSubDeptIds(adminInfo.DeptID, []int{adminInfo.DeptID}))))
  103. }
  104. }
  105. if _, ok := data["month"]; ok {
  106. if month, ok := db.ToInt64(data["month"]); ok {
  107. s.Where = append(s.Where, fmt.Sprintf("(%s >= UNIX_TIMESTAMP(date_add(FROM_UNIXTIME(%s),interval -day(FROM_UNIXTIME(%s))+1 day)))", fmt.Sprintf("`%s`.`time`", model.TableName()), s.Param(month), s.Param(month)))
  108. s.Where = append(s.Where, fmt.Sprintf("(%s < UNIX_TIMESTAMP(date_add(FROM_UNIXTIME(%s)-day(FROM_UNIXTIME(%s))+1,interval 1 month)))", fmt.Sprintf("`%s`.`time`", model.TableName()), s.Param(month), s.Param(month)))
  109. }
  110. }
  111. return true
  112. }
  113. func (model PlanItemStatist) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  114. return append(model.PlanItem.LeftJoin(data, s),
  115. db.JoinModel{
  116. Model: models.Admin{},
  117. As: "admin",
  118. On: []string{
  119. "`admin`.`id` = " + model.TableName() + ".`adminId`",
  120. "`admin`.`state` = 1",
  121. },
  122. },
  123. db.JoinModel{
  124. Model: PlanTypes{},
  125. As: "types",
  126. On: []string{
  127. "FIND_IN_SET(`types`.`id` , " + model.TableName() + ".`types`)",
  128. },
  129. },
  130. db.JoinModel{
  131. Model: PlanFinish{},
  132. As: "monplanfinish",
  133. On: []string{
  134. "`monplanfinish`.`time` = " + model.TableName() + ".`time`",
  135. "`monplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
  136. "`monplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
  137. "`monplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
  138. },
  139. },
  140. db.JoinModel{
  141. Model: PlanFinish{},
  142. As: "tueplanfinish",
  143. On: []string{
  144. "`tueplanfinish`.`time` = " + model.TableName() + ".`time` + 24 * 60 * 60",
  145. "`tueplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
  146. "`tueplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
  147. "`tueplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
  148. },
  149. },
  150. db.JoinModel{
  151. Model: PlanFinish{},
  152. As: "wedplanfinish",
  153. On: []string{
  154. "`wedplanfinish`.`time` = " + model.TableName() + ".`time` + 2 * 24 * 60 * 60",
  155. "`wedplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
  156. "`wedplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
  157. "`wedplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
  158. },
  159. },
  160. db.JoinModel{
  161. Model: PlanFinish{},
  162. As: "thuplanfinish",
  163. On: []string{
  164. "`thuplanfinish`.`time` = " + model.TableName() + ".`time` + 3 * 24 * 60 * 60",
  165. "`thuplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
  166. "`thuplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
  167. "`thuplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
  168. },
  169. },
  170. db.JoinModel{
  171. Model: PlanFinish{},
  172. As: "friplanfinish",
  173. On: []string{
  174. "`friplanfinish`.`time` = " + model.TableName() + ".`time` + 4 * 24 * 60 * 60",
  175. "`friplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
  176. "`friplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
  177. "`friplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
  178. },
  179. },
  180. db.JoinModel{
  181. Model: PlanFinish{},
  182. As: "satplanfinish",
  183. On: []string{
  184. "`satplanfinish`.`time` = " + model.TableName() + ".`time` + 5 * 24 * 60 * 60",
  185. "`satplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
  186. "`satplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
  187. "`satplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
  188. },
  189. },
  190. db.JoinModel{
  191. Model: PlanFinish{},
  192. As: "sunplanfinish",
  193. On: []string{
  194. "`sunplanfinish`.`time` = " + model.TableName() + ".`time` + 6 * 24 * 60 * 60",
  195. "`sunplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
  196. "`sunplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
  197. "`sunplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
  198. },
  199. },
  200. )
  201. }
  202. type PlanAdminStatist struct {
  203. ID int `json:"id"`
  204. UserName string `json:"username" type:"string" search:"like"`
  205. TypeName string `json:"type_name" prop:"select:GROUP_CONCAT(distinct(plantype.name))"`
  206. BuildingName string `json:"building_name" prop:"select:GROUP_CONCAT(distinct(building.name))"`
  207. TypesName string `json:"types_name" prop:"select:GROUP_CONCAT(distinct(types.name))"`
  208. Num int64 `json:"num" type:"int" prop:"select:sum(if(isnull(planitem.id),0,1))"`
  209. MinCreatedAt int64 `json:"min_created_at" type:"int" prop:"select:min(planitem.created_at)"`
  210. MaxCreatedAt int64 `json:"max_created_at" type:"int" prop:"select:max(planitem.created_at)"`
  211. MaxUpdatedAt int64 `json:"max_updated_at" type:"int" prop:"select:max(planitem.updated_at)"`
  212. State int64 `json:"state" type:"int" prop:"select:if(sum(if(isnull(planitem.id),0,1))=0,0,1)" search:"="`
  213. db.BaseModel
  214. }
  215. func (PlanAdminStatist) TableName() string {
  216. return "zy_admin"
  217. }
  218. func (model PlanAdminStatist) GroupBy() string {
  219. return fmt.Sprintf("`%s`.`id`", model.TableName())
  220. }
  221. func (model PlanAdminStatist) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  222. s.Where = append(s.Where, fmt.Sprintf("`%s`.`state` = 1", model.TableName()))
  223. s.Where = append(s.Where, fmt.Sprintf("find_in_set(%s, `%s`.`role_ids`)", s.Param(adminParam.SalesmanRoleId), model.TableName()))
  224. if !admin.IsSuperAdmin(c.GetInt("adminID")) {
  225. var adminInfo *models.Admin
  226. admin.GetInfoByID(c.GetInt("adminID"), nil, &adminInfo)
  227. if adminInfo == nil {
  228. return false
  229. }
  230. adminRole := false
  231. roleSlice := strings.Split(adminInfo.RoleIds, ",")
  232. for _, v := range roleSlice {
  233. if role, err := strconv.Atoi(v); err == nil && (role == adminParam.AdminRoleId) {
  234. adminRole = true
  235. break
  236. }
  237. }
  238. if !adminRole {
  239. s.Where = append(s.Where, fmt.Sprintf("`%s`.`dept_id` IN %s", model.TableName(), s.Param(dept.GetSubDeptIds(adminInfo.DeptID, []int{adminInfo.DeptID}))))
  240. }
  241. }
  242. return true
  243. }
  244. func (model PlanAdminStatist) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  245. tt := time.Now()
  246. if t, ok := data["time"]; ok {
  247. if ti, ok := db.ToInt64(t); ok {
  248. tt = time.Unix(ti, 0)
  249. }
  250. }
  251. offset := int(time.Monday - tt.Weekday())
  252. if offset > 0 {
  253. offset -= 7
  254. }
  255. startweek := time.Date(tt.Year(), tt.Month(), tt.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset).Unix()
  256. return []db.JoinModel{
  257. {
  258. Model: PlanItem{},
  259. As: "planitem",
  260. On: []string{
  261. "`planitem`.`adminId` = " + model.TableName() + ".`id`",
  262. fmt.Sprintf("`planitem`.`time` = %s", s.Param(startweek)),
  263. },
  264. },
  265. {
  266. Model: PlanType{},
  267. As: "plantype",
  268. On: []string{"`plantype`.`id` = `planitem`.`typeId`"},
  269. },
  270. {
  271. Model: Building{},
  272. As: "building",
  273. On: []string{"`building`.`id` = `planitem`.`buildingId`"},
  274. },
  275. db.JoinModel{
  276. Model: PlanTypes{},
  277. As: "types",
  278. On: []string{
  279. "FIND_IN_SET(`types`.`id` , `planitem`.`types`)",
  280. },
  281. },
  282. }
  283. }
  284. func (PlanAdminStatist) Count() bool {
  285. return true
  286. }