package lingtao.net.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.CustomerAward;
import lingtao.net.bean.Msg;
import lingtao.net.bean.SysUser;
import lingtao.net.service.CustomerAwardService;
import lingtao.net.util.PublicMethods;

@Controller
public class CustomerAwardController {

	@Autowired
	private CustomerAwardService customerAwardService;

	// 用户跳转页面
	@RequestMapping("/customerAward/index")
	public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.sendRedirect(request.getContextPath() + "/views/system/customerAward/customerAward.jsp");
	}

	/**
	 * 根据条件查询数据
	 * 
	 */
	@ResponseBody
	@RequestMapping("/getCustomerAward")
	public PageInfo<CustomerAward> getCustomerAward(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, CustomerAward customerAward) {
		PageHelper.startPage(page, limit);
		List<CustomerAward> customerAwardList = customerAwardService.getCustomerAward(customerAward);
		PageInfo<CustomerAward> pageInfo = new PageInfo<CustomerAward>(customerAwardList);
		return pageInfo;
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 */
	@ResponseBody
	@RequestMapping("/deleteDatas")
	public Msg deleteBatch(@RequestParam(value = "ids") String ids, @RequestParam(value = "creators") String creators) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		String[] arrCreators = creators.split(",");
		boolean selfFlag = true;
		for (String creator : arrCreators) {
			if (!user.getRealname().equals(creator)) {
				selfFlag = false;
				break;
			}
		}
		// 超管身份
		boolean flag = new PublicMethods().isSuper();
		// 不是超管
		if (!flag) {
			// 判断是不是自己上传的数据
			if (!selfFlag) {
				return Msg.fail("禁止删除其他人数据！");
			}
		}
		String[] arrIds = ids.split(",");
		// String数组转为Integer数组
		int[] ints = new int[arrIds.length];
		for (int i = 0; i < arrIds.length; i++) {
			ints[i] = Integer.parseInt(arrIds[i]);
		}
		return customerAwardService.deleteBatch(ints);
	}

	/**
	 * 
	 * 文件上传
	 */
	@ResponseBody
	@RequestMapping(value = "/ajaxUpload_customerAward")
	public Msg uploadExcel(@RequestParam("file") MultipartFile file) throws Exception {
		synchronized (this) {
			return customerAwardService.ajaxUploadExcel(file);
		}
	}

	/**
	 * 获取上传过的店铺
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getArardShopname")
	public List<String> getArardShopname() {
		List<String> FilenameList = customerAwardService.getArardShopname();
		return FilenameList;
	}
}
