ludc
2023-05-05 4cd535de8ef099afa96238e5458e6866edccfea7
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/CodeButtonServiceImpl.java
@@ -1,324 +1,204 @@
/*
 *      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.vci.ubcs.code.dao.CodeButtonDaoI;
import com.vci.ubcs.code.model.CodeButtonDO;
import com.vci.ubcs.code.service.CodeButtonServiceI;
import com.vci.frameworkcore.lcstatuspck.FrameworkDataLCStatus;
import com.vci.starter.revision.service.RevisionModelUtil;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.pagemodel.DataGrid;
import com.vci.starter.web.pagemodel.PageHelper;
import com.vci.starter.web.util.BeanUtil;
import com.vci.starter.web.util.BeanUtilForVCI;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.web.pageModel.BatchCBO;
import com.vci.web.service.WebBoServiceI;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vci.ubcs.code.dto.CodeButtonDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.vci.ubcs.code.entity.CodeButton;
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.starter.exception.VciBaseException;
import com.vci.ubcs.starter.web.util.BeanUtilForVCI;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.*;
import static com.vci.frameworkcore.constant.FrameWorkLangCodeConstant.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
 * 主数据中的按钮扩展服务
 * @author weidy
 * @date 2022-01-24
 * 模板扩展池 服务实现类
 *
 * @author yuxc
 * @since 2023-03-29
 */
