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.CacheConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.system.config.BdAreaCurrency;
import com.hzs.common.domain.system.config.BdAreaCurrencyDetail;
import com.hzs.common.domain.system.config.ext.BdAreaCurrencyDetailExt;
import com.hzs.common.domain.system.config.ext.BdAreaCurrencyExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.mapper.BdAreaCurrencyMapper;
import com.hzs.system.config.service.IBdAreaCurrencyDetailService;
import com.hzs.system.config.service.IBdAreaCurrencyService;
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 zhangjing
 * @since 2022-08-26
 */
@Service
public class BdAreaCurrencyServiceImpl extends ServiceImpl<BdAreaCurrencyMapper, BdAreaCurrency> implements IBdAreaCurrencyService {

    @Autowired
    IBdAreaCurrencyDetailService areaCurrencyDetailService;
    @Autowired
    private IBdAreaCurrencyDetailService areaCurrencyParamDetailService;
    @Autowired
    private RedisService redisService;

    /**
     * @description: 新增消费配置+新增消费配置详情
     * @author: zhang jing
     * @date: 2022/10/26 17:27
     * @param: [areaCurrencyExt]
     * @return: boolean
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveAreaCurrency(BdAreaCurrencyExt areaCurrencyExt) {
        List<BdAreaCurrencyDetailExt> detailsList = areaCurrencyExt.getDetailsList();
        baseMapper.insert(areaCurrencyExt);
        if (CollectionUtil.isNotEmpty(detailsList)) {
            for (BdAreaCurrencyDetailExt bcd : detailsList) {
                bcd.setPkAreaCurrency(areaCurrencyExt.getPkId().intValue());
                bcd.setPkAccount(bcd.getPkAccount());
                bcd.setDeductSort(bcd.getDeductSort());
                bcd.setDeductRatio(bcd.getDeductRatio());
                bcd.setDeductMaxRatio(bcd.getDeductMaxRatio());
                bcd.setPkCountry(SecurityUtils.getPkCountry());
                bcd.setPkCreator(SecurityUtils.getUserId());
                bcd.setCreationTime(new Date());
                areaCurrencyDetailService.save(bcd);
            }
        }
        return true;
    }

    /**
     * @description: 修改消费配置+修改消费配置详情
     * @author: zhang jing
     * @date: 2022/10/26 17:27
     * @param: [areaCurrencyExt]
     * @return: boolean
     **/
    @Override
    public boolean updateAreaCurrency(BdAreaCurrencyExt areaCurrencyExt) {
        List<BdAreaCurrencyDetailExt> detailsList = areaCurrencyExt.getDetailsList();
        baseMapper.updateById(areaCurrencyExt);
        BdAreaCurrencyDetail areaCurrencyDetail = new BdAreaCurrencyDetail();
        areaCurrencyDetail.setPkAreaCurrency(areaCurrencyExt.getPkId().intValue());
        UpdateWrapper<BdAreaCurrencyDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("DEL_FLAG", EYesNo.NO.getIntValue());
        updateWrapper.eq("PK_AREA_CURRENCY", areaCurrencyExt.getPkId());
        updateWrapper.eq("DEL_FLAG", EYesNo.YES.getIntValue());
        areaCurrencyDetailService.update(updateWrapper);
        if (CollectionUtil.isNotEmpty(detailsList)) {
            for (BdAreaCurrencyDetailExt bcd : detailsList) {
                bcd.setPkAreaCurrency(areaCurrencyExt.getPkId().intValue());
                bcd.setPkCountry(SecurityUtils.getPkCountry());
                bcd.setPkCreator(SecurityUtils.getUserId());
                bcd.setCreationTime(new Date());
                bcd.setDelFlag(bcd.getDelAreaCurrency());
                bcd.setPkModified(SecurityUtils.getUserId());
                bcd.setModifiedTime(new Date());
                bcd.setDeductSort(bcd.getDeductSort());
                bcd.setDeductMaxRatio(bcd.getDeductMaxRatio());
                areaCurrencyDetailService.save(bcd);
            }
        }
        return true;
    }

    /**
     * @description: 删除
     * @author: zhang jing
     * @date: 2023/5/6 12:02
     * @param: [bdAreaCurrency]
     * @return: boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delAreaCurrency(BdAreaCurrency bdAreaCurrency) {
        BdAreaCurrency areaCurrencyParam = baseMapper.selectById(bdAreaCurrency.getPkId());
        UpdateWrapper<BdAreaCurrencyDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_AREA_CURRENCY", bdAreaCurrency.getPkId());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", bdAreaCurrency.getPkModified());
        updateWrapper.set("MODIFIED_TIME", bdAreaCurrency.getModifiedTime());

        areaCurrencyParamDetailService.update(updateWrapper);
        bdAreaCurrency.setDelFlag(EDelFlag.DELETE.getValue());
        UpdateWrapper<BdAreaCurrency> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("PK_ID", bdAreaCurrency.getPkId());
        updateWrapper1.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper1.set("PK_MODIFIED", bdAreaCurrency.getPkModified());
        updateWrapper1.set("MODIFIED_TIME", bdAreaCurrency.getModifiedTime());
        baseMapper.update(null, updateWrapper1);
        String key = CacheConstants.BD_AREA_CURRENCY + areaCurrencyParam.getPkCountry().toString() + areaCurrencyParam.getSpecialArea();
        if (redisService.hasKey(key)) {
            redisService.deleteObject(key);
        }
        return true;
    }
}
