| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- package AllotTask
- import (
- "ColdVerify_server/conf"
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- "encoding/json"
- "fmt"
- "time"
- "github.com/astaxie/beego/cache"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- )
- var (
- AllotTaskStatusWaitReceive = 1 // 待接收
- AllotTaskStatusReceived = 2 // 已接收
- AllotTaskStatusRefused = 3 // 已拒绝
- AllotTaskStatusSubmit = 4 // 已提交
- AllotTaskStateMap = map[int]string{
- AllotTaskStatusWaitReceive: "待接收",
- AllotTaskStatusReceived: "已接收",
- AllotTaskStatusRefused: "已拒绝",
- AllotTaskStatusSubmit: "已提交",
- }
- )
- // 模版
- type AllotTask struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_Distributor_id string `orm:"size(256);null"` // 分销商id
- T_allot_task_id string `orm:"size(256);null"` // 分配任务ID
- T_uuid string `orm:"size(256);null"` // 公司 UUID
- T_name string `orm:"size(256);null"` // 标题
- T_approach_time string `orm:"size(256);null"` // 进场时间
- T_scheme string `orm:"size(256);null"` // 实施方案 负责人UUID
- T_collection string `orm:"size(256);null"` // 数据采集 负责人UUID
- T_reporting string `orm:"size(256);null"` // 报告编写 负责人UUID
- T_delivery string `orm:"size(256);null"` // 交付审核 负责人UUID
- T_project string `orm:"size(256);null"` // 项目 负责人UUID
- T_province string `orm:"size(256);null"` // 省
- T_city string `orm:"size(256);null"` // 市
- T_district string `orm:"size(256);null"` // 区
- T_province_code string `orm:"size(256);null"` // 省 code
- T_city_code string `orm:"size(256);null"` // 市 code
- T_district_code string `orm:"size(256);null"` // 区 code
- T_category string `orm:"size(256);null"` // 类别
- T_attachment string `orm:"type(text);null"` // 附件
- T_confirm_form string `orm:"type(text);null"` // 信息确认表
- T_submit_time string `orm:"type(text);null"` // 信息确认表提交时间
- T_status int `orm:"size(2);default(0)"` // 状态 1待接收 2已接收 3已拒绝 4已提交
- T_state int `orm:"size(2);default(1)"` // 0 删除 1 正常
- T_record string `orm:"type(text);null"` // 记录
- T_reason string `orm:"type(text);null"` // 拒绝原因
- T_allot_time time.Time `orm:"type(timestamp);null;"` // 派发时间
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- type AllotTask_ struct {
- Id int
- T_Distributor_id string // 分销商id
- T_allot_task_id string // 分配任务ID
- T_uuid string // 公司 UUID
- T_user_name string // 公司 名称
- T_name string // 标题
- T_approach_time string // 进场时间
- T_scheme string // 实施方案 负责人UUID
- T_collection string // 数据采集 负责人UUID
- T_reporting string // 报告编写 负责人UUID
- T_delivery string // 交付审核 负责人UUID
- T_project string // 项目 负责人UUID
- T_scheme_name string // 实施方案 负责人姓名
- T_collection_name string // 数据采集 负责人姓名
- T_reporting_name string // 报告编写 负责人姓名
- T_delivery_name string // 交付审核 负责人姓名
- T_project_name string // 项目 负责人姓名
- T_province string // 省
- T_city string // 市
- T_district string // 区
- T_province_code string // 省 code
- T_city_code string // 市 code
- T_district_code string // 区 code
- T_area []string // 省市区
- T_area_code []string // 省市区
- T_category string // 类别
- T_attachment []string // 合同附件
- T_confirm_form []string // 信息确认表图片
- T_submit_time string // 信息确认表提交时间
- T_status int // 状态 1待接收 2已接收 3已拒绝
- T_state int // 0 删除 1 正常
- T_record []AllotTaskRecord
- T_reason string
- T_allot_time string //指派时间
- CreateTime string //auto_now_add 第一次保存时才设置时间
- UpdateTime string //auto_now 每次 model 保存时都会对时间自动更新
- }
- type AllotTaskRecord struct {
- T_collection string
- T_collection_name string
- T_status int
- T_reason string
- T_time string
- }
- func AllotTaskToAllotTask_(T AllotTask, userMap, adminMap map[string]string) (T_ AllotTask_) {
- T_.Id = T.Id
- T_.T_Distributor_id = T.T_Distributor_id
- T_.T_allot_task_id = T.T_allot_task_id
- T_.T_uuid = T.T_uuid
- T_.T_user_name = userMap[T.T_uuid]
- T_.T_name = T.T_name
- T_.T_approach_time = T.T_approach_time
- T_.T_scheme = T.T_scheme
- T_.T_collection = T.T_collection
- T_.T_reporting = T.T_reporting
- T_.T_delivery = T.T_delivery
- T_.T_project = T.T_project
- T_.T_scheme_name = adminMap[T.T_scheme]
- T_.T_collection_name = adminMap[T.T_collection]
- T_.T_reporting_name = adminMap[T.T_reporting]
- T_.T_delivery_name = adminMap[T.T_delivery]
- T_.T_project_name = adminMap[T.T_project]
- T_.T_province = T.T_province
- T_.T_city = T.T_city
- T_.T_district = T.T_district
- T_.T_area = []string{T.T_province, T.T_city, T.T_district}
- T_.T_province_code = T.T_province_code
- T_.T_city_code = T.T_city_code
- T_.T_district_code = T.T_district_code
- T_.T_area_code = []string{T.T_province_code, T.T_city_code, T.T_district_code}
- T_.T_category = T.T_category
- T_.T_reason = T.T_reason
- T_.T_state = T.T_state
- T_.T_status = T.T_status
- T_.T_attachment = lib.SplitStringSeparator(T.T_attachment, "|")
- T_.T_confirm_form = lib.SplitStringSeparator(T.T_confirm_form, "|")
- T_.T_submit_time = T.T_submit_time
- json.Unmarshal([]byte(T.T_record), &T_.T_record)
- for i, record := range T_.T_record {
- T_.T_record[i].T_collection_name = adminMap[record.T_collection]
- }
- if !T.T_allot_time.IsZero() {
- T_.T_allot_time = T.T_allot_time.Format("2006-01-02 15:04")
- }
- if !T.CreateTime.IsZero() {
- T_.CreateTime = T.CreateTime.Format("2006-01-02 15:04:05")
- }
- if !T.UpdateTime.IsZero() {
- T_.UpdateTime = T.UpdateTime.Format("2006-01-02 15:04:05")
- }
- return T_
- }
- var redisCache_AllotTask cache.Cache
- func (t *AllotTask) TableName() string {
- return "allot_task" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(AllotTask))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_"+"AllotTask", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- logs.Println(config)
- var err error
- redisCache_AllotTask, err = cache.NewCache("redis", config)
- if err != nil || redisCache_AllotTask == nil {
- errMsg := "failed to init redis"
- logs.Println(errMsg, err)
- }
- }
- func Redis_AllotTask_Set(key string, r AllotTask) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_AllotTask.Put(key, str, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", key, ",value:", str, err)
- }
- return
- }
- func Redis_AllotTask_Get(key string) (r AllotTask, is bool) {
- if redisCache_AllotTask.IsExist(key) {
- logs.Println("找到key:", key)
- v := redisCache_AllotTask.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- logs.Println("没有 找到key:", key)
- return AllotTask{}, false
- }
- func Redis_AllotTask_DelK(key string) (err error) {
- err = redisCache_AllotTask.Delete(key)
- return
- }
- // 添加
- func Add_AllotTask(r AllotTask) (string, bool) {
- o := orm.NewOrm()
- // 生成编号
- rand_x := 0
- for true {
- r.T_allot_task_id = lib.GetRandstring(12, "abcdefghijklmnopqrstuvwxyz0123456789", int64(rand_x)) // 1,336,336
- err := o.Read(&r, "T_allot_task_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- break
- }
- rand_x += 1
- }
- _, err := o.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return "", false
- }
- Redis_AllotTask_Set(r.T_allot_task_id, r)
- return r.T_allot_task_id, true
- }
- func Update_AllotTask(m AllotTask, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- logs.Println("Number of records updated in database:", num)
- Redis_AllotTask_Set(m.T_allot_task_id, m)
- return true
- } else {
- logs.Error(lib.FuncName(), err)
- }
- return false
- }
- // 删除
- func Delete_AllotTask(v AllotTask) bool {
- o := orm.NewOrm()
- v.T_state = 0
- if num, err := o.Update(&v, "T_state"); err == nil {
- logs.Println("Number of records updated in database:", num)
- } else {
- logs.Error(lib.FuncName(), err)
- return false
- }
- Redis_AllotTask_DelK(v.T_allot_task_id)
- return true
- }
- // 获取 By
- func Read_AllotTask(T_allot_task_id string) (r AllotTask, is bool) {
- if r, is = Redis_AllotTask_Get(T_allot_task_id); is == true {
- return r, true
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(AllotTask))
- //err := qs.Filter("T_allot_task_id", T_allot_task_id).Filter("T_State", 1).One(&r)
- err := qs.Filter("T_allot_task_id", T_allot_task_id).Filter("T_state", 1).One(&r)
- if err != nil {
- return r, false
- }
- Redis_AllotTask_Set(T_allot_task_id, r)
- return r, true
- }
- // 获取任务列表
- func Read_AllotTask_List(T_Distributor_id, T_admin, T_uuid, T_name, T_project, T_scheme, T_collection, T_reporting, T_delivery string, T_status int,
- T_company_list []string, userMap, adminMap map[string]string, page int, page_z int) ([]AllotTask_, int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(AllotTask))
- var r []AllotTask
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_state", 1)
- if len(T_name) > 0 {
- cond1 = cond1.And("T_name__icontains", T_name)
- }
- if len(T_Distributor_id) > 0 {
- cond1 = cond1.And("T_Distributor_id", T_Distributor_id)
- }
- if len(T_uuid) > 0 {
- cond1 = cond1.And("T_uuid", T_uuid)
- }
- if len(T_admin) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_scheme", T_admin).Or("T_collection", T_admin).
- Or("T_reporting", T_admin).Or("T_delivery", T_admin))
- }
- if T_status > 0 {
- cond1 = cond1.And("T_status", T_status)
- }
- if len(T_company_list) > 0 {
- cond1 = cond1.And("T_uuid__in", T_company_list)
- }
- if len(T_project) > 0 {
- cond1 = cond1.And("T_project", T_project)
- }
- if len(T_scheme) > 0 {
- cond1 = cond1.And("T_scheme", T_scheme)
- }
- if len(T_collection) > 0 {
- cond1 = cond1.And("T_collection", T_collection)
- }
- if len(T_reporting) > 0 {
- cond1 = cond1.And("T_reporting", T_reporting)
- }
- if len(T_delivery) > 0 {
- cond1 = cond1.And("T_delivery", T_delivery)
- }
- if page_z == 9999 {
- qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
- } else {
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
- }
- cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
- // 转换
- var AllotTaskList []AllotTask_
- for _, v := range r {
- AllotTaskList = append(AllotTaskList, AllotTaskToAllotTask_(v, userMap, adminMap))
- }
- return AllotTaskList, int(cnt)
- }
- // 获取超时未接收的任务列表 (超过24小时)
- func Read_AllotTask_Timeout_List() ([]AllotTask, error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(AllotTask))
- var r []AllotTask
- // 计算24小时前的时间
- timeout := time.Now().Add(-24 * time.Hour)
- // 查询CreateTime超过24小时且状态为待接收的任务
- cond := orm.NewCondition()
- cond1 := cond.And("T_state", 1). // 正常状态
- And("T_status", AllotTaskStatusWaitReceive). // 待接收状态
- And("T_allot_time__lt", timeout) // 创建时间早于24小时前
- _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return nil, err
- }
- return r, nil
- }
- // 更新任务为已拒绝状态并添加拒绝记录
- func Update_AllotTask_To_Refused(task AllotTask, reason string) bool {
- o := orm.NewOrm()
- // 获取现有记录
- var existingRecords = make([]AllotTaskRecord, 0)
- if len(task.T_record) > 0 {
- json.Unmarshal([]byte(task.T_record), &existingRecords)
- }
- // 创建新的拒绝记录
- newRecord := AllotTaskRecord{
- T_collection: task.T_collection, // 系统自动拒绝
- T_status: AllotTaskStatusRefused,
- T_reason: reason,
- T_time: time.Now().Format("2006-01-02 15:04:05"),
- }
- existingRecords = append(existingRecords, newRecord)
- // 转换为JSON字符串
- updatedRecordsJSON, err := json.Marshal(existingRecords)
- if err != nil {
- logs.Error(lib.FuncName(), "JSON marshal error:", err)
- return false
- }
- // 更新任务状态和记录
- task.T_record = string(updatedRecordsJSON)
- task.T_status = AllotTaskStatusRefused
- task.T_reason = reason
- if num, err := o.Update(&task, "T_record", "T_status", "T_reason"); err == nil {
- logs.Println("Number of records updated in database:", num)
- // 更新Redis缓存
- Redis_AllotTask_Set(task.T_allot_task_id, task)
- return true
- } else {
- logs.Error(lib.FuncName(), err)
- }
- return false
- }
- // 获取项目负责人列表
- func Get_AllotTask_UserList(T_type string) []string {
- o := orm.NewOrm()
- var err error
- var pl_lists []string
- switch T_type {
- case "T_project":
- _, err = o.Raw("SELECT DISTINCT t_project FROM allot_task LIMIT 0,1000").QueryRows(&pl_lists)
- case "T_scheme":
- _, err = o.Raw("SELECT DISTINCT t_scheme FROM allot_task LIMIT 0,1000").QueryRows(&pl_lists)
- case "T_collection":
- _, err = o.Raw("SELECT DISTINCT t_collection FROM allot_task LIMIT 0,1000").QueryRows(&pl_lists)
- case "T_reporting":
- _, err = o.Raw("SELECT DISTINCT t_reporting FROM allot_task LIMIT 0,1000").QueryRows(&pl_lists)
- }
- if err != nil {
- logs.Error("获取项目负责人列表失败:", err)
- }
- return pl_lists
- }
|