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

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

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

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.core.ResultGenerator;
import cn.com.countrygarden.bi.main.dto.AdvertisingResourcesDto;
import cn.com.countrygarden.bi.main.model.ExeclTemplateConstant;
import cn.com.countrygarden.bi.main.model.mediaResource.AdvertisingResources;
import cn.com.countrygarden.bi.main.service.mediaResource.AdvertisingResourcesService;
import cn.com.countrygarden.bi.main.util.DateUtil;
import cn.com.countrygarden.bi.main.util.DownloadExeclTemplate;
import cn.com.countrygarden.bi.main.util.ExcelUtil;

@RestController
@RequestMapping("/api/media/advertising")
public class AdvertisingResourcesController {
	
	@Autowired
	private AdvertisingResourcesService service;
	
	@PostMapping("/findPage")
	public Result findAdvertisingPage(HttpServletRequest request,AdvertisingResourcesDto dto) {
		try {
			String accessToken = request.getParameter("AccessToken");
			return ResultGenerator.genSuccessResult(service.findPage(accessToken,dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取社区媒体基础信息列表错误！");
		}
	}
	
	
	@GetMapping("/findOne")
	public Result findOne(AdvertisingResources po) {
		try {
			return ResultGenerator.genSuccessResult(service.findOne(po));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取单个社区媒体基础信息错误！");
		}
	}
	
	@PostMapping("/saveOne")
	public Result saveOne(HttpServletRequest request,AdvertisingResources po,Result rt) {
		try {
			String accessToken = request.getParameter("AccessToken");
			String fileDirectory = request.getParameter("fileDirectory");
			return service.saveOne(accessToken,po,fileDirectory,rt);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("保存单个社区媒体基础信息错误！");
		}
	}
	
	@PostMapping("/del")
	public Result del(HttpServletRequest request,AdvertisingResourcesDto dto,Result rt) {
		try {
			if (dto.getMediaIds()==null || dto.getMediaIds().length == 0) {
				return ResultGenerator.genFailResult("社区媒体标识为空");
			}
			String accessToken = request.getParameter("AccessToken");
			return ResultGenerator.genSuccessResult(service.del(dto,accessToken));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("删除社区媒体基础信息错误！");
		}
	}
	
	@PostMapping("/editState")
	public Result editState(AdvertisingResourcesDto dto) {
		try {
			if (dto.getMediaIds()==null || dto.getMediaIds().length == 0) {
				return ResultGenerator.genFailResult("社区媒体标识为空");
			}
			return ResultGenerator.genSuccessResult(service.editState(dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("修改社区媒体基础信息状态错误！");
		}
	}
	
	
	@GetMapping("/exportOut")
	public void export(HttpServletRequest request,HttpServletResponse response,AdvertisingResourcesDto dto) {
		try {
			if (StringUtils.isNotBlank(dto.getDownloadTemplate())) {
				DownloadExeclTemplate.downloadTemplate(response, ExeclTemplateConstant.mediaResourcesTemplate);
			}else {
				String accessToken = request.getParameter("AccessToken");
				String fileName="社区媒体基础信息"+ DateUtil.simpleFormat(new Date());
				String[] title = {"物业区域","物业区域编号","物业项目","项目编号","媒体类型","媒体形式","媒体编号","地理位置","媒体位置2","媒体规格(宽mm)","媒体规格(高mm)",
						 "单位","使用状态"};
				List<Map<String,Object>> exportData = service.findAdvertising(accessToken,dto);
				String[] fields = {"organName","organCode","commName","commId","mediaType","mediaForm","mediaCode","siteOne","siteTwo",
								   "mediaWidth","mediaHeight","mediaUnit","mediaStatus"};
				ExcelUtil.exportExcel(fileName, "sheet1", null, title, exportData, response, fields);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}   
	
	
	@PostMapping("/exportIn")
	public Result exportIn(HttpServletRequest request,Result rt,@RequestParam("file")MultipartFile file) {
		try {
			String accessToken = request.getParameter("AccessToken");
			return service.exportIn(accessToken,rt,file);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("导入社区媒体基础信息错误");
		}
	}
	
	@PostMapping("/singleUpload")
	public Result singleUpload(Result rt,AdvertisingResourcesDto dto,@RequestParam("file")MultipartFile file) {
		try {
			return service.singleUpload(rt,dto,file);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("上传社区媒体基础信息附件错误");
		}
	}
	
	@GetMapping("/viewAdjunct")
	public Result viewAdjunct(AdvertisingResourcesDto dto) {
		try {
			if (StringUtils.isEmpty(dto.getMediaId())) {
				return ResultGenerator.genFailResult("社区媒体基础信息标识为空");
			}
			return ResultGenerator.genSuccessResult(service.viewAdjunct(dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("查看社区媒体基础信息附件错误");
		}
	}
	
	@GetMapping("/findCommAddress")
	public Result findCommAddress(AdvertisingResourcesDto dto) {
		try {
			if (StringUtils.isEmpty(dto.getCommName()) || StringUtils.isEmpty(dto.getOrganName())) {
				return ResultGenerator.genSuccessResult("");
			}
			return ResultGenerator.genSuccessResult(service.getCommAddress(dto));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取项目地址失败");
		}
	}
	
	@PostMapping("/delFile")
	public Result delFile(@RequestParam Map<String, String> map) {
		try {
			return ResultGenerator.genSuccessResult(service.delFile(map));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("删除社区媒体基础信息附件错误");
		}
	}
	

	@GetMapping("/findMediaType")
	public Result findMediaType() {
		try {
			return ResultGenerator.genSuccessResult(service.findMediaType());
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取媒体类型失败");
		}
	}
	
	@GetMapping("/findMediaForm")
	public Result findMediaForm(String key) {
		try {
			return ResultGenerator.genSuccessResult(service.findMediaForm(key));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult("获取媒体形式失败");
		}
	}
	
	public static void main(String[] args) {
		/*LocalDate today = LocalDate.now();
		LocalDate parse = LocalDate.parse("2019-01-09");
		System.out.println(parse.getLong(field));*/
		
		
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    String firstDtStr = sdf.format(new Date(2019, 1, 10));
	    String secentDtStr = sdf.format(new Date(2019, 1, 28));
	    LocalDate localDate1 = LocalDate.parse(firstDtStr);
	    LocalDate localDate2 = LocalDate.parse(secentDtStr);
	    System.out.println(localDate1.until(localDate2, ChronoUnit.DAYS));*/
		
		//List<String> list = new ArrayList<>();
		LocalDate startDate = LocalDate.parse("2019-02-27");
		LocalDate endDate = LocalDate.parse("2019-03-09");
		long distance = ChronoUnit.DAYS.between(startDate, endDate);
		Stream.iterate(startDate, d -> {return d.plusDays(1);}).limit(distance + 1).forEach(f -> {
			System.out.println(f);//list.add(f.toString());
	    });
		
		/*ZoneId zoneId = ZoneId.systemDefault();
		LocalDate localDate = LocalDate.now();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        Date date = Date.from(zdt.toInstant());
        System.out.println(date);*/
        
		//System.out.println(list);
		
		/*Stream<Star> stream = list.stream().sorted(Comparator.comparing(Star::getScore));// .forEach(p ->
        stream.forEach(p -> System.out.println(p));*/

        
        DateTime dt = DateTime.now();
        System.err.println(dt.toString("yyyy-MM-dd HH:mm:ss"));
	}
	
}
