package com.sundot.airport.web.controller.personnel;

import com.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.core.page.TableDataInfo;
import com.sundot.airport.personnel.domain.PersonnelMonthlyAssessment;
import com.sundot.airport.personnel.service.IPersonnelMonthlyAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 干部月度考核Controller
 *
 * @author ruoyi
 * @date 2026-05-07
 */
@RestController
@RequestMapping("/personnel/cadre-assessment")
public class PersonnelMonthlyAssessmentController extends BaseController {
    @Autowired
    private IPersonnelMonthlyAssessmentService personnelMonthlyAssessmentService;

    /**
     * 查询干部月度考核列表
     */
    @PreAuthorize("@ss.hasPermi('personnel:assessment:list')")
    @GetMapping("/list")
    public TableDataInfo list(PersonnelMonthlyAssessment personnelMonthlyAssessment) {
        startPage();
        List<PersonnelMonthlyAssessment> list = personnelMonthlyAssessmentService.selectPersonnelMonthlyAssessmentList(personnelMonthlyAssessment);
        return getDataTable(list);
    }

    /**
     * 导出干部月度考核列表
     */
    @PreAuthorize("@ss.hasPermi('personnel:assessment:export')")
    @Log(title = "干部月度考核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PersonnelMonthlyAssessment personnelMonthlyAssessment) {
        List<PersonnelMonthlyAssessment> list = personnelMonthlyAssessmentService.selectPersonnelMonthlyAssessmentList(personnelMonthlyAssessment);
        ExcelUtil<PersonnelMonthlyAssessment> util = new ExcelUtil<PersonnelMonthlyAssessment>(PersonnelMonthlyAssessment.class);
        util.exportExcel(response, list, "干部月度考核数据");
    }

    /**
     * 获取干部月度考核详细信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:assessment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(personnelMonthlyAssessmentService.selectPersonnelMonthlyAssessmentById(id));
    }

    /**
     * 新增干部月度考核
     */
    @PreAuthorize("@ss.hasPermi('personnel:assessment:add')")
    @Log(title = "干部月度考核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PersonnelMonthlyAssessment personnelMonthlyAssessment) {
        personnelMonthlyAssessment.setCreateBy(getUsername());
        return toAjax(personnelMonthlyAssessmentService.insertPersonnelMonthlyAssessment(personnelMonthlyAssessment));
    }

    /**
     * 修改干部月度考核
     */
    @PreAuthorize("@ss.hasPermi('personnel:assessment:edit')")
    @Log(title = "干部月度考核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PersonnelMonthlyAssessment personnelMonthlyAssessment) {
        personnelMonthlyAssessment.setUpdateBy(getUsername());
        return toAjax(personnelMonthlyAssessmentService.updatePersonnelMonthlyAssessment(personnelMonthlyAssessment));
    }

    /**
     * 删除干部月度考核
     */
    @PreAuthorize("@ss.hasPermi('personnel:assessment:remove')")
    @Log(title = "干部月度考核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(personnelMonthlyAssessmentService.deletePersonnelMonthlyAssessmentByIds(ids));
    }

    /**
     * 生成本月考核数据
     * @param year 年份
     * @param month 月份
     */
    @PreAuthorize("@ss.hasPermi('personnel:assessment:generate')")
    @Log(title = "生成本月考核", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public AjaxResult generate(@RequestParam Integer year, @RequestParam Integer month) {
        try {
            int count = personnelMonthlyAssessmentService.generateMonthlyAssessment(year, month);
            return success(String.format("成功生成/更新%d条考核记录！", count));
        } catch (Exception e) {
            return error("生成考核数据失败：" + e.getMessage());
        }
    }
}
