package com.yingpaipay.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.yingpaipay.business.domain.bo.*;
import com.yingpaipay.business.domain.excel.ProjectChineseExcel;
import com.yingpaipay.business.domain.excel.ProjectEnglishExcel;
import com.yingpaipay.business.domain.excel.ProjectExcel;
import com.yingpaipay.business.domain.vo.*;
import com.yingpaipay.business.service.IDocumentService;
import com.yingpaipay.business.service.IFolderService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysUserService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import com.yingpaipay.business.service.IProjectService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 项目管理
 *
 * @author Huanyi
 * @date 2025-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/management")
public class ProjectController extends BaseController {

    private final IProjectService projectService;
    private final IFolderService folderService;
    private final ISysUserService userService;
    private final IDocumentService documentService;

    /**
     * 查询项目管理列表
     */
    @SaCheckPermission("project:management:list")
    @GetMapping("/list")
    public TableDataInfo<ProjectVo> list(ProjectBo bo, PageQuery pageQuery) {
        return projectService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出项目管理列表
     */
//    @SaCheckPermission("project:management:export")
//    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(ProjectBo bo, HttpServletResponse response) {
//        List<ProjectVo> list = projectService.queryList(bo);
//        ExcelUtil.exportExcel(list, "项目管理", ProjectVo.class, response);
//    }

    /**
     * 获取项目管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:management:query")
    @GetMapping("/{id}")
    public R<ProjectVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(projectService.queryById(id));
    }

    /**
     * 新增项目管理
     */
    @SaCheckPermission("project:management:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProjectBo bo) {
        return toAjax(projectService.insertByBo(bo));
    }

    /**
     * 修改项目管理
     */
    @SaCheckPermission("project:management:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProjectBo bo) {
        return toAjax(projectService.updateByBo(bo));
    }

    /**
     * 删除项目管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("project:management:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(projectService.deleteWithValidByIds(List.of(ids), true));
    }

    @GetMapping("/getByName")
    public R<List<ProjectListByNameVo>> getListByName(String name) {
        return R.ok(projectService.getListByName(name));
    }

    @GetMapping("/listOnUser")
    public R<List<ProjectListByNameVo>> getList() {
        return R.ok(projectService.getList());
    }

    @SaCheckPermission("project:management:updateStatus")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateStatus")
    public R<Void> updateStatus(@RequestBody ProjectUpdateStatusBo bo) {
        return toAjax(projectService.updateStatus(bo));
    }

    @SaCheckPermission("project:management:queryProjectMember")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @GetMapping("/queryProjectMember")
    public TableDataInfo<ProjectMemberListVo> queryProjectMember(Long id, PageQuery pageQuery) {
        return projectService.selectMemberListById(id, pageQuery);
    }

    @GetMapping("/queryMemberNotInCenter")
    public TableDataInfo<ProjectMemberNotInCenterVo> queryMemberNotInCenter(@RequestParam("projectId") Long projectId, @RequestParam("folderId") Long folderId, @RequestParam("name") String name, PageQuery pageQuery) {
        return projectService.queryMemberNotInCenter(projectId, folderId, name, pageQuery);
    }

    @SaCheckPermission("project:management:queryCenterInfo")
    @GetMapping("/queryCenterInfo")
    public TableDataInfo<ProjectCenterListVo> queryCenterInfo(ProjectCenterListBo bo, PageQuery pageQuery) {
        return folderService.queryCenterInfo(bo, pageQuery);
    }

    @SaCheckPermission("project:management:queryProjectMemberAddMember")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/queryProjectMemberAddMember")
    public R<Void> queryProjectMemberAddMember(@RequestBody ProjectAddMemberBo bo) {
        return toAjax(projectService.insertUser(bo));
    }

    @GetMapping("/getFolders")
    public R<ProjectFolderVo> getFolders(@RequestParam("userId") Long userId, @RequestParam("projectId") Long projectId) {
        return R.ok(projectService.getFoldersByUserId(userId, projectId));
    }

    @SaCheckPermission("project:management:queryProjectMemberAssignFolders")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/queryProjectMemberAssignFolders")
    public R<Void> queryProjectMemberAssignFolders(@RequestBody ProjectMemberUpdateFoldersBo bo) {
        return toAjax(projectService.updateFolders(bo));
    }

    @SaCheckPermission("project:management:queryCenterMember")
    @GetMapping("/queryCenterMember")
    public TableDataInfo<ProjectCenterMemberListVo> queryCenterMember(ProjectCenterMemberListBo bo, PageQuery pageQuery) {
        return folderService.queryCenterMember(bo, pageQuery);
    }

    @SaCheckPermission("project:management:queryProjectMemberEditMember")
    @PutMapping("/queryProjectMemberEditMember")
    public R<Void> queryProjectMemberEditMember(@RequestBody ProjectMemberEditMemberBo bo) {
        return toAjax(userService.updateOnProject(bo.getId(), bo.getNickname(), bo.getPhoneNumber(), bo.getEmail(), bo.getPassword()));
    }

    @GetMapping("/queryUser")
    public R<SysUserVo> queryUser(@RequestParam("id") Long id) {
        return R.ok(userService.getById(id));
    }

    @SaCheckPermission("project:management:export")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{projectId}")
    public void export(@PathVariable Long projectId, HttpServletResponse response) {
        List<ProjectExcel> list = documentService.getListByProjectId(projectId);
        if (LocaleContextHolder.getLocale().getLanguage().equals("zh")) {
            List<ProjectChineseExcel> excel = new ArrayList<>();
            list.forEach(e -> excel.add(MapstructUtils.convert(e, ProjectChineseExcel.class)));
            ExcelUtil.exportExcel(excel, "项目管理", ProjectChineseExcel.class, response);
        } else {
            List<ProjectEnglishExcel> excel = new ArrayList<>();
            list.forEach(e -> excel.add(MapstructUtils.convert(e, ProjectEnglishExcel.class)));
            ExcelUtil.exportExcel(excel, "Project Management", ProjectEnglishExcel.class, response);
        }

    }

}