@Service
public class CodeButtonServiceImpl implements CodeButtonServiceI {
public class CodeButtonServiceImpl implements ICodeButtonService {
    /**
    * 日志
    */
    private Logger logger = LoggerFactory.getLogger(getClass());
   @Resource
   CodeButtonMapper codeButtonMapper;
    /**
    * 数据操作层
    */
    @Resource
    private CodeButtonDaoI codeButtonMapper;
   @Override
   public IPage<CodeButtonVO> selectcodebuttonPage(IPage<CodeButtonVO> page, CodeButtonVO codebutton) {
      return page.setRecords(codeButtonMapper.selectcodebuttonPage(page, codebutton));
   }
    /**
    * 业务类型操作的服务
    */
    @Autowired
   @Lazy
   private WebBoServiceI boService;
   /**
    * 主键批量获取主数据中的按钮扩展
    * @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);
   }
    /**
    * 对象的操作
    */
    @Autowired
    private RevisionModelUtil revisionModelUtil;
   /**
    * 批量数据对象转换为显示对象
    * @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()));
      }
      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;
   }
    /**
     * 查询所有的主数据中的按钮扩展
     * @param conditionMap 查询条件
     * @param pageHelper 分页和排序
     * @return 执行结果
     * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
     */
    @Override
    public DataGrid<CodeButtonVO> gridCodeButton(Map<String, String> conditionMap, PageHelper pageHelper) throws VciBaseException{
        if (pageHelper == null) {
            pageHelper = new PageHelper(-1);
        }
        pageHelper.addDefaultDesc("createTime");
        List<CodeButtonDO> doList = codeButtonMapper.selectByCondition(conditionMap,pageHelper);
        DataGrid<CodeButtonVO> dataGrid=new DataGrid<CodeButtonVO>();
        if (!CollectionUtils.isEmpty(doList)) {
            dataGrid.setData(codeButtonDO2VOs(doList));
            dataGrid.setTotal(VciBaseUtil.getInt(String.valueOf(codeButtonMapper.countByCondition(conditionMap))));
        }
        return dataGrid;
   @Override
    public R enableCodeButton(String id) {
      CodeButton codebutton = codeButtonMapper.selectById(id);
      return changeLcStatus(codebutton,true);
    }
    /**
     * 批量数据对象转换为显示对象
     * @param codeButtonDOs 数据对象列表
     * @return 显示对象
     * @throws VciBaseException 参数为空或者不存在的时候会抛出异常
     */
    @Override
    public List<CodeButtonVO> codeButtonDO2VOs(Collection<CodeButtonDO>  codeButtonDOs) throws VciBaseException{
        List<CodeButtonVO> voList = new ArrayList<CodeButtonVO>();
        if(!CollectionUtils.isEmpty(codeButtonDOs)){
           for(CodeButtonDO s: codeButtonDOs){
                CodeButtonVO vo =  codeButtonDO2VO(s);
                if(vo != null){
                    voList.add(vo);
                }
            }
        }
        return voList;
    }
   /**
    * 停用
    *
    * @param oid 数据传输对象
    * @return 执行结果
    */
   @Override
   public R disableOrgDuty(String oid) {
      CodeButton codebutton = codeButtonMapper.selectById(oid);
      return changeLcStatus(codebutton,false);
   }
    /**
     * 数据对象转换为显示对象
     * @param  codeButtonDO 数据对象
     * @return 显示对象
     * @throws VciBaseException 拷贝属性出错的时候会抛出异常
     */
    @Override
    public  CodeButtonVO codeButtonDO2VO(CodeButtonDO codeButtonDO) throws VciBaseException{
              CodeButtonVO vo = new CodeButtonVO();
        if(codeButtonDO != null){
            BeanUtilForVCI.copyPropertiesIgnoreCase(codeButtonDO,vo);
            //如果有lcstatus的类的话
            vo.setLcStatusText(FrameworkDataLCStatus.getTextByValue(vo.getLcStatus()));
   //   @Override
//   public R deleteCodeButton(String ids) {
////      VciBaseUtil.alertNotNull(codeButtonDTO,"主数据中的按钮扩展数据对象",codeButtonDTO.getOid(),"主数据中的按钮扩展的主键");
//      if(StringUtils.isEmpty(ids)){
//         return R.fail("传入非法数据!");
//      }
//      CodeButtonEntity codebutton = codeButtonMapper.selectById(ids);
//
//      CodeButtonDO codeButtonDO = selectByOid(codeButtonDTO.getOid());
//      BaseResult baseResult = checkIsCanDeleteForDO(codeButtonDTO,codeButtonDO);
//      if(baseResult.isSuccess()) {
//      }else{
//         return baseResult;
//      }
//      //执行删除操作
//      BatchCBO batchCBO = codeButtonMapper.deleteByPrimaryKey(codeButtonDO.getOid());
//      return (batchCBO!=null && batchCBO.getDeleteCbos() !=null &&batchCBO.getDeleteCbos().size() > 0)?BaseResult.successMsg(DELETE_SUCCESS):BaseResult.fail(DELETE_FAIL);
//   }
        }
        return vo;
    }
   /**
    * 修改生命周期的状态,如停用和启用
    * @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 主数据中的按钮扩展数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
    @Override
    public CodeButtonVO addSave(CodeButtonDTO codeButtonDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(codeButtonDTO,"需要添加的数据对象");
        //将DTO转换为DO
        CodeButtonDO codeButtonDO = new CodeButtonDO();
        BeanUtilForVCI.copyPropertiesIgnoreCase(codeButtonDTO,codeButtonDO);
        codeButtonMapper.insert(codeButtonDO);
        return codeButtonDO2VO(codeButtonDO);
    }
   //   /**
//    * 校验是否可以删除,如果存在下级,并且下级有数据引用则不能删除
//    * @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[]{""});
//      }
//   }
    /**
     * 修改主数据中的按钮扩展
     * @param codeButtonDTO 主数据中的按钮扩展数据传输对象
     * @return 执行结果
     * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
     */
     @Override
     public CodeButtonVO editSave(CodeButtonDTO codeButtonDTO) throws VciBaseException{
         VciBaseUtil.alertNotNull(codeButtonDTO,"数据对象",codeButtonDTO.getOid(),"主数据中的按钮扩展主键");
         //将DTO转换为DO
         CodeButtonDO codeButtonDO = selectByOid(codeButtonDTO.getOid());
         revisionModelUtil.copyFromDTOIgnore(codeButtonDTO,codeButtonDO);
         codeButtonMapper.updateByPrimaryKey(codeButtonDO);
         return codeButtonDO2VO(codeButtonDO);
     }
    /**
     * 校验是否可以删除,如果存在下级,并且下级有数据引用则不能删除
     * @param codeButtonDTO 数据传输对象
     * @param codeButtonDO 数据库中的数据对象
     * @return success为true为可以删除,false表示有数据引用,obj为true表示有下级
     */
    private BaseResult checkIsCanDeleteForDO(CodeButtonDTO codeButtonDTO, CodeButtonDO codeButtonDO) {
        CodeButtonDO buttonDO = new CodeButtonDO();
        BeanUtil.convert(codeButtonDTO,buttonDO);
        boService.checkTs(buttonDO);
        if(!checkIsLinked(codeButtonDO.getOid())) {
            return BaseResult.success();
        }else{
            return BaseResult.fail(DATA_LINKED_NOT_DELETE,new String[]{""});
        }
    }
    /**
    * 校验是否被引用
    * @param oid 主键
    * @throws VciBaseException 被引用的时候会抛出异常
    */
    private boolean checkIsLinked(String oid) throws VciBaseException{
        //TODO 添加需要校验引用的地方
        return false;
    }
    /**
     * 删除主数据中的按钮扩展
     * @param codeButtonDTO 主数据中的按钮扩展数据传输对象,oid和ts需要传输
     * @return 删除结果反馈::success:成功,fail:失败
     * @throws VciBaseException 参数为空,被引用时抛出异常
     */
    @Override
    public BaseResult deleteCodeButton(CodeButtonDTO codeButtonDTO) throws VciBaseException{
        VciBaseUtil.alertNotNull(codeButtonDTO,"主数据中的按钮扩展数据对象",codeButtonDTO.getOid(),"主数据中的按钮扩展的主键");
        CodeButtonDO codeButtonDO = selectByOid(codeButtonDTO.getOid());
        BaseResult baseResult = checkIsCanDeleteForDO(codeButtonDTO,codeButtonDO);
        if(baseResult.isSuccess()) {
                    }else{
            return baseResult;
        }
        //执行删除操作
        BatchCBO batchCBO = codeButtonMapper.deleteByPrimaryKey(codeButtonDO.getOid());
        return (batchCBO!=null && batchCBO.getDeleteCbos() !=null &&batchCBO.getDeleteCbos().size() > 0)?BaseResult.successMsg(DELETE_SUCCESS):BaseResult.fail(DELETE_FAIL);
    }
    /**
    * 主键获取主数据中的按钮扩展
    * @param oid 主键
    * @return 主数据中的按钮扩展显示对象
    * @throws VciBaseException 参数为空,数据不存在时会抛出异常
    */
    @Override
    public  CodeButtonVO getObjectByOid(String oid) throws VciBaseException{
        return codeButtonDO2VO(selectByOid(oid));
    }
    /**
    * 主键查询数据对象
    * @param oid 主键
    * @return  数据对象
    * @throws VciBaseException 参数为空,并且数据不存在的时候会抛出异常
    */
    private CodeButtonDO selectByOid(String oid) throws VciBaseException{
        VciBaseUtil.alertNotNull(oid,"主键");
        CodeButtonDO codeButtonDO = codeButtonMapper.selectByPrimaryKey(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<CodeButtonDO> codeButtonDOList = listCodeButtonDOByOidCollections(oidCollections);
        return codeButtonDO2VOs(codeButtonDOList);
    }
    /**
    * 使用主键集合查询数据对象
    * @param oidCollections 主键的集合
    * @return 数据对象列表
    */
    private List<CodeButtonDO> listCodeButtonDOByOidCollections(Collection<String> oidCollections){
        List<CodeButtonDO> codeButtonDOList = new ArrayList<CodeButtonDO>();
        if(!CollectionUtils.isEmpty(oidCollections)){
            Collection<Collection<String>> oidCollectionsList = VciBaseUtil.switchCollectionForOracleIn(oidCollections);
            for(Collection<String> oids: oidCollectionsList){
                List<CodeButtonDO> tempDOList =  codeButtonMapper.selectByPrimaryKeyCollection(oids);
                if(!CollectionUtils.isEmpty(tempDOList)){
                        codeButtonDOList.addAll(tempDOList);
                }
            }
        }
        return  codeButtonDOList;
    }
    /**
     * 参照主数据中的按钮扩展列表
     * @param conditionMap 查询条件
     * @param pageHelper 分页和排序
     * @return 主数据中的按钮扩展显示对象列表,生效的内容
     * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
     */
    @Override
    public DataGrid<CodeButtonVO> refDataGridCodeButton(Map<String, String> conditionMap, PageHelper pageHelper) throws VciBaseException{
        if(conditionMap == null){
            conditionMap = new HashMap<String, String>();
        }
        return gridCodeButton(conditionMap,pageHelper);
    }
    /**
     * 启用
     *
     * @param buttonDTO 数据传输对象
     * @return 执行结果
     */
    @Override
    public BaseResult enableCodeButton(CodeButtonDTO buttonDTO) {
        return changeLcStatus(buttonDTO,false);
    }
    /**
     * 停用
     *
     * @param buttonDTO 数据传输对象
     * @return 执行结果
     */
    @Override
    public BaseResult disableCodeButton(CodeButtonDTO buttonDTO) {
        return changeLcStatus(buttonDTO,true);
    }
    /**
     * 修改生命周期的状态,如停用和启用
     * @param buttonDTO 数据传输对象,必须要有oid和ts
     * @param disable 是否为停用
     * @return 执行的结果
     */
    private BaseResult changeLcStatus(CodeButtonDTO buttonDTO, boolean disable){
        VciBaseUtil.alertNotNull(buttonDTO,"数据对象",buttonDTO.getOid(),"主键");
        CodeButtonDO buttonDO = selectByOid(buttonDTO.getOid());
        CodeButtonDO outButtonDO = new CodeButtonDO();
        BeanUtil.convert(buttonDTO,outButtonDO);
        boService.checkTs(outButtonDO);
        if(disable){
            buttonDO.setLcStatus(FrameworkDataLCStatus.DISABLED.getValue());
        }else{
            buttonDO.setLcStatus(FrameworkDataLCStatus.ENABLED.getValue());
        }
        return (codeButtonMapper.updateLcStatus(buttonDO.getOid(),buttonDO.getLcStatus()) >0 )?(BaseResult.successMsg((disable?DISABLE_SUCCESS:ENABLE_SUCCESS))):(BaseResult.fail((disable?DISABLE_FAIL:ENABLE_FAIL)));
    }
}