package samplesinfo import ( "encoding/json" "fmt" "math" "strconv" "strings" "time" "dashoo.cn/backend/api/business/codecsequence" "dashoo.cn/backend/api/business/coderule" "dashoo.cn/backend/api/business/printscheme" "dashoo.cn/backend/api/business/samplesgroup" "dashoo.cn/backend/api/business/samplesubpackage" "dashoo.cn/backend/api/business/sampletype" . "dashoo.cn/backend/api/controllers" "dashoo.cn/utils" ) type SampleSubpackageController struct { BaseController } // @Title get // @Description get SampleType by token // @Success 200 {object} samplesubpackage.SampleSubpackage // @router /list [get] func (this *SampleSubpackageController) List() { var list []samplesubpackage.SamplesDetailModel page := this.GetPageInfoForm() svc := samplesubpackage.GetSampleSubpackageService(utils.DBE) where := " a.IState =2 and a.DeletionStateCode=0 " sampleCode := this.GetString("SampleCode") if sampleCode != "" { where = where + " and a.SampleCode like '%" + sampleCode + "%' " } barCode := this.GetString("BarCode") if barCode != "" { where = where + " and a.BarCode like '%" + barCode + "%' " } sampletype := this.GetString("SampleType") if sampletype != "" { where = where + " and b.SampleType like '%" + sampletype + "%'" } total, list := svc.GetPagingEntitiesWithOrderSerch(this.User.AccCode, page.CurrentPage, page.Size, "a.Id desc", where) var datainfo DataInfo datainfo.Items = list datainfo.CurrentItemCount = total this.Data["json"] = &datainfo this.ServeJSON() } // @Title 获取采集单列表 // @Description get SampleSubpackage by token // @Success 200 {object} samplesubpackage.SampleSubpackage // @router /getbooklist [get] func (this *SampleSubpackageController) GetBookList() { var list []samplesubpackage.SamplesBookDetailModel //获取分页信息 page := this.GetPageInfoForm() svc := samplesubpackage.GetSampleSubpackageService(utils.DBE) SampleCode := this.GetString("SampleCode") BarCode := this.GetString("BarCode") sampletypename := this.GetString("SampleType") orderby := "SampleCode desc" Order := this.GetString("Order") Prop := this.GetString("Prop") if Order != "" && Prop != "" { orderby = Prop + " " + Order } where := "1 = 1" if SampleCode != "" { where = where + " and SampleCode like '%" + SampleCode + "%'" } if BarCode != "" { where = where + " and BarCode like '%" + BarCode + "%'" } if sampletypename != "" { where = where + " and SampleType like '%" + sampletypename + "%'" } total, list := svc.GetSampleCodeBooklist(page.CurrentPage, page.Size, orderby, this.User.AccCode+SamplesBookDetailName, SampleTypeName, where) var datainfo DataInfo datainfo.Items = list datainfo.CurrentItemCount = total this.Data["json"] = &datainfo this.ServeJSON() } // @Title 判断身份证是否存在 // @Description get user by token // @Success 200 {object} models.Userblood // @router /getIdlist [get] func (this *SampleSubpackageController) GetIdlist() { SourceId := this.GetString("SourceId") svc := samplesubpackage.GetSampleSubpackageService(utils.DBE) where := " Id= '" + SourceId + "'" total := svc.GetIdlist(this.User.AccCode+DonorstbName, where) var datainfo DataInfo datainfo.Items = total this.Data["json"] = &datainfo this.ServeJSON() } // @Title get // @Description get SampleType by token // @Success 200 {object} samplesubpackage.SampleSubpackage // @router /subpackagelist/:samplecode [get] func (this *SampleSubpackageController) SubpackageList() { page := this.GetPageInfoForm() var Samplesubpackagelist []samplesubpackage.SamplesDetailModel svc := samplesubpackage.GetSampleSubpackageService(utils.DBE) where := " a.IState =2 and a.DeletionStateCode=0 " sampleCode := this.Ctx.Input.Param(":samplecode") samplecode := strings.Split(sampleCode, ",") if len(samplecode) > 1 { where = where + "and ( a.SampleCode = '" + samplecode[0] + "'" for i := 1; i < len(samplecode)-1; i++ { where = where + " or a.SampleCode = '" + samplecode[i] + "'" } where = where + " or a.SampleCode = '" + samplecode[len(samplecode)-1] + "')" } else { where = where + " and a.SampleCode like '%" + samplecode[0] + "%'" } total, Samplesubpackagelist := svc.GetPagingEntitiesSerch(this.User.AccCode, page.CurrentPage, page.Size, "a.Id desc", where) var datainfo DataInfo datainfo.Items = Samplesubpackagelist datainfo.CurrentItemCount = total this.Data["json"] = &datainfo this.ServeJSON() } // @Title 创建样本类型 // @Description 创建样本类型 // @Param body body business.device.DeviceChannels "传感器信息" // @Success 200 {object} controllers.Request // @router / [post] func (this *SampleSubpackageController) Add() { var errinfo ErrorDataInfo var jsonblob = this.Ctx.Input.RequestBody var datamain samplesubpackage.SamplesMain var datadetail samplesubpackage.SamplesDetail var dataother SampmanageModel var datamainnew samplesubpackage.SamplesMain var datadetailnew samplesubpackage.SamplesDetail json.Unmarshal(jsonblob, &datamain) json.Unmarshal(jsonblob, &datadetail) json.Unmarshal(jsonblob, &datamainnew) json.Unmarshal(jsonblob, &datadetailnew) json.Unmarshal(jsonblob, &dataother) groupid := this.GetString("groupid") groupname := this.GetString("groupname") subpackagenum := this.GetString("subpackagenum") Subpackagenum, _ := utils.StrTo(subpackagenum).Int() subnum, _ := utils.StrTo(utils.ToStr(subpackagenum)).Float32() capacity := this.GetString("capacity") datadetail.Capacity, _ = utils.StrTo(utils.ToStr(capacity)).Float32() subpackagecapacity := this.GetString("subpackagecapacity") Subpackagecapacity, _ := utils.StrTo(utils.ToStr(subpackagecapacity)).Float32() UsedCapacity := subnum * Subpackagecapacity svc := samplesubpackage.GetSampleSubpackageService(utils.DBE) datadetail.IState = 2 datadetail.CreateUserId, _ = utils.StrTo(this.User.Id).Int() datadetail.CreateBy = this.User.Realname var err error if datadetail.Capacity > 0 { if datadetail.BarCode == "" { datadetail.BarCode = datamain.SampleCode + "-1" } datamain.AccCode = this.User.AccCode datamain.MCreateUserId = datadetail.CreateUserId datamain.MCreateBy = datadetail.CreateBy datadetail.InitCapacity = datadetail.Capacity + UsedCapacity datamain.ReceiveDate = time.Unix(dataother.ReceiveDateint/1000, 0) _, err = svc.InsertSamplePre(this.User.AccCode+SamplesMaintbName, this.User.AccCode+SamplesDetailtbName, &datamain, &datadetail) } else { datamain.AccCode = this.User.AccCode datamain.MCreateUserId = datadetail.CreateUserId datamain.MCreateBy = datadetail.CreateBy datamain.ReceiveDate = time.Unix(dataother.ReceiveDateint/1000, 0) _, err = svc.InsertEntityBytbl(this.User.AccCode+SamplesMaintbName, &datamain) } if Subpackagenum > 1 && Subpackagecapacity > 0 { for subcapacity := Subpackagenum; subcapacity > 0; subcapacity-- { datadetailnew.IState = 2 datadetailnew.Capacity = Subpackagecapacity datadetailnew.InitCapacity, _ = utils.StrTo(utils.ToStr(subpackagecapacity)).Float32() datadetailnew.BarCode = svc.AutoGetBarCode(this.User.AccCode, datamainnew.SampleCode) datadetailnew.ParentBarCode = datadetail.BarCode _, err = svc.InsertEntityBytbl(this.User.AccCode+SamplesDetailtbName, &datadetailnew) if groupid != "" { var entity samplesgroup.GroupDetail entity.GroupId = groupid entity.GroupName = groupname entity.SampleId = datadetailnew.Id entity.SampleCode = datamain.SampleCode entity.BarCode = datadetailnew.BarCode entity.GroupType = "SampleGroup" entity.CreateBy = this.User.Realname entity.CreateUserId, _ = utils.StrTo(this.User.Id).Int() groupSvc := samplesgroup.GetSamplesGroupService(utils.DBE) groupSvc.InsertEntityBytbl(this.User.AccCode+GroupDetailName, &entity) } } } if dataother.Code_codeId > 0 { svc_coderule := coderule.GetCodeRuleService(utils.DBE) svc_coderule.UpdateLastNum(dataother.Code_lastnum, utils.ToStr(dataother.Code_codeId)) } //打印相关 var sisAutoPrintSample string sdefaultfangan := "" if dataother.Autoprintchecked { sisAutoPrintSample = "true" var PrintSchemeList []printscheme.PrintScheme svcprint := printscheme.GetPrintSchemeService(utils.DBE) where1 := " Funcode = '样本' and AccCode = '" + this.User.AccCode + "' and IsDefault=1 " PrintSchemeList = svcprint.GetPrintSchemeList(where1) if len(PrintSchemeList) > 0 { sdefaultfangan = PrintSchemeList[0].FileName } } else { sisAutoPrintSample = "false" } saveandadd := dataother.Saveandadd var Auto_Code_Struct AutoCodeStruct if err == nil { if saveandadd == "1" { //保存并新增新编码 if dataother.Code_codeId > 0 { //使用默认规则 n, _ := utils.StrTo(dataother.Code_lastnum).Int() count := len(dataother.Code_lastnum) Auto_Code_Struct.Num = strconv.FormatFloat(float64(n+1)*(1/math.Pow(10, float64(count))), 'f', count, 64)[2:] Auto_Code_Struct.SampleCode = this.AutoBarCodeAdd() } else { //不使用默认规则 Auto_Code_Struct.Num = "" Auto_Code_Struct.SampleCode = "" } if datamain.AccCode == "" { Auto_Code_Struct.PrintAccCode = this.User.AccCode } else { Auto_Code_Struct.PrintAccCode = datamain.AccCode } Auto_Code_Struct.PrintCode = datadetail.Id Auto_Code_Struct.PrintSAuto = sisAutoPrintSample Auto_Code_Struct.Printfangan = sdefaultfangan errinfo.Code = 1 errinfo.Message = "保存成功" errinfo.Item = Auto_Code_Struct } else if saveandadd == "2" { //保存并添加分管 if dataother.Code_codeId > 0 { //使用默认规则 Auto_Code_Struct.Num = dataother.Code_lastnum } else { //不使用默认规则 Auto_Code_Struct.Num = "" } Auto_Code_Struct.SampleCode = datamain.SampleCode Auto_Code_Struct.PrintAccCode = this.User.AccCode Auto_Code_Struct.PrintCode = datadetail.Id Auto_Code_Struct.PrintSAuto = sisAutoPrintSample Auto_Code_Struct.Printfangan = sdefaultfangan errinfo.Code = 2 errinfo.Message = "保存成功" errinfo.Item = Auto_Code_Struct } else { if sisAutoPrintSample == "true" { errinfo.Code = 0 errinfo.Message = "保存成功" } } } else { if saveandadd == "1" { Auto_Code_Struct.SampleCode = "-1" errinfo.Code = -2 errinfo.Message = "保存失败!" errinfo.Item = Auto_Code_Struct } else { errinfo.Code = -1 errinfo.Message = "保存失败!" + err.Error() } } this.Data["json"] = &errinfo this.ServeJSON() } // @Title 创建样本类型--蜜蜂所 // @Description 创建样本类型 // @Param body body business.device.DeviceChannels "传感器信息" // @Success 200 {object} controllers.Request // @router /subpackage [post] func (this *SampleSubpackageController) Subpackage() { var errinfo ErrorDataInfo var jsonblob = this.Ctx.Input.RequestBody var datamain samplesubpackage.SamplesMain var datadetail samplesubpackage.SamplesDetail var dataother SampmanageModel var datamainnew samplesubpackage.SamplesMain var datadetailnew samplesubpackage.SamplesDetail json.Unmarshal(jsonblob, &datamain) json.Unmarshal(jsonblob, &datadetail) json.Unmarshal(jsonblob, &datamainnew) json.Unmarshal(jsonblob, &datadetailnew) json.Unmarshal(jsonblob, &dataother) timecode := this.GetString("timecode") //蜜蜂所编码 groupid := this.GetString("groupid") groupname := this.GetString("groupname") subpackagenum := this.GetString("subpackagenum") Subpackagenum, _ := utils.StrTo(subpackagenum).Int() subnum, _ := utils.StrTo(utils.ToStr(subpackagenum)).Float32() capacity := this.GetString("capacity") datadetail.Capacity, _ = utils.StrTo(utils.ToStr(capacity)).Float32() subpackagecapacity := this.GetString("subpackagecapacity") Subpackagecapacity, _ := utils.StrTo(utils.ToStr(subpackagecapacity)).Float32() UsedCapacity := subnum * Subpackagecapacity svc := samplesubpackage.GetSampleSubpackageService(utils.DBE) datadetail.IState = 2 datadetail.CreateUserId, _ = utils.StrTo(this.User.Id).Int() datadetail.CreateBy = this.User.Realname //判断是否需要循环添加 arr := strings.Split(datamain.SampleCode, "-") if arr[1] == "全部" { var codeModel codecsequence.CodecSequence where_seq := " SeqName = '" + strconv.Itoa(datamain.SourceId) + "'" svc.GetEntityByWhere(CodecSequenceName, where_seq, &codeModel) for i := codeModel.MinValue; i < codeModel.MaxValue+1; i++ { datamain.SampleCode = arr[0] + "-" + fmt.Sprintf("%04s", strconv.Itoa(i)) + "-" + arr[2] datamainnew.SampleCode = arr[0] + "-" + fmt.Sprintf("%04s", strconv.Itoa(i)) + "-" + arr[2] var err error if datadetail.Capacity > 0 { if datadetail.BarCode == "" { datadetail.BarCode = datamain.SampleCode + "01" } datamain.AccCode = this.User.AccCode datamain.MCreateUserId = datadetail.CreateUserId datamain.MCreateBy = datadetail.CreateBy datadetail.InitCapacity = datadetail.Capacity + UsedCapacity datamain.ReceiveDate = time.Unix(dataother.ReceiveDateint/1000, 0) _, err = svc.InsertSamplePre(this.User.AccCode+SamplesMaintbName, this.User.AccCode+SamplesDetailtbName, &datamain, &datadetail) } else { datamain.AccCode = this.User.AccCode datamain.MCreateUserId = datadetail.CreateUserId datamain.MCreateBy = datadetail.CreateBy datamain.ReceiveDate = time.Unix(dataother.ReceiveDateint/1000, 0) _, err = svc.InsertEntityBytbl(this.User.AccCode+SamplesMaintbName, &datamain) } if Subpackagenum > 0 && Subpackagecapacity > 0 { for subcapacity := Subpackagenum; subcapacity > 0; subcapacity-- { datadetailnew.IState = 2 datadetailnew.Capacity = Subpackagecapacity datadetailnew.InitCapacity, _ = utils.StrTo(utils.ToStr(subpackagecapacity)).Float32() datadetailnew.BarCode = svc.AutoGetBarCodeAnimal(this.User.AccCode, datamainnew.SampleCode, timecode) datadetailnew.SampleCode = datamain.SampleCode datadetailnew.ParentBarCode = datadetail.BarCode _, err = svc.InsertEntityBytbl(this.User.AccCode+SamplesDetailtbName, &datadetailnew) if groupid != "" { var entity samplesgroup.GroupDetail entity.GroupId = groupid entity.GroupName = groupname entity.SampleId = datadetailnew.Id entity.SampleCode = datamain.SampleCode entity.BarCode = datadetailnew.BarCode entity.GroupType = "SampleGroup" entity.CreateBy = this.User.Realname entity.CreateUserId, _ = utils.StrTo(this.User.Id).Int() groupSvc := samplesgroup.GetSamplesGroupService(utils.DBE) groupSvc.InsertEntityBytbl(this.User.AccCode+GroupDetailName, &entity) } } } if dataother.Code_codeId > 0 { svc_coderule := coderule.GetCodeRuleService(utils.DBE) svc_coderule.UpdateLastNum(dataother.Code_lastnum, utils.ToStr(dataother.Code_codeId)) } saveandadd := dataother.Saveandadd var Auto_Code_Struct AutoCodeStruct if err == nil { if saveandadd == "1" { //保存并新增新编码 n, _ := utils.StrTo(dataother.Code_lastnum).Int() count := len(dataother.Code_lastnum) Auto_Code_Struct.Num = strconv.FormatFloat(float64(n+1)*(1/math.Pow(10, float64(count))), 'f', count, 64)[2:] Auto_Code_Struct.SampleCode = this.AutoBarCodeAdd() + "-" + timecode errinfo.Code = 1 errinfo.Message = "保存成功" errinfo.Item = Auto_Code_Struct } else if saveandadd == "2" { //保存并添加分管 Auto_Code_Struct.Num = dataother.Code_lastnum + "-" + timecode Auto_Code_Struct.SampleCode = datamain.SampleCode errinfo.Code = 2 errinfo.Message = "保存成功" errinfo.Item = Auto_Code_Struct } } else { if saveandadd == "1" { Auto_Code_Struct.SampleCode = "01" errinfo.Code = -2 errinfo.Message = "保存失败!" errinfo.Item = Auto_Code_Struct } else { errinfo.Code = -1 errinfo.Message = "保存失败!" + err.Error() } } this.Data["json"] = &errinfo this.ServeJSON() } } else { var err error if datadetail.Capacity > 0 { if datadetail.BarCode == "" { datadetail.BarCode = datamain.SampleCode + "01" } datamain.AccCode = this.User.AccCode datamain.MCreateUserId = datadetail.CreateUserId datamain.MCreateBy = datadetail.CreateBy datadetail.InitCapacity = datadetail.Capacity + UsedCapacity datamain.ReceiveDate = time.Unix(dataother.ReceiveDateint/1000, 0) _, err = svc.InsertSamplePre(this.User.AccCode+SamplesMaintbName, this.User.AccCode+SamplesDetailtbName, &datamain, &datadetail) } else { datamain.AccCode = this.User.AccCode datamain.MCreateUserId = datadetail.CreateUserId datamain.MCreateBy = datadetail.CreateBy datamain.ReceiveDate = time.Unix(dataother.ReceiveDateint/1000, 0) _, err = svc.InsertEntityBytbl(this.User.AccCode+SamplesMaintbName, &datamain) } if Subpackagenum > 1 && Subpackagecapacity > 0 { for subcapacity := Subpackagenum; subcapacity > 0; subcapacity-- { datadetailnew.IState = 2 datadetailnew.Capacity = Subpackagecapacity datadetailnew.InitCapacity, _ = utils.StrTo(utils.ToStr(subpackagecapacity)).Float32() datadetailnew.BarCode = svc.AutoGetBarCodeAnimal(this.User.AccCode, datamainnew.SampleCode, timecode) datadetailnew.ParentBarCode = datadetail.BarCode _, err = svc.InsertEntityBytbl(this.User.AccCode+SamplesDetailtbName, &datadetailnew) if groupid != "" { var entity samplesgroup.GroupDetail entity.GroupId = groupid entity.GroupName = groupname entity.SampleId = datadetailnew.Id entity.SampleCode = datamain.SampleCode entity.BarCode = datadetailnew.BarCode entity.GroupType = "SampleGroup" entity.CreateBy = this.User.Realname entity.CreateUserId, _ = utils.StrTo(this.User.Id).Int() groupSvc := samplesgroup.GetSamplesGroupService(utils.DBE) groupSvc.InsertEntityBytbl(this.User.AccCode+GroupDetailName, &entity) } } } if dataother.Code_codeId > 0 { svc_coderule := coderule.GetCodeRuleService(utils.DBE) svc_coderule.UpdateLastNum(dataother.Code_lastnum, utils.ToStr(dataother.Code_codeId)) } saveandadd := dataother.Saveandadd var Auto_Code_Struct AutoCodeStruct if err == nil { if saveandadd == "1" { //保存并新增新编码 n, _ := utils.StrTo(dataother.Code_lastnum).Int() count := len(dataother.Code_lastnum) Auto_Code_Struct.Num = strconv.FormatFloat(float64(n+1)*(1/math.Pow(10, float64(count))), 'f', count, 64)[2:] Auto_Code_Struct.SampleCode = this.AutoBarCodeAdd() + "-" + timecode errinfo.Code = 1 errinfo.Message = "保存成功" errinfo.Item = Auto_Code_Struct } else if saveandadd == "2" { //保存并添加分管 Auto_Code_Struct.Num = dataother.Code_lastnum + "-" + timecode Auto_Code_Struct.SampleCode = datamain.SampleCode errinfo.Code = 2 errinfo.Message = "保存成功" errinfo.Item = Auto_Code_Struct } } else { if saveandadd == "1" { Auto_Code_Struct.SampleCode = "-1" errinfo.Code = -2 errinfo.Message = "保存失败!" errinfo.Item = Auto_Code_Struct } else { errinfo.Code = -1 errinfo.Message = "保存失败!" + err.Error() } } this.Data["json"] = &errinfo this.ServeJSON() } } func (this *SampleSubpackageController) AutoBarCodeAdd() string { ss, _ := this.AutoBarCodeModel() return ss } func (this *SampleSubpackageController) AutoBarCodeModel() (string, string) { //自动生成编码 var coderule_list []coderule.CodeRule var coderuledetail_list []coderule.CodeRuleDetail svc_coderule := coderule.GetCodeRuleService(utils.DBE) where2 := " AccCode='" + this.User.AccCode + "' and IsDefault=1" coderule_list = svc_coderule.GetCodeRuleList(where2) str_adds := "" value_sampletype := this.GetString("SampleType") value_samplesite := this.GetString("fid") //获取是否更改流水号。分装提取需要修改。20160520 isupdate := this.GetString("isupdate") lastnum := "" if len(coderule_list) == 1 { //获取编码规则的流水号 lastnum = coderule_list[0].LastNum where := " AccCode='" + this.User.AccCode + "' and MainId=" + utils.ToStr(coderule_list[0].Id) coderuledetail_list = svc_coderule.GetCodeRuleDetailOrderList(where, "SortCode") count := len(coderuledetail_list) for i := 0; i < count; i++ { count := coderuledetail_list[i].ItemNum if coderuledetail_list[i].ItemName == "样本类型" { sample_type := sampletype.GetSampleTypeService(utils.DBE) where2 := " AccCode='" + this.User.AccCode + "' and Id=" + value_sampletype entry := sample_type.GetSampleType(where2) if count <= len(entry.Code) { str_adds += entry.Code[:count] } else { str_add := "" for i := 0; i < (count - len(entry.Code)); i++ { str_add = entry.Code + "0" } str_adds += str_add } } else if coderuledetail_list[i].ItemName == "组织器官" { if count <= len(value_samplesite) { str_adds += value_samplesite[:count] } else { str_add := "" for i := 0; i < (count - len(value_samplesite)); i++ { str_add = value_samplesite + "0" } str_adds += str_add } } else if coderuledetail_list[i].ItemName == "其他" { str_adds += coderuledetail_list[i].ItemContent } else if coderuledetail_list[i].ItemName == "年份" { str := utils.ToStr(time.Now().Year()) str_adds += str[len(str)-count : len(str)] } else if coderuledetail_list[i].ItemName == "月份" { str := utils.ToStr(time.Now()) str_adds += str[5 : 5+count] } else if coderuledetail_list[i].ItemName == "日" { str := utils.ToStr(time.Now()) str_adds += str[8 : 8+count] } else if coderuledetail_list[i].ItemName == "流水号" { if lastnum == "" { lastnum = strconv.FormatFloat(float64(1)*(1/math.Pow(10, float64(count))), 'f', count, 64)[2:] //生成相应位数的流水号 } else { n, _ := utils.StrTo(lastnum).Int() lastnum = strconv.FormatFloat(float64(n+1)*(1/math.Pow(10, float64(count))), 'f', count, 64)[2:] } str_adds += lastnum } } //使用默认编码规则序列号自增跟新数据库 if isupdate == "true" { svc_coderule.UpdateLastNum(lastnum, utils.ToStr(coderule_list[0].Id)) } this.Data["code_lastnum"] = lastnum } else { } return str_adds, lastnum }