package com.hzs.system.config.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.domain.system.config.BdSpecialCurrencyDetail;
import com.hzs.system.config.mapper.BdSpecialCurrencyDetailMapper;
import com.hzs.system.config.param.SpecialCurrencyDetailParam;
import com.hzs.system.config.param.SpecialCurrencyParam;
import com.hzs.system.config.service.IBdSpecialCurrencyDetailService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 特殊消费配置详情表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-07-18
 */
@Service
public class BdSpecialCurrencyDetailServiceImpl extends ServiceImpl<BdSpecialCurrencyDetailMapper, BdSpecialCurrencyDetail> implements IBdSpecialCurrencyDetailService {

    @Override
    public void saveBdSpecialCurrencyDetail(SpecialCurrencyParam specialCurrencyParam) {
        if(CollectionUtil.isNotEmpty(specialCurrencyParam.getSpecialCurrencyDetailParamList())){
            baseMapper.saveBdSpecialCurrencyDetail(specialCurrencyParam);
        }
    }

    @Override
    public void deleteSpecialCurrencyDetail(SpecialCurrencyParam specialCurrencyParam) {
        List<SpecialCurrencyDetailParam> specialCurrencyDetailParamList = new ArrayList<>();
        for (SpecialCurrencyDetailParam specialCurrencyDetailParam : specialCurrencyParam.getSpecialCurrencyDetailParamList()) {
            if(specialCurrencyDetailParam.getPkId() != null){
                specialCurrencyDetailParamList.add(specialCurrencyDetailParam);
            }
        }
        if(specialCurrencyDetailParamList.size() >0) {
            baseMapper.deleteSpecialCurrencyDetail(specialCurrencyParam, specialCurrencyDetailParamList);
        }
    }

    @Override
    public void deleteBdSpecialCurrencyDetail(SpecialCurrencyParam specialCurrencyParam) {
        UpdateWrapper<BdSpecialCurrencyDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, specialCurrencyParam.getCurrentTime());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, specialCurrencyParam.getUserId());
        updateWrapper.eq("pk_special_currency", specialCurrencyParam.getPkId());
        baseMapper.delete(updateWrapper);
    }
}
