123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- // Copyright 2019 githup.com. All rights reserved.
- // Use of this source code is governed by githup.com.
- package model
- import (
- "fmt"
- "github.com/jinzhu/gorm"
- "time"
- )
- type ProviderDeviceTesting struct {
- Id int64 `gorm:"column:ID" json:"id" form:"id"`
- ProviderId int64 `gorm:"column:ProviderId" json:"provider_id" form:"provider_id"`
- DeviceCode int32 `gorm:"column:DeviceCode" json:"device_code" form:"device_code"`
- Status int32 `gorm:"column:Status" json:"status" form:"status"`
- CreatedAt time.Time `gorm:"column:CreatedAt"`
- Feedback string `gorm:"column:Feedback"`
- Comment string `gorm:"column:Comment"`
- Appendix string `gorm:"column:Appendix"`
- Agent string `gorm:"column:Agent"`
- AgentPhone string `gorm:"column:AgentPhone"`
- ApproveTime time.Time `gorm:"column:ApproveTime"`
- }
- func (ProviderDeviceTesting) TableName() string {
- return "ProviderDeviceTesting"
- }
- func (p *ProviderDeviceTesting) Insert(db *gorm.DB) error {
- return db.Create(p).Error
- }
- func (p *ProviderDeviceTesting) Find(db *gorm.DB, where map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).First(p).Error
- }
- func (p *ProviderDeviceTesting) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
- }
- func (p *ProviderDeviceTesting) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- return err
- }
- ps := []ProviderDeviceTesting{}
- err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
- if err != nil {
- return err
- }
- if len(ps) > 0 {
- *p = ps[0]
- }
- return nil
- }
- func (p *ProviderDeviceTesting) List(db *gorm.DB, where map[string]interface{}) (list []ProviderDeviceTesting, err error) {
- if len(where) > 0 {
- cond, val, err := whereBuild(where)
- if err != nil {
- return list, err
- }
- result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
- return list, result.Error
- }
- result := db.Table(p.TableName()).Find(&list)
- return list, result.Error
- }
- func (p *ProviderDeviceTesting) All(db *gorm.DB) (list []ProviderDeviceTesting, err error) {
- result := db.Table(p.TableName()).Find(&list)
- return list, result.Error
- }
- func (p *ProviderDeviceTesting) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
- if len(where) > 0 {
- cond, val, err := whereBuild(where)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
- return ret, err
- }
- ret := int64(0)
- err := db.Table(p.TableName()).Count(&ret).Error
- return ret, err
- }
- func (p *ProviderDeviceTesting) Del(db *gorm.DB, where map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
- }
- type ListWithProviderItem struct {
- Id int64
- ProviderName string
- SocialCode string
- TypeCode int32
- Status int32
- Comment string
- DevelopApproveTime time.Time
- ApproveTime time.Time
- Feedback string
- CreatedAt time.Time
- }
- func listWithProviderSql(page int32, filter string, filter_status []int32)(string, string, []interface{}) {
- offset := (page - 1) *int32(PageSize)
- whereArray := []string{}
- args := []interface{}{}
- sql := "select t1.ID as id, t1.ApproveTime as develop_approve_time, t1.Feedback as feedback, t1.Comment as comment, t1.DeviceCode as type_code, t1.Status as status, t1.CreatedAt as created_at, t2.Name as provider_name, t2.SocialCode as social_code from ProviderDeviceTesting as t1 left join Provider as t2 on t1.ProviderId=t2.ID "
- if filter != "" {
- whereArray = append(whereArray, fmt.Sprintf("(t2.Name like '%%%s%%' or t2.SocialCode like '%%%s%%')", filter, filter))
- }
- if len(filter_status) > 0 {
- whereArray = append(whereArray, "t1.Status in (?)")
- args = append(args, filter_status)
- }
- where := ""
- for _, v := range whereArray {
- if where == "" {
- where = fmt.Sprintf("where %s", v)
- continue
- }
- where = fmt.Sprintf("%s and %s", where, v)
- }
- sql = fmt.Sprintf("%s %s limit %d offset %d", sql, where, PageSize, offset)
- countSql := "select count(1) as count from ProviderDeviceTesting as t1 left join Provider as t2 on t1.ProviderId=t2.ID"
- countSql = fmt.Sprintf("%s %s ", countSql, where)
- return sql, countSql, args
- }
- func (p *ProviderDeviceTesting) ListWithProvider(db *gorm.DB, page int32, filter string, filter_status []int32)([]ListWithProviderItem, int64, error) {
- type ResultCount struct {
- Count int64
- }
- sql, countSql, args := listWithProviderSql(page, filter, filter_status)
- array := []ResultCount{}
- err := db.Raw(countSql, args...).Scan(&array).Error
- if err != nil {
- return nil, 0, err
- }
- if len(array) == 0 {
- return nil, 0, nil
- }
- if array[0].Count == 0 {
- return nil, 0, nil
- }
- ret := []ListWithProviderItem{}
- err = db.Raw(sql, args...).Scan(&ret).Error
- if err != nil {
- return nil, array[0].Count, err
- }
- return ret, array[0].Count, nil
- }
|