package com.ruoyi.zhushi.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.zhushi.entity.Inventory;
import com.ruoyi.zhushi.service.InventoryService;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;
    @SaIgnore
    @PostMapping
    public boolean addInventory(@RequestBody List<Inventory> inventorys) {
        return inventoryService.saveInventory(inventorys);
    }

    @DeleteMapping("/{id}")
    public boolean deleteInventory(@PathVariable String id) {
        return inventoryService.deleteInventoryById(id);
    }
    @PutMapping
    public boolean updateInventory(@RequestBody Inventory inventory) {
        return inventoryService.updateInventory(inventory);
    }

    @GetMapping("/{id}")
    public Inventory getInventoryById(@PathVariable String id) {
        return inventoryService.getInventoryById(id);
    }

    @GetMapping
    public List<Inventory> getAllInventories() {
        return inventoryService.getAllInventories();
    }
    @PutMapping("/updateByUrl")
    public boolean updateInventoryByUrl(@RequestBody List<Inventory> inventorys) {
        return inventoryService.updateInventoryByUrl(inventorys);
    }

    @GetMapping("/param")
    public List<Inventory> getInventoriesByParam(@RequestParam(value = "queryWord", defaultValue = "") String queryWord,
                                                 @RequestParam(value = "queryDate", defaultValue = "") String queryDate,
                                                 @RequestParam(value = "type", defaultValue = "") String type) {
        return inventoryService.getInventoriesByParam(queryWord,queryDate,type);
    }
    @PostMapping("/exportExcel")
    public void getFile(@RequestParam(value = "queryWord", defaultValue = "") String queryWord,
                        @RequestParam(value = "queryDate", defaultValue = "") String queryDate,
                        @RequestParam(value = "type", defaultValue = "") String type,
                        HttpServletResponse response) throws Exception {
        List<Inventory> lists = inventoryService.getInventoriesByParam(queryWord,queryDate,type);

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("库存抓取记录");

        // Create header row
        Row headerRow = sheet.createRow(0);
        String[] headers = {"标题", "链接", "图片", "店铺名称", "库存信息", "价格", "备注信息", "创建时间",
                "更新时间"};
        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, 40 * 256);
                    break;
                case 2:
                    sheet.setColumnWidth(i, 30 * 256);
                    break;
                case 3:
                    sheet.setColumnWidth(i, 30 * 256);
                    break;
                case 4:
                    sheet.setColumnWidth(i, 30 * 256);
                    break;
                case 5:
                    sheet.setColumnWidth(i, 20 * 256);
                    break;
                case 6:
                    sheet.setColumnWidth(i, 20 * 256);
                    break;
                case 7:
                    sheet.setColumnWidth(i, 30 * 256);
                    break;
                case 8:
                    sheet.setColumnWidth(i, 25 * 256);
                    break;
                case 9:
                    sheet.setColumnWidth(i, 25 * 256);
                    break;
                // 如果dayOfWeek的值不是1到7之间的整数，则执行default
                default:
                    sheet.setColumnWidth(i, 30 * 256);
            }
        }
        // Fill data rows
        int rowNum = 1;
        for (Inventory inventory : lists) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(inventory.getTitle());
            row.createCell(1).setCellValue(inventory.getUrl());
            row.createCell(2).setCellValue(inventory.getImage());
            row.createCell(3).setCellValue(inventory.getShopName());
            row.createCell(4).setCellValue(inventory.getEnableQuantity());
            row.createCell(5).setCellValue(inventory.getPrice());
            row.createCell(6).setCellValue(inventory.getRemark());
            row.createCell(7).setCellValue(inventory.getCreateDate());
            row.createCell(8).setCellValue(inventory.getUpdateDate());
        }

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

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

}
