package com.ruoyi.kaohe.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.kaohe.domain.*;
import com.ruoyi.kaohe.service.IKhVoteEmpService;
import com.ruoyi.kaohe.service.IKhVoteService;
import com.ruoyi.kaohe.util.EnhancedJsonExporter;
import com.ruoyi.kaohe.util.PinYinUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kaohe.service.IKhKhrwResultService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考核结果Controller
 *
 * @author hs
 * @date 2025-07-05
 */
@RestController
@RequestMapping("/kaohe/kh_result")
public class KhKhrwResultController extends BaseController {
    @Autowired
    private IKhKhrwResultService khKhrwResultService;
    @Autowired
    private IKhVoteEmpService voteEmpService;
    @Autowired
    private IKhVoteService khVoteService;

    /**
     * 生成考核结果
     */
    @PreAuthorize("@ss.hasPermi('kaohe:kh_result:generate')")
    @PostMapping("/generateResult")
    public AjaxResult generateResult(KhPingce pc) {   //传递参数  id、pcTitle、bkhdxtype
        boolean isUpdate=false;
        //查询是否已经生成结果
        KhKhrwResult resultQuery = new KhKhrwResult();
        resultQuery.setPcId(pc.getId());
        List<KhKhrwResult> khKhrwResults = khKhrwResultService.selectKhKhrwResultList(resultQuery);
        if(khKhrwResults!=null&&khKhrwResults.size()>0){
            isUpdate=true;
            pc.setPcResultId(khKhrwResults.get(0).getId());
        }
       return khKhrwResultService.generateResult(pc,isUpdate);
    }

    /**
     * 查询考核结果列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:kh_result:list')")
    @GetMapping("/list")
    public TableDataInfo list(KhKhrwResult khKhrwResult) {
        startPage();
        List<KhKhrwResult> list = khKhrwResultService.selectKhKhrwResultList(khKhrwResult);
        return getDataTable(list);
    }

    /**
     * 导出考核结果列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:kh_result:export')")
    @Log(title = "考核结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KhKhrwResult khKhrwResult) {
        List<KhKhrwResult> list = khKhrwResultService.selectKhKhrwResultList(khKhrwResult);
        ExcelUtil<KhKhrwResult> util = new ExcelUtil<KhKhrwResult>(KhKhrwResult.class);
        util.exportExcel(response, list, "考核结果数据");
    }
    /**
     * 根据考核结果id导出具体考核结果
     */
    @PreAuthorize("@ss.hasPermi('kaohe:kh_result:export')")
    @Log(title = "考核结果", businessType = BusinessType.EXPORT)
    @PostMapping("/exportById")
    public void exportById(HttpServletResponse response, Long id) throws Exception {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("汇总数据.xlsx", "UTF-8"));
        KhKhrwResult khrwResult = khKhrwResultService.selectKhKhrwResultById(id);
        String tableHeader = khrwResult.getTableHeader();
        String tableData = khrwResult.getTableData();
        EnhancedJsonExporter.exportWithMergedHeader(tableHeader,tableData,response);
    }

    /**
     * 获取考核结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('kaohe:kh_result:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(khKhrwResultService.selectKhKhrwResultById(id));
    }

    /**
     * 新增考核结果
     */
    @PreAuthorize("@ss.hasPermi('kaohe:kh_result:add')")
    @Log(title = "考核结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KhKhrwResult khKhrwResult) {
        return toAjax(khKhrwResultService.insertKhKhrwResult(khKhrwResult));
    }

    /**
     * 修改考核结果
     */
    @PreAuthorize("@ss.hasPermi('kaohe:kh_result:edit')")
    @Log(title = "考核结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KhKhrwResult khKhrwResult) {
        return toAjax(khKhrwResultService.updateKhKhrwResult(khKhrwResult));
    }

    /**
     * 删除考核结果
     */
    @PreAuthorize("@ss.hasPermi('kaohe:kh_result:remove')")
    @Log(title = "考核结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(khKhrwResultService.deleteKhKhrwResultByIds(ids));
    }
}
