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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.domain.system.config.BdStoreLevel;
import com.hzs.common.domain.system.config.BdStoreLevelDetail;
import com.hzs.system.config.mapper.BdStoreLevelMapper;
import com.hzs.system.config.service.IBdStoreLevelDetailService;
import com.hzs.system.config.service.IBdStoreLevelService;
import com.hzs.system.config.vo.BdStoreLevelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 店铺等级配置 服务实现类
 */
@Service
public class BdStoreLevelServiceImpl extends ServiceImpl<BdStoreLevelMapper, BdStoreLevel> implements IBdStoreLevelService {

    @Autowired
    private IBdStoreLevelDetailService iBdStoreLevelDetailService;

    @Override
    public boolean saveStoreLevel(BdStoreLevelVO storeLevelVO) {
        BdStoreLevel storeLevel = BeanUtil.copyProperties(storeLevelVO, BdStoreLevel.class);
        baseMapper.insert(storeLevel);
        storeLevelVO.setPkId(storeLevel.getPkId());
        if (CollectionUtil.isNotEmpty(storeLevelVO.getDetailList())) {
            for (BdStoreLevelDetail sd : storeLevelVO.getDetailList()) {
                sd.setPkStoreLevel(storeLevel.getPkId());
                sd.setCreationTime(storeLevelVO.getCreationTime());
                sd.setPkCountry(storeLevelVO.getPkCountry());
                sd.setPkCreator(storeLevelVO.getPkCreator());
                iBdStoreLevelDetailService.save(sd);
            }
        }
        return true;
    }

    @Override
    public boolean updateStoreLevel(BdStoreLevelVO storeLevelVO) {
        BdStoreLevel storeLevel = BeanUtil.copyProperties(storeLevelVO, BdStoreLevel.class);
        LambdaUpdateWrapper<BdStoreLevelDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdStoreLevelDetail::getPkStoreLevel, storeLevelVO.getPkId());
        updateWrapper.set(BdStoreLevelDetail::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdStoreLevelDetail::getPkModified, storeLevelVO.getPkModified());
        updateWrapper.set(BdStoreLevelDetail::getModifiedTime, storeLevelVO.getModifiedTime());
        iBdStoreLevelDetailService.update(updateWrapper);
        baseMapper.updateById(storeLevel);
        if (CollectionUtil.isNotEmpty(storeLevelVO.getDetailList())) {
            for (BdStoreLevelDetail sd : storeLevelVO.getDetailList()) {
                sd.setPkStoreLevel(storeLevel.getPkId());
                sd.setCreationTime(storeLevelVO.getModifiedTime());
                sd.setPkCountry(storeLevelVO.getPkCountry());
                sd.setPkCreator(storeLevelVO.getPkModified());
                iBdStoreLevelDetailService.save(sd);
            }
        }
        return true;
    }

    @Override
    public boolean delStoreLevel(BdStoreLevelVO storeLevelVO) {
        BdStoreLevel storeLevel = BeanUtil.copyProperties(storeLevelVO, BdStoreLevel.class);
        LambdaUpdateWrapper<BdStoreLevel> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdStoreLevel::getPkId, storeLevel.getPkId());
        updateWrapper.set(BdStoreLevel::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdStoreLevel::getPkModified, storeLevelVO.getPkModified());
        updateWrapper.set(BdStoreLevel::getModifiedTime, storeLevelVO.getModifiedTime());
        baseMapper.update(null, updateWrapper);
        LambdaUpdateWrapper<BdStoreLevelDetail> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(BdStoreLevelDetail::getPkStoreLevel, storeLevelVO.getPkId());
        updateWrapper1.set(BdStoreLevelDetail::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper1.set(BdStoreLevelDetail::getPkModified, storeLevelVO.getPkModified());
        updateWrapper1.set(BdStoreLevelDetail::getModifiedTime, storeLevelVO.getModifiedTime());
        iBdStoreLevelDetailService.update(updateWrapper1);
        return true;
    }

}
