package com.ruoyi.zhushi.controller;
import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.zhushi.entity.ProductRanking;
import com.ruoyi.zhushi.service.ProductRankingService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 插入和查询乐天和雅虎的排行榜数据
 */
@RestController
@RequestMapping("/productranking")
public class ProductRankingController {
    @Autowired
    private ProductRankingService productRankingService;

    //批量插入
    @PostMapping("/insertBatch")
    @SaIgnore
    public String insertProductRankings(@RequestBody List<ProductRanking> productRankings) throws Exception {
        productRankingService.insertProductRankings(productRankings);
        return "Insert successful";
    }

    // 主页面查询
    @GetMapping("/list")
    public List<ProductRanking> getProductRankingList(@RequestParam(value ="createDate", defaultValue = "") String createDate,
                                                      @RequestParam(value ="website", defaultValue = "") Integer website,
                                                      @RequestParam(value ="category", defaultValue = "") String category,
                                                      @RequestParam(value ="keyword", defaultValue = "") String keyword) throws Exception{
        return productRankingService.getProductRankingList(createDate,website,category,keyword);
    }

    // 查询单个商品的历史情况
    @GetMapping("/select")
    public List<ProductRanking> getProductRankingByParam(@RequestParam(value ="sn", defaultValue = "") String sn,
                                                         @RequestParam(value ="shop", defaultValue = "") String shop,
                                                         @RequestParam(value ="website", defaultValue = "") Integer website,
                                                         @RequestParam(value ="category", defaultValue = "") String category) throws Exception{
        return productRankingService.getProductRankingByParam(sn,shop,website,category);
    }


    //导出excel文件
    @PostMapping("/exportExcel")
    public void getFile(@RequestParam(value ="createDate", defaultValue = "") String createDate,
                        @RequestParam(value ="website", defaultValue = "") Integer website,
                        @RequestParam(value ="category", defaultValue = "") String category,
                        @RequestParam(value ="keyword", defaultValue = "") String keyword,
                        HttpServletResponse response) throws Exception {
        List<ProductRanking> productRankings = productRankingService.getProductRankingList(createDate, website, category, keyword);

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Product Ranking");

        // Create header row
        Row headerRow = sheet.createRow(0);
        String[] headers = {"排名", "商品名字", "商品价格", "图片链接", "商品品牌", "商品评论数", "商品积分", "商铺名称",
                "补充信息1", "补充信息2", "补充信息3",
                "商品链接", "数据类型", "创建日期", "创建日期时分秒", "商品编号", "商品分类"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            // 设置列宽度
            switch (i + 1) {
                case 1:
                    sheet.setColumnWidth(i, 10 * 256);
                    break;
                case 2:
                    sheet.setColumnWidth(i, 30 * 256);
                    break;
                case 3:
                    sheet.setColumnWidth(i, 15 * 256);
                    break;
                case 4:
                    sheet.setColumnWidth(i, 30 * 256);
                    break;
                case 5:
                    sheet.setColumnWidth(i, 15 * 256);
                    break;
                case 6:
                    sheet.setColumnWidth(i, 15 * 256);
                    break;
                case 7:
                    sheet.setColumnWidth(i, 15 * 256);
                    break;
                case 8:
                    sheet.setColumnWidth(i, 40 * 256);
                    break;
                case 12:
                    sheet.setColumnWidth(i, 30 * 256);
                    break;
                case 13:
                    sheet.setColumnWidth(i, 10 * 256);
                    break;
                case 14:
                    sheet.setColumnWidth(i, 15 * 256);
                    break;
                case 15:
                    sheet.setColumnWidth(i, 20 * 256);
                    break;
                case 16:
                    sheet.setColumnWidth(i, 20 * 256);
                    break;
                case 17:
                    sheet.setColumnWidth(i, 20 * 256);
                    break;
                // 如果dayOfWeek的值不是1到7之间的整数，则执行default
                default:
                    sheet.setColumnWidth(i, 20 * 256);
            }
        }

        // Fill data rows
        int rowNum = 1;
        for (ProductRanking productRanking : productRankings) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(productRanking.getRank());
            row.createCell(1).setCellValue(productRanking.getName());
            row.createCell(2).setCellValue(productRanking.getPrice());
            row.createCell(3).setCellValue(productRanking.getImg());
            row.createCell(4).setCellValue(productRanking.getBrand());
            row.createCell(5).setCellValue(productRanking.getComments());
            row.createCell(6).setCellValue(productRanking.getIntegral());
            row.createCell(7).setCellValue(productRanking.getShop());
            row.createCell(8).setCellValue(productRanking.getDesc1());
            row.createCell(9).setCellValue(productRanking.getDesc2());
            row.createCell(10).setCellValue(productRanking.getDesc3());
            row.createCell(11).setCellValue(productRanking.getUrl());
            row.createCell(12).setCellValue("1".equals(productRanking.getWebsite()) ? "乐天" : "雅虎");
            row.createCell(13).setCellValue(productRanking.getCreatedate());
            row.createCell(14).setCellValue(productRanking.getCreatetime());
            row.createCell(15).setCellValue(productRanking.getSn());
            row.createCell(16).setCellValue(getCategory(productRanking.getCategory())
            );
        }

        // Set response headers
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=product_ranking.xlsx");

        // Write workbook to response
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    public String getCategory(String category) {
        String newCategory = "";
        if ("10001".equals(category)) {
            newCategory = "メモリーカード";
        } else if ("10002".equals(category)) {
            newCategory = "USBメモリ";
        }
        else if ("10003".equals(category)) {
            newCategory = "内蔵型SSD";
        }
        else if ("10004".equals(category)) {
            newCategory = "メモリー";
        }
        else if ("20001".equals(category)) {
            newCategory = "メモリーカード";
        }
        else if ("20002".equals(category)) {
            newCategory = "USBメモリ";
        }
        else if ("20003".equals(category)) {
            newCategory = "内蔵型SSD";
        }
        if ("20004".equals(category)) {
            newCategory = "メモリー";
        }
        return newCategory;
    }




    // 暂时没用
    @GetMapping("/currentDate/list")
    // 主页面查询 返回每个商品最大日期的数据
    public List<ProductRanking> getProductRankingListCurrent(@RequestParam(value ="website", defaultValue = "") Integer website,
                                                             @RequestParam(value ="category", defaultValue = "") String category
    ) throws Exception{
        return productRankingService.getProductRankingListCurrent(website,category);
    }



}
