ludc
2023-12-05 b40cd8c94978d43f9d13ba9ee93a4164b68a8165
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.vci.ubcs.code.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
 
import com.vci.ubcs.code.dto.CodeButtonDTO;
import com.vci.ubcs.code.dto.CodeFixedValueDTO;
import com.vci.ubcs.code.entity.CodeButton;
import com.vci.ubcs.code.entity.CodeFixedValue;
import com.vci.ubcs.code.enumpack.CodeUseButtonPositionTypeEnum;
import com.vci.ubcs.code.enumpack.FrameworkDataLCStatus;
import com.vci.ubcs.code.mapper.CodeButtonMapper;
import com.vci.ubcs.code.service.ICodeButtonService;
import com.vci.ubcs.code.vo.pagemodel.CodeButtonVO;
import com.vci.ubcs.common.utils.PageDO2PageVO;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.revision.service.RevisionModelUtil;
import com.vci.ubcs.starter.util.DefaultAttrAssimtUtil;
import com.vci.ubcs.starter.util.MdmBtmTypeConstant;
import com.vci.ubcs.starter.web.pagemodel.DataGrid;
import com.vci.ubcs.starter.web.pagemodel.PageHelper;
import com.vci.ubcs.starter.web.util.BeanUtil;
import com.vci.ubcs.starter.web.util.BeanUtilForVCI;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.*;
 
import static com.vci.ubcs.code.constant.FrameWorkLangCodeConstant.*;
 
/**
 * 模板扩展池 服务实现类
 *
 * @author yuxc
 * @since 2023-03-29
 */
@Service
public class CodeButtonServiceImpl implements ICodeButtonService {
 
    @Resource
    private CodeButtonMapper codeButtonMapper;
 
    /**
     * 对象的操作
     */
    @Autowired
    private RevisionModelUtil revisionModelUtil;
 
    @Override
    public IPage<CodeButtonVO> selectcodebuttonPage(IPage<CodeButtonVO> page, CodeButtonVO codebutton) {
        return page.setRecords(codeButtonMapper.selectcodebuttonPage(page, codebutton));
    }
 
    /**
     * 查询所有的主数据中的按钮扩展
     * @param conditionMap 查询条件
     * @param pageHelper 分页和排序
     * @return 执行结果
     * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
     */
    @Override
    public IPage<CodeButtonVO> gridCodeButton(Map<String, String> conditionMap, PageHelper pageHelper) throws VciBaseException{
        Query query = new Query();
        if (pageHelper == null) {
            query.setSize(-1);
        }else {
            query.setSize(pageHelper.getLimit());
            query.setCurrent(pageHelper.getPage());
        }
        query.setDescs("createTime");
        CodeButton codeButton = new CodeButton();
        BeanMap beanMap = BeanMap.create(codeButton);
        beanMap.putAll(conditionMap);
        IPage<CodeButton> doList = codeButtonMapper.selectPage(Condition.getPage(query),Condition.getQueryWrapper(codeButton));
        IPage<CodeButtonVO> voList = new Page<>();
        if (!CollectionUtils.isEmpty(doList.getRecords())) {
            voList.setRecords(codeButtonDO2VOs(doList.getRecords()));
            PageDO2PageVO.pageDO2PageVO(doList,voList);
        }
        return voList;
    }
 
    /**
     * 增加主数据中的按钮扩展
     * @param codeButtonDTO 主数据中的按钮扩展数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public CodeButtonVO addSave(CodeButtonDTO codeButtonDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(codeButtonDTO,"需要添加的数据对象");
        //将DTO转换为DO
        CodeButton codeButtonDO = new CodeButton();
        BeanUtilForVCI.copyPropertiesIgnoreCase(codeButtonDTO,codeButtonDO);
        DefaultAttrAssimtUtil.addDefaultAttrAssimt(codeButtonDO, MdmBtmTypeConstant.CODE_BUTTON,null);
        boolean resInsertCodeButton = codeButtonMapper.insert(codeButtonDO) > 0;
        return resInsertCodeButton ? codeButtonDO2VO(codeButtonDO):null;
    }
 
    /**
     * 修改主数据中的按钮扩展
     * @param codeButtonDTO 主数据中的按钮扩展数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public CodeButtonVO editSave(CodeButtonDTO codeButtonDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(codeButtonDTO,"数据对象",codeButtonDTO.getOid(),"主数据中的按钮扩展主键");
        //将DTO转换为DO
        CodeButton codeButtonDO = selectByOid(codeButtonDTO.getOid());
        revisionModelUtil.copyFromDTOIgnore(codeButtonDTO,codeButtonDO);
        DefaultAttrAssimtUtil.updateDefaultAttrAssimt(codeButtonDO,null);
        boolean resUpdateCodeButton = codeButtonMapper.updateById(codeButtonDO) > 0;
        return resUpdateCodeButton ? codeButtonDO2VO(codeButtonDO):null;
    }
 
    /**
     * 删除主数据中的按钮扩展
     * @param codeButtonDTO 主数据中的按钮扩展数据传输对象,oid和ts需要传输
     * @return 删除结果反馈::success:成功,fail:失败
     * @throws VciBaseException 参数为空,被引用时抛出异常
     */
    @Override
    public R deleteCodeButton(CodeButtonDTO codeButtonDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(codeButtonDTO,"主数据中的按钮扩展数据对象",codeButtonDTO.getOid(),"主数据中的按钮扩展的主键");
        CodeButton codeButtonDO = selectByOid(codeButtonDTO.getOid());
        R baseResult = checkIsCanDeleteForDO(codeButtonDTO,codeButtonDO);
        if(baseResult.isSuccess()) {
        }else{
            return baseResult;
        }
        //执行删除操作
        boolean resDeleteCodeButton = codeButtonMapper.deleteById(codeButtonDO.getOid()) > 0;
        return resDeleteCodeButton ? R.success(DELETE_SUCCESS):R.fail(DELETE_FAIL);
    }
 
