package com.pj.project4sp.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pj.project4sp.SP;
import com.pj.utils.sg.AjaxError;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;

import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: sp_config -- 配置信息表
 * @author click33 
 */
@RestController
@RequestMapping("/SpConfig/")
public class SpConfigController {

	/** 底层 Mapper 对象 */
	@Autowired
	SpConfigMapper spConfigMapper;

	/** 增 */  
	@RequestMapping("add")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(SpConfig s){
		SpConfigUtil.check(s);
		spConfigMapper.add(s);
		s = spConfigMapper.getById(SP.publicMapper.getPrimarykey());
		return AjaxJson.getSuccessData(s);
	}

	/** 删 */  
	@RequestMapping("delete")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	public AjaxJson delete(Long id){
		int line = spConfigMapper.delete(id);
		return AjaxJson.getByLine(line);
	}
	
	/** 删 - 根据id列表 */  
	@RequestMapping("deleteByIds")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class); 
		int line = SP.publicMapper.deleteByIds(SpConfig.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}
	
	/** 改 */  
	@RequestMapping("update")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	public AjaxJson update(SpConfig s){
		SpConfigUtil.check(s);
		int line = spConfigMapper.update(s);
		SpConfigUtil.clearCache(s.getName());
		return AjaxJson.getByLine(line);
	}

	/** 批量修改配置 JSON格式： {name: value} */  
	@RequestMapping("updateConfig")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson updateConfig(){
		// 逐条修改 
		SoMap so = SoMap.getRequestSoMap();
		for (String name : so.keySet()) {
			// 改库 
			int line = SP.publicMapper.updateColumnBy(SpConfig.TABLE_NAME, "value", so.get(name), "name", name);
			AjaxError.notLine(line, "修改配置出错：" + name + "=" + so.get(name));
			// 清空缓存 
			SpConfigUtil.clearCache(name);
		}
		// 返回 
		return AjaxJson.getSuccess();
	}

	/** 查 - 根据id */  
	@RequestMapping("getById")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	public AjaxJson getById(Long id){
		SpConfig s = spConfigMapper.getById(id);
		return AjaxJson.getSuccessData(s);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	@RequestMapping("getList")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	public AjaxJson getList() { 
		SoMap so = SoMap.getRequestSoMap();
		List<SpConfig> list = spConfigMapper.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/** 查指一组配置，根据配置名称集合 */  
	@RequestMapping("getListByNames")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	public AjaxJson getListByNames() { 
		List<String> names = SoMap.getRequestSoMap().getListByComma("names", String.class);
		List<SpConfig> list = spConfigMapper.getListByNames(names);
		return AjaxJson.getSuccessData(list);
	}

	/** 查询所有分组 */  
	@RequestMapping("getGroupList")
	@SaCheckPermission(SpConfig.PERMISSION_CODE)
	public AjaxJson getGroupList() { 
		List<String> list = spConfigMapper.getGroupList();
		return AjaxJson.getSuccessData(list);
	}
	
}
