package com.unis.module.coding.controller.admin.delivery;

import cn.hutool.core.bean.BeanUtil;
import com.unis.framework.apilog.core.annotation.ApiAccessLog;
import com.unis.framework.common.pojo.CommonResult;
import com.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.util.object.BeanUtils;
import com.unis.framework.excel.core.util.ExcelUtils;
import com.unis.module.coding.controller.admin.delivery.vo.*;
import com.unis.module.coding.service.caxx.CaxxService;
import com.unis.module.coding.service.dwxx.DwxxService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import java.io.IOException;
import java.util.List;

import static com.unis.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.unis.framework.common.pojo.CommonResult.success;

/**
 * @author qingsong.han
 * @description:
 * @create 2025-02-07 8:56
 */
@Tag(name = "交收仓列表")
@RestController
@RequestMapping("/coding/deliverySpare")
@Validated
@Slf4j
public class DeliverySpareController {
    @Resource
    private CaxxService caxxService;
    @Resource
    private DwxxService dwxxService;

    @PostMapping("/page")
    @Operation(summary = "交收仓列表分页")
    @PreAuthorize("@ss.hasPermission('coding:deliverySpare:query')")
    public CommonResult<PageResult<SpareRespVO>> getDeliverySparePage(@Valid @RequestBody SparePageReqVO pageReqVO) {
        return success(caxxService.getDeliverySparePage(pageReqVO));
    }

    @PostMapping("/identify/page")
    @Operation(summary = "认定交收仓分页")
    @PreAuthorize("@ss.hasPermission('DetermineDeliveryWarehouse:query')")
    public CommonResult<PageResult<SpareRespVO>> getIdentifySparePage(@Valid @RequestBody SparePageReqVO pageReqVO) {
        return success(caxxService.getIdentifySparePage(pageReqVO));
    }

    @PostMapping("/select/page")
    @Operation(summary = "选定交收仓分页")
    @PreAuthorize("@ss.hasPermission('SelectDeliveryWarehouseList:query')")
    public CommonResult<PageResult<SpareRespVO>> getSelectSparePage(@Valid @RequestBody SparePageReqVO pageReqVO) {
        return success(caxxService.getSelectSparePage(pageReqVO));
    }

    @GetMapping("/export/alternativeExcel")
    @Operation(summary = "导出备选交收库信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:alternative:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAlternativeSpareExcel(@Valid SparePageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpareRespVO> list = caxxService.getDeliverySparePage(pageReqVO).getList();

        List<AlternativeSpareRespVO> alternativeList = BeanUtil.copyToList(list, AlternativeSpareRespVO.class);
        // 导出 Excel
        ExcelUtils.write(response, "备选交收库信息.xls", "备选交收库信息", AlternativeSpareRespVO.class,
                BeanUtils.toBean(alternativeList, AlternativeSpareRespVO.class));
    }

    @GetMapping("/export/shengAlternativeExcel")
    @Operation(summary = "导出省备选交收库信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:shengalternative:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShengAlternativeSpareExcel(@Valid SparePageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpareRespVO> list = dwxxService.getProvinceSelectedPage(pageReqVO).getList();

        List<ShengAlternativeSpareRespVO> shengAlternativeList = BeanUtil.copyToList(list, ShengAlternativeSpareRespVO.class);
        // 导出 Excel
        ExcelUtils.write(response, "省级备选交收库信息.xls", "省级备选交收库信息", ShengAlternativeSpareRespVO.class,
                BeanUtils.toBean(shengAlternativeList, ShengAlternativeSpareRespVO.class));
    }

    @GetMapping("/export/identifySpareExcel")
    @Operation(summary = "导出认定交收库信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:identifySpare:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIdentifySpareExcel(@Valid SparePageReqVO pageReqVO,
                                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpareRespVO> list = caxxService.getIdentifySparePage(pageReqVO).getList();

        List<IdentifySpareRespVO> identifySpareList = BeanUtil.copyToList(list, IdentifySpareRespVO.class);
        // 导出 Excel
        ExcelUtils.write(response, "认定交收库信息.xls", "认定交收库信息", IdentifySpareRespVO.class,
                BeanUtils.toBean(identifySpareList, IdentifySpareRespVO.class));
    }

    @GetMapping("/export/selectSpareExcel")
    @Operation(summary = "导出选定交收库信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:selectSpare:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSelectSpareExcel(@Valid SparePageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpareRespVO> list = caxxService.getSelectSparePage(pageReqVO).getList();

        List<SelectSpareRespVO> selectSpareList = BeanUtil.copyToList(list, SelectSpareRespVO.class);
        // 导出 Excel
        ExcelUtils.write(response, "选定交收库信息.xls", "选定交收库信息", SelectSpareRespVO.class,
                BeanUtils.toBean(selectSpareList, SelectSpareRespVO.class));
    }
}
