package com.fs.project.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.fs.common.config.LoginContextManager;
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.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.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.project.domain.ProjectSettings;
import com.fs.project.service.IProjectSettingsService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 项目设置Controller
 *
 * @author fs
 * @date 2026-01-12
 */
@RestController
@RequestMapping("/project/settings")
public class ProjectSettingsController extends BaseController
{
    @Autowired
    private IProjectSettingsService projectSettingsService;

    /**
     * 查询项目设置列表
     */
    @PreAuthorize("@ss.hasPermi('project:settings:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectSettings projectSettings)
    {
        startPage();
        List<ProjectSettings> list = projectSettingsService.selectProjectSettingsList(projectSettings);
        return getDataTable(list);
    }

    /**
     * 导出项目设置列表
     */
    @PreAuthorize("@ss.hasPermi('project:settings:export')")
    @Log(title = "项目设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProjectSettings projectSettings)
    {
        List<ProjectSettings> list = projectSettingsService.selectProjectSettingsList(projectSettings);
        ExcelUtil<ProjectSettings> util = new ExcelUtil<ProjectSettings>(ProjectSettings.class);
        return util.exportExcel(list, "项目设置数据");
    }

    /**
     * 获取项目设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:settings:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(projectSettingsService.selectProjectSettingsById(id));
    }

    /**
     * 新增项目设置
     */
    @PreAuthorize("@ss.hasPermi('project:settings:add')")
    @Log(title = "项目设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectSettings projectSettings)
    {
        return toAjax(projectSettingsService.insertProjectSettings(projectSettings));
    }

    /**
     * 修改项目设置
     */
    @PreAuthorize("@ss.hasPermi('project:settings:edit')")
    @Log(title = "项目设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectSettings projectSettings)
    {
        return toAjax(projectSettingsService.updateProjectSettings(projectSettings));
    }

    /**
     * 删除项目设置
     */
    @PreAuthorize("@ss.hasPermi('project:settings:remove')")
    @Log(title = "项目设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectSettingsService.deleteProjectSettingsByIds(ids));
    }

    /**
     * 查询指定公司下的项目设置列表
     */
    @GetMapping("/company/{companyId}")
    public AjaxResult listByCompanyId(@PathVariable String companyId)
    {
        return AjaxResult.success(projectSettingsService.selectProjectSettingsByCompanyId(companyId));
    }



    /**
     * 查询项目设置具体配置
     */
    @PreAuthorize("@ss.hasPermi('project:settings:list')")
    @GetMapping("/getSettingTaskTypeList")
    public AjaxResult getprojectSetting(ProjectSettings projectSettings) throws Exception {
        startPage();
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        projectSettings.setCompanyId(currentCompanyId);
        List<ProjectSettings> list = projectSettingsService.selectProjectSettingsList(projectSettings);
        ProjectSettings projectSettings1 = list.get(0);
        if(Objects.isNull(projectSettings1)){
            throw new Exception("当前部门下未配置项目设置，请先配置");
        }
        //逗号分割的任务类型
        String inHisTaskTypes = projectSettings1.getInHisTaskTypes();
        List<Integer> taskTypeIdList = Arrays.stream(inHisTaskTypes.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        return AjaxResult.success(taskTypeIdList);
    }
}
