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

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

import com.sundot.airport.common.core.domain.BaseLargeScreenQueryParamDto;
import com.sundot.airport.system.domain.SysLargeScreenLearningGrowthDto;
import com.sundot.airport.system.domain.SysLargeScreenLearningGrowthOrganizationalSupportDto;
import com.sundot.airport.system.domain.SysLargeScreenLearningGrowthQueryParamDto;
import lombok.SneakyThrows;
import org.springframework.cache.annotation.Cacheable;
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.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.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.system.domain.SysLearningGrowth;
import com.sundot.airport.system.service.ISysLearningGrowthService;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学习成长Controller
 *
 * @author ruoyi
 * @date 2025-10-22
 */
@RestController
@RequestMapping("/system/growth")
public class SysLearningGrowthController extends BaseController {
    @Autowired
    private ISysLearningGrowthService sysLearningGrowthService;

    /**
     * 查询学习成长列表
     */
    @PreAuthorize("@ss.hasPermi('system:growth:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLearningGrowth sysLearningGrowth) {
        startPage();
        List<SysLearningGrowth> list = sysLearningGrowthService.selectSysLearningGrowthList(sysLearningGrowth);
        return getDataTable(list);
    }

    /**
     * 导出学习成长列表
     */
    @PreAuthorize("@ss.hasPermi('system:growth:export')")
    @Log(title = "学习成长", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLearningGrowth sysLearningGrowth) {
        List<SysLearningGrowth> list = sysLearningGrowthService.selectSysLearningGrowthList(sysLearningGrowth);
        ExcelUtil<SysLearningGrowth> util = new ExcelUtil<SysLearningGrowth>(SysLearningGrowth.class);
        util.exportExcel(response, list, "学习成长数据");
    }

    /**
     * 获取学习成长详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:growth:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysLearningGrowthService.selectSysLearningGrowthById(id));
    }

    /**
     * 新增学习成长
     */
    @PreAuthorize("@ss.hasPermi('system:growth:add')")
    @Log(title = "学习成长", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysLearningGrowth sysLearningGrowth) {
        sysLearningGrowth.setCreateBy(getUsername());
        return toAjax(sysLearningGrowthService.insertSysLearningGrowth(sysLearningGrowth));
    }

    /**
     * 修改学习成长
     */
    @PreAuthorize("@ss.hasPermi('system:growth:edit')")
    @Log(title = "学习成长", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysLearningGrowth sysLearningGrowth) {
        sysLearningGrowth.setUpdateBy(getUsername());
        return toAjax(sysLearningGrowthService.updateSysLearningGrowth(sysLearningGrowth));
    }

    /**
     * 删除学习成长
     */
    @PreAuthorize("@ss.hasPermi('system:growth:remove')")
    @Log(title = "学习成长", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysLearningGrowthService.deleteSysLearningGrowthByIds(ids));
    }

    /**
     * 下载模板
     */
    @Log(title = "学习成长", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:growth:import')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysLearningGrowth> util = new ExcelUtil<SysLearningGrowth>(SysLearningGrowth.class);
        util.importTemplateExcel(response, "学习成长");
    }

    /**
     * 导入数据
     */
    @SneakyThrows
    @Log(title = "学习成长", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:growth:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) {
        ExcelUtil<SysLearningGrowth> util = new ExcelUtil<SysLearningGrowth>(SysLearningGrowth.class);
        List<SysLearningGrowth> list = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysLearningGrowthService.importData(list, updateSupport, operName);
        return success(message);
    }

    /**
     * 能力画像-学习成长
     */
    @Cacheable(
            value = "statistics_list",
            keyGenerator = "statisticsKeyGenerator",
            unless = "!T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isSuccess(#result) ||T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isEmptyData(#result)"
    )
    @GetMapping("/portrait")
    public AjaxResult portrait(SysLargeScreenLearningGrowthQueryParamDto dto) {
        List<SysLargeScreenLearningGrowthDto> result = sysLearningGrowthService.portrait(dto);
        return success(result);
    }

    /**
     * 工作画像-组织支撑-培训测试平均分趋势图
     */
    @Cacheable(
            value = "statistics_list",
            keyGenerator = "statisticsKeyGenerator",
            unless = "!T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isSuccess(#result) ||T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isEmptyData(#result)"
    )
    @GetMapping("/organizationalSupport")
    public AjaxResult organizationalSupport(SysLargeScreenLearningGrowthQueryParamDto dto) {
        List<SysLargeScreenLearningGrowthOrganizationalSupportDto> result = sysLearningGrowthService.organizationalSupport(dto);
        return success(result);
    }

    /**
     * 首页-学习成长-平均分
     */
//    @Cacheable(
//            value = "statistics_learningGrowth_average",
//            keyGenerator = "statisticsKeyGenerator",
//            unless = "!T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isSuccess(#result) ||T(com.sundot.airport.common.cache.StatisticsCacheConditionUtil).isEmptyData(#result)"
//    )
    @GetMapping("/learningGrowthAverage")
    public AjaxResult learningGrowthAverage(BaseLargeScreenQueryParamDto dto) {
        BigDecimal result = sysLearningGrowthService.learningGrowthAverage(dto);
        return success(result);
    }
}
