package com.gyee.frame.controller.gen;

import com.gyee.frame.common.conf.redis.CacheConfig;
import com.gyee.frame.model.auto.TsysUser;
import com.gyee.frame.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.gyee.frame.common.base.BaseController;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.auto.Userauthority;
import com.gyee.frame.model.custom.TableSplitResult;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.model.custom.TitleVo;
import com.gyee.frame.service.UserauthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("UserauthorityController")
@Api(value = "")
public class UserauthorityController extends BaseController{
	
	private String prefix = "gen/userauthority";
	@Autowired
	private UserauthorityService userauthorityService;
	
	@GetMapping("view")
	@RequiresPermissions("gen:userauthority:view")
    public String view(ModelMap model)
    {	
		String str="";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	//@Log(title = "集合查询", action = "111")
	@PostMapping("list")
	@RequiresPermissions("gen:userauthority:list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchText){
		PageInfo<Userauthority> page=userauthorityService.list(tablepar,searchText) ; 
		TableSplitResult<Userauthority> result=new TableSplitResult<Userauthority>(page.getPageNum(), page.getTotal(), page.getList()); 
		return  result;
	}
	
	/**
     * 新增
     */

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }
	
	//@Log(title = "新增", action = "111")
	@PostMapping("add")
	@RequiresPermissions("gen:userauthority:add")
	@ResponseBody
	public AjaxResult add(Userauthority userauthority){
		int b=userauthorityService.insertSelective(userauthority);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	//@Log(title = "删除", action = "111")
	@PostMapping("remove")
	@RequiresPermissions("gen:userauthority:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=userauthorityService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查用户
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(Userauthority userauthority){
		int b=userauthorityService.checkNameUnique(userauthority);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}






	@Autowired
	private SysUserService sysUserService;

	/**
	 * 获取用户的权限
	 *
	 */
	@ResponseBody
	@GetMapping("/selectuserauthority")
	public List<String> selectUserauthorityListByUseid(String userName,String password) throws Exception {
		TsysUser tsysUser = sysUserService.selectUserauthorityListByUsernameAndPassward(userName, password);
		List<Userauthority> userauthorities = userauthorityService.selectUserauthorityListByUserid(tsysUser.getId());
		List<String> stringList = new ArrayList<>();
		for(Userauthority userauthority:userauthorities){
			stringList.add(userauthority.getPowerstationid());
		}
		return stringList;
	}


	@Autowired
	private CacheConfig cacheConfig;

	/**
	 * 获取用户的权限
	 *
	 */
	@ResponseBody
	@GetMapping("/getUserSession")
	public String[] getUserSession(HttpServletRequest httpServletRequest) {
		String remoteAddr = httpServletRequest.getRemoteAddr();
		HttpSession session = httpServletRequest.getSession();
		String username = (String)session.getAttribute("username");
		String password = (String)session.getAttribute("password");
		username = cacheConfig.getFromredisToString(remoteAddr+"username");
		password = cacheConfig.getFromredisToString(remoteAddr+"password");
		return new String[]{username,password};
	}


	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("Userauthority", userauthorityService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "修改", action = "111")
    @RequiresPermissions("gen:userauthority:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Userauthority record)
    {
        return toAjax(userauthorityService.updateByPrimaryKeySelective(record));
    }

    
    /**
   	 * 根据主键查询
   	 * 
   	 * @param id
   	 * @return
   	 */
   	@PostMapping("/get/{id}")
   	@ApiOperation(value = "根据id查询唯一", notes = "根据id查询唯一")
   	public Userauthority edit(@PathVariable("id") String id) {
   		return userauthorityService.selectByPrimaryKey(id);
   	}
    

	
}
