package com.dsk.cscec.controller;


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.AdvisoryBodyCustomFormData;
import com.dsk.cscec.domain.AdvisoryBodyCustomFormTemplate;
import com.dsk.cscec.domain.bo.*;
import com.dsk.cscec.domain.vo.*;
import com.dsk.cscec.service.AdvisoryBodyCustomFormDataService;
import com.dsk.cscec.service.AdvisoryBodyCustomFormTemplateService;
import com.dsk.cscec.service.AdvisoryBodyService;
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 java.util.List;

/**
 * 咨询机构管理控制层
 *
 * @author sxk
 * @since 2023-12-10 15:34:46
 */
@RestController
@RequestMapping("advisory/body")
public class AdvisoryBodyManageController extends BaseController {
    @Resource
    private IDProjectService projectService;
    @Resource
    private AdvisoryBodyService advisoryBodyService;
    @Resource
    private AdvisoryBodyCustomFormDataService customFormDataService;
    @Resource
    private AdvisoryBodyCustomFormTemplateService templateService;

    /**
     * 获取项目列表
     */
    @GetMapping("/getProjectList")
    public TableDataInfo<ProjectSearchVo> getProjectList(ProjectSearchBo projectSearchBo, PageQuery pageQuery) {
        return projectService.queryProjectList(projectSearchBo, pageQuery);
    }

    /**
     * 获取所有企业列表(咨询机构)数据
     */
    @GetMapping("/getAdvisoryBodyList")
    public TableDataInfo<AdvisoryBodySearchVo> getAdvisoryBodyList(AdvisoryBodySearchBo advisoryBodySearchBo, PageQuery pageQuery) {
        return advisoryBodyService.queryAdvisoryBodyList(advisoryBodySearchBo, pageQuery);
    }

    /**
     * 获取项目承接类型下拉列表
     */
    @GetMapping("/getIsInvestProject")
    public R<List<String>> getIsInvestProject() {
        return R.ok(projectService.queryIsInvestProject());
    }

    /**
     * 获取合作项目明细
     */
    @GetMapping("/getCooperateProjectDetailList")
    public TableDataInfo<CooperateProjectDetailSearchVo> getCooperateProjectDetailList(@Validated CooperateProjectDetailSearchBo cooperateProjectDetailSearchBo, PageQuery pageQuery) {
        return projectService.queryCooperateProjectDetailList(cooperateProjectDetailSearchBo, pageQuery);
    }

    /**
     * 获取项目详情
     */
    @GetMapping("/getProjectDetail")
    public R<ProjectDetailVo> getProjectDetail(@Validated ProjectDetailBo projectDetailBo) {
        return R.ok(projectService.queryProjectDetail(projectDetailBo));
    }

    /**
     * 校验咨询机构是否存在
     */
    @GetMapping("/checkAdvisoryBodyExist")
    public R<AdvisoryBodyExistVo> checkAdvisoryBodyExist(@NotBlank(message = "咨询机构名称不能为空") String advisoryBodyName) {
        return R.ok(advisoryBodyService.checkAdvisoryBodyExist(advisoryBodyName));
    }

    /**
     * 编辑咨询机构结算信息
     * 包含:
     * 新增咨询机构(advisory_body表)
     * 新增/更新咨询机构与项目关联信息(advisory_body_project表)
     */
    @PostMapping("/editAdvisoryBody")
    public R<Void> editAdvisoryBody(@Validated @RequestBody EditAdvisoryBodyInfoBo editInfoBo) {
        return toAjax(advisoryBodyService.editAdvisoryBody(editInfoBo));
    }

    /**
     * 获取自定义表单模板
     */
    @GetMapping("/getCustomFormTemplate")
    public R<AdvisoryBodyCustomFormTemplate> getCustomFormTemplate() {
        //业务上确定只有一个模板
        List<AdvisoryBodyCustomFormTemplate> templates = templateService.list();
        return R.ok(templates.isEmpty() ? null : templates.get(0));
    }

    /**
     * 编辑自定义表单模板
     */
    @PostMapping("/editCustomFormTemplate")
    public R<Void> editCustomFormTemplate(@Validated @RequestBody AdvisoryBodyCustomFormTemplate customFormTemplate) {
        return toAjax(templateService.editCustomFormTemplate(customFormTemplate));
    }

    /**
     * 根据项目主键查询自定义表单数据表数据
     */
    @GetMapping("/getCustomFormDataByProjectKey/{projectKey}")
    public R<AdvisoryBodyCustomFormData> getCustomFormDataByProjectKey(@PathVariable Long projectKey) {
        return R.ok(customFormDataService.getById(projectKey));
    }

    /**
     * 编辑自定义表单数据表数据
     */
    @PostMapping("/editCustomFormData")
    public R<Void> editCustomFormData(@Validated @RequestBody AdvisoryBodyCustomFormData customFormData) {
        return toAjax(customFormDataService.editCustomFormData(customFormData));
    }
}