package com.ruoyi.kaohe.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.kaohe.domain.KhPcEmp;
import com.ruoyi.kaohe.domain.KhPingce;
import com.ruoyi.kaohe.domain.KhVoteEmp;
import com.ruoyi.kaohe.service.IKhPcEmpService;
import com.ruoyi.kaohe.service.IKhVoteEmpService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
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.domain.KhVote;
import com.ruoyi.kaohe.service.IKhVoteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 投票评测Controller
 *
 * @author hs
 * @date 2025-06-17
 */
@RestController
@RequestMapping("/kaohe/vote")
public class KhVoteController extends BaseController {
    @Autowired
    private IKhVoteService khVoteService;
    @Autowired
    private IKhPcEmpService pcEmpService;
    @Autowired
    private IKhVoteEmpService voteEmpService;

    /*@Scheduled(cron = "0 * * * * ?") // 每分钟的第0秒执行一次   监测时间修改评分状态
    public void performTask() {
        System.out.println("进入定时任务-------------------------------------------------------------");
        //获取所有未完成的评分任务
        List<KhVote> list = khVoteService.selectUnfinishedVote();
        for (KhVote vote : list) {
            Date sTime = vote.getsTime();
            Date eTime = vote.geteTime();
            String state = vote.getState();
            // 获取当前日期和时间
            Date now = new Date();
            // 根据当前时间与开始、结束时间关系 修改状态
            if (state.equals("0") && (now.after(sTime) && now.before(eTime))) {
                vote.setState("1");
                khVoteService.updateKhVote(vote);
                System.out.println("执行定时任务: " + System.currentTimeMillis() + " 修改评分任务： " + vote.getVoteTitle() + "  状态为进行中");
            }
            if (now.after(eTime)) {
                vote.setState("2");
                khVoteService.updateKhVote(vote);
                System.out.println("执行定时任务: " + System.currentTimeMillis() + " 修改评分任务： " + vote.getVoteTitle() + "  状态为已完成");
            }
        }
    }*/

    /**
     * 查询投票评测列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:vote:list')")
    @GetMapping("/list")
    public TableDataInfo list(KhVote khVote) {
        startPage();
        List<KhVote> list = khVoteService.selectKhVoteList(khVote);
        return getDataTable(list);
    }

    /**
     * 首页根据pingceId、itemIds、state查询投票评测列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:vote:list')")
    @GetMapping("/listByConditons")
    public TableDataInfo getListByConditons(KhVote khVote) {
        startPage();
        List<KhVote> list = khVoteService.getListByPcIdAndItemIds(khVote);
        return getDataTable(list);
    }

    /**
     * 导出投票评测列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:vote:export')")
    @Log(title = "投票评测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KhVote khVote) {
        List<KhVote> list = khVoteService.selectKhVoteList(khVote);
        ExcelUtil<KhVote> util = new ExcelUtil<KhVote>(KhVote.class);
        util.exportExcel(response, list, "投票评测数据");
    }

    /**
     * 获取投票评测详细信息
     */
    @PreAuthorize("@ss.hasPermi('kaohe:vote:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(khVoteService.selectKhVoteById(id));
    }

    /**
     * 新增投票评测
     */
    @PreAuthorize("@ss.hasPermi('kaohe:vote:add')")
    @Log(title = "投票评测", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody KhVote khVote) {
        khVoteService.insertKhVote(khVote);
        //根据评测id查询职工评测关联 职工
        KhPcEmp query = new KhPcEmp();
        query.setPcId(khVote.getPingceId());
        List<KhPcEmp> pcEmps = pcEmpService.selectKhPcEmpList(query);
        BigDecimal initv= new BigDecimal("O");
        for (KhPcEmp pcEmp : pcEmps) {
            //新增投票选项关联表
            KhVoteEmp voteEmp = new KhVoteEmp();
            voteEmp.setBkhdxId(pcEmp.getBkhdxId());
            voteEmp.setBkhdxName(pcEmp.getBkhdxName());
            voteEmp.setVoteId(khVote.getId());
            voteEmp.setVoteTitle(khVote.getVoteTitle());
            voteEmp.setPercentage(khVote.getPercentage());
            voteEmp.setKhitemTypeid(khVote.getKhitemTypeid());
            if(khVote.getKhitemTypeid().equals(1L)){
                voteEmp.setOptionA(initv);
                voteEmp.setOptionB(initv);
                voteEmp.setOptionC(initv);
                voteEmp.setOptionD(initv);
            }
            voteEmpService.insertKhVoteEmp(voteEmp);
        }
        return AjaxResult.success();
    }

    /**
     * 修改投票评测
     */
    @PreAuthorize("@ss.hasPermi('kaohe:vote:edit')")
    @Log(title = "投票评测", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KhVote khVote) {
        return toAjax(khVoteService.updateKhVote(khVote));
    }

    /**
     * 删除投票评测
     */
    @PreAuthorize("@ss.hasPermi('kaohe:vote:remove')")
    @Log(title = "投票评测", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(khVoteService.deleteKhVoteByIds(ids));
    }
}
