package com.njcn.product.system.theme.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;

import com.njcn.product.system.dict.enums.SystemResponseEnum;
import com.njcn.product.system.theme.mapper.ConfigMapper;
import com.njcn.product.system.theme.pojo.param.ConfigParam;
import com.njcn.product.system.theme.pojo.po.Config;
import com.njcn.product.system.theme.service.IConfigService;
import com.njcn.web.utils.RequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements IConfigService {


    @Override
    public boolean addSysConfig(ConfigParam configParam) {
        Config config = new Config();
        BeanUtils.copyProperties(configParam, config);
        config.setCreateBy(RequestUtil.getUserIndex());
        config.setCreateTime(LocalDateTime.now());
        config.setUpdateBy(RequestUtil.getUserIndex());
        config.setUpdateTime(LocalDateTime.now());
        config.setState(DataStateEnum.ENABLE.getCode());
        this.baseMapper.insert(config);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSysConfig(ConfigParam.ConfigUpdateParam configUpdateParam) {
        Config config = this.baseMapper.selectById(configUpdateParam.getId());
        if (!Objects.isNull(config)) {
            if (config.getState().equals(DataStateEnum.ENABLE.getCode())) {
                if (Objects.equals(configUpdateParam.getState(), config.getState())) {
                    BeanUtils.copyProperties(configUpdateParam, config);
                    config.setUpdateBy(RequestUtil.getUserIndex());
                    config.setUpdateTime(LocalDateTime.now());
                    this.baseMapper.updateById(config);
                    return true;
                } else {
                    // 不可更改当前激活状态,必须保留一个激活系统
                    throw new BusinessException(SystemResponseEnum.ACTIVATED_STATE);
                }
            } else {
                if (configUpdateParam.getState().equals(DataStateEnum.ENABLE.getCode())) {
                    // 先将所有的都置为非激活状态
                    LambdaUpdateWrapper<Config> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.set(Config::getState, DataStateEnum.DELETED.getCode());
                    this.baseMapper.update(null, updateWrapper);
                }
                BeanUtils.copyProperties(configUpdateParam, config);
                config.setUpdateBy(RequestUtil.getUserIndex());
                config.setUpdateTime(LocalDateTime.now());
                this.baseMapper.updateById(config);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Config> getList() {
        return this.baseMapper.getList();
    }


}
