package com.huatek.busi.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.entity.LogoSetting;
import com.huatek.busi.mapper.LogoSettingMapper;
import com.huatek.busi.service.ILogoSettingService;
import com.huatek.busi.vo.LogoSettingVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

@Component
@Service(interfaceClass = ILogoSettingService.class)
@Transactional(rollbackFor = Exception.class)
public class LogoSettingServiceImpl extends BaseDao<LogoSetting, String> implements ILogoSettingService {

    @Resource
    private LogoSettingMapper logoSettingMapper;

    @Override
    public LogoSetting findOne(){
        return logoSettingMapper.findOne();
    }

    @Override
    public Map<String, Object> saveOrUpdate(String token, LogoSettingVo logoSettingVo){
        if(StrUtil.isEmpty(token)){
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "BUSINESS.TOKEN", "");
        }

        // 初始化数据
        logoSettingMapper.delete();
        LocalUser user = UserUtil.getLocalUser(token);
        logoSettingVo.setId(Uuid.getUUID());
        if(user != null){
            logoSettingVo.setCreateUser(user.getUid());
            logoSettingVo.setCreateTime(LocalDateTime.now());
        }

        LogoSetting logoSetting = new LogoSetting();
        BeanUtils.copyProperties(logoSettingVo, logoSetting);
        this.add(logoSetting);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.INSERT.SUCCESS", logoSetting);
    }
}
