| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package learning
- import (
- "context"
- "database/sql"
- "fmt"
- "lims_adapter/dao/learning"
- "lims_adapter/model/equipment"
- "lims_adapter/model/learning"
- "dashoo.cn/micro_libary/micro_srv"
- "dashoo.cn/micro_libary/myerrors"
- "dashoo.cn/micro_libary/request"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- )
- type LearningSkillService struct {
- LearningSkillDao *dao.LearningSkillDao
- LearningSkillInstrumentDao *dao.LearningSkillInstrumentDao
- Tenant string
- userInfo request.UserInfo
- }
- func NewLearningSkillService(ctx context.Context) (*LearningSkillService, error) {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
- }
- // 获取用户信息
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- return &LearningSkillService{
- LearningSkillDao: dao.NewLearningSkillDao(tenant),
- LearningSkillInstrumentDao: dao.NewLearningSkillInstrumentDao(tenant),
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s LearningSkillService) Get(ctx context.Context, req *learning.LearningSkillGetReq) (ent *learning.LearningSkillGetRsp, err error) {
- var skill *learning.LearningSkill
- if req.Id != 0 {
- skill, err = s.LearningSkillDao.Where("Id = ?", req.Id).One()
- if err != nil {
- return
- }
- }
- if req.Name != "" {
- skill, err = s.LearningSkillDao.Where("Name = ?", req.Name).One()
- if err != nil {
- return
- }
- }
- if skill == nil {
- return nil, myerrors.NewMsgError(nil, "技能不存在")
- }
- instr := []equipment.Instrument{}
- err = s.LearningSkillDao.DB.
- Table("learning_skill_instrument a").
- LeftJoin("instrument b", "a.InstrumentId=b.Id").
- Where("a.SkillId = ?", skill.Id).
- Fields("b.*").
- Structs(&instr)
- if err != nil && err != sql.ErrNoRows {
- return nil, err
- }
- return &learning.LearningSkillGetRsp{
- Id: skill.Id,
- Name: skill.Name,
- Instrument: instr,
- }, nil
- }
- func (s LearningSkillService) List(ctx context.Context) ([]*learning.LearningSkill, error) {
- return s.LearningSkillDao.All()
- }
- func (s LearningSkillService) Add(ctx context.Context, req *learning.LearningSkillAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, validErr.Current()
- }
- skill, err := s.LearningSkillDao.Where("Name = ?", req.Name).One()
- if err != nil {
- return 0, err
- }
- if skill != nil {
- return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能已存在: %s", req.Name))
- }
- skillId, err := s.LearningSkillDao.InsertAndGetId(learning.LearningSkill{
- Name: req.Name,
- CreatedAt: gtime.Now(),
- UpdatedAt: gtime.Now(),
- })
- if err != nil {
- return 0, err
- }
- relation := []learning.LearningSkillInstrument{}
- for _, instrumentId := range req.InstrumentId {
- r, err := g.DB(s.Tenant).Table("instrument").Where("Id = ?", instrumentId).One()
- if err != nil {
- return 0, err
- }
- if r.IsEmpty() {
- return 0, myerrors.NewMsgError(nil, fmt.Sprintf("设备不存在: %d", instrumentId))
- }
- relation = append(relation, learning.LearningSkillInstrument{
- SkillId: int(skillId),
- InstrumentId: instrumentId,
- })
- }
- _, err = s.LearningSkillInstrumentDao.Insert(relation)
- return int(skillId), err
- }
- func (s LearningSkillService) Update(ctx context.Context, req *learning.LearningSkillUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return validErr.Current()
- }
- if req.Name != "" {
- skill, err := s.LearningSkillDao.Where("Name = ?", req.Name).One()
- if err != nil {
- return err
- }
- if skill != nil {
- return myerrors.NewMsgError(nil, fmt.Sprintf("技能已存在: %s", req.Name))
- }
- _, err = s.LearningSkillDao.
- Where("Id = ?", req.Id).
- Data("Name = ?", req.Name).
- Update()
- if err != nil {
- return err
- }
- }
- if req.InstrumentId != nil {
- _, err := s.LearningSkillInstrumentDao.
- Where("SkillId = ?", req.Id).Delete()
- if err != nil {
- return err
- }
- relation := []learning.LearningSkillInstrument{}
- for _, instrumentId := range *req.InstrumentId {
- r, err := g.DB(s.Tenant).Table("instrument").Where("Id = ?", instrumentId).One()
- if err != nil {
- return err
- }
- if r.IsEmpty() {
- return myerrors.NewMsgError(nil, fmt.Sprintf("设备不存在: %d", instrumentId))
- }
- relation = append(relation, learning.LearningSkillInstrument{
- SkillId: int(req.Id),
- InstrumentId: instrumentId,
- })
- }
- _, err = s.LearningSkillInstrumentDao.Insert(relation)
- return err
- }
- return nil
- }
- func (s LearningSkillService) Delete(ctx context.Context, id int) error {
- _, err := s.LearningSkillInstrumentDao.Where("SkillId = ?", id).Delete()
- if err != nil {
- return err
- }
- _, err = s.LearningSkillDao.Where("Id = ?", id).Delete()
- if err != nil {
- return err
- }
- return nil
- }
|