package com.hotwj.platform.reportStatistics.provider.driver;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hotwj.platform.driverManagement.driver.domain.vo.HotDriverVo;
import com.hotwj.platform.driverManagement.driver.service.IHotDriverService;
import com.hotwj.platform.driverManagement.driverRewardPunishment.domain.bo.HotDriverRewardPunishmentBo;
import com.hotwj.platform.driverManagement.driverRewardPunishment.domain.vo.HotDriverRewardPunishmentVo;
import com.hotwj.platform.driverManagement.driverRewardPunishment.service.IHotDriverRewardPunishmentService;
import com.hotwj.platform.reportStatistics.service.IDriverFilePrintService;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

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

/**
 * 驾驶员奖惩记录表数据提供者
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class RewardPunishmentRecordProvider implements IReportDataProvider {

    private final IHotDriverService driverService;
    private final IHotDriverRewardPunishmentService rewardPunishmentService;

    @Override
    public String getTemplateCode() {
        return "templates/driver/rewardPunishmentRecord.html.vm";
    }

    @Override
    public String getReportType() {
        return IDriverFilePrintService.TYPE_REWARD_PUNISHMENT_RECORD;
    }

    @Override
    public String getReportTitle() {
        return "奖惩记录表";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        String driverId = MapUtil.getStr(params, "driverId");
        if (StrUtil.isBlank(driverId)) {
            return new HashMap<>();
        }

        HotDriverVo driver = driverService.queryById(driverId);
        if (driver == null) {
            return new HashMap<>();
        }

        Map<String, Object> ctx = new HashMap<>();
        ctx.put("name", driver.getName());
        ctx.put("idCardNo", driver.getIdCardNo());

        HotDriverRewardPunishmentBo bo = new HotDriverRewardPunishmentBo();
        bo.setDriverId(driverId);
        List<HotDriverRewardPunishmentVo> list = rewardPunishmentService.queryList(bo);

        List<Map<String, Object>> rows = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            for (HotDriverRewardPunishmentVo vo : list) {
                Map<String, Object> m = new HashMap<>();
                m.put("eventDate", vo.getEventDate() != null ? DateUtil.format(vo.getEventDate(), "yyyy-MM-dd") : "");

                String categoryLabel = StringUtils.isBlank(vo.getCategory()) ? "" : vo.getCategory();
                m.put("item", categoryLabel);

                String decisionText = vo.getDecision();
                if (StringUtils.isBlank(decisionText)) {
                    decisionText = vo.getReason();
                }
                m.put("decision", decisionText);
                rows.add(m);
            }
        }
        ctx.put("rows", rows);

        return ctx;
    }
}
