package com.hdkj.lt.bf.service.impl;

import com.hdkj.lt.bf.entity.vo.ThemeColorVo;
import com.hdkj.lt.bf.mapper.SystemThemeMapper;
import com.hdkj.lt.bf.service.ISystemThemeService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
public class SystemThemeServiceImpl implements ISystemThemeService {
    private final SystemThemeMapper systemThemeMapper;

    /**
     * 如果color表没有用户的userId则在color表增加该用户userId
     * @return 系统颜色
     */
    @Override
    public ThemeColorVo getThemeColor() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ThemeColorVo themeColorVo = null;
        if (ObjectUtils.isNotEmpty(loginUser)){
            themeColorVo = systemThemeMapper.getThemeColor(loginUser.getUserid());
            if(themeColorVo == null){
                themeColorVo = new ThemeColorVo();
                themeColorVo.setUserId(loginUser.getUserid());
                themeColorVo.setThemeColor("#00BBB7");
                try {
                    systemThemeMapper.addThemeColor(themeColorVo);
                }catch (Exception e){
                    e.printStackTrace();
                    return null;
                }
                return themeColorVo;
            }
        }
        return themeColorVo;
    }
    /**
     * 修改系统主题颜色
     */
    @Override
    public ThemeColorVo updateThemeColor() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtils.isNotEmpty(loginUser)) {
            Long userId = loginUser.getUserid();
            Integer allSort = systemThemeMapper.getAllSort();
            Integer themeColorSort = systemThemeMapper.getThemeColorSort(userId);
            if (themeColorSort > 0 && themeColorSort < allSort) {
                themeColorSort = themeColorSort + 1;
            } else if (themeColorSort.equals(allSort)) {
                themeColorSort = 1;
            } else {
                return null;
            }
            systemThemeMapper.updateColor(themeColorSort, userId);
            return getThemeColor();
        }
        return null;
    }
}
