package cn.cslg.report.controller;

import cn.cslg.report.common.core.base.Constants;
import cn.cslg.report.common.model.dto.ProductDTO;
import cn.cslg.report.common.model.dto.ProductIncludeFilesDTO;
import cn.cslg.report.common.model.vo.ProductIncludeFilesVO;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.exception.XiaoShiException;
import cn.cslg.report.service.IProductService;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 产品的Controller层
 *
 * @Author chenyu
 * @Data 2022/12/20
 */
@Tag(name = "产品管理模块")
@Slf4j
@RestController
@RequestMapping(Constants.REPORT_API + "/product")
@RequiredArgsConstructor
public class ProductController {
    private final IProductService productService;  //装配产品的Service层接口和实现类

    @Operation(summary = "新增产品")
    @PostMapping("/addProduct")
    public String addProduct(String jsons, List<MultipartFile> files) {
        log.info("开始处理【新增产品】的请求，参数为：{}, {}", jsons, files);
        if (jsons == null) {
            return Response.error("产品创建失败，无新增产品的数据");
        }
        ProductDTO productDTO = JSONObject.parseObject(jsons, ProductDTO.class);
        try {
            productService.addProduct(productDTO, files);
        } catch (XiaoShiException e) {
            return Response.error(e.getMessage());
        }
        return Response.success("新增产品完成");
    }

    @Operation(summary = "修改产品")
    @PostMapping("/updateProduct")
    public String updateProduct(String jsons, List<MultipartFile> files) {
        log.info("开始处理【修改产品】的业务，参数为：{}, {}", jsons, files);
        if (jsons != null) {
            ProductIncludeFilesDTO productIncludeFilesDTO = JSONObject.parseObject(jsons, ProductIncludeFilesDTO.class);
            productService.updateProduct(productIncludeFilesDTO, files);
            return Response.success("产品修改成功");
        } else {
            return Response.error("产品信息不能为空");
        }
    }

    @Operation(summary = "根据报告id查询产品数据信息")
    @GetMapping("/getProduct")
    public String getProduct(Integer reportId) {
        log.info("开始处理【查询产品】的请求，参数为：{}", reportId);
        if (reportId != null) {
            ProductIncludeFilesVO queryResult = productService.getProduct(reportId);
            return Response.success(queryResult);
        } else {
            log.info("前端未传报告reportId");
            return Response.error("前端未传报告reportId");
        }

    }

}
