|
package models
|
|
|
|
import (
|
|
"lapp_-wy/db"
|
|
"lapp_-wy/utils"
|
|
"lapp_-wy/web/middleware/glog"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
"xorm.io/core"
|
|
)
|
|
|
|
type CSurveysheetResult struct {
|
|
Cid int `json:"cid" xorm:"not null pk comment('公司ID') INT(11)"`
|
|
Cnr int `json:"cnr" xorm:"not null pk comment('小区编号') INT(11)"`
|
|
Surveysheetid string `json:"surveysheetid" xorm:"not null pk VARCHAR(40)"`
|
|
Surveynr int `json:"surveynr" xorm:"not null pk comment('调研问卷流水号') INT(11)"`
|
|
Surveyperson string `json:"surveyperson" xorm:"default 'NULL' comment('指定调研人') VARCHAR(40)"`
|
|
Surveysamplecount int `json:"surveysamplecount" xorm:"not null comment('调研取样数') INT(11)"`
|
|
Surveyfinishedcount int `json:"surveyfinishedcount" xorm:"not null comment('调研问卷实际已完成数') INT(11)"`
|
|
Buildingid string `json:"buildingid" xorm:"VARCHAR(100)"`
|
|
Unit string `json:"unit" xorm:"VARCHAR(40)"`
|
|
Room string `json:"room" xorm:"VARCHAR(40)"`
|
|
Planstartdate string `json:"planstartdate" xorm:"default 'NULL' VARCHAR(8)"`
|
|
Planendate string `json:"planendate" xorm:"default 'NULL' VARCHAR(8)"`
|
|
Actstartdate string `json:"actstartdate" xorm:"default 'NULL' VARCHAR(8)"`
|
|
Actenddate string `json:"actenddate" xorm:"default 'NULL' VARCHAR(8)"`
|
|
Status int `json:"status" xorm:"not null comment('状态') INT(11)"`
|
|
Remark string `json:"remark" xorm:"default 'NULL' comment('备注') VARCHAR(255)"`
|
|
Signature string `json:"signature" xorm:"default 'NULL' comment('电子签名') VARCHAR(255)"`
|
|
Createtime string `json:"createtime" xorm:"default 'NULL' comment('创建时间') VARCHAR(40)"`
|
|
Lastmodifytime string `json:"lastmodifytime" xorm:"default 'NULL' comment('最近更新时间') VARCHAR(40)"`
|
|
Lastmodifyby string `json:"lastmodifyby" xorm:"default 'NULL' comment('修改人员') VARCHAR(40)"`
|
|
Valst []CSurveysheetResultlst `json:"valst" xorm:"-"`
|
|
}
|
|
|
|
func (t *CSurveysheetResult) TableName() string {
|
|
return "c_surveysheet_result"
|
|
}
|
|
|
|
// 清除string字段的右侧空格
|
|
func (t *CSurveysheetResult) Clipped() {
|
|
utils.TrimStruct(t, *t)
|
|
}
|
|
|
|
//添加
|
|
//增
|
|
func (t *CSurveysheetResult) Add() error {
|
|
engine := db.MasterEngine()
|
|
session := engine.NewSession()
|
|
defer session.Close()
|
|
// add Begin() before any action
|
|
err := session.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
snr := new(Snrtab)
|
|
snr.Cid = t.Cid
|
|
id, err := snr.GetNextSnr("Surveynr")
|
|
if err != nil {
|
|
session.Rollback()
|
|
return err
|
|
}
|
|
//查询信息
|
|
subject := new(CSurveysheet)
|
|
subject.Cid = t.Cid
|
|
subject.Surveysheetid = t.Surveysheetid
|
|
subject.Cnr = t.Cnr
|
|
_, err = engine.Table("c_surveysheet").ID(core.PK{subject.Cid, subject.Cnr, subject.Surveysheetid}).Get(subject)
|
|
if err != nil {
|
|
session.Rollback()
|
|
return err
|
|
}
|
|
//更新调研个数
|
|
CSurveysheet := new(CSurveysheet)
|
|
CSurveysheet.Cid = t.Cid
|
|
CSurveysheet.Surveysheetid = t.Surveysheetid
|
|
CSurveysheet.Cnr = t.Cnr
|
|
CSurveysheet.Surveyfinishedcount = subject.Surveyfinishedcount + 1
|
|
if CSurveysheet.Surveyfinishedcount == subject.Surveysamplecount {
|
|
CSurveysheet.Status = 2
|
|
} else {
|
|
CSurveysheet.Status = 1
|
|
}
|
|
_, err = session.Table("c_surveysheet").Cols("status", "surveyfinishedcount").ID(core.PK{subject.Cid, subject.Cnr, subject.Surveysheetid}).Update(CSurveysheet)
|
|
if err != nil {
|
|
session.Rollback()
|
|
return err
|
|
}
|
|
//添加答题
|
|
Survey := new(CSurveysheetResult)
|
|
Survey.Cid = t.Cid
|
|
Survey.Cnr = t.Cnr
|
|
Survey.Surveysheetid = t.Surveysheetid
|
|
Survey.Buildingid = t.Buildingid
|
|
Survey.Unit = t.Unit
|
|
Survey.Room = t.Room
|
|
Survey.Surveynr = utils.ValueToInt(id, 0)
|
|
Survey.Surveyperson = subject.Surveyperson
|
|
Survey.Surveysamplecount = subject.Surveysamplecount
|
|
Survey.Surveyfinishedcount = subject.Surveyfinishedcount
|
|
Survey.Remark = t.Remark
|
|
Survey.Signature = t.Signature
|
|
Survey.Planstartdate = t.Planstartdate
|
|
Survey.Planendate = t.Planendate
|
|
Survey.Actenddate = utils.TimeFormat(time.Now(), "yyyy-MM-dd HH:mm:ss")
|
|
Survey.Status = t.Status
|
|
Survey.Lastmodifyby = t.Lastmodifyby
|
|
Survey.Createtime = t.Createtime
|
|
Survey.Lastmodifytime = t.Lastmodifytime
|
|
_, err = session.Table("c_surveysheet_result").Insert(Survey)
|
|
if err != nil {
|
|
glog.InfoExt("问题调研", "添加题目 err1 is :", err)
|
|
session.Rollback()
|
|
return err
|
|
}
|
|
|
|
//记录答案
|
|
for _, v := range t.Valst {
|
|
//查询答案分值
|
|
datalist := make([]SubjectOptionlst, 0)
|
|
SelectedOptions := strings.Split(v.SelectedOptions, ",")
|
|
glog.InfoExt("问题调研", "添加问题 SelectedOptionsis :", SelectedOptions)
|
|
err = engine.Table("subject_optionlst").Where("cid = ? and subjectid = ?", t.Cid, v.Subjectid).In("optionid", SelectedOptions).Find(&datalist)
|
|
glog.InfoExt("问题调研", "添加问题 datalist :", datalist)
|
|
for _, vv := range datalist {
|
|
subject := new(CSurveysheetResultlst)
|
|
subject.Cid = t.Cid
|
|
subject.Cnr = t.Cnr
|
|
subject.Subjectid = vv.Subjectid
|
|
subject.Surveynr = utils.ValueToInt(id, 0)
|
|
subject.Surveysheetid = t.Surveysheetid
|
|
subject.Status = v.Status
|
|
subject.SelectedOptions = vv.Optionid
|
|
subject.Assessmentvalue = vv.Optionvalue
|
|
subject.Remark = v.Remark
|
|
subject.Optiontype = vv.Optiontype
|
|
subject.Optioninput = v.Optioninput
|
|
subject.Lastmodifyby = t.Lastmodifyby
|
|
subject.Createtime = t.Createtime
|
|
subject.Lastmodifytime = t.Lastmodifytime
|
|
_, err = session.Table("c_surveysheet_resultlst").Insert(subject)
|
|
}
|
|
|
|
if err != nil {
|
|
glog.InfoExt("问题调研", "添加问题 err2 is :", err)
|
|
session.Rollback()
|
|
return err
|
|
}
|
|
|
|
}
|
|
|
|
err = session.Commit()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Satisfaction struct {
|
|
CSurveysheetSubjectlst `xorm:"extends"`
|
|
CSurveysheetOptionlst `xorm:"extends"`
|
|
}
|
|
|
|
type DataTotal struct {
|
|
SubjectCategoryid string `json:"subject_categoryid"`
|
|
PerTotal float64 `json:"per_total"`
|
|
SonData []SonData `json:"son_data"`
|
|
}
|
|
|
|
//子项
|
|
type SonData struct {
|
|
SubjectCategoryid string `json:"subject_categoryid"`
|
|
SubjectName string `json:"subject_name"`
|
|
Asum int `json:"asum"`
|
|
APer float64 `json:"aper"`
|
|
Bsum int `json:"bsum"`
|
|
BPer float64 `json:"bper"`
|
|
Csum int `json:"csum"`
|
|
CPer float64 `json:"cper"`
|
|
Dsum int `json:"dsum"`
|
|
DPer float64 `json:"dper"`
|
|
Esum int `json:"esum"`
|
|
EPer float64 `json:"eper"`
|
|
AllPer float64 `json:"allper"`
|
|
}
|
|
|
|
type SearchConditions struct {
|
|
AgeSubjectid string `json:"age_subjectid"`
|
|
AgeOption string `json:"age_option"`
|
|
SexSubjectid string `json:"sex_subjectid"`
|
|
SexOption string `json:"sex_option"`
|
|
LifeSubjectid string `json:"life_subjectid"`
|
|
LifeOption string `json:"life_option"`
|
|
}
|
|
|
|
/****
|
|
*思路:查询所有的题目和答案,然后在内存里做处理
|
|
**********/
|
|
//满意度统计(单选题)
|
|
func (t *CSurveysheetResult) SatisfactionTotal(search SearchConditions) ([]DataTotal, error) {
|
|
total := make([]DataTotal, 0)
|
|
temdatas := make([]DataTotal, 0)
|
|
//联查出类型是整体满意度的所有记录
|
|
engine := db.MasterEngine()
|
|
//查询出统计问题及其答案
|
|
data := make([]Satisfaction, 0)
|
|
err := engine.Table("c_surveysheet_subjectlst").Join("INNER", "c_surveysheet_optionlst", "c_surveysheet_subjectlst.cid = c_surveysheet_optionlst.cid and c_surveysheet_subjectlst.subjectid = c_surveysheet_optionlst.subjectid and c_surveysheet_subjectlst.surveysheetid = c_surveysheet_optionlst.surveysheetid").Where("c_surveysheet_subjectlst.cid = ? and c_surveysheet_subjectlst.surveysheetid = ? and c_surveysheet_subjectlst.subjecttype = ? and c_surveysheet_subjectlst.subject_categoryid != ?", t.Cid, t.Surveysheetid, "单选题", "基本信息").Desc("c_surveysheet_subjectlst.subject_categoryid").Find(&data)
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
query := engine.Table("c_surveysheet_resultlst").Where("cid = ? and surveysheetid = ? ", t.Cid, t.Surveysheetid)
|
|
if search.AgeSubjectid != "" && search.AgeOption != "" {
|
|
query = query.And("surveynr in (SELECT surveynr FROM `c_surveysheet_resultlst` WHERE (cid = ? and surveysheetid = ? and subjectid = ? and selected_options = ?))", t.Cid, t.Surveysheetid, search.AgeSubjectid, search.AgeOption)
|
|
}
|
|
if search.SexSubjectid != "" && search.SexOption != "" {
|
|
query = query.And("surveynr in (SELECT surveynr FROM `c_surveysheet_resultlst` WHERE (cid = ? and surveysheetid = ? and subjectid = ? and selected_options = ?))", t.Cid, t.Surveysheetid, search.SexSubjectid, search.SexOption)
|
|
}
|
|
if search.LifeSubjectid != "" && search.LifeOption != "" {
|
|
query = query.And("surveynr in (SELECT surveynr FROM `c_surveysheet_resultlst` WHERE (cid = ? and surveysheetid = ? and subjectid = ? and selected_options = ?))", t.Cid, t.Surveysheetid, search.LifeSubjectid, search.LifeOption)
|
|
}
|
|
results := make([]CSurveysheetResultlst, 0)
|
|
err = query.Find(&results)
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
dataMap := make(map[string]int)
|
|
//统计出个数
|
|
for _, v := range results {
|
|
key := v.Subjectid + "_" + v.SelectedOptions
|
|
val, ok := dataMap[key]
|
|
if ok {
|
|
val = val + 1
|
|
dataMap[key] = val
|
|
} else {
|
|
val = 1
|
|
dataMap[key] = val
|
|
}
|
|
}
|
|
result := make([]SonData, 0)
|
|
tem := make(map[string][]Satisfaction)
|
|
//计算题目和答案对应的百分比
|
|
for _, v := range data {
|
|
key := v.CSurveysheetSubjectlst.Subjectid
|
|
tem[key] = append(tem[key], v)
|
|
}
|
|
for _, v := range tem {
|
|
one := SonData{}
|
|
all := 0
|
|
for _, vv := range v {
|
|
key := vv.CSurveysheetSubjectlst.Subjectid + "_" + vv.Optionid
|
|
one.SubjectCategoryid = vv.CSurveysheetSubjectlst.SubjectCategoryid
|
|
one.SubjectName = vv.CSurveysheetSubjectlst.Subjecttext
|
|
switch vv.Optionid {
|
|
case "A":
|
|
val, ok := dataMap[key]
|
|
if ok {
|
|
one.Asum = val
|
|
}
|
|
all += val
|
|
case "B":
|
|
val, ok := dataMap[key]
|
|
if ok {
|
|
one.Bsum = val
|
|
}
|
|
all += val
|
|
case "C":
|
|
val, ok := dataMap[key]
|
|
if ok {
|
|
one.Csum = val
|
|
}
|
|
all += val
|
|
case "D":
|
|
val, ok := dataMap[key]
|
|
if ok {
|
|
one.Dsum = val
|
|
}
|
|
all += val
|
|
case "E":
|
|
val, ok := dataMap[key]
|
|
if ok {
|
|
one.Esum = val
|
|
}
|
|
all += val
|
|
}
|
|
}
|
|
|
|
//满意度平均分 = (a×5+b×4+c×3+d×2+e×1) ÷ (a+b+c+d+e)
|
|
//满意度 = (满意度平均分÷5 )× 100%
|
|
if all > 0 {
|
|
one.APer = utils.ValueToFloat(one.Asum, 0.0) / utils.ValueToFloat(all, 0.0)
|
|
one.BPer = utils.ValueToFloat(one.Bsum, 0.0) / utils.ValueToFloat(all, 0.0)
|
|
one.CPer = utils.ValueToFloat(one.Csum, 0.0) / utils.ValueToFloat(all, 0.0)
|
|
one.DPer = utils.ValueToFloat(one.Dsum, 0.0) / utils.ValueToFloat(all, 0.0)
|
|
one.EPer = utils.ValueToFloat(one.Esum, 0.0) / utils.ValueToFloat(all, 0.0)
|
|
AllPer := (one.Asum*5 + one.Bsum*4 + one.Csum*3 + one.Dsum*2 + one.Esum*1)
|
|
one.AllPer = utils.ValueToFloat(AllPer, 0.0) / utils.ValueToFloat(all, 0.0) / utils.ValueToFloat(5, 0.0)
|
|
} else {
|
|
one.APer = 0
|
|
one.BPer = 0
|
|
one.CPer = 0
|
|
one.DPer = 0
|
|
one.EPer = 0
|
|
one.AllPer = 0
|
|
}
|
|
result = append(result, one)
|
|
}
|
|
temdata := make(map[string][]SonData)
|
|
//计算题目和答案对应的百分比
|
|
for _, v := range result {
|
|
key := v.SubjectCategoryid
|
|
temdata[key] = append(temdata[key], v)
|
|
}
|
|
//排序
|
|
Categoryids := []string{}
|
|
for k, v := range temdata {
|
|
one := DataTotal{}
|
|
one.SubjectCategoryid = k
|
|
l := 0
|
|
s := 0.0
|
|
for _, vv := range v {
|
|
l++
|
|
s += vv.AllPer
|
|
one.SonData = append(one.SonData, vv)
|
|
}
|
|
if l > 0 {
|
|
one.PerTotal = utils.ValueToFloat(s, 0.0) / utils.ValueToFloat(l, 0.0)
|
|
} else {
|
|
one.PerTotal = 0
|
|
}
|
|
total = append(total, one)
|
|
}
|
|
|
|
temdatastr := make(map[string]DataTotal)
|
|
//计算题目和答案对应的百分比
|
|
for _, v := range total {
|
|
key := v.SubjectCategoryid
|
|
Categoryids = append(Categoryids, key)
|
|
temdatastr[key] = v
|
|
|
|
}
|
|
//排序
|
|
sort.Strings(Categoryids)
|
|
for _, key := range Categoryids {
|
|
temdatas = append(temdatas, temdatastr[key])
|
|
}
|
|
return temdatas, nil
|
|
}
|
|
|
|
//多选题
|
|
type SubjectsData struct {
|
|
SubjectName string `json:"subject_name"`
|
|
SubjectCount int `json:"subject_count"`
|
|
MultipleData []MultipleData `json:"multiple_data"`
|
|
}
|
|
|
|
type MultipleData struct {
|
|
SubjectName string `json:"subject_name"`
|
|
OptionName string `json:"option_name"`
|
|
Sum int `json:"sum"`
|
|
Per float64 `json:"per"`
|
|
}
|
|
|
|
//满意度统计(多选题)
|
|
func (t *CSurveysheetResult) SatisfactionMultipleTotal(search SearchConditions) ([]SubjectsData, error) {
|
|
total := make([]SubjectsData, 0)
|
|
engine := db.MasterEngine()
|
|
//联查出类型是整体满意度的所有记录
|
|
data := make([]Satisfaction, 0)
|
|
err := engine.Table("c_surveysheet_subjectlst").Join("INNER", "c_surveysheet_optionlst", "c_surveysheet_subjectlst.cid = c_surveysheet_optionlst.cid and c_surveysheet_subjectlst.subjectid = c_surveysheet_optionlst.subjectid and c_surveysheet_subjectlst.surveysheetid = c_surveysheet_optionlst.surveysheetid").Where("c_surveysheet_subjectlst.cid = ? and c_surveysheet_subjectlst.surveysheetid = ? and c_surveysheet_subjectlst.subjecttype = ?", t.Cid, t.Surveysheetid, "多选题").Find(&data)
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
//查询出统计问题及其答案
|
|
query := engine.Table("c_surveysheet_resultlst").Where("cid = ? and surveysheetid = ? ", t.Cid, t.Surveysheetid)
|
|
if search.AgeSubjectid != "" && search.AgeOption != "" {
|
|
query = query.And("surveynr in (SELECT surveynr FROM `c_surveysheet_resultlst` WHERE (cid = ? and surveysheetid = ? and subjectid = ? and selected_options = ?))", t.Cid, t.Surveysheetid, search.AgeSubjectid, search.AgeOption)
|
|
}
|
|
if search.SexSubjectid != "" && search.SexOption != "" {
|
|
query = query.And("surveynr in (SELECT surveynr FROM `c_surveysheet_resultlst` WHERE (cid = ? and surveysheetid = ? and subjectid = ? and selected_options = ?))", t.Cid, t.Surveysheetid, search.SexSubjectid, search.SexOption)
|
|
}
|
|
if search.LifeSubjectid != "" && search.LifeOption != "" {
|
|
query = query.And("surveynr in (SELECT surveynr FROM `c_surveysheet_resultlst` WHERE (cid = ? and surveysheetid = ? and subjectid = ? and selected_options = ?))", t.Cid, t.Surveysheetid, search.LifeSubjectid, search.LifeOption)
|
|
}
|
|
results := make([]CSurveysheetResultlst, 0)
|
|
err = query.Find(&results)
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
dataMap := make(map[string]int)
|
|
//统计出个数
|
|
for _, v := range results {
|
|
key := v.Subjectid + "_" + v.SelectedOptions
|
|
val, ok := dataMap[key]
|
|
if ok {
|
|
val = val + 1
|
|
dataMap[key] = val
|
|
} else {
|
|
val = 1
|
|
dataMap[key] = val
|
|
}
|
|
}
|
|
Categoryids := []string{}
|
|
temdatas := make(map[string][]Satisfaction)
|
|
tem := make(map[string][]Satisfaction)
|
|
//计算题目和答案对应的百分比
|
|
for _, v := range data {
|
|
key := v.CSurveysheetSubjectlst.Subjecttext
|
|
temdatas[key] = append(temdatas[key], v)
|
|
Categoryids = append(Categoryids, key)
|
|
}
|
|
//排序
|
|
sort.Strings(Categoryids)
|
|
for _, key := range Categoryids {
|
|
tem[key] = temdatas[key]
|
|
}
|
|
for k, v := range tem {
|
|
result := make([]MultipleData, 0)
|
|
subject := SubjectsData{}
|
|
subject.SubjectName = k
|
|
all := 0
|
|
for _, vv := range v {
|
|
one := MultipleData{}
|
|
key := vv.CSurveysheetSubjectlst.Subjectid + "_" + vv.Optionid
|
|
one.SubjectName = vv.CSurveysheetSubjectlst.Subjecttext
|
|
one.OptionName = vv.CSurveysheetOptionlst.Optiontext
|
|
val, ok := dataMap[key]
|
|
if ok {
|
|
one.Sum = val
|
|
all += val
|
|
}
|
|
result = append(result, one)
|
|
}
|
|
subject.SubjectCount = all
|
|
subject.MultipleData = result
|
|
total = append(total, subject)
|
|
}
|
|
//计算题目和答案对应的百分比
|
|
for k, _ := range total {
|
|
for kk, _ := range total[k].MultipleData {
|
|
total[k].MultipleData[kk].Per = utils.ValueToFloat(total[k].MultipleData[kk].Sum, 0.0) / utils.ValueToFloat(total[k].SubjectCount, 0.0)
|
|
}
|
|
}
|
|
return total, nil
|
|
}
|