123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package project
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jaryhe/gopkgs/logger"
- "go.uber.org/zap"
- "smart-site-management/errors"
- "smart-site-management/model"
- "smart-site-management/pb/v1"
- "strconv"
- "time"
- )
- var projectFieldMap = map[string]string{
- "safety_record_no":"安监备案号",
- "project_no":"项目编号",
- "name":"项目名称",
- "short_name":"项目简称",
- "project_function":"项目功能",
- "project_type":"项目类型",
- "price":"项目造价",
- "desc":"项目介绍",
- "project_leader":"项目负责人",
- "covered_area":"建筑面积",
- "phone":"项目负责人电话",
- "total_level":"总层数",
- "building_storey":"建筑层数",
- "construction":"施工单位",
- "development":"建设单位",
- "oversee":"监理单位",
- "design":"设计单位",
- "exploration":"勘察单位",
- "location":"地址",
- "lon":"经度",
- "lat":"纬度",
- "is_finish":"是否完工",
- "project_effect_pic":"项目效果图",
- "project_plan":"项目平面图",
- }
- var projectFieldTypeMap = map[string]string{
- "id": "int",
- "cid": "int",
- "safety_record_no": "string",
- "project_no": "string",
- "name": "string",
- "short_name": "string",
- "project_function": "string",
- "project_type": "string",
- "price": "float",
- "desc": "string",
- "project_leader": "string",
- "covered_area": "string",
- "phone": "string",
- "total_level": "string",
- "building_storey": "string",
- "construction": "string",
- "development": "string",
- "oversee": "string",
- "design": "string",
- "exploration": "string",
- "location": "string",
- "lon": "float",
- "lat": "float",
- "is_finish": "int",
- "project_effect_pic":"string",
- "project_plan":"string",
- }
- var layout = "2006-01-02 15:04:05"
- func ProjectChange(ctx context.Context, req *v1.ProjectChangeRequest) (reply *v1.ProjectChangeReply, err error) {
- // 构造更新map
- dstm := map[string]interface{}{}
- for _, v := range req.List {
- ftype, ok := projectFieldTypeMap[v.Name]
- if !ok {
- logger.Error("ProjectChange",
- zap.String("err", fmt.Sprintf("字段不存在%s", v.Name)))
- return nil, errors.ParamsError
- }
- switch ftype {
- case "int":
- value, _ := strconv.ParseInt(v.Value, 10, 64)
- dstm[v.Name] = value
- case "float":
- value, _ := strconv.ParseFloat(v.Value, 64)
- dstm[v.Name] = value
- default:
- dstm[v.Name] = v.Value
- }
- }
- // 获取原始数据
- origin := &model.TProject{}
- where := map[string]interface{}{"id":req.Id}
- err = origin.Find(database.DB(), where)
- if err != nil {
- logger.Error("ProjectChange",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- if origin.Id == 0 {
- return nil, errors.NoRecordError
- }
- // 构造更新字段的json字符串
- originFull := map[string]interface{}{}
- bytes, _ := json.Marshal(origin)
- json.Unmarshal([]byte(bytes), &originFull)
- originChoose := map[string]interface{}{}
- for k, _ := range dstm {
- if _, ok := originFull[k];!ok {
- return nil, errors.SystemError
- }
- originChoose[k] = originFull[k]
- }
- dstBytes, _ := json.Marshal(dstm)
- originBytes, _ := json.Marshal(originChoose)
- // 提工单
- job := model.TProjectJob{}
- now := time.Now()
- job.CreatedAt = now
- job.UpdatedAt = now
- job.Type = model.ProjectJobTypeUpdateProject
- job.ProjectId = origin.Id
- job.Origin = string(originBytes)
- job.Content = string(dstBytes)
- err = job.Insert(database.DB())
- if err != nil {
- logger.Error("ProjectChange",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- return &v1.ProjectChangeReply{}, nil
- }
|