package cn.cslg.pas.controller;

import cn.cslg.pas.common.JsonPage;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.dto.*;
import cn.cslg.pas.common.model.vo.PMDTrendVO;
import cn.cslg.pas.common.model.vo.ProductMarketDataTrendVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.service.IAssoProductPatentService;
import cn.cslg.pas.service.IProductMarketDataService;
import cn.cslg.pas.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.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 产品的Controller层
 *
 * @Author chenyu
 * @Date 2023/3/8
 */
@Tag(name = "产品管理模块")
@Slf4j
@RequiredArgsConstructor
@RequestMapping(Constants.API_VERSION_V2 + "/products")
@RestController
public class ProductController {
    private final IProductService productService;
    private final IProductMarketDataService productMarketDataService;
    private final IAssoProductPatentService assoProductPatentService;

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

    @Operation(summary = "修改产品")
    @PostMapping("/update")
    public String update(String jsons, List<MultipartFile> files) {
        if (jsons != null) {
            ProductUpdateDTO productUpdateDTO = JSONObject.parseObject(jsons, ProductUpdateDTO.class);
            log.info("开始处理【修改产品】的请求，请求参数为：{}, {}", productUpdateDTO, files);
            try {
                productService.update(productUpdateDTO, files);
            } catch (XiaoShiException e) {
                return Response.error(e.getMessage());
            }
            return Response.success("修改产品完成");
        }
        return Response.error("请求参数为null");
    }

    @Operation(summary = "分页查询产品")
    @PostMapping("/query")
    public String query(@RequestBody ProductQueryPageDTO productQueryPageDTO) {
        log.info("开始处理【查询产品】的请求，请求参数为：{}", productQueryPageDTO);
        JsonPage jsonPage = productService.query(productQueryPageDTO);
        return Response.success(jsonPage);
    }

    @Operation(summary = "查询产品趋势图")
    @PostMapping("/showTrend")
    public String showTrend(@RequestBody ProductMarketDataDTO dto) {
        List<ProductMarketDataTrendVO> queryResult = productMarketDataService.showTrend(dto);
        PMDTrendVO pmdTrendVO = new PMDTrendVO();
        pmdTrendVO.setProductId(dto.getProductId());
        pmdTrendVO.setProductName(productMarketDataService.queryNameByProductId(dto.getProductId()));
        pmdTrendVO.setSaleVOS(queryResult);
        if (pmdTrendVO != null) {
            return Response.success(pmdTrendVO);
        } else {
            return Response.error("查询失败");
        }
    }

    @Operation(summary = "删除产品")
    @GetMapping("/delete")
    public String delete(Integer id) {
        log.info("开始处理【删除产品】的请求，请求参数为：{}", id);
        try {
            productService.delete(id);
        } catch (XiaoShiException e) {
            return Response.error(e.getMessage());
        }
        return Response.success("删除产品完成");
    }

    @Operation(summary = "新增多个专利和多个产品关联")
    @PostMapping("/addNewAssoProductPatent")
    public String addNewAssoProductPatent(@RequestBody AssoProductPatentAddNewDTO assoProductPatentAddNewDTO) {
        log.info("开始处理【新增多个专利和多个产品关联】的请求，请求参数为：{}", assoProductPatentAddNewDTO);
        assoProductPatentService.addNewAssoProductPatent(assoProductPatentAddNewDTO);
        return Response.success("新增多个专利和多个产品关联完成");
    }


}
