package com.dsk.cscec.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.dsk.common.constant.TenantConstants;
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.DProject;
import com.dsk.cscec.domain.bo.*;
import com.dsk.cscec.domain.vo.CbProjectCbStageNotDraftVo;
import com.dsk.cscec.domain.vo.CbProjectHeadInfoVo;
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 com.dsk.search.util.ObjectUtils;
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")
    public R<String> checkProjectCodeExist(@Validated CbCheckProjectCodeExistBo checkBo) {
        return R.ok(dProjectService.checkProjectCodeExist(checkBo));
    }

    /**
     * 根据项目名称模糊查询项目列表
     */
    @GetMapping("/queryProjectNameList/projectName")
    public R<List<DProject>> queryProjectNameList(@RequestParam @NotBlank(message = "项目名称不能为空") String projectName) {
        return R.ok(dProjectService.queryProjectNameList(projectName));
    }

    /**
     * 校验项目名称是否存在
     */
    @GetMapping("/checkProjectNameExist/projectName")
    public R<CbProjectRecord> checkProjectNameExist(@RequestParam @NotBlank(message = "项目名称不能为空") String projectName) {
        CbProjectRecord projectRecord = baseService.checkProjectNameExist(projectName);
        return R.ok(ObjectUtil.isNotNull(projectRecord) ? "已存在相同名称项目,是否继续创建该项目的新阶段?" : "操作成功", projectRecord);
    }

    /**
     * 新增项目
     */
    @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("/getCbProjectHeadInfo/{projectId}")
    public R<CbProjectHeadInfoVo> getCbProjectHeadInfo(@NotNull(message = "项目ID不能为空") @PathVariable Long projectId) {
        return R.ok(baseService.getCbProjectHeadInfo(projectId));
    }

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

