package com.cf.imes.module.manage.controller.admin.plate;

import com.cf.imes.module.manage.controller.admin.plate.vo.plate.*;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;

import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import static com.cf.imes.framework.common.pojo.CommonResult.success;

import com.cf.imes.framework.excel.core.util.ExcelUtils;

import com.cf.imes.framework.operatelog.core.annotations.OperateLog;

import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cf.imes.module.manage.dal.dataobject.plate.PlateDO;
import com.cf.imes.module.manage.service.plate.PlateService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 板材信息表 plate")
@RestController
@RequestMapping("/manage/plate")
@Validated
public class PlateController {

    @Resource
    private PlateService plateService;

    @PostMapping("/create")
    @Operation(summary = "创建板材信息表 plate_{N}")
    @PreAuthorize("@ss.hasPermission('manage:plate:create')")
    public CommonResult<Long> createPlate(@Valid @RequestBody PlateSaveReqVO createReqVO) {
        return success(plateService.createPlate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新板材信息表 plate_{N}")
    @PreAuthorize("@ss.hasPermission('manage:plate:update')")
    public CommonResult<Boolean> updatePlate(@Valid @RequestBody PlateSaveReqVO updateReqVO) {
        plateService.updatePlate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除板材信息表 plate_{N}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('manage:plate:delete')")
    public CommonResult<Boolean> deletePlate(@RequestParam("id") Long id) {
        plateService.deletePlate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得板材信息表 plate_{N}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('manage:plate:query')")
    public CommonResult<PlateRespVO> getPlate(@RequestParam("id") Long id) {
        PlateDO plate = plateService.getPlate(id);
        return success(BeanUtils.toBean(plate, PlateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得板材信息表 plate_{N}分页")
    @PreAuthorize("@ss.hasPermission('manage:plate:query')")
    public CommonResult<PageResult<PlateRespVO>> getPlatePage(@Valid PlatePageReqVO pageReqVO) {
        PageResult<PlateDO> pageResult = plateService.getPlatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlateRespVO.class));
    }

    @GetMapping("/export")
    @Operation(summary = "导出板材信息")
    @PreAuthorize("@ss.hasPermission('manage:plate:export')")
    @OperateLog(type = EXPORT)
    public void exportPlateExcel(@Valid PlatePageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlateDO> list = plateService.getPlatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "板材信息表 plate_{N}.xls", "数据", PlateRespVO.class,
                BeanUtils.toBean(list, PlateRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入用户模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<PlateImportExcelVO> list = Arrays.asList(
                PlateImportExcelVO.builder().goodsId("SP1123456").goodsName("xixih").material("颗粒板").width(BigDecimal.valueOf(1212.3)).height(BigDecimal.valueOf(12131.6))
                        .thickness(BigDecimal.valueOf(0.2)).price(33333.2).brand("SexEnum.MALE.getSex()").spec("大厂").remark("测试").build(),
                PlateImportExcelVO.builder().goodsId("SP1123457").goodsName("2L").material("yuanma@cf.com").width(BigDecimal.valueOf(21.6)).height(BigDecimal.valueOf(123231.6))
                        .thickness(BigDecimal.valueOf(5.3)).price(333.555).brand("kkkkkk").spec("大厂").remark("测试").build()
        );
        // 输出
        ExcelUtils.write(response, "板材导入模板.xls", "板材列表", PlateImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入板材")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('manage:plate:import')")
    public CommonResult<PlateImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                       @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<PlateImportExcelVO> list = ExcelUtils.read(file, PlateImportExcelVO.class);
        return success(plateService.importPlateList(list, updateSupport));
//        return null;
    }

}