package cn.cslg.report.controller;

import cn.cslg.report.common.core.base.Constants;
import cn.cslg.report.common.model.PageHelperT;
import cn.cslg.report.common.model.QueryFUEntity;
import cn.cslg.report.common.model.dto.FollowUpDTO;
import cn.cslg.report.common.model.dto.FollowUpUpdateDTO;
import cn.cslg.report.common.model.dto.RegisterDTO;
import cn.cslg.report.common.model.vo.FollowUpVO;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.Report;
import cn.cslg.report.mapper.FollowUpMapper;
import cn.cslg.report.service.IFollowUpService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.List;

/**
 * 后续事项的Controller层
 *
 * @Author xiexiang
 * @Date 2023/4/2
 */
@Tag(name = "后续事项模块")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(Constants.REPORT_API + "/followUps")
public class FollowUpController {
    private final IFollowUpService followUpService;
    private final FollowUpMapper followUpMapper;


    @Operation(summary = "新增后续事项")
    @PostMapping("/add")
    public String add(@RequestBody List<FollowUpDTO> followUps) {
        if (followUps == null) {
            return Response.error("请求参数为null");
        } else {
            log.info("开始处理【新增后续事项】的请求，请求参数为：{}", followUps);
            followUpService.add(followUps);
            return Response.success("新增后续事项完成");
        }
    }

    @Operation(summary = "修改后续事项")
    @PostMapping("/update")
    public String update(@RequestBody List<FollowUpUpdateDTO> followUpUpdateDTOList) {
        if (followUpUpdateDTOList == null) {
            return Response.error("请求参数为null");
        } else {
            log.info("开始处理【修改后续事项】的请求，请求参数为：{}", followUpUpdateDTOList);
            followUpService.update(followUpUpdateDTOList);
            return Response.success("修改后续事项完成");
        }
    }
    @Operation(summary = "根据报告id查询后续事项数据信息")
    @PostMapping("/query")
    public String query(@RequestBody QueryFUEntity queryFUEntity){
        List<FollowUpVO> queryResult = followUpService.findAllByPage(queryFUEntity);
        PageHelperT pageHelperT = new PageHelperT();
        pageHelperT.setList(queryResult);
        pageHelperT.setCurrent(queryFUEntity.getCurrent());
        pageHelperT.setSize(queryFUEntity.getSize());
        pageHelperT.setTotal(followUpMapper.countByReportId(queryFUEntity.getReportId()));
        return Response.success(pageHelperT);
    }

    @Operation(summary = "根据ids删除后续事项数据信息")
    @PostMapping("/delete")
    public String delete(@RequestBody List<Integer> ids) {
        log.info("开始处理【删除后续事项】的请求，请求参数为：{}", ids);
        if (ids != null) {
            followUpService.delete(ids);
            return Response.success("删除后续事项完成");
        }
        return Response.error("请求参数为null");
    }

    @Operation(summary = "登记结果")
    @PostMapping("/register")
    public String register(String register, List<MultipartFile> files) throws ParseException {
        if (register != null) {
            RegisterDTO register1 = JSONObject.parseObject(register, RegisterDTO.class);
            followUpService.register(register1, files);
            return Response.success("登记结果成功");
        }
        return Response.error();
    }
}
