package org.dromara.system.controller.project;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.dto.project.*;
import org.dromara.system.domain.vo.project.BusProjectSafetyDayVo;
import org.dromara.system.domain.vo.project.BusProjectVo;
import org.dromara.system.domain.vo.project.BusSubProjectVo;
import org.dromara.system.service.IBusProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目
 *
 * @author lilemy
 * @date 2025-03-04
 */
@Validated
@RestController
@RequestMapping("/project/project")
public class BusProjectController extends BaseController {

    @Resource
    private IBusProjectService projectService;

    /**
     * 切换项目
     *
     * @param id 项目id
     */
    @GetMapping("/changeProject/{id}")
    public R<Void> changeUserProject(@NotNull(message = "项目id不能为空")
                                     @PathVariable Long id) {
        return toAjax(projectService.changeUserProject(id));
    }

    /**
     * 查询项目列表
     */
    @SaCheckPermission("project:project:list")
    @GetMapping("/list")
    public TableDataInfo<BusProjectVo> list(BusProjectQueryReq req, PageQuery pageQuery) {
        return projectService.queryPageList(req, pageQuery);
    }

    /**
     * 查询项目下的子项目列表
     */
    @SaCheckPermission("project:project:listSub")
    @GetMapping("/list/sub/{id}")
    public R<List<BusSubProjectVo>> listSubProject(@NotNull(message = "项目id不能为空")
                                                   @PathVariable Long id) {
        return R.ok(projectService.querySubList(id));
    }

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

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

    /**
     * 新增项目
     */
    @SaCheckPermission("project:project:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody BusProjectCreateReq req) {
        if (req == null) {
            throw new ServiceException("参数不能为空", HttpStatus.BAD_REQUEST);
        }
        return R.ok(projectService.insertByBo(req));
    }

    /**
     * 新增子项目
     */
    @SaCheckPermission("project:project:sub")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/sub")
    public R<Long> addSub(@Validated(AddGroup.class) @RequestBody BusProjectCreateSubReq req) {
        if (req == null) {
            throw new ServiceException("参数不能为空", HttpStatus.BAD_REQUEST);
        }
        return R.ok(projectService.insertSubByProject(req));
    }

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

    /**
     * 保存项目招标文件
     */
    @SaCheckPermission("project:project:saveTenderFile")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/save/tender/file")
    public R<Void> uploadTenderFile(@RequestBody BusProjectSaveTenderFileReq req) {
        return toAjax(projectService.saveTenderFile(req));
    }

    /**
     * 删除项目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("project:project: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("/safetyDay/{id}")
    public R<BusProjectSafetyDayVo> getSafetyDay(@NotNull(message = "主键不能为空")
                                                 @PathVariable Long id) {
        return R.ok(projectService.getSafetyDay(id));
    }

}
