作者 tangxvhui

更新模板编辑

@@ -195,36 +195,123 @@ func addSelfCheckData(templateId int64, data []protocol.TemplateSelfCheck, om or @@ -195,36 +195,123 @@ func addSelfCheckData(templateId int64, data []protocol.TemplateSelfCheck, om or
195 } 195 }
196 196
197 //editSelfCheckData 机会模板设置-自查内容设置 197 //editSelfCheckData 机会模板设置-自查内容设置
  198 +// func editSelfCheckData(templateId int64, data []protocol.TemplateSelfCheck, om orm2.Ormer) error {
  199 +// if len(data) == 0 {
  200 +// return nil
  201 +// }
  202 +// var (
  203 +// err error
  204 +// )
  205 +// /**简单做法**/
  206 +// err = models.DeleteAuditCheckByTempelate(templateId, om)
  207 +// if err != nil {
  208 +// log.Error("软删除audit_check数据失败,err;%s", err)
  209 +// return err
  210 +// }
  211 +// err = addSelfCheckData(templateId, data, om)
  212 +// if err != nil {
  213 +// log.Error("添加audit_check数据失败,err:%s", err)
  214 +// return err
  215 +// }
  216 +// return nil
  217 +// }
  218 +
  219 +//editSelfCheckData2 编辑自查内容
198 func editSelfCheckData(templateId int64, data []protocol.TemplateSelfCheck, om orm2.Ormer) error { 220 func editSelfCheckData(templateId int64, data []protocol.TemplateSelfCheck, om orm2.Ormer) error {
199 - if len(data) == 0 {  
200 - return nil  
201 - }  
202 var ( 221 var (
203 - err error 222 + deleteId []int64
  223 + updateSelfCheck []models.AuditCheck
  224 + newSelfCheckData []models.AuditCheck
  225 + oldSelfCheckMap = make(map[int64]*models.AuditCheck)
204 ) 226 )
205 - /**简单做法**/  
206 - err = models.DeleteAuditCheckByTempelate(templateId, om) 227 + oldSelfCheck, err := models.GetAuditCheckByTemplate(templateId)
207 if err != nil { 228 if err != nil {
208 - log.Error("软删除audit_check数据失败,err;%s", err) 229 + log.Error("获取audit_check失败,err:%s", err)
209 return err 230 return err
210 } 231 }
211 - err = addSelfCheckData(templateId, data, om) 232 + for i := range oldSelfCheck {
  233 + oldSelfCheckMap[oldSelfCheck[i].Id] = &oldSelfCheck[i]
  234 + }
  235 + nowTime := time.Now()
  236 + defaultItem := models.AuditCheckItems{
  237 + Items: []models.AuditCheckItem{
  238 + models.AuditCheckItem{Item: "是", NeedOther: 0},
  239 + models.AuditCheckItem{Item: "否", NeedOther: 1},
  240 + models.AuditCheckItem{Item: "不清楚", NeedOther: 0},
  241 + },
  242 + }
  243 + defaultItemJson, _ := json.Marshal(defaultItem)
  244 + for k, v := range data {
  245 + m := models.AuditCheck{
  246 + Id: v.Id,
  247 + Pid: v.Pid,
  248 + Title: v.Title,
  249 + CreateTime: nowTime,
  250 + Items: string(defaultItemJson),
  251 + Enable: models.AUDITCHECK_ENABLE_YES,
  252 + TemplateId: templateId,
  253 + }
  254 + if m.Id == 0 {
  255 + m.Id = idworker.NextId()
  256 + newSelfCheckData = append(newSelfCheckData, m)
  257 + } else if _, ok := oldSelfCheckMap[m.Id]; ok {
  258 + updateSelfCheck = append(updateSelfCheck, m)
  259 + delete(oldSelfCheckMap, m.Id)
  260 + }
  261 + for _, vv := range data[k].Child {
  262 + mm := models.AuditCheck{
  263 + Id: vv.Id,
  264 + Pid: m.Id,
  265 + Title: vv.Title,
  266 + CreateTime: nowTime,
  267 + Items: string(defaultItemJson),
  268 + Enable: models.AUDITCHECK_ENABLE_YES,
  269 + TemplateId: templateId,
  270 + }
  271 + if mm.Id == 0 {
  272 + mm.Id = idworker.NextId()
  273 + newSelfCheckData = append(newSelfCheckData, mm)
  274 + } else if _, ok := oldSelfCheckMap[m.Id]; ok {
  275 + updateSelfCheck = append(updateSelfCheck, mm)
  276 + delete(oldSelfCheckMap, mm.Id)
  277 + }
  278 + }
  279 + }
  280 + for i := range oldSelfCheckMap {
  281 + deleteId = append(deleteId, oldSelfCheckMap[i].Id)
  282 + }
  283 +
  284 + //删除废弃项
  285 + if len(deleteId) > 0 {
  286 + _, err = om.QueryTable(&models.AuditCheck{}).
  287 + Filter("id__in", deleteId).
  288 + Update(orm2.Params{
  289 + "enable": models.AUDITCHECK_ENABLE_NO,
  290 + })
212 if err != nil { 291 if err != nil {
213 - log.Error("添加audit_check数据失败,err:%s", err) 292 + log.Error("删除audit_check旧数据失败,err:%s", err)
214 return err 293 return err
215 } 294 }
216 - return nil  
217 -}  
218 -  
219 -//editSelfCheckData2 编辑自查内容  
220 -func editSelfCheckData2(templateId int64, data []protocol.TemplateSelfCheck, om orm2.Ormer) error {  
221 - // var (  
222 - // updateId []int64  
223 - // deleteId []int64  
224 - // updateSelfCheck []models.AuditCheck  
225 - // newSelfCheckData []models.AuditCheck  
226 - // )  
227 - 295 + }
  296 + //添加新的项
  297 + if len(newSelfCheckData) > 0 {
  298 + if _, err = om.InsertMulti(10, newSelfCheckData); err != nil {
  299 + log.Error("添加audit_check数据失败:%s", err)
  300 + return err
  301 + }
  302 + }
  303 + //更新项
  304 + for i := range updateSelfCheck {
  305 + _, err = om.QueryTable(&models.AuditCheck{}).
  306 + Filter("id", updateSelfCheck[i].Id).
  307 + Update(orm2.Params{
  308 + "title": updateSelfCheck[i].Title,
  309 + })
  310 + if err != nil {
  311 + log.Error("更新audit_check数据失败:%s", err)
  312 + return err
  313 + }
  314 + }
228 return nil 315 return nil
229 } 316 }
230 317