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

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.delivery.DeliveryService;
import com.unis.module.coding.service.dwxx.DwxxService;
import com.unis.module.coding.service.kqxx.KqxxService;
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-01-16 15:22
 */
@Tag(name = "交割仓选择")
@RestController
@RequestMapping("/coding/delivery")
@Validated
@Slf4j
public class DeliveryController {
    @Resource
    private DwxxService dwxxService;
    @Resource
    private DeliveryService deliveryService;
    @Resource
    private KqxxService kqxxService;

    @GetMapping("/page")
    @Operation(summary = "获得交收仓选择分页")
    @PreAuthorize("@ss.hasPermission('coding:delivery:query')")
    public CommonResult<PageResult<DeliveryRespVO>> getDeliveryPage(@Valid DeliveryPageReqVO pageReqVO) {
        return success(dwxxService.getDeliveryPage(pageReqVO));
    }

    @GetMapping("/province-selected/page")
    @Operation(summary = "国家查看省已选交收仓选择分页")
    @PreAuthorize("@ss.hasPermission('provinceSelectedPage:query')")
    public CommonResult<PageResult<SpareRespVO>> getProvinceSelectedPage(@Valid SparePageReqVO pageReqVO) {
        return success(dwxxService.getProvinceSelectedPage(pageReqVO));
    }

    @GetMapping("/storePage")
    @Operation(summary = "获得仓房列表分页")
    @PreAuthorize("@ss.hasPermission('coding:cfxx:query')")
    public CommonResult<List<StoreRespVO>> getStorePage(@Valid StorePageReqVO pageReqVO) {
        return success(dwxxService.getStorePage(pageReqVO));
    }

    @PostMapping("/updateCaxxByIds")
    @Operation(summary = "仓房列表已选更新")
    @PreAuthorize("@ss.hasPermission('coding:cfxx:select')")
    public CommonResult<Boolean> updateCaxxByIds(@Valid @RequestBody CaxxUpdateReqVO updateReqVO) {
        dwxxService.updateSelectedCaxx(updateReqVO);
        return success(true);
    }

    @PostMapping("/revertCaxx")
    @Operation(summary = "仓房退回")
    @PreAuthorize("@ss.hasPermission('coding:cfxx:return')")
    public CommonResult<Boolean> revertCaxx(@Valid @RequestBody CaxxUpdateReqVO updateReqVO) {
        dwxxService.revertCaxx(updateReqVO);
        return success(true);
    }

    @PostMapping("/revertKqxx")
    @Operation(summary = "库区退回")
    @PreAuthorize("@ss.hasPermission('coding:cfxx:return')")
    public CommonResult<Boolean> revertKqxx(@Valid @RequestBody CaxxUpdateReqVO updateReqVO) {
        dwxxService.revertKqxx(updateReqVO);
        return success(true);
    }

    @PostMapping("/createByAll")
    @Operation(summary = "创建单位/库区/仓房/财务/交通运输")
    @PreAuthorize("@ss.hasPermission('coding:kqxx:creat')")
    public CommonResult<Object> createByAll(@Valid @RequestBody DeliverySaveVO createReqVO) {
        return success(deliveryService.createByAll(createReqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出交收库信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:delivery:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeliveryExcel(@Valid DeliveryPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeliveryRespVO> list = dwxxService.getDeliveryPage(pageReqVO).getList();

        // 导出 Excel
        ExcelUtils.write(response, "交收库信息.xls", "交收库信息", DeliveryRespVO.class,
                BeanUtils.toBean(list, DeliveryRespVO.class));
    }

    @GetMapping("/删除测试")
    @Operation(summary = "删除测试数据")
    @PreAuthorize("@ss.hasPermission('coding:delivery:deleteces')")
    public void deleteceshi(@RequestParam("id") Long id) throws IOException {

        dwxxService.deleteceshi(id);

    }
}
