func (this *OrganizationController) Add() { pid, err := this.GetInt("pid") unitname := this.GetString("UnitName") sortrank, _ := this.GetInt("SortRank") remark := this.GetString("Remark") o := orm.NewOrm() qs := o.QueryTable("organization_unit") num, _ := qs.Filter("unitname", unitname).Count() if num > 0 { this.Data["json"] = &Rsp{Success: false, Msg: "已有相同名称的学校!"} this.ServeJson() return } var organization models.OrganizationUnit organization.UnitName = unitname organization.Pid = int(pid) organization.SortRank = uint8(sortrank) organization.Remark = remark organization.Status = 1 _, err = o.Insert(&organization) if err != nil { beego.Error(err) this.Data["json"] = &Rsp{Success: false, Msg: "新建学校失败!"} this.ServeJson() return } this.Data["json"] = &Rsp{Success: true, Msg: "新建学校成功!"} this.ServeJson() }
func (this *OrganizationController) Modify() { id, err := this.GetInt("id") beego.Info(id) pid, err := this.GetInt("Pid") unitname := this.GetString("UnitName") sortrank, err := this.GetInt("SortRank") remark := this.GetString("Remark") o := orm.NewOrm() qs := o.QueryTable("organization_unit") num, _ := qs.Filter("id", id).Count() if num != 1 { this.Data["json"] = &Rsp{Success: false, Msg: "找不到此组织机构,无法修改!"} this.ServeJson() return } var organization models.OrganizationUnit err = qs.Filter("id", id).One(&organization) if err != nil { this.Data["json"] = &Rsp{Success: false, Msg: "读取组织机构信息出错!"} this.ServeJson() return } organization.Pid = int(pid) organization.UnitName = unitname organization.SortRank = uint8(sortrank) organization.Remark = remark organization.Status = 1 _, err = o.Update(&organization) if err != nil { beego.Error(err) this.Data["json"] = &Rsp{Success: false, Msg: "无法修改组织机构,插入数据有错!"} this.ServeJson() return } //更新有子节点的status状态为0,无子结点的状态为1 _, err = o.Raw("update `organization_unit` a inner join (select id from `organization_unit` where id in (select distinct pid from `organization_unit`)) b on a.id=b.id set status=0").Exec() if err != nil { beego.Error(err) } _, err = o.Raw("update `organization_unit` a inner join (select id from `organization_unit` where id not in (select distinct pid from `organization_unit`)) b on a.id=b.id set status=1").Exec() if err != nil { beego.Error(err) } this.Data["json"] = &Rsp{Success: true, Msg: "成功修改组织机构!"} this.ServeJson() }