check.go
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package protocol
import (
"bytes"
"encoding/json"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"opp/internal/utils"
"strings"
)
/*机会-自查内容*/
var CheckOptionsCommit = []CheckOption{{Item: "是", NeedOther: false}, {Item: "否", NeedOther: false}, {Item: "不清楚", NeedOther: false}}
//自查结果列表
type SelfCheckResults []selfCheckResult
type selfCheckResult struct {
Item string `json:"item"`
Total int `json:"total"`
}
//自查项列表
func NewSelfChecks(data string) (rsp SelfChecks) {
if len(data) == 0 {
return
}
e := json.Unmarshal([]byte(data), &rsp)
if e != nil {
log.Error(e)
}
return
}
type SelfChecks []SelfCheck
type SelfCheck struct {
CheckItem string `json:"checkItem"`
GroupId int64 `json:"groupId"` //分组
Answer string `json:"answer,omitempty"`
Reason string `json:"reason,omitempty"`
}
func (c SelfCheck) Key() string {
return fmt.Sprintf("%v-%v", c.GroupId, c.CheckItem)
}
//统计自查结果
func (s SelfChecks) Static() SelfCheckResults {
if len(s) == 0 {
return []selfCheckResult{}
}
results := []selfCheckResult{{Item: "是"}, {Item: "否"}, {Item: "不清楚"}}
for i := range s {
check := (s)[i]
for k := range results {
if strings.EqualFold(results[k].Item, check.Answer) {
results[k].Total = results[k].Total + 1
break
}
}
}
return SelfCheckResults(results)
}
//自查校验
func (s SelfChecks) Valid() (err error) {
if len(s) == 0 {
return
}
for i := range s {
c := s[i]
if c.GroupId == 0 {
err = NewErrWithMessage(2)
return
}
if len(c.CheckItem) == 0 {
err = NewErrWithMessage(2)
return
}
if len(c.Answer) == 0 {
err = NewCustomMessage(2, "自查项未填写")
return
}
}
return
}
//自查内容
func (s SelfChecks) String() string {
var buf bytes.Buffer
for i := range s {
c := s[i]
if len(c.Reason) == 0 {
buf.WriteString(fmt.Sprintf("\n%v:%v;", c.CheckItem, c.Answer))
} else {
buf.WriteString(fmt.Sprintf("\n%v:%v,理由:%v;", c.CheckItem, c.Answer, c.Reason))
}
}
return buf.String()
}
func (s SelfChecks) Compare(dst string) (rspChecks SelfChecks, err error) {
var (
dstChecks SelfChecks
mapChecks = make(map[string]SelfCheck)
)
rspChecks = make([]SelfCheck, 0)
if len(s) == 0 {
return
}
utils.JsonUnmarshal(dst, &dstChecks)
if len(s) != len(dstChecks) {
err = NewCustomMessage(1, "自查项有误")
log.Error(err, s, dstChecks)
return
}
for i := range dstChecks {
c := dstChecks[i]
mapChecks[c.Key()] = c
}
for i := range s {
c := s[i]
if v, ok := mapChecks[c.Key()]; ok {
//回答不一直
if !strings.EqualFold(c.Answer, v.Answer) {
rspChecks = append(rspChecks, c)
continue
}
if len(c.Reason) > 0 {
rspChecks = append(rspChecks, c)
continue
}
}
}
return
}
//自查问题
func NewCheckQuestion(checkItem, title string, groupId int64, ops []CheckOption) *CheckQuestion {
return &CheckQuestion{
CheckItem: checkItem,
Title: title,
GroupId: groupId,
CheckOptions: ops,
}
}
type CheckQuestion struct {
CheckItem string `json:"checkItem"`
Title string `json:"title"`
GroupId int64 `json:"groupId"`
CheckOptions []CheckOption `json:"options"`
}
type CheckOption struct {
Item string `json:"item"`
NeedOther bool `json:"-"` //是否需填写其他的内容【1:需要】【2:不需要】
}
/*CheckQuestions 自查问题列表*/
type CheckQuestionsRequest struct {
ChanceId int64 `json:"chanceId" valid:"Required"`
}
type CheckQuestionsResponse struct {
Questions []*CheckQuestion `json:"questions"`
}