package com.dsk.cscec.controller;

import cn.hutool.core.lang.Assert;
import com.dsk.common.core.controller.BaseController;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.cscec.domain.CbProjectRecord;
import com.dsk.cscec.domain.bo.*;
import com.dsk.cscec.domain.vo.CbProjectCbStageNotDraftVo;
import com.dsk.cscec.domain.vo.CbProjectInfoVo;
import com.dsk.cscec.domain.vo.CbProjectRecordSearchVo;
import com.dsk.cscec.service.CbProjectRecordService;
import com.dsk.cscec.service.IDProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 项目台账表(CbProjectRecord)表控制层
 *
 * @author sxk
 * @since 2024-02-05 11:37:49
 */
@RestController
@RequestMapping("cbProjectRecord")
public class CbProjectRecordController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private CbProjectRecordService baseService;
    @Resource
    private IDProjectService dProjectService;

    /**
     * 校验项目编码是否存在
     */
    @GetMapping("/checkProjectCodeExist/{projectCode}")
    public R<Boolean> checkProjectCodeExist(@NotBlank(message = "项目编码不能为空") @PathVariable String projectCode) {
        return R.ok(dProjectService.checkProjectCodeExist(projectCode));
    }

    /**
     * 新增项目
     */
    @PostMapping("/addProject")
    public R<CbProjectRecord> addProject(@Validated @RequestBody AddCbProjectBo addCbProjectBo) {
        CbProjectRecord projectRecord = baseService.addProject(addCbProjectBo);
        Assert.notNull(projectRecord, "新增项目失败,请联系管理员");
        return R.ok(projectRecord);
    }

    /**
     * 根据项目文件导入数据
     */
    @PostMapping("/importData/{projectId}")
    public R<Void> importData(@NotNull(message = "项目ID不能为空") @PathVariable Long projectId) throws Exception {
        baseService.importData(projectId);
        return R.ok("已开始解析成本数据,过程需3~10分钟,稍后可到项目列表查看导入结果");
    }

    /**
     * 获取项目台账列表
     */
    @GetMapping("/getProjectList")
    public TableDataInfo<CbProjectRecordSearchVo> getProjectList(CbProjectSearchBo searchBo, PageQuery pageQuery) {
        return baseService.getProjectList(searchBo, pageQuery);
    }

    /**
     * 获取项目历史阶段信息(懒加载)
     */
    @GetMapping("/getProjectHistoryInfo")
    public R<List<CbProjectRecord>> getProjectHistoryInfo(CbProjectHistorySearchBo searchBo) {
        return R.ok(baseService.getProjectHistoryInfo(searchBo));
    }

    /**
     * 修改项目信息
     */
    @PutMapping("/editProjectInfo")
    public R<Void> editProjectInfo(@Validated @RequestBody EditProjectInfoBo editBo) {
        return toAjax(baseService.editProjectInfo(editBo));
    }

    /**
     * 获取新阶段项目可选成本阶段
     */
    @GetMapping("/getProjectAvailableCbStage/{relatedId}")
    public R<List<Integer>> getProjectAvailableCbStage(@NotNull(message = "关联ID不能为空") @PathVariable Long relatedId) {
        return R.ok(baseService.getProjectAvailableCbStage(relatedId));
    }

    /**
     * 新增新阶段项目
     */
    @PostMapping("/addNewStageProject")
    public R<CbProjectRecord> addNewStageProject(@Validated @RequestBody AddNewCbProjectBo newProjectBo) {
        return R.ok(baseService.addNewStageProject(newProjectBo));
    }

    /**
     * 查询当前项目可删除成本阶段
     * 即查询当前项目非草稿的成本阶段
     */
    @GetMapping("/getProjectCbStageNotDraft/{relatedId}")
    public R<List<CbProjectCbStageNotDraftVo>> getProjectCbStageNotDraft(@PathVariable Long relatedId) {
        return R.ok(baseService.getProjectCbStageNotDraft(relatedId));
    }

    /**
     * 批量删除项目
     */
    @DeleteMapping("/batchDeleteProject/{projectIds}")
    public R<Void> batchDeleteProject(@NotNull(message = "项目ID不能为空") @PathVariable Long[] projectIds) {
        return toAjax(baseService.batchDeleteProject(Arrays.asList(projectIds)));
    }

    /**
     * 获取草稿弹窗列表
     */
    @GetMapping("/getDraftDialogList")
    public TableDataInfo<CbProjectRecord> getDraftDialogList(PageQuery pageQuery) {
        return baseService.getDraftDialogList(pageQuery);
    }

    /**
     * 删除草稿
     */
    @DeleteMapping("/deleteDraft/{projectId}")
    public R<Void> deleteDraft(@NotNull(message = "项目ID不能为空") @PathVariable Long projectId) {
        return toAjax(baseService.deleteDraft(projectId));
    }

    /**
     * 获取工程项目信息
     */
    @GetMapping("/getCbProjectInfo/{projectId}")
    public R<CbProjectInfoVo> getCbProjectInfo(@NotNull(message = "项目ID不能为空") @PathVariable Long projectId) {
        return R.ok(dProjectService.getCbProjectInfo(projectId));
    }
}

