package com.sifang.modules.order.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.order.entity.OrderCollections;
import com.sifang.modules.order.service.OrderCollectionsService;
import com.sifang.modules.platform.utils.PlantformUtils;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 工单收款Controller
 * @author Ivan
 * @version 2019-11-04
 */
@Controller
@RequestMapping(value = "${adminPath}/order/orderCollections")
public class OrderCollectionsController extends BaseController {
	@Autowired
	private OrderCollectionsService orderCollectionsService;

	@RequestMapping(value = "headList")
	public String getHeadList(HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
		String startTime = formatter.format(date) + "-01 00:00:00";
		model.addAttribute("date", date);
		model.addAttribute("startTime", startTime);
		model.addAttribute("userType", user.getUserType());
		model.addAttribute("siteList", PlantformUtils.getAllSiteList());
		return "modules/yangguang/finance/rechargeDetail";
	}

	@RequestMapping(value = "orderCollectionsList")
	@ResponseBody
	public String orderCollectionsList(OrderCollections orderCollections, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, String> map = getParams(request);
		LayuiPage page= orderCollectionsService.orderCollectionsList(new LayuiPage(request) , map);
		return page.renderJson();
	}

	@RequestMapping(value = "sumAmount")
	@ResponseBody
	public Map<String, Object> sumAmount(OrderCollections orderCollections, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, String> map = getParams(request);
		BigDecimal sumamount = orderCollectionsService.orderCollectionsSumamount(map);
		BigDecimal sumcommission = orderCollectionsService.commissionSumanount(map);
		Map<String, Object> amountMap = new HashMap<>();
		amountMap.put("sumamount", sumamount);
		amountMap.put("sumcommission", sumcommission);
		return amountMap;
	}

	@RequestMapping(value = "getDetailbyid")
	@ResponseBody
	public Record getDetailbyid(HttpServletRequest request, HttpServletResponse response, String id) {
		return orderCollectionsService.getDetailbyid(id);
	}

	@RequestMapping(value = "confirmdz")
	@ResponseBody
	public String confirmdz(HttpServletRequest request, HttpServletResponse response, String id) {
		return orderCollectionsService.confirmdz(id);
	}

}
