package com.willalp.web.controller.report;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.willalp.common.file.ReportExportFileSer;
import com.willalp.canteen.service.IHsCanteenOrderService;
import com.willalp.integral.service.IIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Print")
public class printController {

    @Autowired
    private ReportExportFileSer reportExportFileSer;

    @Autowired
    private IIntegralService integralService;

    @Autowired
    private IHsCanteenOrderService hsCanteenOrderService;

    @GetMapping(path = "/pjPrint")
    public void hospitalChargesDailyPrint(String req, HttpServletResponse response) {

//        QueryReq queryReq = JSONUtil.toBean(req,QueryReq.class);
//        System.out.println(queryReq);
//        PrintVo printVo = getDataFromDataBase(queryReq);
//        Map<String, Object> map = BeanUtil.beanToMap(printVo.getPrintMap());
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
//        printVo.getPrintList().stream().forEach(o -> {
//            list.add(BeanUtil.beanToMap(o));
//        });
        reportExportFileSer.reportHtmlForStream(list, map, "jrxml/report2.jasper", response);

    }

    /**
     * 积分充值打印
     *
     * @param req
     * @param response
     */
    @GetMapping(path = "/integralReport")
    public void integralReport(String req, HttpServletResponse response){
        Map<String,Object> params = JSONUtil.toBean(req,Map.class);
        List<Map<String,Object>> list = integralService.getIntegralReportList(params);

        Map<String,Object> map = new HashMap<>();
        map.put("begintime",params.get("begin"));
        map.put("endtime",params.get("end"));
        map.put("name",params.get("name"));
        map.put("printtime", DateUtil.now());
        //计算合计
        BigDecimal integral_cz = BigDecimal.ZERO;
        BigDecimal integral_tk = BigDecimal.ZERO;
        BigDecimal jzed_cz = BigDecimal.ZERO;
        BigDecimal jzed_tk = BigDecimal.ZERO;
        BigDecimal yj_cz = BigDecimal.ZERO;
        BigDecimal yj_tk = BigDecimal.ZERO;
        for (Map o:list) {
            integral_cz = integral_cz.add(new BigDecimal(String.valueOf(o.get("list_czjf"))));
            integral_tk = integral_tk.add(new BigDecimal(String.valueOf(o.get("list_tkjf"))));
            jzed_cz = jzed_cz.add( new BigDecimal(String.valueOf(o.get("list_czed"))));
            jzed_tk = jzed_tk.add( new BigDecimal(String.valueOf(o.get("list_tked"))));
            yj_cz = yj_cz.add( new BigDecimal(String.valueOf(o.get("list_czyj"))));
            yj_tk = yj_tk.add( new BigDecimal(String.valueOf(o.get("list_tkyj"))));
        }
        //积分
        map.put("jfcz",integral_cz);
        map.put("jfth",integral_tk);
        map.put("jfhj",integral_cz.add(integral_tk));
        //额度
        map.put("edcz",jzed_cz);
        map.put("edth",jzed_tk);
        map.put("edhj",jzed_cz.add(jzed_tk));
        //押金
        map.put("yjcz",yj_cz);
        map.put("yjth",yj_tk);
        map.put("yjhj",yj_cz.add(yj_tk));

        reportExportFileSer.reportHtmlForStream(list, map, "jrxml/jfczjl_czy.jasper", response);
    }

    /**
     * 消费记录打印
     *
     * @param req
     * @param response
     */
    @GetMapping(path = "/consumeReport")
    public void consumeReport(String req, HttpServletResponse response){
        Map<String,Object> params = JSONUtil.toBean(req,Map.class);
        List<Map<String,Object>> list = hsCanteenOrderService.getConsumeReport(params);

        Map<String,Object> map = new HashMap<>();
        map.put("begintime", params.get("begin"));
        map.put("endtime", params.get("end"));
        map.put("printtime", DateUtil.today());
        BigDecimal jfxf = BigDecimal.ZERO;
        BigDecimal jfth = BigDecimal.ZERO;
        BigDecimal edxf = BigDecimal.ZERO;
        BigDecimal edth = BigDecimal.ZERO;
        for (Map o : list) {
            jfxf = jfxf.add(new BigDecimal(String.valueOf(o.get("list_czjf"))));
            jfth = jfth.add(new BigDecimal(String.valueOf(o.get("list_tkjf"))));
            edxf = edxf.add(new BigDecimal(String.valueOf(o.get("list_czed"))));
            edth = edth.add(new BigDecimal(String.valueOf(o.get("list_tked"))));
        }
        map.put("jfxf", jfxf);
        map.put("jfth", jfth);
        map.put("edxf", edxf);
        map.put("edth", edth);
        map.put("jfhj", jfxf.add(jfth));
        map.put("edhj", edxf.add(edth));

        reportExportFileSer.reportHtmlForStream(list, map, "jrxml/st_sfjl.jasper", response);
    }
}
