package com.hotent.enterpriseDeclare.controller.material;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import com.hotent.project.manager.CostProjectTaskMaterialDetailManager;
import com.hotent.project.manager.CostProjectTaskMaterialSummaryManager;
import com.hotent.project.model.CostProjectTaskMaterialSummary;
import com.hotent.project.req.MaterialSummarySortReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资料归纳主表 前端控制器
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-01-27
 */
@RestController
@RequestMapping("/api/materialSummary/v1/")
@Api(tags = "资料归纳主表")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class CostProjectTaskMaterialSummaryController extends BaseController<CostProjectTaskMaterialSummaryManager, CostProjectTaskMaterialSummary> {

    @Autowired
    private CostProjectTaskMaterialDetailManager costProjectTaskMaterialDetailManager;

    /**
     * 根据taskId获取资料归纳列表
     * @param taskId 任务ID
     * @return 资料归纳列表（包含明细）
     */
    @GetMapping(value = "/listByTaskId")
    @ApiOperation(value = "根据taskId获取资料归纳列表", httpMethod = "GET", notes = "根据taskId获取资料归纳列表（包含明细）")
    public CommonResult<List<CostProjectTaskMaterialSummary>> listByTaskId(
            @ApiParam(name = "taskId", value = "任务ID", required = true)
            @RequestParam(required = true) String taskId) {
        return CommonResult.<List<CostProjectTaskMaterialSummary>>ok().value(baseService.listByTaskId(taskId));
    }

    /**
     * 根据id获取资料归纳详情
     * @param id 主表ID
     * @return 资料归纳详情（包含明细）
     */
    @GetMapping(value = "/getDetail")
    @ApiOperation(value = "根据id获取资料归纳详情", httpMethod = "GET", notes = "根据id获取资料归纳详情（包含明细）")
    public CommonResult<CostProjectTaskMaterialSummary> getDetail(
            @ApiParam(name = "id", value = "主表ID", required = true)
            @RequestParam(required = true) String id) {
        return CommonResult.<CostProjectTaskMaterialSummary>ok().value(baseService.getDetail(id));
    }

    /**
     * 新增、更新资料归纳主表
     * @param summary 资料归纳主表
     * @return
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "新增、更新资料归纳主表", httpMethod = "POST", notes = "新增、更新资料归纳主表")
    @Transactional
    public CommonResult<String> save(
            @ApiParam(name = "summary", value = "资料归纳主表", required = true)
            @RequestBody CostProjectTaskMaterialSummary summary) {
        String msg = StringUtil.isEmpty(summary.getId()) ? "添加资料归纳成功" : "更新资料归纳成功";
        baseService.createOrUpdate(summary);
        return CommonResult.<String>ok().message(msg);
    }

    /**
     * 批量保存资料归纳（主表+明细）
     * @param summary 资料归纳主表（包含detailList）
     * @return
     */
    @PostMapping(value = "/saveWithDetails")
    @ApiOperation(value = "批量保存资料归纳（主表+明细）", httpMethod = "POST", notes = "批量保存资料归纳（主表+明细）")
    @Transactional
    public CommonResult<String> saveWithDetails(
            @ApiParam(name = "summary", value = "资料归纳主表（包含detailList）", required = true)
            @RequestBody CostProjectTaskMaterialSummary summary) {
        String msg = StringUtil.isEmpty(summary.getId()) ? "保存资料归纳成功" : "更新资料归纳成功";
        baseService.saveWithDetails(summary);
        return CommonResult.<String>ok().message(msg);
    }

    /**
     * 删除资料归纳（级联删除明细）
     * @param id 主表ID
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除资料归纳", httpMethod = "DELETE", notes = "删除资料归纳（级联删除明细）")
    @Transactional
    public CommonResult<String> delete(
            @ApiParam(name = "id", value = "主表ID", required = true)
            @RequestParam(required = true) String id) {
        baseService.deleteById(id);
        return CommonResult.<String>ok().message("删除资料归纳成功");
    }

    /**
     * 资料排序
     * @param req 排序请求
     * @return
     */
    @PostMapping(value = "/sort")
    @ApiOperation(value = "资料排序", httpMethod = "POST", notes = "资料排序（上移/下移）")
    @Transactional
    public CommonResult<String> sort(
            @ApiParam(name = "req", value = "排序请求", required = true)
            @RequestBody MaterialSummarySortReq req) {
        if (StringUtil.isEmpty(req.getId()) || StringUtil.isEmpty(req.getDirection())) {
            return CommonResult.<String>error().message("ID和排序方向不能为空");
        }
        baseService.sort(req.getId(), req.getDirection());
        return CommonResult.<String>ok().message("排序成功");
    }
}

