package cn.hmsoft.pay.control;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.frame.control.FrameControl;
import cn.hmsoft.helper.excel.Excel2007Writer;
import cn.hmsoft.jdbc.entity.DateFormatType;
import cn.hmsoft.pay.constants.PayChannel;
import cn.hmsoft.pay.data.dao.PayBillDao;
import cn.hmsoft.pay.data.model.PayBill;
import cn.hmsoft.pay.data.model.PayBillAlipay;
import cn.hmsoft.pay.impl.weixin.entity.PayBillWeixin;

@RestController
public class PayBillControl extends FrameControl {
	@Autowired
	private PayBillDao daoPay;

	@RequestMapping("/pay/bill/page")
	public Ajax page(String query, Integer limit, Integer start, String order, String type) {
		return new Ajax(this.daoPay.pageBill(start, limit, query, this.getQueryOrder(order, type)));
	}

	@RequestMapping("/pay/bill/find")
	public Ajax find(int bill_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		PayBill bill = this.daoPay.findByBillId(bill_id);
		map.put("PayBill", bill);
		if (PayChannel.WeiXin.equals(bill.getChannel_id())) {
			map.put("BillDetail", this.daoPay.find(PayBillWeixin.class, bill.getBill_id()));
		} else if (PayChannel.AliPay.equals(bill.getChannel_id())) {
			map.put("BillDetail", this.daoPay.find(PayBillAlipay.class, bill.getBill_id()));
		} else {
			map.put("BillDetail", null);
		}
		return new Ajax(map);
	}

	private String REPORT_CHANNEL = "select d.channel_id,d.channel_name,sum(bill_amount) bill_amount,max(bill_time) max_bill_time,min(bill_time) min_bill_time, "
			+ "max(reply_time) max_reply_time,min(reply_time) min_reply_time,max(pay_time) max_pay_time,min(pay_time) min_pay_time "
			+ "from pay_bill p,pay_channel_define d where d.channel_id=p.channel_id and bill_status='Complete' "
			+ "group by d.channel_id,d.channel_name";

	@RequestMapping("/pay/bill/report/channel")
	public Ajax reportWithChannel() {
		return new Ajax(this.daoPay.listMapBySql(REPORT_CHANNEL));
	}

	@RequestMapping("/pay/bill/report/channel/download")
	public void reportWithChannelDownload() {
		List<Map<String, Object>> array = this.daoPay.listMapBySql(REPORT_CHANNEL);
		Map<String, Integer> titleMap = Excel2007Writer.createTitleMap("#", 8);
		titleMap.put("渠道编号", 10);
		titleMap.put("渠道名称", 14);
		titleMap.put("缴费金额", 15);
		titleMap.put("创建订单区间", 40);
		titleMap.put("支付时间区间", 40);
		List<List<Object>> values = new ArrayList<List<Object>>();
		int i = 1;
		for (Map<String, Object> map : array) {
			List<Object> vArray = new ArrayList<Object>();
			vArray.add(i++);
			vArray.add(map.get("channel_id"));
			vArray.add(map.get("channel_name"));
			int amount = Integer.parseInt(map.get("bill_amount").toString());
			NumberFormat currency = NumberFormat.getCurrencyInstance();// 建立货币格式化引用
			// NumberFormat percent = NumberFormat.getPercentInstance();
			/// percent.setMaximumFractionDigits(3);
			vArray.add(currency.format(amount / 100));
			vArray.add(map.get("min_bill_time") + " - " + map.get("max_bill_time"));
			vArray.add(map.get("min_pay_time") + " - " + map.get("max_pay_time"));
			values.add(vArray);

		}
		Excel2007Writer.downloadAsExcel(this.getResponse(), "缴费订单汇总-按渠道汇总.xlsx", values, titleMap);
	}

	@RequestMapping("/pay/bill/report/date")
	public Ajax reportWithDate(Integer limit, Integer start) {
		String sql = "select " + this.daoPay.formatDate("pay_time", DateFormatType.ShortDate)
				+ " pay_date,count(1) bill_total,sum(bill_amount) bill_amount from pay_bill p where bill_status='Complete' group by "
				+ this.daoPay.formatDate("pay_time", DateFormatType.ShortDate) + "  order by "
				+ this.daoPay.formatDate("pay_time", DateFormatType.ShortDate);
		return new Ajax(this.daoPay.pageMapBySql(start, limit, sql));
	}

	@RequestMapping("/pay/bill/report/date/download")
	public void reportWithDateDownload() {

		String sql = "select " + this.daoPay.formatDate("pay_time", DateFormatType.ShortDate)
				+ " pay_date,count(1) bill_total,sum(bill_amount) bill_amount from pay_bill p where bill_status='Complete' group by "
				+ this.daoPay.formatDate("pay_time", DateFormatType.ShortDate) + "  order by "
				+ this.daoPay.formatDate("pay_time", DateFormatType.ShortDate);
		List<Map<String, Object>> array = this.daoPay.listMapBySql(sql);
		Map<String, Integer> titleMap = Excel2007Writer.createTitleMap("#", 8);
		titleMap.put("缴费日期", 15);
		titleMap.put("订单笔数", 10);
		titleMap.put("订单总金额", 20);
		List<List<Object>> values = new ArrayList<List<Object>>();
		int i = 1;
		for (Map<String, Object> map : array) {
			List<Object> vArray = new ArrayList<Object>();
			vArray.add(i++);
			vArray.add(map.get("pay_date"));
			vArray.add(map.get("bill_total"));
			int amount = Integer.parseInt(map.get("bill_amount").toString());
			NumberFormat currency = NumberFormat.getCurrencyInstance();// 建立货币格式化引用
			// NumberFormat percent = NumberFormat.getPercentInstance();
			/// percent.setMaximumFractionDigits(3);
			vArray.add(currency.format(amount / 100));
			values.add(vArray);

		}
		Excel2007Writer.downloadAsExcel(this.getResponse(), "缴费订单汇总-按日期.xlsx", values, titleMap);
	}
	
	/**************
	 *   按照批次统计
	 */
	@RequestMapping("/pay/bill/report/batch")
	public Ajax requestWithBatch() {
		String sql = " select BUSINESS_TYPE,sum(bill_amount)  bill_amount from pay_bill p where BILL_STATUS='Complete' group by BUSINESS_TYPE ";
		return new Ajax(this.daoPay.listMapBySql(sql));
	}
}
