package com.hotent.enterpriseDeclare.controller.material;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.model.CommonResult;
import com.hotent.constant.BaseConstant;
import com.hotent.project.manager.CostProjectTaskAdjustmentRecordManager;
import com.hotent.project.manager.CostProjectTaskDraftManager;
import com.hotent.project.model.CostProjectTaskAdjustmentRecord;
import com.hotent.project.model.CostProjectTaskDraft;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作底稿控制器
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-01-27
 */
@Api(tags = "工作底稿管理")
@RestController
@RequestMapping("/api/enterprise/taskDraft")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class CostProjectTaskDraftController {

    @Autowired
    private CostProjectTaskDraftManager costProjectTaskDraftManager;

    @Autowired
    private CostProjectTaskAdjustmentRecordManager adjustmentRecordManager;

    /**
     * 根据任务ID获取工作底稿
     */
    @GetMapping(value = "/getByTaskId")
    @ApiOperation(value = "根据任务ID获取工作底稿", httpMethod = "GET", notes = "根据任务ID获取工作底稿")
    public CommonResult<CostProjectTaskDraft> getByTaskId(@RequestParam String taskId) {
        CostProjectTaskDraft draft = costProjectTaskDraftManager.getByTaskId(taskId);
        return CommonResult.<CostProjectTaskDraft>ok().value(draft);
    }


    /**
     * 新增或更新工作底稿
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "新增或更新工作底稿", httpMethod = "POST", notes = "新增或更新工作底稿")
    public CommonResult<String> save(@RequestBody CostProjectTaskDraft draft) {
        costProjectTaskDraftManager.createOrUpdate(draft);
        return CommonResult.<String>ok().value("操作成功");
    }

    /**
     * 删除工作底稿
     */
    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除工作底稿", httpMethod = "POST", notes = "删除工作底稿")
    public CommonResult<String> delete(@RequestParam String id) {
        CostProjectTaskDraft draft = costProjectTaskDraftManager.getById(id);
        if (draft != null) {
            draft.setIsDeleted(BaseConstant.DELETE_FLAG);
            costProjectTaskDraftManager.updateById(draft);
        }
        return CommonResult.<String>ok().value("删除成功");
    }

    /**
     * 根据任务ID查询核增核减记录列表
     */
    @GetMapping(value = "/record/listByTaskId")
    @ApiOperation(value = "根据任务ID查询核增核减记录列表", httpMethod = "GET", notes = "根据任务ID查询核增核减记录列表")
    public CommonResult<List<CostProjectTaskAdjustmentRecord>> listRecordByTaskId(@RequestParam String taskId) {
        List<CostProjectTaskAdjustmentRecord> list = adjustmentRecordManager.listByTaskId(taskId);
        return CommonResult.<List<CostProjectTaskAdjustmentRecord>>ok().value(list);
    }

    /**
     * 新增核增核减记录
     */
    @PostMapping(value = "/record/opera")
    @ApiOperation(value = "新增核增核减记录", httpMethod = "POST", notes = "新增核增核减记录")
    public CommonResult<String> addRecord(@RequestBody CostProjectTaskAdjustmentRecord record) {
        adjustmentRecordManager.createOrUpdate(record);
        return CommonResult.<String>ok().value("新增成功");
    }


    /**
     * 删除核增核减记录
     */
    @PostMapping(value = "/record/delete")
    @ApiOperation(value = "删除核增核减记录", httpMethod = "POST", notes = "删除核增核减记录")
    public CommonResult<String> deleteRecord(@RequestParam String id) {
        adjustmentRecordManager.removeById(id);
        return CommonResult.<String>ok().value("删除成功");
    }
}