    /**
     * 主键获取主数据中的按钮扩展
     * @param oid 主键
     * @return 主数据中的按钮扩展显示对象
     * @throws VciBaseException 参数为空,数据不存在时会抛出异常
     */
    @Override
    public CodeButtonVO getObjectByOid(String oid) throws VciBaseException{
        return codeButtonDO2VO(selectByOid(oid));
    }
 
    /**
     * 参照主数据中的按钮扩展列表
     * @param conditionMap 查询条件
     * @param pageHelper 分页和排序
     * @return 主数据中的按钮扩展显示对象列表,生效的内容
     * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
     */
    @Override
    public IPage<CodeButtonVO> refDataGridCodeButton(Map<String, String> conditionMap, PageHelper pageHelper) throws VciBaseException{
        if(conditionMap == null){
            conditionMap = new HashMap<String, String>();
        }
        return gridCodeButton(conditionMap,pageHelper);
    }
 
    /**
     * 校验是否可以删除,如果存在下级,并且下级有数据引用则不能删除
     * @param codeButtonDTO 数据传输对象
     * @param codeButtonDO 数据库中的数据对象
     * @return success为true为可以删除,false表示有数据引用,obj为true表示有下级
     */
    private R checkIsCanDeleteForDO(CodeButtonDTO codeButtonDTO, CodeButton codeButtonDO) {
        CodeButton buttonDO = new CodeButton();
        BeanUtil.convert(codeButtonDTO,buttonDO);
        if (!checkTs(codeButtonDTO,buttonDO)) {
            return R.fail(TS_NOT_PROCESS);
        }
        if(!checkIsLinked(codeButtonDO.getOid())) {
            return R.success("success");
        }else{
            return R.fail(DATA_LINKED_NOT_DELETE);
        }
    }
 
    /**
     * 检查ts
     * @param tempDO
     * @return
     */
    private boolean checkTs(CodeButtonDTO tempDO, CodeButton codeButton){
        Date dbTs = codeButton.getTs();
        Date currentTs = tempDO.getTs();
        if(currentTs == null ?  dbTs == null:currentTs.compareTo(dbTs)==0){
            return true;
        }
        return false;
    }
 
    /**
     * 校验是否被引用
     * @param oid 主键
     * @throws VciBaseException 被引用的时候会抛出异常
     */
    private boolean checkIsLinked(String oid) throws VciBaseException{
        //TODO 添加需要校验引用的地方
        return false;
    }
 
    /**
     * 主键查询数据对象
     * @param oid 主键
     * @return  数据对象
     * @throws VciBaseException 参数为空,并且数据不存在的时候会抛出异常
     */
    private CodeButton selectByOid(String oid) throws VciBaseException{
        VciBaseUtil.alertNotNull(oid,"主键");
        CodeButton codeButtonDO = codeButtonMapper.selectById(oid.trim());
        if(codeButtonDO == null || StringUtils.isBlank(codeButtonDO.getOid())){
            throw new VciBaseException(DATA_OID_NOT_EXIST);
        }
        return codeButtonDO;
    }
 
    /**
     * 主键批量获取主数据中的按钮扩展
     * @param oidCollections 主键集合,但是受性能影响,建议一次查询不超过10000个
     * @return 主数据中的按钮扩展显示对象
     * @throws VciBaseException 查询出现异常时会抛出
     */
    @Override
    public Collection<CodeButtonVO> listCodeButtonByOids(Collection<String> oidCollections) throws VciBaseException {
        VciBaseUtil.alertNotNull(oidCollections,"数据对象主键集合");
        List<CodeButton> codeButtonDOList = listCodeButtonDOByOidCollections(oidCollections);
        return codeButtonDO2VOs(codeButtonDOList);
    }
 
