package com.fjs.scenic.controller.ord;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.fjs.scenic.annotation.LoggerManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fjs.scenic.entity.order.ErrorOrder;
import com.fjs.scenic.service.ord.ErrorOrderService;
import com.fjs.scenic.utils.ReturnResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/errorOrders/")
public class ErrorOrderController {

	@Autowired
	private ErrorOrderService errorOrderService;
	
	/**
	 * 进入errorOrders页面
	 * 
	 * @param session
	 * @return
	 */
	@GetMapping("index")
	public ModelAndView index(HttpSession session) {
		return new ModelAndView("/ord/ErrorOrders");
	}

	/**
	 * 查询所有异常订单
	 * 
	 * @param manage
	 *            条件
	 * @param currentPage
	 *            页码
	 * @param pageSize
	 *            每页显示数量
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("selectPage")
	@ResponseBody
	@LoggerManage(logDescription = "查询异常订单列表")
	public ReturnResult selectPage(@RequestParam(required=false) String startTime, @RequestParam(required=false) String endTime,
			ErrorOrder record, int currentPage, int pageSize) throws Exception {
		  currentPage = (currentPage==0)?1:currentPage;
	        pageSize = (pageSize==0)?1:pageSize;
	        PageHelper.startPage(currentPage, pageSize);
	        
	        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String str = " where";
	        
	        //根据订单号模糊查询
			if(record.getOrderNo() != "" && record.getOrderNo() != null){
				str += " e.order_no like '%" + record.getOrderNo() + "%'";
			}else{
				//避免前端缓存 传值为 ""||null
				record.setOrderNo(null);
			}
			
			//根据订单类型查询
			if(record.getType() != null){
				if(!" where".equals(str)){
					str += " and ";
				}

				str += " e.type like '%" + record.getType() + "%'";
			}
			
			List<ErrorOrder> userList = null;
			//根据时间区间查询
			if((startTime != null && !"".equals(startTime)) || (endTime != null  && !"".equals(endTime))){
				
				//开始时间为空 默认为一个月以前 -- 结束时间为空默认当前时间
				Calendar c = Calendar.getInstance();
				if(startTime == null || "".equals(startTime)){
					c.setTime(df.parse(endTime));
					c.add(Calendar.MONTH, -1);
					Date mouthLater = c.getTime();
					startTime = df.format(mouthLater);
				}
				if(endTime == null || "".equals(endTime)){
					endTime = df.format(new Date());
				}
				
				startTime = df.format(df.parse(startTime));
				endTime = df.format(df.parse(endTime));
				
				if(!" where".equals(str)){
					str += " and ";
				}

				str += " e.time between '" + startTime + "' and '" + endTime + "'";
			}
			
			if(!" where".equals(str)){
				str += " order by e.time desc";
				userList = errorOrderService.selectByJoin(null, str);
			}else{
				userList = errorOrderService.select(record);
			}
	        PageInfo<ErrorOrder> pageInfo = new PageInfo<>(userList);
	        return ReturnResult.ok(pageInfo);
	}
	
	/**
	 * 根据ID查询新闻
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("get/{id}")
	@ResponseBody
	public ReturnResult get(@PathVariable("id") Integer id) {
		return ReturnResult.ok(errorOrderService.select(new ErrorOrder().setId(id)));
	}
	
	/**
	 * 删除异常订单
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("del")
	@ResponseBody
	@LoggerManage(logDescription = "删除异常订单")
	public ReturnResult del(Integer id) {
		errorOrderService.delete(new ErrorOrder().setId(id));
		return ReturnResult.ok();
	}
	
	/**
	 * 更新异常订单
	 * 
	 * @return
	 */
	@PostMapping("update")
	@ResponseBody
	@LoggerManage(logDescription = "更新异常订单")
	public ReturnResult update(ErrorOrder user) {
		errorOrderService.update(user);
		return ReturnResult.ok();
	}
}
