package com.hotent.enterpriseDeclare.controller.material;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import com.hotent.constant.BaseConstant;
import com.hotent.enterpriseDeclare.req.CostTaskEvidencePageReq;
import com.hotent.project.manager.CostProjectTaskEvidenceManager;
import com.hotent.project.model.CostProjectTaskEvidence;
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;

@Api(tags = "子任务提取资料登记")
@RestController
@RequestMapping("/api/enterprise/taskEvidence")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class CostProjectTaskEvidenceController {

    @Autowired
    private CostProjectTaskEvidenceManager costProjectTaskEvidenceManager;

    /**
     * 分页查询资料登记列表
     */
    @PostMapping(value = "/pageList")
    @ApiOperation(value = "分页查询资料登记列表", httpMethod = "POST", notes = "分页查询资料登记列表")
    public CommonResult<IPage<CostProjectTaskEvidence>> pageList(@RequestBody CostTaskEvidencePageReq req) {
        LambdaQueryWrapper<CostProjectTaskEvidence> queryWrapper = new LambdaQueryWrapper<>();

        // 查询未删除的记录
        queryWrapper.eq(CostProjectTaskEvidence::getIsDeleted, BaseConstant.NORMAL_STATUS);

        // 根据任务ID查询
        if (StringUtil.isNotEmpty(req.getTaskId())) {
            queryWrapper.eq(CostProjectTaskEvidence::getTaskId, req.getTaskId());
        }

        // 根据资料名称模糊查询
        if (StringUtil.isNotEmpty(req.getMaterialName())) {
            queryWrapper.like(CostProjectTaskEvidence::getMaterialName, req.getMaterialName());
        }

        // 按序号排序
        queryWrapper.orderByAsc(CostProjectTaskEvidence::getOrderNum);

        // 创建分页对象并执行查询
        Page<CostProjectTaskEvidence> page = new Page<>(req.getPageNum(), req.getPageSize());
        IPage<CostProjectTaskEvidence> result = costProjectTaskEvidenceManager.page(page, queryWrapper);

        return CommonResult.<IPage<CostProjectTaskEvidence>>ok().value(result);
    }

    /**
     * 根据任务ID查询资料列表
     */
    @GetMapping(value = "/listByTaskId")
    @ApiOperation(value = "根据任务ID查询资料列表", httpMethod = "GET", notes = "根据任务ID查询资料列表")
    public CommonResult<List<CostProjectTaskEvidence>> listByTaskId(@RequestParam String taskId) {
        LambdaQueryWrapper<CostProjectTaskEvidence> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostProjectTaskEvidence::getTaskId, taskId)
                   .eq(CostProjectTaskEvidence::getIsDeleted, BaseConstant.NORMAL_STATUS)
                   .orderByAsc(CostProjectTaskEvidence::getOrderNum);

        List<CostProjectTaskEvidence> list = costProjectTaskEvidenceManager.list(queryWrapper);
        return CommonResult.<List<CostProjectTaskEvidence>>ok().value(list);
    }

    /**
     * 根据ID获取详情
     */
    @GetMapping(value = "/getDetail")
    @ApiOperation(value = "根据ID获取详情", httpMethod = "GET", notes = "根据ID获取详情")
    public CommonResult<CostProjectTaskEvidence> getDetail(@RequestParam String id) {
        CostProjectTaskEvidence evidence = costProjectTaskEvidenceManager.getDetail(id);
        return CommonResult.<CostProjectTaskEvidence>ok().value(evidence);
    }

    /**
     * 新增资料登记
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增资料登记", httpMethod = "POST", notes = "新增资料登记")
    public CommonResult<String> add(@RequestBody CostProjectTaskEvidence evidence) {
        costProjectTaskEvidenceManager.createOrUpdate(evidence);
        return CommonResult.<String>ok().value("新增成功");
    }

    /**
     * 更新资料登记
     */
    @PostMapping(value = "/update")
    @ApiOperation(value = "更新资料登记", httpMethod = "POST", notes = "更新资料登记")
    public CommonResult<String> update(@RequestBody CostProjectTaskEvidence evidence) {
        costProjectTaskEvidenceManager.createOrUpdate(evidence);
        return CommonResult.<String>ok().value("更新成功");
    }

    /**
     * 删除资料登记
     */
    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除资料登记", httpMethod = "POST", notes = "删除资料登记")
    public CommonResult<String> delete(@RequestParam String id) {
        costProjectTaskEvidenceManager.removeById(id);
        return CommonResult.<String>ok().value("删除成功");
    }
}
