package com.sifang.modules.sunshine.financialManagement.web;

import java.util.List;
import java.util.Map;

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

import org.apache.commons.lang3.StringUtils;
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 com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.ComUtils;
import com.sifang.modules.sunshine.SystemSetting.utils.RandomUtil;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.financialManagement.service.CrmOrderSiteSettlementService;
import com.sifang.modules.sys.common.persistence.Page;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;

/*
 * 工单的平台给服务商结算记录
 * */
@Controller
@RequestMapping(value = "${adminPath}/yangguang/finance/siteSettlement")
public class CrmOrderSiteSettlementController extends BaseController {

	@Autowired
	private CrmOrderSiteSettlementService crmOrderSiteSettlementService;

	// 跳转到服务商待结工单页面
	@RequestMapping(value = "siteWaitSettlement")
	public String siteWaitSettlement(HttpServletRequest request, Model model) {
		//model.addAttribute("siteList", ComUtils.getSiteList());// 获取服务商列表
		return "modules/yangguang/finance/siteWaitSettlement";
	}

	/*
	 * 加载服务商待结工单列表数据
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ResponseBody
	@RequestMapping(value = "loadWaitSettlementList")
	public Object loadWaitSettlementList(HttpServletRequest request, HttpServletResponse response) {
		Page<Record> page = new Page<Record>(request, response);
		Map<String, String> mapRq = getParams(request);
		if (mapRq != null) {
			if (mapRq.get("limit") != null && StringUtils.isNotBlank(mapRq.get("limit").toString())) {
				page.setPageSize(Integer.valueOf(mapRq.get("limit").toString()));
				page.setPageNo(Integer.valueOf(mapRq.get("page").toString()));
			} else {
				page.setPageSize(20);
				page.setPageNo(1);
			}
		}
		crmOrderSiteSettlementService.loadWaitSettlementList(page, mapRq);
		List<Record> list = page.getList();
		int i = 1;
		for (Record rd : list) {
			rd.set("sort", (page.getPageNo() - 1) * page.getPageSize() + i);// 列表排序
			i++;
		}
		return new LayuiPage(list, page.getCount()).renderJson();
	}

	/*
	 * 跳转到结账页面
	 */
	@RequestMapping(value = "siteSettlePage")
	public String siteSettlePage(HttpServletRequest request, Model model) {
		String siteId = request.getParameter("siteId");
		model.addAttribute("site", ComUtils.getSiteMsg(siteId));
		return "modules/yangguang/finance/siteSettle";
	}


	/*
	 * 服务商待结账单合计数据
	 */
	@ResponseBody
	@RequestMapping(value = "countSettlementDatas")
	public Record countSettlementDatas(HttpServletRequest request) {
		Map<String, String> map = getParams(request);
		return crmOrderSiteSettlementService.countSettlementDatas(map);
	}

	public static void main(String[] args) {
		System.out.println(RandomUtil.getBillingNumber());
	}
}
