/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lightinit.hsdatagateway.modules.gateway.web;

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

import com.lightinit.hsdatagateway.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lightinit.hsdatagateway.common.config.Global;
import com.lightinit.hsdatagateway.common.persistence.Page;
import com.lightinit.hsdatagateway.common.web.BaseController;
import com.lightinit.hsdatagateway.modules.gateway.entity.Charge;
import com.lightinit.hsdatagateway.modules.gateway.service.ChargeService;

import java.util.Date;

/**
 * 计费Controller
 * @Author: yangrui
 * @version 2019-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/charge")
public class ChargeController extends BaseController {

	@Autowired
	private ChargeService chargeService;
	
	@ModelAttribute
	public Charge get(@RequestParam(required=false) Long id) {
		Charge entity = null;
		if (id != null){
			entity = chargeService.get(id);
		}
		if (entity == null){
			entity = new Charge();
		}
		return entity;
	}
	
	@RequiresPermissions("gateway:charge:view")
	@RequestMapping(value = {"list", ""})
	public String list(Charge charge, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Charge> page = chargeService.findPage(new Page<Charge>(request, response), charge); 
		model.addAttribute("page", page);
		return "modules/gateway/charge/chargeList";
	}

	@RequiresPermissions("gateway:charge:view")
	@RequestMapping(value = "form")
	public String form(Charge charge, Model model) {
		model.addAttribute("charge", charge);
		return "modules/gateway/charge/chargeForm";
	}

	@RequiresPermissions("gateway:charge:edit")
	@RequestMapping(value = "save")
	public String save(Charge charge, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, charge)){
			return form(charge, model);
		}
		if (charge.getId() == null) {
			charge.setCreatedBy(UserUtils.getUser().getName());
			charge.setCreatedAt(new Date());
			/*if ("次数".equals(charge.getChargeMethod())) {
				charge.setUsedValue(0L);
			} else {
				charge.setUsedValue(0L);
			}*/

			charge.setUsedValue(0L);
			charge.setRestValue(charge.getChargeValue());
		}
		String message = "路由ID、应用ID、用户ID重复，操作失败";
		if (chargeService.isRepetition(charge)) {
			chargeService.save(charge);
			message = "保存计费成功";
		}

		addMessage(redirectAttributes, message);
		return "redirect:"+Global.getAdminPath()+"/charge/?repage";
	}
	
	@RequiresPermissions("gateway:charge:edit")
	@RequestMapping(value = "delete")
	public String delete(Charge charge, RedirectAttributes redirectAttributes) {
		charge.setDeletedBy(UserUtils.getUser().getName());
		chargeService.delete(charge);
		addMessage(redirectAttributes, "删除计费成功");
		return "redirect:"+Global.getAdminPath()+"/charge/?repage";
	}

}