package com.ruoyi.kaohe.controller;

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

import com.ruoyi.kaohe.domain.*;
import com.ruoyi.kaohe.service.IKhPcEmpService;
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.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.IKhPingceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考核评测Controller
 * 
 * @author hs
 * @date 2025-06-17
 */
@RestController
@RequestMapping("/kaohe/pingce")
public class KhPingceController extends BaseController
{
    @Autowired
    private IKhPingceService khPingceService;
    @Autowired
    private IKhPcEmpService pcEmpService;

    /**
     * 查询考核评测列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:pingce:list')")
    @GetMapping("/list")
    public TableDataInfo list(KhPingce khPingce)
    {
        startPage();
        List<KhPingce> list = khPingceService.selectKhPingceList(khPingce);
        return getDataTable(list);
    }

    /**
     * 导出考核评测列表
     */
    @PreAuthorize("@ss.hasPermi('kaohe:pingce:export')")
    @Log(title = "考核评测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KhPingce khPingce)
    {
        List<KhPingce> list = khPingceService.selectKhPingceList(khPingce);
        ExcelUtil<KhPingce> util = new ExcelUtil<KhPingce>(KhPingce.class);
        util.exportExcel(response, list, "考核评测数据");
    }

    /**
     * 获取考核评测详细信息
     */
    @PreAuthorize("@ss.hasPermi('kaohe:pingce:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(khPingceService.selectKhPingceById(id));
    }

    /**
     * 新增考核评测
     */
    @PreAuthorize("@ss.hasPermi('kaohe:pingce:add')")
    @Log(title = "考核评测", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody KhPingce khPingce)
    {
        khPingceService.insertKhPingce(khPingce);
        //增加关联表
        List<Bkhdx> pcEmps = khPingce.getPcBkhdxs();
        if(pcEmps !=null&& pcEmps.size()>0){
            for(Bkhdx item:pcEmps){
                KhPcEmp pcEmp = new KhPcEmp();
               pcEmp.setPcId(khPingce.getId());
               pcEmp.setBkhdxId(item.getBkhdxId());
               pcEmp.setBkhdxName(item.getBkhdxName());
               pcEmp.setDeptId(item.getDeptId());
               pcEmp.setDeptName(item.getDeptName());
                pcEmpService.insertKhPcEmp(pcEmp);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改考核评测
     */
    @PreAuthorize("@ss.hasPermi('kaohe:pingce:edit')")
    @Log(title = "考核评测", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody KhPingce khPingce)
    {
        khPingceService.updateKhPingce(khPingce);

        List<Bkhdx> pcEmps = khPingce.getPcBkhdxs();
        if(pcEmps !=null&& pcEmps.size()>0){
            //删除原来关联
            KhPcEmp query = new KhPcEmp();
            query.setPcId(khPingce.getId());
            List<KhPcEmp> temItems = pcEmpService.selectKhPcEmpList(query);
            List<Long> ids = temItems.stream().map(KhPcEmp::getId).collect(Collectors.toList());
            if(ids!=null&&ids.size()>0){
                pcEmpService.deleteKhPcEmpByIds(ids.toArray(new Long[ids.size()]));
            }
            //增加新的关联
            for(Bkhdx item:pcEmps){
                KhPcEmp pcEmp = new KhPcEmp();
                pcEmp.setPcId(khPingce.getId());
                pcEmp.setBkhdxId(item.getBkhdxId());
                pcEmp.setBkhdxName(item.getBkhdxName());
                pcEmp.setDeptId(item.getDeptId());
                pcEmp.setDeptName(item.getDeptName());
                pcEmpService.insertKhPcEmp(pcEmp);
            }
        }
        return  AjaxResult.success();
    }

    /**
     * 删除考核评测
     */
    @PreAuthorize("@ss.hasPermi('kaohe:pingce:remove')")
    @Log(title = "考核评测", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for(Long id:ids){
            KhPingce khPingce = khPingceService.selectKhPingceById(id);
            if(!khPingce.getState().equals("0")){
                return AjaxResult.error("评测："+khPingce.getPcTitle()+" 不是未开始状态，不允许删除");
            }
        }
        return toAjax(khPingceService.deleteKhPingceByIds(ids));
    }
}
