| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package system
- import (
- "dashoo.cn/micro_libary/request"
- "errors"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/os/gtime"
- "lims_adapter/dao/system"
- model "lims_adapter/model/system"
- "lims_adapter/model/user"
- "strconv"
- )
- type Service struct {
- Dao *system.BaseItemdetailsDao
- Tenant string
- }
- // NewSrv 服务初始化
- func NewSrv(tenant string) Service {
- return Service{Dao: system.NewBaseItemdetailsDao(tenant), Tenant: tenant}
- }
- // DictInfo 获取字典数据
- func (s Service) DictInfo(req *model.DictReq) (dictInfo []model.BaseItemdetails, err error) {
- where := "DictCode = '" + req.DictCode + "' and Enabled = 1"
- if req.ItemName != "" {
- where += " and ItemName = '" + req.ItemName + "'"
- }
- if req.ItemValue != "" {
- where += " and ItemValue = '" + req.ItemValue + "'"
- }
- if req.SortCode != "" {
- where += " and SortCode = '" + req.SortCode + "'"
- }
- err = s.Dao.M.Where(where).Scan(&dictInfo)
- if err != nil {
- return dictInfo, err
- }
- return dictInfo, nil
- }
- // UserList 设备预约权管理:获取用户信息
- func (s Service) UserList(req *user.UserInfoReq) (userInfos user.UserInfoRsp, err error) {
- where := "Enabled = 1"
- departmentId := strconv.Itoa(req.DepartmentId)
- instrumentId := strconv.Itoa(req.InstrumentId)
- current := req.PageNun
- size := req.PageSize
- count := 0
- model := s.Dao.DB.Model("base_user a")
- if departmentId != "" && departmentId != "0" {
- where += " and DepartmentId = " + departmentId
- }
- if req.ReqType == 1 { // 预约资格
- count, err = model.Where(where).Count()
- } else if req.ReqType == 2 { // 优先预约权
- where += " and Id not in(select UserId from base_equipment_qualification where EquipmentId = " +
- instrumentId + " and Qualification in (0,3))"
- count, err = model.Where(where).Count()
- }
- if err != nil {
- return userInfos, err
- }
- userInfos.Total = count
- model.Page(current, size).Scan(&userInfos.Records)
- return userInfos, nil
- }
- // GetProgramGroup 获取课题组信息
- func (s Service) GetProgramGroup(id int) ([]model.BaseProgramGroup, error) {
- programGroup := []model.BaseProgramGroup{}
- err := s.Dao.DB.Model("base_program_group").Where("OrganizeId = ?", id).Scan(&programGroup)
- if err != nil {
- return nil, err
- }
- return programGroup, nil
- }
- // GetUserList 主从用户管理:获取用户信息
- func (s Service) GetUserList(req *user.UserInfoReq, userId int32) (userInfos user.UserInfoRsp, err error) {
- where := "a.Enabled = 1"
- departmentId := strconv.Itoa(req.DepartmentId)
- current := req.PageNun
- size := req.PageSize
- count := 0
- model := s.Dao.DB.Model("base_user a").LeftJoin("base_role b", "a.RoleId = b.Id").
- LeftJoin("user_account_bind c", "a.Id = c.UserId").
- LeftJoin("base_account d", "c.AccountId = d.Id")
- if departmentId != "" && departmentId != "0" {
- where += " and DepartmentId = " + departmentId
- }
- if req.UserName != "" {
- where += " and a.UserName = '" + req.UserName + "' or a.RealName = '" + req.UserName + "'"
- }
- if req.Mobile != "" {
- where += " and a.Mobile = '" + req.Mobile + "'"
- }
- if req.ReqType == 1 { // 获取主用户信息
- count, err = model.LeftJoin("master_user e", "a.Id = e.UserId ").
- Where(where + " and e.Id is not null").Count()
- } else if req.ReqType == 2 { // 从用户信息
- count, err = model.LeftJoin("base_user_relation e", "a.Id = e.UserId").
- Where(where + " and e.Pid = " + strconv.Itoa(int(userId))).Count()
- } else if req.ReqType == 3 { // 不是主从用户信息
- count, err = model.Where(where + " and a.Id not in (select UserId from master_user) and a.Id not in (select UserId from base_user_relation)").Count()
- }
- if err != nil {
- return userInfos, err
- }
- userInfos.Total = count
- model.Fields("a.Id, a.UserName, a.RealName, a.Mobile, a.DepartmentName, b.RealName as RoleInfo, d.AccountName, d.Id AccountId").
- Page(current, size).Scan(&userInfos.Records)
- return userInfos, nil
- }
- // AddMainUserOrSubUser 添加主从用户
- func (s Service) AddMainUserOrUser(req *user.AddMainOrSubReq, userInfo request.UserInfo) error {
- for _, v := range req.Ids {
- if req.ReqType == 1 { // 新增主用户
- s.Dao.DB.Model("master_user").
- Insert(g.Map{"UserId": v})
- } else if req.ReqType == 2 { // 添加主从用户关系
- // 添加主从用户关系表
- saveEntity := user.BaseUserRelation{
- UserId: v,
- Pid: int(userInfo.Id),
- PName: userInfo.RealName,
- CreatedBy: int(userInfo.Id),
- CreatedAt: gtime.Now(),
- }
- s.Dao.DB.Model("base_user_relation").Insert(saveEntity)
- }
- }
- return nil
- }
- // DeleteMainUserOrSubUser 删除主从用户
- func (s Service) DeleteMainUserOrSubUser(req *user.AddMainOrSubReq, info request.UserInfo) error {
- if req.ReqType == 1 { // 删除主用户 先判断是否有从用户
- count, err := s.Dao.DB.Model("base_user_relation").Where("Pid = ?", req.Id).Count()
- if err != nil {
- return err
- }
- if count > 0 {
- return errors.New("该用户有从用户,无法删除!")
- }
- s.Dao.DB.Model("master_user").Delete("UserId = ?", req.Id)
- } else if req.ReqType == 2 { // 删除从用户 先判断是否有未结账
- count, err := s.Dao.DB.Model("settle_account_main").
- Where("AppointUserId = " + strconv.Itoa(req.Id) + " and SettleStatus = 1").Count()
- if err != nil {
- return err
- }
- if count > 0 {
- return errors.New("该用户有未结账明细,无法删除!")
- }
- s.Dao.DB.Model("base_user_relation").Delete("UserId = ?", req.Id)
- }
- return nil
- }
- // UserBindAccount 用户绑定
- func (s Service) UserBindAccount(req *user.UserBindAccountReq) error {
- _, err := s.Dao.DB.Model("user_account_bind").Delete("UserId = " + strconv.Itoa(req.UserId))
- if err != nil {
- return err
- }
- _, err = s.Dao.DB.Model("user_account_bind").
- Insert(g.Map{"UserId": req.UserId, "AccountId": req.Id, "CreateOn": gtime.Now()})
- if err != nil {
- return err
- }
- return nil
- }
|