| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- 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
- LearningMaterialSrv *LearningMaterialService
- LearningRecordSrv *LearningLearningRecordService
- 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())
- }
- lrSrv, err := NewLearningLearningRecordService(ctx)
- if err != nil {
- return nil, err
- }
- lmSrv, err := NewLearningMaterialService(ctx)
- if err != nil {
- return nil, err
- }
- return &LearningSkillService{
- LearningSkillDao: dao.NewLearningSkillDao(tenant),
- LearningSkillInstrumentDao: dao.NewLearningSkillInstrumentDao(tenant),
- LearningMaterialSrv: lmSrv,
- LearningRecordSrv: lrSrv,
- 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, myerrors.NewMsgError(nil, validErr.Current().Error())
- }
- 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 myerrors.NewMsgError(nil, validErr.Current().Error())
- }
- skill, err := s.LearningSkillDao.Where("Id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if skill == nil {
- return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.Id))
- }
- if req.Name != "" {
- existSkill, err := s.LearningSkillDao.Where("Name = ?", req.Name).One()
- if err != nil {
- return err
- }
- if existSkill != nil && existSkill.Id != skill.Id {
- 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)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (s LearningSkillService) Delete(ctx context.Context, id []int) error {
- _, err := s.LearningSkillInstrumentDao.Where("SkillId IN (?)", id).Delete()
- if err != nil {
- return err
- }
- _, err = s.LearningSkillDao.Where("Id IN (?)", id).Delete()
- return err
- }
- func (s LearningSkillService) ListMy(ctx context.Context) ([]*learning.LearningSkillMy, error) {
- ent, err := s.LearningSkillDao.All()
- if err != nil {
- return nil, err
- }
- list := []*learning.LearningSkillMy{}
- for _, skill := range ent {
- materialIds, err := s.LearningMaterialSrv.MaterialIds(ctx, skill.Id)
- if err != nil {
- return nil, err
- }
- recordIds, err := s.LearningRecordSrv.LearntMaterialIds(ctx, int(s.userInfo.Id))
- if err != nil {
- return nil, err
- }
- // 如果没有关联的资料,直接算学完了
- learntAll := true
- for _, mid := range materialIds {
- found := false
- for _, rid := range recordIds {
- if mid == rid {
- found = true
- }
- }
- if !found {
- learntAll = false
- break
- }
- }
- // fmt.Println(skill.Id, materialIds, recordIds, learntAll)
- list = append(list, &learning.LearningSkillMy{
- LearningSkill: *skill,
- LearntAll: learntAll,
- })
- }
- return list, err
- }
|