123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- package plan
- import (
- "fmt"
- "strconv"
- "strings"
- "time"
- "zhiyuan/models"
- "zhiyuan/pkg/db"
- adminParam "zhiyuan/pkg/param/admin"
- "zhiyuan/services/admin"
- "zhiyuan/services/dept"
- "github.com/gin-gonic/gin"
- )
- type PlanItem struct {
- ID int64 `json:"id" prop:"add:false"`
- AdminId int64 `json:"adminId" label:"人员ID" type:"int" prop:"add:false" search:"="`
- Time int64 `json:"time" label:"时间" type:"int" prop:"add" search:"="`
- BuildingId int64 `json:"buildingId" label:"楼盘ID" type:"int" prop:"add" search:"="`
- TypeId int64 `json:"typeId" label:"类型ID" type:"int" prop:"add edit" search:"="`
- Types string `json:"types" label:"动作" type:"string" prop:"add edit" search:"like"`
- Num int64 `json:"num" label:"数量" type:"int" prop:"add edit"`
- TypeName string `json:"type_name" prop:"add:false select:plantype.name"`
- BuildingName string `json:"building_name" prop:"add:false select:building.name"`
- DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
- CreatedAt int64 `json:"created_at" prop:"add:false"`
- UpdatedAt int64 `json:"updated_at" prop:"add:false select:false"`
- db.BaseModel
- }
- func (PlanItem) TableName() string {
- return "zy_plan_item"
- }
- func (model PlanItem) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
- s.Where = append(s.Where, fmt.Sprintf("(%s = %s)", fmt.Sprintf("`%s`.`adminId`", model.TableName()), s.Param(c.GetInt("adminID"))))
- return true
- }
- func (PlanItem) OnePrivilege(c *gin.Context, id int64) bool {
- return true
- }
- func (PlanItem) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
- data["adminId"] = c.GetInt("adminID")
- return nil
- }
- func (PlanItem) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
- return nil
- }
- func (PlanItem) DelPrivilege(c *gin.Context, id int64) error {
- return nil
- }
- func (PlanItem) Page() bool {
- return false
- }
- func (PlanItem) Count() bool {
- return true
- }
- func (model PlanItem) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
- return []db.JoinModel{
- {
- Model: PlanType{},
- As: "plantype",
- On: []string{"`plantype`.`id` = " + model.TableName() + ".`typeId`"},
- },
- {
- Model: Building{},
- As: "building",
- On: []string{"`building`.`id` = " + model.TableName() + ".`buildingId`"},
- },
- }
- }
- type PlanItemStatist struct {
- AdminName string `json:"admin_name" prop:"select:admin.username"`
- TypesName string `json:"types_name" label:"具体动作" prop:"select:GROUP_CONCAT(types.name)"`
- MonNum int64 `json:"mon_num" label:"周一数量" prop:"select:if(isnull(monplanfinish.num),0,monplanfinish.num)"`
- TueNum int64 `json:"tue_num" label:"周二数量" prop:"select:if(isnull(tueplanfinish.num),0,tueplanfinish.num)"`
- WedNum int64 `json:"wed_num" label:"周三数量" prop:"select:if(isnull(wedplanfinish.num),0,wedplanfinish.num)"`
- ThuNum int64 `json:"thu_num" label:"周四数量" prop:"select:if(isnull(thuplanfinish.num),0,thuplanfinish.num)"`
- FriNum int64 `json:"fri_num" label:"周五数量" prop:"select:if(isnull(friplanfinish.num),0,friplanfinish.num)"`
- SatNum int64 `json:"sat_num" label:"周六数量" prop:"select:if(isnull(satplanfinish.num),0,satplanfinish.num)"`
- SunNum int64 `json:"sun_num" label:"周日数量" prop:"select:if(isnull(sunplanfinish.num),0,sunplanfinish.num)"`
- 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)"`
- 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:"="`
- PlanItem
- }
- func (model PlanItemStatist) GroupBy() string {
- return fmt.Sprintf("`%s`.`id`", model.TableName())
- }
- func (model PlanItemStatist) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
- if !admin.IsSuperAdmin(c.GetInt("adminID")) {
- var adminInfo *models.Admin
- admin.GetInfoByID(c.GetInt("adminID"), nil, &adminInfo)
- if adminInfo == nil {
- return false
- }
- adminRole := false
- roleSlice := strings.Split(adminInfo.RoleIds, ",")
- for _, v := range roleSlice {
- if role, err := strconv.Atoi(v); err == nil && (role == adminParam.AdminRoleId) {
- adminRole = true
- break
- }
- }
- if !adminRole {
- s.Where = append(s.Where, fmt.Sprintf("`admin`.`dept_id` IN %s", s.Param(dept.GetSubDeptIds(adminInfo.DeptID, []int{adminInfo.DeptID}))))
- }
- }
- if _, ok := data["month"]; ok {
- if month, ok := db.ToInt64(data["month"]); ok {
- 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)))
- 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)))
- }
- }
- return true
- }
- func (model PlanItemStatist) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
- return append(model.PlanItem.LeftJoin(data, s),
- db.JoinModel{
- Model: models.Admin{},
- As: "admin",
- On: []string{
- "`admin`.`id` = " + model.TableName() + ".`adminId`",
- "`admin`.`state` = 1",
- },
- },
- db.JoinModel{
- Model: PlanTypes{},
- As: "types",
- On: []string{
- "FIND_IN_SET(`types`.`id` , " + model.TableName() + ".`types`)",
- },
- },
- db.JoinModel{
- Model: PlanFinish{},
- As: "monplanfinish",
- On: []string{
- "`monplanfinish`.`time` = " + model.TableName() + ".`time`",
- "`monplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
- "`monplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
- "`monplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
- },
- },
- db.JoinModel{
- Model: PlanFinish{},
- As: "tueplanfinish",
- On: []string{
- "`tueplanfinish`.`time` = " + model.TableName() + ".`time` + 24 * 60 * 60",
- "`tueplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
- "`tueplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
- "`tueplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
- },
- },
- db.JoinModel{
- Model: PlanFinish{},
- As: "wedplanfinish",
- On: []string{
- "`wedplanfinish`.`time` = " + model.TableName() + ".`time` + 2 * 24 * 60 * 60",
- "`wedplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
- "`wedplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
- "`wedplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
- },
- },
- db.JoinModel{
- Model: PlanFinish{},
- As: "thuplanfinish",
- On: []string{
- "`thuplanfinish`.`time` = " + model.TableName() + ".`time` + 3 * 24 * 60 * 60",
- "`thuplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
- "`thuplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
- "`thuplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
- },
- },
- db.JoinModel{
- Model: PlanFinish{},
- As: "friplanfinish",
- On: []string{
- "`friplanfinish`.`time` = " + model.TableName() + ".`time` + 4 * 24 * 60 * 60",
- "`friplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
- "`friplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
- "`friplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
- },
- },
- db.JoinModel{
- Model: PlanFinish{},
- As: "satplanfinish",
- On: []string{
- "`satplanfinish`.`time` = " + model.TableName() + ".`time` + 5 * 24 * 60 * 60",
- "`satplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
- "`satplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
- "`satplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
- },
- },
- db.JoinModel{
- Model: PlanFinish{},
- As: "sunplanfinish",
- On: []string{
- "`sunplanfinish`.`time` = " + model.TableName() + ".`time` + 6 * 24 * 60 * 60",
- "`sunplanfinish`.`adminId` = " + model.TableName() + ".`adminId`",
- "`sunplanfinish`.`buildingId` = " + model.TableName() + ".`buildingId`",
- "`sunplanfinish`.`typeId` = " + model.TableName() + ".`typeId`",
- },
- },
- )
- }
- type PlanAdminStatist struct {
- ID int `json:"id"`
- UserName string `json:"username" type:"string" search:"like"`
- TypeName string `json:"type_name" prop:"select:GROUP_CONCAT(distinct(plantype.name))"`
- BuildingName string `json:"building_name" prop:"select:GROUP_CONCAT(distinct(building.name))"`
- TypesName string `json:"types_name" prop:"select:GROUP_CONCAT(distinct(types.name))"`
- Num int64 `json:"num" type:"int" prop:"select:sum(if(isnull(planitem.id),0,1))"`
- MinCreatedAt int64 `json:"min_created_at" type:"int" prop:"select:min(planitem.created_at)"`
- MaxCreatedAt int64 `json:"max_created_at" type:"int" prop:"select:max(planitem.created_at)"`
- MaxUpdatedAt int64 `json:"max_updated_at" type:"int" prop:"select:max(planitem.updated_at)"`
- State int64 `json:"state" type:"int" prop:"select:if(sum(if(isnull(planitem.id),0,1))=0,0,1)" search:"="`
- db.BaseModel
- }
- func (PlanAdminStatist) TableName() string {
- return "zy_admin"
- }
- func (model PlanAdminStatist) GroupBy() string {
- return fmt.Sprintf("`%s`.`id`", model.TableName())
- }
- func (model PlanAdminStatist) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
- s.Where = append(s.Where, fmt.Sprintf("`%s`.`state` = 1", model.TableName()))
- s.Where = append(s.Where, fmt.Sprintf("find_in_set(%s, `%s`.`role_ids`)", s.Param(adminParam.SalesmanRoleId), model.TableName()))
- if !admin.IsSuperAdmin(c.GetInt("adminID")) {
- var adminInfo *models.Admin
- admin.GetInfoByID(c.GetInt("adminID"), nil, &adminInfo)
- if adminInfo == nil {
- return false
- }
- adminRole := false
- roleSlice := strings.Split(adminInfo.RoleIds, ",")
- for _, v := range roleSlice {
- if role, err := strconv.Atoi(v); err == nil && (role == adminParam.AdminRoleId) {
- adminRole = true
- break
- }
- }
- if !adminRole {
- s.Where = append(s.Where, fmt.Sprintf("`%s`.`dept_id` IN %s", model.TableName(), s.Param(dept.GetSubDeptIds(adminInfo.DeptID, []int{adminInfo.DeptID}))))
- }
- }
- return true
- }
- func (model PlanAdminStatist) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
- tt := time.Now()
- if t, ok := data["time"]; ok {
- if ti, ok := db.ToInt64(t); ok {
- tt = time.Unix(ti, 0)
- }
- }
- offset := int(time.Monday - tt.Weekday())
- if offset > 0 {
- offset -= 7
- }
- startweek := time.Date(tt.Year(), tt.Month(), tt.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset).Unix()
- return []db.JoinModel{
- {
- Model: PlanItem{},
- As: "planitem",
- On: []string{
- "`planitem`.`adminId` = " + model.TableName() + ".`id`",
- fmt.Sprintf("`planitem`.`time` = %s", s.Param(startweek)),
- },
- },
- {
- Model: PlanType{},
- As: "plantype",
- On: []string{"`plantype`.`id` = `planitem`.`typeId`"},
- },
- {
- Model: Building{},
- As: "building",
- On: []string{"`building`.`id` = `planitem`.`buildingId`"},
- },
- db.JoinModel{
- Model: PlanTypes{},
- As: "types",
- On: []string{
- "FIND_IN_SET(`types`.`id` , `planitem`.`types`)",
- },
- },
- }
- }
- func (PlanAdminStatist) Count() bool {
- return true
- }
|