    /**
     * 批量数据对象转换为显示对象
     * @param codeButtonDOs 数据对象列表
     * @return 显示对象
     * @throws VciBaseException 参数为空或者不存在的时候会抛出异常
     */
    @Override
    public List<CodeButtonVO> codeButtonDO2VOs(Collection<CodeButton> codeButtonDOs) throws VciBaseException{
        List<CodeButtonVO> voList = new ArrayList<CodeButtonVO>();
        if(!CollectionUtils.isEmpty(codeButtonDOs)){
            for(CodeButton s: codeButtonDOs){
                CodeButtonVO vo =  codeButtonDO2VO(s);
                if(vo != null){
                    voList.add(vo);
                }
            }
        }
        return voList;
    }
 
    /**
     * 数据对象转换为显示对象
     * @param  codeButtonDO 数据对象
     * @return 显示对象
     * @throws VciBaseException 拷贝属性出错的时候会抛出异常
     */
    @Override
    public  CodeButtonVO codeButtonDO2VO(CodeButton codeButtonDO) throws VciBaseException{
        CodeButtonVO vo = new CodeButtonVO();
        if(codeButtonDO != null){
            BeanUtilForVCI.copyPropertiesIgnoreCase(codeButtonDO,vo);
            //如果有lcstatus的类的话
            vo.setLcStatusText(FrameworkDataLCStatus.getTextByValue(vo.getLcStatus()));
            vo.setUsedpositiontypeText(CodeUseButtonPositionTypeEnum.getTextByValue(codeButtonDO.getUsedPositionType()));
        }
        return vo;
    }
 
    @Override
    public List<CodeButton> selectByPrimaryKeyCollection(Collection<String> oids) {
        VciBaseUtil.alertNotNull(oids,"主键集合");
        return codeButtonMapper.selectBatchIds(oids);
    }
 
    /**
     * 使用主键集合查询数据对象
     * @param oidCollections 主键的集合
     * @return 数据对象列表
     */
    private List<CodeButton> listCodeButtonDOByOidCollections(Collection<String> oidCollections){
        List<CodeButton> codeButtonDOList = new ArrayList<CodeButton>();
        if(!CollectionUtils.isEmpty(oidCollections)){
            Collection<Collection<String>> oidCollectionsList = VciBaseUtil.switchCollectionForOracleIn(oidCollections);
            for(Collection<String> oids: oidCollectionsList){
                List<CodeButton> tempDOList =  codeButtonMapper.selectBatchIds(oids);
                if(!CollectionUtils.isEmpty(tempDOList)){
                    codeButtonDOList.addAll(tempDOList);
                }
            }
        }
        return  codeButtonDOList;
    }
 
    @Override
    public R enableCodeButton(String id) {
        CodeButton codebutton = codeButtonMapper.selectById(id);
        return changeLcStatus(codebutton,true);
    }
 
    /**
     * 停用
     *
     * @param oid 数据传输对象
     * @return 执行结果
     */
    @Override
    public R disableOrgDuty(String oid) {
        CodeButton codebutton = codeButtonMapper.selectById(oid);
        return changeLcStatus(codebutton,false);
    }
 
    /**
     * 修改生命周期的状态,如停用和启用
     * @param buttonDTO 数据传输对象,必须要有oid和ts
     * @param disable 是否为停用
     * @return 执行的结果
     */
    private R changeLcStatus(CodeButton buttonDTO, boolean disable){
        // VciBaseUtil.alertNotNull(buttonDTO,"数据对象",buttonDTO.getOid(),"主键");
        if(disable){
            buttonDTO.setLcStatus(FrameworkDataLCStatus.ENABLED.getValue());
        }else{
            buttonDTO.setLcStatus(FrameworkDataLCStatus.DISABLED.getValue());
        }
        return SqlHelper.retBool(codeButtonMapper.updateById(buttonDTO))?
            R.success(FrameworkDataLCStatus.ENABLED.getValue()):R.fail(FrameworkDataLCStatus.DISABLED.getValue());
    }
 
    //    /**
//     * 校验是否可以删除,如果存在下级,并且下级有数据引用则不能删除
//     * @param codeButtonDTO 数据传输对象
//     * @param codeButtonDO 数据库中的数据对象
//     * @return success为true为可以删除,false表示有数据引用,obj为true表示有下级
//     */
//    private R checkIsCanDeleteForDO(CodeButtonEntity codeButtonDTO, CodeButtonVO codeButtonDO) {
//        CodeButtonVO buttonDO = new CodeButtonVO();
//        BeanUtil.convert(codeButtonDTO,buttonDO);
//        boService.checkTs(buttonDO);
//        if(!checkIsLinked(codeButtonDO.getOid())) {
//            return BaseResult.success();
//        }else{
//            return BaseResult.fail(DATA_LINKED_NOT_DELETE,new String[]{""});
//        }
//    }
 
}