package com.ruoyi.kaohe.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.kaohe.domain.KhItems;
import com.ruoyi.kaohe.domain.KhTemItem;
import com.ruoyi.kaohe.domain.KhVote;
import com.ruoyi.kaohe.service.IKhTemItemService;
import com.ruoyi.kaohe.service.IKhVoteService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
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.KhTemplate;
import com.ruoyi.kaohe.service.IKhTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考核模板Controller
 * 
 * @author hs
 * @date 2025-06-17
 */
@RestController
@RequestMapping("/kaohe/template")
public class KhTemplateController extends BaseController
{
    @Autowired
    private IKhTemplateService khTemplateService;
    @Autowired
    private IKhTemItemService temItemService;
    @Autowired
    private IKhVoteService voteService;

    /**
     * 查询考核模板列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(KhTemplate khTemplate)
    {
        startPage();
        List<KhTemplate> list = khTemplateService.selectKhTemplateList(khTemplate);
        return getDataTable(list);
    }

    /**
     * 导出考核模板列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:template:export')")
    @Log(title = "考核模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KhTemplate khTemplate)
    {
        List<KhTemplate> list = khTemplateService.selectKhTemplateList(khTemplate);
        ExcelUtil<KhTemplate> util = new ExcelUtil<KhTemplate>(KhTemplate.class);
        util.exportExcel(response, list, "考核模板数据");
    }

    /**
     * 获取考核模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('kaohe:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(khTemplateService.selectKhTemplateById(id));
    }

    /**
     * 根据模板查询出考核项及考核项对应的投票
     * @param id
     * @return
     */
    @GetMapping(value = "/getItemVote")
    public AjaxResult getInfo(@RequestParam("id") Long id,@RequestParam("pingceId") Long pingceId)
    {
        KhTemplate khTemplate = khTemplateService.selectKhTemplateById(id);
        List<KhItems> items = khTemplate.getItems();
        for(KhItems item:items){
            KhVote query = new KhVote();
            query.setPingceId(pingceId);
            query.setKhitemId(item.getId());
            List<KhVote> khVotes = voteService.selectKhVoteList(query);
            if(khVotes!=null&&khVotes.size()>0){
                item.setVote(khVotes.get(0));
            }
        }
        return success(khTemplate);
    }

    /**
     * 新增考核模板
     */
    @PreAuthorize("@ss.hasPermi('kaohe:template:add')")
    @Log(title = "考核模板", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody KhTemplate khTemplate)
    {
        khTemplateService.insertKhTemplate(khTemplate);
        //增加关联表
        List<KhItems> items = khTemplate.getItems();
        if(items !=null&& items.size()>0){
            for(KhItems item:items){
                KhTemItem temItem = new KhTemItem();
                temItem.setTemName(khTemplate.getTemName());
                temItem.setTemId(khTemplate.getId());
                temItem.setItemName(item.getItemName());
                temItem.setKhitemId(item.getId());
                temItem.setType(item.getType());
                temItem.setTypeId(item.getTypeId());
                temItemService.insertKhTemItem(temItem);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改考核模板  同时修改模板选项关联表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:template:edit')")
    @Log(title = "考核模板", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody KhTemplate khTemplate)
    {
        khTemplateService.updateKhTemplate(khTemplate);
        List<KhItems> items = khTemplate.getItems();
        if(items !=null&& items.size()>0){
            //删除原来关联
            KhTemItem query = new KhTemItem();
            query.setTemId(khTemplate.getId());
            List<KhTemItem> temItems = temItemService.selectKhTemItemList(query);
            List<Long> ids = temItems.stream().map(KhTemItem::getId).collect(Collectors.toList());
            temItemService.deleteKhTemItemByIds(ids.toArray(new Long[ids.size()]));
            //增加新的关联
            for(KhItems item:items){
                KhTemItem temItem = new KhTemItem();
                temItem.setTemName(khTemplate.getTemName());
                temItem.setTemId(khTemplate.getId());
                temItem.setItemName(item.getItemName());
                temItem.setKhitemId(item.getId());
                temItemService.insertKhTemItem(temItem);
            }
        }
        return  AjaxResult.success();
    }

    /**
     * 删除考核模板
     */
    @PreAuthorize("@ss.hasPermi('kaohe:template:remove')")
    @Log(title = "考核模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(khTemplateService.deleteKhTemplateByIds(ids));
    }
}
