package com.hfkj.device.sanmisen;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.entity.BsGasOrder;
import com.hfkj.model.GasClassGroupTaskDataCount;
import com.hfkj.model.GasClassGroupTaskOilCount;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 三木森 小票打印机内容模板
 * @className: PrinterTemplate
 * @author: HuRui
 * @date: 2024/8/8
 **/
public class PrinterTemplate {

    /**
     * 班次统计模板
     * @param dataCount 班次数据统计
     * @param makeUp   是否补打 true:是  flase:否
     * @return
     */
    public static JSONArray classGroupCountTemp(GasClassGroupTaskDataCount dataCount, boolean makeUp) {
        JSONArray contents = new JSONArray();

        JSONObject merName = new JSONObject();
        merName.put("cont", dataCount.getClassNum() + "班结流水" + (makeUp?"（补打）":""));
        merName.put("type", "text");
        merName.put("align", "center");
        merName.put("bold", true);
        contents.add(merName);

        // 空格
        JSONObject blank1 = new JSONObject();
        blank1.put("cont", "");
        blank1.put("type", "text");
        contents.add(blank1);

        // 分割线
        JSONObject divLine1 = new JSONObject();
        divLine1.put("cont", "");
        divLine1.put("type", "div_star");
        contents.add(divLine1);

        // 开始时间
        JSONObject startTime = new JSONObject();
        startTime.put("cont", "开始时间：" + DateUtil.date2String(dataCount.getStartTime(), DateUtil.Y_M_D_HMS));
        startTime.put("type", "text");
        contents.add(startTime);

        // 结束时间
        JSONObject endTime = new JSONObject();
        endTime.put("cont", "结束时间：" + DateUtil.date2String(dataCount.getEndTime(), DateUtil.Y_M_D_HMS));
        endTime.put("type", "text");
        contents.add(endTime);

        // 空格
        JSONObject blank2 = new JSONObject();
        blank2.put("cont", "");
        blank2.put("type", "text");
        contents.add(blank2);

        // 加油金额汇总
        JSONObject totalOilPrice = new JSONObject();
        totalOilPrice.put("cont", "加油金额汇总：" + dataCount.getRefuelPrice() + "元");
        totalOilPrice.put("type", "text");
        contents.add(totalOilPrice);

        // 加油笔数汇总
        JSONObject totalOilNum = new JSONObject();
        totalOilNum.put("cont", "加油笔数汇总：" + dataCount.getRefuelNum() + "笔");
        totalOilNum.put("type", "text");
        contents.add(totalOilNum);

        // 加油升数汇总
        JSONObject totalOilLiters = new JSONObject();
        totalOilLiters.put("cont", "加油升数汇总：" + dataCount.getRefuelLiters() + "升");
        totalOilLiters.put("type", "text");
        contents.add(totalOilLiters);

        // 空格
        JSONObject blank3 = new JSONObject();
        blank3.put("cont", "");
        blank3.put("type", "text");
        contents.add(blank3);

        // 退款金额汇总
        JSONObject totalRefundPrice = new JSONObject();
        totalRefundPrice.put("cont", "退款金额汇总：" + dataCount.getRefundPrice() + "元");
        totalRefundPrice.put("type", "text");
        contents.add(totalRefundPrice);

        // 退款笔数汇总
        JSONObject totalRefundNum = new JSONObject();
        totalRefundNum.put("cont", "退款笔数汇总：" + dataCount.getRefundNum() + "笔");
        totalRefundNum.put("type", "text");
        contents.add(totalRefundNum);

        // 退款升数汇总
        JSONObject totalRefundLiters = new JSONObject();
        totalRefundLiters.put("cont", "退款升数汇总：" + dataCount.getRefundLiters() + "升");
        totalRefundLiters.put("type", "text");
        contents.add(totalRefundLiters);

        // 空格
        JSONObject blank4 = new JSONObject();
        blank4.put("cont", "");
        blank4.put("type", "text");
        contents.add(blank4);

        // 详情
        JSONObject detail = new JSONObject();

        // 详情table标题
        Map<String,Object> thead = new LinkedHashMap<>();
        thead.put("油号", "20%");
        thead.put("金额（元）", "40%");
        thead.put("升数", "25%");
        thead.put("笔数", "15%");
        detail.put("thead", thead);

        JSONArray tbody = new JSONArray();
        for (GasClassGroupTaskOilCount oilCount : dataCount.getGroupTaskOilCountList()) {
            tbody.add(Stream.of(oilCount.getOilNo()+"#",oilCount.getRefuelPrice(),oilCount.getRefuelLiters(),oilCount.getRefuelNum()).collect(Collectors.toList()));
        }
        // 详情内容
        detail.put("tbody", tbody);
        System.out.println(detail.toJSONString());
        contents.add(detail);

        // 分割线
        JSONObject divLine2 = new JSONObject();
        divLine2.put("cont", "");
        divLine2.put("type", "div_star");
        contents.add(divLine2);

        // 打印时间
        JSONObject printTime = new JSONObject();
        printTime.put("cont", DateUtil.date2String(new Date(), DateUtil.Y_M_D_HMS));
        printTime.put("type", "text");
        printTime.put("align", "center");
        contents.add(printTime);

        // 切刀
        JSONObject cut = new JSONObject();
        cut.put("cont", "1");
        cut.put("type", "cut");
        contents.add(cut);

        return contents;
    }

