package com.goafanti.voucher.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.JtVoucher;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.voucher.service.VoucherDetailService;
import com.goafanti.voucher.service.VoucherService;

@RestController
@RequestMapping(value = "/api/user/voucher")
public class UserVoucherController extends CertifyApiController {
	 @Resource
	 private VoucherService		voucherService;
	 @Resource
	 private VoucherDetailService	voucherDetailService;
	

	 
	 
	 @RequestMapping(value = "/save" ,method=RequestMethod.POST)
	 public Result saveVoucher(JtVoucher voucher ){
		 Result result=new Result();
		 chack(voucher, result);
		 if (!result.getError().isEmpty())return result;	
		 result.data(voucherService.saveVoucher(voucher));
		 return result;
	 }
	 @RequestMapping(value = "/update" ,method=RequestMethod.POST)
	 public Result updateVoucher(JtVoucher voucher ){
		 Result result=new Result();
		 /*chack(voucher, result);
		 if (!result.getError().isEmpty()) {
			 return result;	
		}
		 if(voucher.getActiveState()==null) {
	    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"活动标识","活动标识"));return result;
	    	}*/
		 result.data(voucherService.updateVoucher(voucher));
		 return result;
	 }
	 @RequestMapping(value = "/delect" ,method=RequestMethod.POST)
	 public Result delectVoucher(String  id ){
		 Result result=new Result();
		 result.data(voucherService.delectVoucher(id));
		 return result;
	 }
	 @RequestMapping(value = "/select" ,method=RequestMethod.GET)
	 public Result selectVoucher(String  id ){
		 Result result=new Result();
		 result.data(voucherService.selectVoucher(id));
		 return result;
	 }
	 @RequestMapping(value = "/selectList" ,method=RequestMethod.GET)
	 public Result selectVoucherList(String name,Integer limitUse,Integer activeState,Integer source,Integer pageNo,Integer pageSize){
		 Result result=new Result();
		 result.data(voucherService.selectVoucherList(name, limitUse, activeState, source, null,pageNo, pageSize));
		 return result;
	 }
	 @RequestMapping(value = "/selectDetailList" ,method=RequestMethod.GET)
	 public Result selectVoucherDetailList(String name,Integer status,String userMobile,Integer source,
			 Integer sort,Integer sortType,Integer pageNo,Integer pageSize){
		 Result result=new Result();
		 result.data(voucherDetailService.selectVoucherDetailList(name, status, userMobile,source,sort, sortType, pageNo, pageSize));
		 return result;
	 }
	 private Result chack(JtVoucher voucher, Result result) {
		
		if(StringUtils.isBlank(voucher.getName())) {
	    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"名称","名称"));return result;
	    	}
		 if(voucher.getSource()==null) {
	    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"来源","来源"));return result;
	    	}
	    if(voucher.getMoney()==null) {
	    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"面额","面额"));return result;
	    	}
	    if(voucher.getDurationDay()==null) {
	    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"使用期限","使用期限"));return result;
	    	}
	    
    
	    return result;
	}
}
