package cn.com.countrygarden.bi.main.controller.project;

import java.math.BigDecimal; 
import java.util.Date;
import java.util.regex.Pattern;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.project.ProjectSign;
import cn.com.countrygarden.bi.main.model.project.ProjectSignItem;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.project.ProjectSignItemServer;
import cn.com.countrygarden.bi.main.service.project.ProjectSignService;
import cn.com.countrygarden.bi.main.service.project.impl.vo.ProjectSignItemQuery;
/**
 *  签约项目详情
 * 
 * <pre>
 * 。
 * </pre>
 * 
 * @author developer developer@meicloud.com
 * @version 1.00.00
 * 
 *          <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 */
@RestController("ProjectSignItemController")
@RequestMapping("/api/project/signItem")
public class ProjectSignItemController {
	@Autowired
	ProjectSignItemServer projectSignItemServer;
	@Autowired
    private AccessTokenService accessTokenService;
	
	@RequestMapping(value = "/list", method = {RequestMethod.GET},produces = "application/json;charset=utf-8")
	public Object search( @RequestParam(
			value = "skip", required = false, defaultValue = "") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "") Integer size,
            @RequestParam(value = "organId", required = false) Integer organId,
            @RequestParam(value = "organName", required = false) String organName,
            @RequestParam(value = "projectName", required = false, defaultValue = "") String projectName,
            @RequestParam(value = "signTimeStr", required = false, defaultValue = "") @DateTimeFormat(pattern="yyyy-MM-dd")Date signTimeStr,
            @RequestParam (value = "signTimeEnd", required = false, defaultValue = "") @DateTimeFormat(pattern="yyyy-MM-dd")Date signTimeEnd) {
		ProjectSignItemQuery ps = new ProjectSignItemQuery();
		ps.setOrganId(organId);
		ps.setProjectName(projectName);
		ps.setSignTimeStr(signTimeStr);
		ps.setSignTimeEnd(signTimeEnd);
		ps.setOrganName(organName);
		return Response.response(projectSignItemServer.search(ps,page,size)); 
	}
	 @RequestMapping(value = "/findOne/{id}", method = {RequestMethod.GET}, produces = {"application/json;charset=utf-8"})
	public Object findOne(@PathVariable Integer id) {
		return Response.response(projectSignItemServer.findOne(id));
	}
	 
	 @RequestMapping(value = "/delete/{id}", method = {RequestMethod.GET}, produces = {"application/json;charset=utf-8"})
	public Object delete(@PathVariable Integer id) {
		return Response.response(projectSignItemServer.delete(id));
	}
	 @RequestMapping(value = "/save", method = {RequestMethod.POST}, produces = {"application/json;charset=utf-8"})
	public Object save(  
			
			  @RequestParam(value = "remark", required = false, defaultValue = "0") String remark,
			 
			  HttpServletRequest request, HttpServletResponse response
			  ) {
		 Integer id = null;
		 String businessType  = null;
		 BigDecimal countProportion  = null ;
		 Integer projectSignId = null;
		 BigDecimal ratesArea = null;
		 BigDecimal monthIncome = null;
		 Integer monthNumber = null;
		 BigDecimal price = null;
		 BigDecimal yearIncome = null;
		 Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");  
		 if(  null != request.getParameter("id") &&pattern.matcher(request.getParameter("id")).matches() ) {
			 id = Integer.parseInt(request.getParameter("id"));
		 }
		 if( pattern.matcher(request.getParameter("projectSignId")).matches() ) {
			 projectSignId = Integer.parseInt(request.getParameter("projectSignId"));
		 }
		
			 businessType = request.getParameter("businessTypeName");
		 
		 if( pattern.matcher(request.getParameter("monthNumber")).matches() ) {
			 monthNumber = Integer.parseInt(request.getParameter("monthNumber"));
		 }
		 if( pattern.matcher(request.getParameter("countProportion")).matches() ) {
			 countProportion = new BigDecimal(request.getParameter("countProportion"));
		 }
		 if( pattern.matcher(request.getParameter("ratesArea")).matches() ) {
			 ratesArea = new BigDecimal(request.getParameter("ratesArea"));
		 }
		 if( pattern.matcher(request.getParameter("monthIncome")).matches() ) {
			 monthIncome = new BigDecimal(request.getParameter("monthIncome"));
		 }
		 if( pattern.matcher(request.getParameter("price")).matches() ) {
			 price = new BigDecimal(request.getParameter("price"));
		 }
		 if( pattern.matcher(request.getParameter("yearIncome")).matches() ) {
			 yearIncome = new BigDecimal(request.getParameter("yearIncome"));
		 }
		 String accessToken = request.getParameter("AccessToken");
		  AccessToken token = accessTokenService.findByToken(accessToken);
		ProjectSignItem p = new ProjectSignItem();
		p.setBusinessTypeName(businessType);
		p.setCountProportion(countProportion);
		p.setId(id);
		p.setProjectSignId(projectSignId);
		p.setRatesArea(ratesArea);
		p.setRemark(remark);
		p.setMonthIncome(monthIncome);
		p.setMonthNumber(monthNumber);
		p.setPrice(price);
		p.setIsDelete(0);
	//	old.setProjectSignId(p.getProjectSignId());
		p.setYearIncome(yearIncome);
		
		
		return  Response.response(projectSignItemServer.save(p, token.getUserCode()));
	}
	 
	 @ResponseBody
	    @RequestMapping(value = "/upload", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	    public Result upload(@RequestParam(value = "name", defaultValue = "1", required = true) String name,
	                         @RequestParam(value = "uploadfile", defaultValue = "", required = true) MultipartFile multipartFile,
	                         @RequestParam(value = "month", defaultValue = "", required = true) String month,
	                         HttpServletRequest request, HttpServletResponse response) throws Exception {
	        String accessToken = request.getParameter("AccessToken");
	        AccessToken token = accessTokenService.findByToken(accessToken);
	        if (multipartFile == null) {
				throw new ServiceException("请选择需要上传的文件");
			} else if (name == null) {
				throw new ServiceException("请输入文件名");
			}
	        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
	        if (commonsMultipartFile.isEmpty()) {
	            return Response.responseFail("上传文件为空文件");
	        }
	        String fileName = commonsMultipartFile.getOriginalFilename();
//	        if (fileName.indexOf(".xlsx") <= 0 || fileName.indexOf(".xls") <= 0) {
//	            return Response.responseFail("上传文件应为excel文件格式为：xlsx 或 xls");
//	        }
	        Result excelToObject = projectSignItemServer.excelToObject(multipartFile,name,token.getUserCode(),month);

	        return excelToObject;

	    }
	
}
