zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package cn.autoform.web.service.templatecategoryedit;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.github.pagehelper.Page;
 
import cn.autoform.bean.TemplateCategoryOutput;
import cn.autoform.bean.TemplateManagerOutput;
import cn.autoform.bean.page.PageResult;
import cn.autoform.db.entity.TemplateCategoryEntity;
import cn.autoform.db.entity.TemplateManagerEntity;
import cn.autoform.web.mapper.templatecategoryedit.TemplateCategoryEditMapper;
 
/**
 * 模板分类编辑Service
 * @author lt-zwy
 *
 */
@Service
public class TemplateCategoryEditService {
    
    @Autowired
    private TemplateCategoryEditMapper templateCategoryEditMapper = null;
 
    public int updateTempFormName(TemplateManagerEntity templateManagerEntity){
        return templateCategoryEditMapper.updateTempFormName(templateManagerEntity);
    }
    
    /**
     * 根据参数查询模板分类信息
     * @param tenantID
     * @param businessCategoryID
     * @param templateCategoryID
     * @param pageNum
     * @return
     */
    public List<PageResult<TemplateCategoryOutput>> queryTemplateList(String tenantID, String businessCategoryID, String selectFor,int pageNum,int pageSize,String selectName) {
        //分页查询开始的条数
        int pageStrat = 0;
        if(pageNum!=0){
            pageStrat=(pageNum-1)*pageSize;
        }
        // 根据租户id和业务分类id查询模板分类
        List<TemplateCategoryOutput> temCateList = templateCategoryEditMapper.queryTempCategory(tenantID, businessCategoryID,pageStrat,pageSize,selectName);
        int count = templateCategoryEditMapper.getTempLatePageCount(tenantID, businessCategoryID,selectName);
        System.out.println("+++++++++"+temCateList.size());
        if(!temCateList.isEmpty()){
            //当模板分类只有一条时,不可删除
            if(temCateList.size()==1){
                temCateList.get(0).setCanDeleteFlag(true);
            }
            // 设置第一个模板分类【isOpened打开】
            temCateList.get(0).setOpenedFlag(true);
            // 再根据租户id,以及返回的业务分类id和模板分类id查询模板信息 
            for(int i=0; i<temCateList.size(); i++) {
                List<TemplateManagerOutput> temManaList = templateCategoryEditMapper.queryTempManager(tenantID, 
                    temCateList.get(i).getBusinessCategoryID(), 
                    temCateList.get(i).getTemplateCategoryID(), 
                    selectFor);
                if(!temManaList.isEmpty()){
                    // 设置查询使用数量,templateCategoryName
                    for(int j=0; j<temManaList.size(); j++) {
                        int templateUseSum = templateCategoryEditMapper.queryTemplateUsedSum(tenantID, temManaList.get(j).getTemplateID());
                        temManaList.get(j).setTemplateUseSum(templateUseSum);
                        temManaList.get(j).setTemplateCategoryName(temCateList.get(i).getTemplateCategoryName());
                        //CheckBox显示模板是否启用            
                        if("1".equals(temManaList.get(j).getEnablesetting())){
                            temManaList.get(j).setUsableFlag(true);
                        }else{
                            temManaList.get(j).setUsableFlag(false);
                        }
                    }
                    temCateList.get(i).setTemplateManagerOutput(temManaList);
                }
            }
            if(temCateList.get(0).getTemplateManagerOutput() != null){
                // 第一个分类下的第一个模板信息【selected显示】
                temCateList.get(0).getTemplateManagerOutput().get(0).setSelected(true);
            }
        }
        PageResult<TemplateCategoryOutput> pageResult=new PageResult<TemplateCategoryOutput>();
        pageResult.setList(temCateList);
        pageResult.setCount(Long.valueOf(count));
        List<PageResult<TemplateCategoryOutput>> list =new ArrayList<PageResult<TemplateCategoryOutput>>();
        list.add(pageResult);
        return list;
    }
    /**
     * 新增模板分类
     * @param templateCategoryEntity
     * @return
     */
    public int createTemplateCategoryInfo(TemplateCategoryEntity templateCategoryEntity) {
        int sameCount = templateCategoryEditMapper.categoryNameCheck(templateCategoryEntity);
        if(sameCount == 0){
            return templateCategoryEditMapper.createTemplateCategory(templateCategoryEntity);
        }else if(sameCount != 0){
            return 2;
        }else{
            return 0;
        }
    }
 
