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.UpdateWrapper;
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;

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

    @Autowired
    private IBdStoreLevelDetailService storeLevelDetailService;

    /**
     * @description: 新增店铺等级配置
     * @author: zhang jing
     * @date: 2023/5/8 19:55
     * @param: [storeLevelVO]
     * @return: boolean
     **/
    @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());
                storeLevelDetailService.save(sd);
            }
        }
        return true;
    }

    /**
     * @description: 修改店铺等级配置
     * @author: zhang jing
     * @date: 2023/5/8 19:57
     * @param: [storeLevelVO]
     * @return: boolean
     **/
    @Override
    public boolean updateStoreLevel(BdStoreLevelVO storeLevelVO) {
        BdStoreLevel storeLevel = BeanUtil.copyProperties(storeLevelVO, BdStoreLevel.class);
        UpdateWrapper<BdStoreLevelDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_STORE_LEVEL", storeLevelVO.getPkId());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", storeLevelVO.getPkModified());
        updateWrapper.set("MODIFIED_TIME", storeLevelVO.getModifiedTime());
        storeLevelDetailService.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());
                storeLevelDetailService.save(sd);
            }
        }
        return true;
    }

    /**
     * @description: 删除
     * @author: zhang jing
     * @date: 2023/5/8 20:09
     * @param: [storeLevelVO]
     * @return: boolean
     **/
    @Override
    public boolean delStoreLevel(BdStoreLevelVO storeLevelVO) {
        BdStoreLevel storeLevel = BeanUtil.copyProperties(storeLevelVO, BdStoreLevel.class);
        UpdateWrapper<BdStoreLevel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_STORE_LEVEL", storeLevel.getPkId());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", storeLevelVO.getPkModified());
        updateWrapper.set("MODIFIED_TIME", storeLevelVO.getModifiedTime());
        baseMapper.update(null, updateWrapper);
        UpdateWrapper<BdStoreLevelDetail> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("PK_STORE_LEVEL", storeLevelVO.getPkId());
        updateWrapper1.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper1.set("PK_MODIFIED", storeLevelVO.getPkModified());
        updateWrapper1.set("MODIFIED_TIME", storeLevelVO.getModifiedTime());
        storeLevelDetailService.update(updateWrapper1);
        return true;
    }

}
