package com.qmth.sop.server.api;

import com.google.common.collect.ImmutableMap;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.report.FlowMsgReferReportBean;
import com.qmth.sop.business.service.FlowMsgReferReportService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.MessageTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api(tags = "流程检查统计Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_FLOW_MSG_REFER)
public class FlowMsgReferReportController {

    public static final String READ_TOTAL = "readTotal";

    public static final String UN_READ_TOTAL = "unReadTotal";

    public static final String READ_RATE = "readRate";

    public static final String UN_READ_RATE = "unReadRate";

    @Resource
    FlowMsgReferReportService flowMsgReferReportService;

    @ApiOperation(value = "大区经理查阅统计")
    @RequestMapping(value = "/area_manager/report", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "流程查阅提醒报表", response = FlowMsgReferReportBean.class) })
    public Result areaManagerReport(@ApiParam(value = "服务单元id", required = true) @RequestParam Long serviceId) {
        return ResultUtil.ok(
                this.calculateRate(flowMsgReferReportService.FlowMsgReport(serviceId, MessageTypeEnum.OFFICE_SOP)));
    }

    @ApiOperation(value = "区域协调人查阅统计")
    @RequestMapping(value = "/regional_coordinator/report", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "流程查阅提醒报表", response = FlowMsgReferReportBean.class) })
    public Result regionalCoordinatorReport(@ApiParam(value = "服务单元id", required = true) @RequestParam Long serviceId) {
        return ResultUtil.ok(
                this.calculateRate(flowMsgReferReportService.FlowMsgReport(serviceId, MessageTypeEnum.CLOUD_MARK_SOP)));
    }

    /**
     * 计算比率
     *
     * @param flowMsgReferReportBeanList
     * @return
     */
    protected Map<String, Object> calculateRate(List<FlowMsgReferReportBean> flowMsgReferReportBeanList) {
        Integer total = flowMsgReferReportBeanList.stream().mapToInt(s -> s.getSum()).sum();
        Integer readMsgTotal = flowMsgReferReportBeanList.stream().mapToInt(s -> s.getReadMsg()).sum();
        Integer unReadMsgTotal = flowMsgReferReportBeanList.stream().mapToInt(s -> s.getUnReadMsg()).sum();
        BigDecimal readRate = new BigDecimal(readMsgTotal);
        BigDecimal unReadRate = new BigDecimal(unReadMsgTotal);
        if (Objects.nonNull(total) && total.intValue() > 0) {
            readRate = readRate.divide(new BigDecimal(total), 2, BigDecimal.ROUND_HALF_UP).multiply(SystemConstant.RATE)
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            unReadRate = unReadRate.divide(new BigDecimal(total), 2, BigDecimal.ROUND_HALF_UP)
                    .multiply(SystemConstant.RATE).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return new ImmutableMap.Builder<String, Object>().put(ResourceSecurityReportController.TOTAL, total)
                .put(READ_TOTAL, readMsgTotal).put(UN_READ_TOTAL, unReadMsgTotal).put(READ_RATE, readRate)
                .put(UN_READ_RATE, unReadRate).put(ResourceSecurityReportController.RECORDS, flowMsgReferReportBeanList)
                .build();
    }
}
