package com.gx.obe.server.management.config.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gx.obe.server.common.enumeration.CommonEnum;
import com.gx.obe.server.common.utils.IDUtils;
import com.gx.obe.server.common.utils.StringUtils;
import com.gx.obe.server.management.config.dao.SysConfigValueMapper;
import com.gx.obe.server.management.config.entity.SysConfigValue;
import com.gx.obe.server.management.config.service.SysConfigValueService;

/** 
 * @Description: 
 * @author mazc 
 */
@Service
@Transactional
public class SysConfigValueServiceImpl extends ServiceImpl<SysConfigValueMapper, SysConfigValue> implements SysConfigValueService {

	@Autowired
	private SysConfigValueMapper sysConfigValueMapper;
	
	/**
	 * @Description: 批量更新
	 * @author mazc
	 * @param SysConfigValueList 
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateByBatch(List<SysConfigValue> SysConfigValueList) {
		return sysConfigValueMapper.updateBatchList(SysConfigValueList);
	}
	
	/**
	 * @Description: 批量插入
	 * @author mazc
	 * @param SysConfigValueList 
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int insertByBatch(List<SysConfigValue> SysConfigValueList) {
		return sysConfigValueMapper.insertByBatch(SysConfigValueList);
	}
	
	/**
     * @Description: 指定字段修改
     * @author mazc
     * @param SysConfigValue
	 * @param attributes
     */
	@Override
    public boolean updateAssignProperty(SysConfigValue SysConfigValue, String[] attributes) {
		return sysConfigValueMapper.updateAssignProperty(SysConfigValue, attributes) > 0;
	}
    
    /**
     * @Description: 批量指定字段修改
     * @author mazc
     * @param SysConfigValueList
	 * @param attributes
     */
	@Override
    public int batchUpdateProperty(List<SysConfigValue> SysConfigValueList, String[] attributes) {
		return sysConfigValueMapper.batchUpdateProperty(SysConfigValueList, attributes);
	}
	
	/** 
	 * @Description: 批量添加或跟新
	 * @author mazc
	 * @param SysConfigValueList
	 * @param attributes
	 * @return 
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
    public int batchSaveOrUpdate(List<SysConfigValue> SysConfigValueList, String[] attributes) {
        List<SysConfigValue> insertSysConfigValueList = new ArrayList<SysConfigValue>();
        List<SysConfigValue> updateSysConfigValueList = new ArrayList<SysConfigValue>();
        for (SysConfigValue SysConfigValue : SysConfigValueList) {
            if (StringUtils.isEmpty(SysConfigValue.getId())) {
                SysConfigValue.setId(IDUtils.getId());
                insertSysConfigValueList.add(SysConfigValue);
            } else {
                updateSysConfigValueList.add(SysConfigValue);
            }
        }
        int count = 0;
        if (insertSysConfigValueList.size() > 0) {
            count = count + sysConfigValueMapper.insertByBatch(insertSysConfigValueList);
        }
        if (updateSysConfigValueList.size() > 0) {
            count = count + sysConfigValueMapper.batchUpdateProperty(updateSysConfigValueList, attributes);
        }
        return count;
    }
	
	/**
	 * @Description: 根据配置编码,获得有效配置值集合。
	 * @author mazc
	 * @param configValueCode 配置编码
	 * @return List<SysConfigValue>
	 */
	@Override
	public List<SysConfigValue> getConfigValueListByConfigCode(String configValueCode) {
		return sysConfigValueMapper.getConfigValueListByConfigCode(configValueCode, CommonEnum.REAL_STATUS);
	}

	/** 
	 * @Description: 验证配置值编码唯一性
	 * @author mazc
	 * @param configId
	 * @param id
	 * @param configValueCode
	 * @return 
	 */
	@Override
	public boolean isUniqueConfigValueCode(String configId, String id, String configValueCode) {
		return sysConfigValueMapper.isUniqueConfigValueCode(configId, id, configValueCode) > 0 ? false : true;
	}

	/** 
	 * @Description: 获得数据配置值的最大序号
	 * @author mazc
	 * @return 
	 */
	@Override
	public int getMaxConfigValueSortNo(String configId) {
		Integer maxSort = sysConfigValueMapper.getMaxConfigValueSortNo(configId);
		return null != maxSort ? maxSort.intValue() : 0;
	}

	/**
	 * @Description: 获得系统配置的最晚修改时间
	 * @author mazc
	 * @return
	 */
	@Override
	public Date getLatestModifyTime(String configCode) {
		Date lastModifyTime = sysConfigValueMapper.getLatestModifyTime(configCode);
		return lastModifyTime;
	}
	
	/** 
	 * @Description: 交换数据配置值的排序
	 * @author mazc
	 * @param sourceSortNo
	 * @param sourceid
	 * @param targetSortNo
	 * @param targetId
	 * @return 
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean changeConfigValueSortNo(Integer sourceSortNo, String sourceid, Integer targetSortNo, String targetId) {
		int count = 0;
		UpdateWrapper<SysConfigValue> sourceUpdateWrapper = new UpdateWrapper<>();
		sourceUpdateWrapper.set(SysConfigValue.SORT_NO,targetSortNo);
		sourceUpdateWrapper.lambda().eq(SysConfigValue::getId, sourceid);
		count += sysConfigValueMapper.update(null, sourceUpdateWrapper);
		
		UpdateWrapper<SysConfigValue> targetUpdateWrapper = new UpdateWrapper<>();
		targetUpdateWrapper.set(SysConfigValue.SORT_NO,sourceSortNo);
		targetUpdateWrapper.lambda().eq(SysConfigValue::getId, targetId);
		count +=  sysConfigValueMapper.update(null, targetUpdateWrapper); 
		
		return count == 2;
 	}
 	
	
	/** 
	 * @Description:  获得配置ID下的配置值集合
	 * @author mazc
	 * @param configId
	 * @param useStatus
	 * @return 
	 */
	@Override
	public List<SysConfigValue> getConfigValueListByConfigId(String configId, String useStatus) {
		return sysConfigValueMapper.getConfigValueListByConfigId(configId, useStatus);
	}
}