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

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.core.utils.DateUtils;
import com.hzs.common.domain.system.config.BdSpecialCurrency;
import com.hzs.common.domain.system.config.BdSpecialCurrencyDetail;
import com.hzs.common.domain.system.config.ext.BdSpecialCurrencyExt;
import com.hzs.system.config.mapper.BdSpecialCurrencyMapper;
import com.hzs.system.config.param.SpecialCurrencyParam;
import com.hzs.system.config.service.IBdSpecialCurrencyDetailService;
import com.hzs.system.config.service.IBdSpecialCurrencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 特殊消费配置 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-07-18
 */
@Service
public class BdSpecialCurrencyServiceImpl extends ServiceImpl<BdSpecialCurrencyMapper, BdSpecialCurrency> implements IBdSpecialCurrencyService {

    @Autowired
    private IBdSpecialCurrencyDetailService bdSpecialCurrencyDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveSpecialCurrency(SpecialCurrencyParam specialCurrencyParam) {
        if (specialCurrencyParam.getPkId() == null) {
            // 保存
            BdSpecialCurrency bdSpecialCurrency = BdSpecialCurrency.builder()
                    .tempName(specialCurrencyParam.getTempName())
                    .build();
            bdSpecialCurrency.setPkCountry(specialCurrencyParam.getPkCountry());
            bdSpecialCurrency.setPkCreator(specialCurrencyParam.getUserId());
            bdSpecialCurrency.setCreationTime(specialCurrencyParam.getCurrentTime());
            save(bdSpecialCurrency);
            specialCurrencyParam.setPkId(bdSpecialCurrency.getPkId());
        } else {
            UpdateWrapper<BdSpecialCurrency> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("TEMP_NAME", specialCurrencyParam.getTempName());
            updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, specialCurrencyParam.getCurrentTime());
            updateWrapper.set(SystemFieldConstants.PK_MODIFIED, specialCurrencyParam.getUserId());
            updateWrapper.eq(SystemFieldConstants.PK_ID, specialCurrencyParam.getPkId());
            update(updateWrapper);
            bdSpecialCurrencyDetailService.deleteSpecialCurrencyDetail(specialCurrencyParam);
        }
        bdSpecialCurrencyDetailService.saveBdSpecialCurrencyDetail(specialCurrencyParam);
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteSpecialCurrency(SpecialCurrencyParam specialCurrencyParam) {
        UpdateWrapper<BdSpecialCurrency> 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(SystemFieldConstants.PK_ID, specialCurrencyParam.getPkId());
        baseMapper.delete(updateWrapper);
        bdSpecialCurrencyDetailService.deleteBdSpecialCurrencyDetail(specialCurrencyParam);
        return Boolean.TRUE;
    }

    @Override
    public List<BdSpecialCurrencyExt> querySpecialCurrencyList(SpecialCurrencyParam specialCurrencyParam) {
        return baseMapper.querySpecialCurrencyList(specialCurrencyParam);
    }
}
