package com.bootdo.data.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.data.domain.DataApplyDO;
import com.bootdo.data.service.DataApplyService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 数据备案表
 * 
 * @author admin
 * @email admin@163.com
 * @date 2022-03-01 14:23:15
 */
 
@Controller
@RequestMapping("/data/dataApply")
public class DataApplyController {

	private static Logger log = LoggerFactory.getLogger(DataApplyController.class);

	@Autowired
	private DataApplyService dataApplyService;
	
	@GetMapping()
	@RequiresPermissions("data:dataApply:dataApply")
	String DataApply(){
	    return "data/dataApply/dataApply";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("data:dataApply:dataApply")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DataApplyDO> dataApplyList = dataApplyService.list(query);
		int total = dataApplyService.count(query);
		PageUtils pageUtils = new PageUtils(dataApplyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("data:dataApply:add")
	String add(){
	    return "data/dataApply/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("data:dataApply:edit")
	String edit(@PathVariable("id") Long id,Model model){
		DataApplyDO dataApply = dataApplyService.get(id);
		model.addAttribute("dataApply", dataApply);
	    return "data/dataApply/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("data:dataApply:add")
	public R save( DataApplyDO dataApply){
		UserDO currUser = ShiroUtils.getUser();
		log.info("currUser:" + currUser.toString());
		if (null == currUser) {
			dataApply.setUploadUserid("-1");
			dataApply.setUploadUser("获取用户信息为空");
		} else {
			dataApply.setUploadUserid(String.valueOf(ShiroUtils.getUserId()));
			dataApply.setUploadUser(ShiroUtils.getUser().getUsername());
		}
		dataApply.setStatus("0");
		if(dataApplyService.save(dataApply)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("data:dataApply:edit")
	public R update( DataApplyDO dataApply){
		dataApplyService.update(dataApply);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("data:dataApply:remove")
	public R remove( Long id){
		if(dataApplyService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("data:dataApply:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		dataApplyService.batchRemove(ids);
		return R.ok();
	}
	
}
