package com.jwipc.spe.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.jwipc.dto.ExportDataDTO;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.spe.constant.QuotaType;
import com.jwipc.spe.dto.PlanFieldDTO;
import com.jwipc.spe.dto.SupAppraiseTemplateSaveDTO;
import com.jwipc.spe.dto.SupTemQuotaDTO;
import com.jwipc.spe.logic.SupAppraiseTemplateLogic;
import com.jwipc.spe.logic.SupTemDimensionLogic;
import com.jwipc.spe.logic.SupTemQuotaLogic;
import com.jwipc.spe.model.query.SupAppraiseTemplateQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.jwipc.spe.constant.ModuleConst.MODULE_ROOT;

/**
 * @author Al
 * @create 2024-02-28 16:01
 * 考核指标
 */
@RestController
@RequestMapping(MODULE_ROOT + "/supAppraiseTemplate/rest/")
@RequiredArgsConstructor
public class SupAppraiseTemplateRestController {
    private final SupAppraiseTemplateLogic supAppraiseTemplateLogic;
    private final SupTemQuotaLogic supTemQuotaLogic;
    private final SupTemDimensionLogic supTemDimensionLogic;

    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(SupAppraiseTemplateQuery query) {
        return supAppraiseTemplateLogic.findByPage(query);
    }

    /**
     * 启用或停用考核模板数据
     *
     * @param ids    数据ID
     * @param status 1:启用 2:停用
     * @return 结果
     */
    @PostMapping("/invalidOrEffective")
    public ResponseEntity invalidOrEffectiveTarget(String ids, Integer status) {
        final CurrentUserDTO user = LoginUtil.getCurrentUser();
        final boolean b = supAppraiseTemplateLogic.invalidOrEffectiveTarget(ids, status, user);
        if (!b) {
            return ResponseEntity.badRequest().body("操作失败");
        }
        return ResponseEntity.ok(StrUtil.EMPTY);
    }

    /**
     * 考核模板下指标项目数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @GetMapping("/supTemQuota/list")
    public PageView SupTemQuotaList(SupAppraiseTemplateQuery query) {
        return supTemQuotaLogic.selectDataByQuery(query);
    }

    /**
     * 考核模板下维度项目数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @GetMapping("/supTemDimension/list")
    public PageView SupTemDimensionList(SupAppraiseTemplateQuery query) {
        return supTemDimensionLogic.selectDataByQuery(query);
    }

    /**
     * 保存考核模板数据
     *
     * @param tem 考核模板数据
     * @return 结果
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody SupAppraiseTemplateSaveDTO tem)
    {
        final boolean b = supAppraiseTemplateLogic.saveOrUpdate(tem);
        if (!b) {
            return ResponseEntity.badRequest().body("保存考核模板数据失败");
        }
        return ResponseEntity.ok(StrUtil.EMPTY);
    }

    @GetMapping("listAll")
    public ResponseEntity listAll(String types) {
        return ResponseEntity.ok(supAppraiseTemplateLogic.listAll(types));
    }

    @GetMapping("exportTemplate")
    public void exportTemplate(HttpServletResponse response, Long templateId, String date) throws IOException {
        List<SupTemQuotaDTO> exportFiled = supAppraiseTemplateLogic.getField(templateId);
        List<ExportDataDTO> list = Lists.newArrayList();
        //需要添加固定字段
        ExportDataDTO a = ExportDataDTO.builder().field("date").title("考核期间").header("考核期间")
                .color(IndexedColors.YELLOW.getIndex()).build();
        ExportDataDTO b = ExportDataDTO.builder().field("supplierCode").title("供应商编码").header("供应商编码")
                .color(IndexedColors.YELLOW.getIndex())
                .build();
        ExportDataDTO c = ExportDataDTO.builder().field("supplierName").title("供应商名称").header("供应商名称")
                .color(IndexedColors.YELLOW.getIndex())
                .build();

        list.add(a);
        list.add(b);
        list.add(c);
        for (SupTemQuotaDTO temQuotaDTO : exportFiled) {
            ExportDataDTO tmp = ExportDataDTO.builder().field(temQuotaDTO.getQuotaCode())
                    .title(temQuotaDTO.getQuotaDesc()).header(temQuotaDTO.getDimensionName()).build();
            final Integer quotaType = temQuotaDTO.getQuotaType();
            if (quotaType == QuotaType.IMPORT.getValue()) {
                tmp.setColor(IndexedColors.YELLOW.getIndex());
            } else {
                tmp.setColor(IndexedColors.GREY_50_PERCENT.getIndex());
            }
            list.add(tmp);
        }
        //需要查询mes的数据
        List<Map<String, String>> result = supAppraiseTemplateLogic.getMesData(date);
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,
                "计划导入模板-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN), "sheet",
                result, list, true);
    }

    @GetMapping("getFieldByPlan")
    public ResponseEntity getFieldByPlan(Long planId) {
        List<PlanFieldDTO> planFieldDTOS = supAppraiseTemplateLogic.getFieldByPlan(planId);
        return ResponseEntity.ok(planFieldDTOS);
    }
}
