package worker import ( "errors" "fmt" "zhiyuan/models" "zhiyuan/models/final" "zhiyuan/pkg/app" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "zhiyuan/services/form" "zhiyuan/services/sms" ) var Worker models.Worker func VerifyByID(id int) error { info, err := GetInfoByID(id, nil, nil) if info == nil { return errors.New("invalid worker id") } if info.State == 1 { return errors.New("该工人已审核") } workerMap := map[string]interface{}{ "state": 1, } _, err = db.Update(Worker.TableName(), map[string]interface{}{"id": id}, workerMap) return err } func Register(form form.WorkerAdd) (int64, error) { if !sms.CheckVerifyCode(form.Phone, form.Code) { return 0, errors.New("验证码错误") } if CheckWorkerDuplicate(form.Phone) { return 0, errors.New("该手机号已注册") } workerMap := map[string]interface{}{ "`name`": form.Name, "`phone`": form.Phone, "`password`": utils.MD5(form.Password), "`type_ids`": form.TypeIds, "`idcode`": form.Idcode, "`idcard1`": form.Idcard1, "`idcard2`": form.Idcard2, } workerID, err := db.InsertOne(Worker.TableName(), workerMap) if err != nil { return 0, nil } return workerID, nil } func CountRaw(where string, param map[string]interface{}) (int64, error) { query := "`zy_worker` WHERE " + where return db.CountRaw(query, param) } func GetWorkersRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error { field := "SELECT `zy_worker`.*, group_concat(`zy_work_type`.`name` separator ',') as type_names FROM `zy_worker` left join `zy_work_type` on FIND_IN_SET(`zy_work_type`.`id`, `zy_worker`.`type_ids`) " return db.GetMultiRaw(field, where, param, retVal) } func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Worker, error) { return Worker.GetOne(where, fields, retVal) } func GetOneRaw(where map[string]string, param map[string]interface{}, retVal interface{}) (*models.Worker, error) { return Worker.GetOneRaw(where, param, retVal) } func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Worker, error) { return GetOne(map[string]interface{}{"id": id}, fields, retVal) } func GetInfoByToken(token string, fields []string, retVal interface{}) (*models.Worker, error) { return GetOne(map[string]interface{}{"access_token": token}, fields, retVal) } func CheckWorkerDuplicate(phone string) bool { workerInfo, err := GetOne(map[string]interface{}{"phone": phone}, nil, nil) return workerInfo != nil && err == nil } func GetWorkerItems(siteId int, roomType int, workerID int) ([]map[string]interface{}, error) { var s db.Select s.TableName = "`zy_mat_pick_work`" s.Select = map[string]string{ "confirm_type": "`zy_mat_pick_work`.`confirm_type`", "content": "`zy_mat_pick_work`.`content`", "created_at": "`zy_mat_pick_work`.`created_at`", "example_at": "`zy_mat_pick_work`.`example_at`", "cycle": "`zy_mat_pick_work`.`cycle`", "id": "`zy_mat_pick_work`.`id`", "item_status": "`zy_mat_pick_work`.`item_status`", "manager_confirm_at": "`zy_mat_pick_work`.`manager_confirm_at`", "manager_id": "`zy_mat_pick_work`.`manager_id`", "manager_status": "`zy_mat_pick_work`.`manager_status`", "order_acceptance_at": "`zy_mat_pick_work`.`order_acceptance_at`", "order_status": "`zy_mat_pick_work`.`order_status`", "pick_id": "`zy_mat_pick_work`.`pick_id`", "pictures": "`zy_mat_pick_work`.`pictures`", "pkg_id": "`zy_mat_pick_work`.`pkg_id`", "room_type": "`zy_mat_pick_work`.`room_type`", "site_id": "`zy_mat_pick_work`.`site_id`", "worker_confirm_at": "`zy_mat_pick_work`.`worker_confirm_at`", "worker_id": "`zy_mat_pick_work`.`worker_id`", "worker_start_at": "`zy_mat_pick_work`.`worker_start_at`", "worker_status": "`zy_mat_pick_work`.`worker_status`", "acceptance_failed_remark": "`zy_mat_pick_work`.`acceptance_failed_remark`", "pick_name": "`pick`.`pick_name`", "standard": "`pick`.`standard`", "starttime": "`site`.`starttime`", "endtime": "`site`.`endtime`", } s.Where = append(s.Where, fmt.Sprintf("`zy_mat_pick_work`.`worker_id` = %d", workerID)) s.Where = append(s.Where, fmt.Sprintf("`zy_mat_pick_work`.`room_type` = %d", roomType)) s.Where = append(s.Where, fmt.Sprintf("`zy_mat_pick_work`.`site_id` = %d", siteId)) s.LeftJoin = []db.Join{ { TableName: final.FinalSite{}.TableName(), As: "site", On: []string{ "`site`.`id` = `zy_mat_pick_work`.`site_id`", }, }, { TableName: models.MatPick{}.TableName(), As: "pick", On: []string{ "`pick`.`id` = `zy_mat_pick_work`.`pick_id`", }, }, } query, params := s.Query() fmt.Println("query", query) list, err := db.QueryMap(query, params, nil) if err != nil { return nil, err } return list, nil } func GetListWidthAttendance(where map[string]string, param map[string]interface{}, page app.Page, retVal interface{}) error { if page.PageNum > 0 && page.PageSize > 0 { where["_page_num"] = db.ToString(page.PageNum) where["_page_size"] = db.ToString(page.PageSize) } sql := "SELECT a.*, " + "w.name" + " FROM " + " zy_worker_attendance a " + " LEFT JOIN zy_worker w ON w.id = a.worker_id " return db.GetMultiRaw(sql, where, param, retVal) }