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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.domain.system.config.BdMemberSpecialCurrency;
import com.hzs.common.domain.system.config.BdMemberSpecialDetail;
import com.hzs.common.domain.system.config.ext.BdMemberSpecialCurrencyExt;
import com.hzs.common.domain.system.config.ext.BdMemberSpecialDetailExt;
import com.hzs.system.config.mapper.BdMemberSpecialCurrencyMapper;
import com.hzs.system.config.param.BdMemberSpecialCurrencyParam;
import com.hzs.system.config.param.BdMemberSpecialCurrencySaveParam;
import com.hzs.system.config.service.IBdMemberSpecialCurrencyService;
import com.hzs.system.config.service.IBdMemberSpecialDetailService;
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;

/**
 * 会员特殊消费配置 服务实现类
 *
 * @author hzs
 * @since 2025-02-20
 */
@Service
public class BdMemberSpecialCurrencyServiceImpl extends ServiceImpl<BdMemberSpecialCurrencyMapper, BdMemberSpecialCurrency> implements IBdMemberSpecialCurrencyService {

    @Autowired
    private IBdMemberSpecialDetailService iBdMemberSpecialDetailService;

    @Override
    public List<BdMemberSpecialCurrencyExt> listData(BdMemberSpecialCurrencyParam param) {
        return baseMapper.listData(param);
    }

    @Override
    public BdMemberSpecialCurrencyExt getData(Integer pkId) {
        return baseMapper.getData(pkId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveData(BdMemberSpecialCurrencySaveParam param, Long userId) {
        Date nowDate = new Date();

        BdMemberSpecialCurrency bdMemberSpecialCurrency = BeanUtil.copyProperties(param, BdMemberSpecialCurrency.class);
        if (null != param.getPkId()) {
            // 修改
            bdMemberSpecialCurrency.setPkModified(userId);
            bdMemberSpecialCurrency.setModifiedTime(nowDate);
            this.updateById(bdMemberSpecialCurrency);

            // 处理明细表（历史全删）
            // 删除配置明细信息
            iBdMemberSpecialDetailService.update(Wrappers.<BdMemberSpecialDetail>lambdaUpdate()
                    .eq(BdMemberSpecialDetail::getPkMemberSpecialCurrency, param.getPkId())
                    .set(BdMemberSpecialDetail::getDelFlag, EDelFlag.DELETE.getValue())
                    .set(BdMemberSpecialDetail::getPkModified, userId)
                    .set(BdMemberSpecialDetail::getModifiedTime, nowDate));
        } else {
            // 保存
            bdMemberSpecialCurrency.setPkCreator(userId);
            bdMemberSpecialCurrency.setCreationTime(nowDate);
            bdMemberSpecialCurrency.setPkCountry(param.getPkCountry());
            bdMemberSpecialCurrency.setSystemType(param.getSystemType());
            this.save(bdMemberSpecialCurrency);
        }

        // 添加新明细数据
        List<BdMemberSpecialDetail> detailList = param.getDetailList();
        for (BdMemberSpecialDetail bdMemberSpecialDetail : detailList) {
            bdMemberSpecialDetail.setPkMemberSpecialCurrency(bdMemberSpecialCurrency.getPkId());
            bdMemberSpecialDetail.setPkCreator(userId);
            bdMemberSpecialDetail.setCreationTime(nowDate);
            bdMemberSpecialDetail.setPkCountry(param.getPkCountry());
        }
        iBdMemberSpecialDetailService.saveBatch(detailList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteData(Integer pkId, Long userId) {
        Date nowDate = new Date();
        // 删除配置信息
        this.update(Wrappers.<BdMemberSpecialCurrency>lambdaUpdate()
                .eq(BdMemberSpecialCurrency::getPkId, pkId)
                .set(BdMemberSpecialCurrency::getDelFlag, EDelFlag.DELETE.getValue())
                .set(BdMemberSpecialCurrency::getPkModified, userId)
                .set(BdMemberSpecialCurrency::getModifiedTime, nowDate)
        );
        // 删除配置明细信息
        iBdMemberSpecialDetailService.update(Wrappers.<BdMemberSpecialDetail>lambdaUpdate()
                .eq(BdMemberSpecialDetail::getPkMemberSpecialCurrency, pkId)
                .set(BdMemberSpecialDetail::getDelFlag, EDelFlag.DELETE.getValue())
                .set(BdMemberSpecialDetail::getPkModified, userId)
                .set(BdMemberSpecialDetail::getModifiedTime, nowDate));
    }

    @Override
    public List<BdMemberSpecialDetailExt> listMemberDetail(Long pkMember, Integer specialArea) {
        return baseMapper.listMemberDetail(pkMember, specialArea);
    }

}
