package com.njcn.serviceimpl.system;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.njcn.mapper.system.ThemeMapper;
import com.njcn.pojo.commons.SimpleValue;
import com.njcn.pojo.system.Theme;
import com.njcn.service.system.ThemeService;

/**
 * @author hongawen
 * @date: 2019/9/24 11:17
 */
@Service
public class ThemeServiceImpl implements ThemeService {

    @Resource
    private ThemeMapper themeMapper;

    /**
     * 获取主题色
     */
    @Override
    public String getThemeColor() {
        Theme theme = new Theme();
        theme.setState(1);
        theme.setActive(1);
        theme=themeMapper.selectOne(theme);
        if(null==theme){
            //返回国网绿
            return "#006565";
        }
        return theme.getThemeColor();
    }

    /**
     * 获取主题信息
     */
    @Override
    public SimpleValue getThemeInfo() {
        SimpleValue simpleValue=new SimpleValue();
        Theme theme = new Theme();
        theme.setState(1);
        theme.setActive(1);
        theme=themeMapper.selectOne(theme);
        if(null==theme){
            //返回国网绿
            simpleValue.setValue("#006565");
            simpleValue.setName("国网");
        }else{
            simpleValue.setValue(theme.getThemeColor());
            simpleValue.setName(theme.getThemeDesc());
        }
        return simpleValue;
    }

    /**
     * 更新主题颜色和描述
     * @param themeDesc 主题描述
     * @param color 主题颜色
     */
    @Override
    public void updateTheme(String themeDesc, String color, String logoName, String faviconName) {
        Theme theme = new Theme();
        
        theme.setState(1);
        theme.setActive(0);
        theme.setThemeDesc(themeDesc);
        theme.setThemeColor(color);
        theme.setThemeIndex(UUID.randomUUID().toString());
        theme.setLogoName(logoName);
        theme.setFaviconName(faviconName);
        themeMapper.insert(theme);
    }
    
    @Override
    public List<Theme> getThemeList(){
    	List<Theme> result = new ArrayList<>();

    	Theme tmp = new Theme();
    	tmp.setState(1);
    	result=themeMapper.select(tmp);
    	if(null==result){
            //返回国网绿
    		tmp.setThemeColor("#006565");
    		tmp.setThemeDesc("国网");
    		tmp.setLogoName("gwlogo");
    		tmp.setFaviconName("gwfavicon");
    		result.add(tmp);
        }
    	return result;
    }
    
    @Override
    public Theme activate(String themeIndex){
    	int flag1 = themeMapper.setAllZero();
    	
    	int flag2 = themeMapper.updateByIndex(themeIndex);
    	
    	return themeMapper.selectByPrimaryKey(themeIndex);
    }
    
    @Override
    public void deleteTheme(String themeIndex){
    	int flag = themeMapper.deleteByPrimaryKey(themeIndex);
    }
}