    /**
     * 加油小票
     * @param stubType 存根类型  1：收银员存根   2：客户存根
     * @param gasOrder 加油订单
     * @param makeUp   是否补打 true:是  flase:否
     * @return
     */
    public static JSONArray oilReceipt(Integer stubType, BsGasOrder gasOrder, Map<String, Object> receiptMap, boolean makeUp) {
        JSONArray contents = new JSONArray();
        // 标题
        JSONObject title = new JSONObject();
        title.put("cont", (StringUtils.isNotBlank(MapUtils.getString(receiptMap, "receiptTop"))?MapUtils.getString(receiptMap, "receiptTop"):"嗨加油"));
        title.put("type", "title");
        contents.add(title);

        // 油站名称
        JSONObject merName = new JSONObject();
        merName.put("cont", gasOrder.getMerName() + (makeUp?"（补打）":""));
        merName.put("type", "text");
        merName.put("align", "center");
        merName.put("bold", true);
        contents.add(merName);

        // 存根
        JSONObject stub = new JSONObject();
        stub.put("cont", stubType==1?"（收银员存根）":"（客户存根）");
        stub.put("type", "text");
        stub.put("align", "center");
        contents.add(stub);

        // 分割线
        JSONObject divLine1 = new JSONObject();
        divLine1.put("cont", "");
        divLine1.put("type", "div_star");
        contents.add(divLine1);

        // 订单号
        JSONObject orderNo = new JSONObject();
        orderNo.put("cont", "流水：" + gasOrder.getOrderNo());
        orderNo.put("type", "text");
        contents.add(orderNo);

        // 分割线
        JSONObject divLine2 = new JSONObject();
        divLine2.put("cont", "");
        divLine2.put("type", "div_star");
        contents.add(divLine2);

        // 打印时间
        JSONObject printTime = new JSONObject();
        printTime.put("cont", "打印时间：" + DateUtil.date2String(new Date(), DateUtil.Y_M_D_HMS));
        printTime.put("type", "text");
        contents.add(printTime);

        // 支付时间
        JSONObject payTime = new JSONObject();
        payTime.put("cont", "支付时间：" + DateUtil.date2String(gasOrder.getPayTime(), DateUtil.Y_M_D_HMS));
        payTime.put("type", "text");
        contents.add(payTime);

        // 电话
        JSONObject userPhone = new JSONObject();
        userPhone.put("cont", "电话：" + (StringUtils.isNotBlank(gasOrder.getUserPhone())?gasOrder.getUserPhone().substring(0, 3) + "****" + gasOrder.getUserPhone().substring(7):""));
        userPhone.put("type", "text");
        contents.add(userPhone);

        // 来源
        JSONObject source = new JSONObject();
        source.put("cont", "来源：" + (StringUtils.isNotBlank(MapUtils.getString(receiptMap, "receiptSource"))?MapUtils.getString(receiptMap, "receiptSource"):"嗨加油"));
        source.put("type", "text");
        contents.add(source);

        // 枪号
        JSONObject gunNo = new JSONObject();
        gunNo.put("cont", "油枪：" + gasOrder.getGasGunNo()+"号");
        gunNo.put("type", "text");
        gunNo.put("size", "11");
        contents.add(gunNo);

        // 油品
        JSONObject oilNo = new JSONObject();
        oilNo.put("cont", "油品：" + gasOrder.getGasOilNo()+"#");
        oilNo.put("type", "text");
        oilNo.put("size", "11");
        contents.add(oilNo);

        // 升数
        JSONObject oilLiters = new JSONObject();
        oilLiters.put("cont", "升数：" + gasOrder.getGasOilLiters()+"升");
        oilLiters.put("type", "text");
        oilLiters.put("size", "11");
        contents.add(oilLiters);

        // 升数描述
        JSONObject oilLitersDesc = new JSONObject();
        oilLitersDesc.put("cont", "实际加油升数以油站加油机为准！");
        oilLitersDesc.put("type", "text");
        contents.add(oilLitersDesc);

        // 分割线
        JSONObject divLine3 = new JSONObject();
        divLine3.put("cont", "");
        divLine3.put("type", "div_star");
        contents.add(divLine3);

        // 加油金额
        JSONObject refuelPrice = new JSONObject();
        refuelPrice.put("cont", "加油金额：" + gasOrder.getGasRefuelPrice());
        refuelPrice.put("type", "text");
        refuelPrice.put("size", "11");
        contents.add(refuelPrice);

        // 优惠金额
        JSONObject discountPrice = new JSONObject();
        discountPrice.put("cont", "优惠金额：" + gasOrder.getTotalDeductionPrice());
        discountPrice.put("type", "text");
        discountPrice.put("size", "11");
        contents.add(discountPrice);

        // 分割线
        JSONObject divLine4 = new JSONObject();
        divLine4.put("cont", "");
        divLine4.put("type", "div_star");
        contents.add(divLine4);

        // 实际支付
        JSONObject actualPayment = new JSONObject();
        actualPayment.put("cont", "实际支付：" + gasOrder.getActualPayPrice());
        actualPayment.put("type", "text");
        actualPayment.put("size", "11");
        contents.add(actualPayment);

    /*        // 加油金额标题
            JSONObject refuelPriceTitle = new JSONObject();
            refuelPriceTitle.put("cont", "加油金额");
            refuelPriceTitle.put("type", "text");
            refuelPriceTitle.put("size", "11");
            contents.add(refuelPriceTitle);
            // 加油金额
            JSONObject refuelPrice = new JSONObject();
            refuelPrice.put("cont", "￥" + gasOrder.getGasRefuelPrice() + "元");
            refuelPrice.put("type", "text");
            refuelPrice.put("size", "11");
            contents.add(refuelPrice);*/

        // 分割线
        JSONObject divLine5 = new JSONObject();
        divLine5.put("cont", "");
        divLine5.put("type", "div_star");
        contents.add(divLine5);

        // 小票底部
        JSONObject receiptBottom = new JSONObject();
        receiptBottom.put("cont", (StringUtils.isNotBlank(MapUtils.getString(receiptMap, "receiptBottom"))?MapUtils.getString(receiptMap, "receiptBottom"):"开心又省钱;  来＂嗨加油＂"));
        receiptBottom.put("type", "text");
        receiptBottom.put("align", "center");
        contents.add(receiptBottom);

        JSONObject cut = new JSONObject();
        cut.put("cont", "1");
        cut.put("type", "cut");
        contents.add(cut);

        return contents;
    }
}
