package com.pj.project.sys_client;

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.AjaxJson;
import com.pj.utils.so.SoMap;

import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: sys_client -- 应用表
 * @author shengzhang 
 */
@RestController
@RequestMapping("/SysClient/")
public class SysClientController {

	/** 底层 Mapper 对象 */
	@Autowired
	SysClientMapper sysClientMapper;

	/** 增 */  
	@RequestMapping("add")
	@SaCheckPermission(SysClient.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(SysClient s){
		SysClientUtil.check(s);
		try {
			sysClientMapper.add(s);
			s = sysClientMapper.getById(SP.publicMapper.getPrimarykey());
			return AjaxJson.getSuccessData(s);
		} finally {
			SysClientUtil.clearAllowUrls();
		}
	}

	/** 删 */  
	@RequestMapping("delete")
	@SaCheckPermission(SysClient.PERMISSION_CODE)
	public AjaxJson delete(Long id){
		try {
			int line = sysClientMapper.delete(id);
			return AjaxJson.getByLine(line);
		} finally {
			SysClientUtil.clearAllowUrls();
		}
	}
	
	/** 删 - 根据id列表批量删除 */  
	@RequestMapping("deleteByIds")
	@SaCheckPermission(SysClient.PERMISSION_CODE)
	public AjaxJson deleteByIds(){
		try {
			List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class); 
			int line = SP.publicMapper.deleteByIds(SysClient.TABLE_NAME, ids);
			return AjaxJson.getByLine(line);
		} finally {
			SysClientUtil.clearAllowUrls();
		}
	}
	
	/** 改 */  
	@RequestMapping("update")
	@SaCheckPermission(SysClient.PERMISSION_CODE)
	public AjaxJson update(SysClient s){
		try {
			int line = sysClientMapper.update(s);
			return AjaxJson.getByLine(line);
		} finally {
			SysClientUtil.clearAllowUrls();
		}
	}

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

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

	/** 查sso所有授权url */  
	@RequestMapping("getAllowUrls")
	@SaCheckPermission(SysClient.PERMISSION_CODE)
	public AjaxJson getAllowUrls() { 
		return AjaxJson.getSuccessData(SysClientUtil.getAllowUrls());
	}
	
	/** 改 - 应用状态(1=启用, 2=禁用) */  
	@RequestMapping("updateStatus")
	@SaCheckPermission(SysClient.PERMISSION_CODE)
	public AjaxJson updateStatus(Long id, Integer value){
		try {
			int line = SP.publicMapper.updateColumnById(SysClient.TABLE_NAME, "status", value, id);
			return AjaxJson.getByLine(line);
		} finally {
			SysClientUtil.clearAllowUrls();
		}
	}
	
}