    /**
     * 编辑模板分类
     * @param templateCategoryEntity
     * @return
     */
    public int updateTemlateCategoryInfo(TemplateCategoryEntity templateCategoryEntity) {
        int sameCount = templateCategoryEditMapper.categoryNameCheck(templateCategoryEntity);
        if(sameCount == 0){
            return templateCategoryEditMapper.saveTemplateCategory(templateCategoryEntity);
        }else if(sameCount != 0){
            return 2;
        }else{
            return 0;
        }
    }
 
    /**
     * 新增模板
     * @param templateManagerEntity
     * @return
     */
    public int createTemplateInfo(TemplateManagerOutput templateManagerOutput) {
        int sameCount = templateCategoryEditMapper.templateNameCheck(templateManagerOutput);
        if(sameCount == 0){
            //新增模板管理表
            int templateReturn = templateCategoryEditMapper.createTemplate(templateManagerOutput);
            if(templateReturn == 1){
                return 1;
            }else{
                return 0;
            }
        }else{
            return 2;//名称重复
        }
    }
 
    /**
     * 编辑模板
     * @param templateManagerEntity
     * @return
     */
    public int updateTemplateInfo(TemplateManagerOutput templateManagerOutput) {
        int sameCount = templateCategoryEditMapper.templateNameCheck(templateManagerOutput);
        if(sameCount == 0){
            //设置模板是否启用
            if(templateManagerOutput.isUsableFlag()){
                templateManagerOutput.setEnablesetting("1");
            }else{
                templateManagerOutput.setEnablesetting("0");
            }
            return templateCategoryEditMapper.saveTemplate(templateManagerOutput);
        }else{
            return 2;
        }
    }
 
    /**
     * 删除模板分类及下属模板
     * @param templateCategoryOutput
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteTemplateCategory(TemplateCategoryOutput templateCategoryOutput) {
        
        //删除模板分类标签
        int categoryDelete = templateCategoryEditMapper.deleteCategory(templateCategoryOutput);
        int templateDelete = 0;
        if(templateCategoryOutput.getTemplateManagerOutput() != null){
            //删除分类下的模板    
            List<TemplateManagerOutput> tempList = templateCategoryOutput.getTemplateManagerOutput();
            for(int i=0; i<tempList.size(); i++){
                templateDelete = templateCategoryEditMapper.deleteTemplate(tempList.get(i));
            }
            if(categoryDelete == 1 && templateDelete == 1){
                return 1;
            }else{
                return 0;
            }
        }else{
            if(categoryDelete == 1){
                return 1;
            }else{
                return 0;
            }
        }
    }
    
    /**
     * 删除分类模板标签下的模板
     * @param templateManagerOutput
     * return 
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteTemplate(TemplateManagerOutput templateManagerOutput){
        int templateDeleteReturnNum = templateCategoryEditMapper.deleteTemplate(templateManagerOutput);
        if(templateDeleteReturnNum == 1){
            return 1;
        }else{
            return 0;
        }
    }
 
    /**
     * 验证当前模板是否已启用,已经停用的模板不可以创建表单
     * @param tenantID
     * @param templateID
     * @return
     */
    public int checkTemplateEnable(String tenantID, String templateID) {
        String enableSeting = templateCategoryEditMapper.checkTemplateEnable(tenantID, templateID);
        if("1".equals(enableSeting)){
            return 1;
        }else{
            return 0;
        }
    }
}