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

import com.unis.module.coding.controller.admin.province.vo.ProvincePageReqVO;
import com.unis.module.coding.controller.admin.province.vo.ProvinceRespVO;
import com.unis.module.coding.controller.admin.province.vo.ProvinceSaveReqVO;
import com.unis.module.coding.dal.dataobject.province.ProvinceDO;
import com.unis.module.coding.service.province.ProvinceService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
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 javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

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

import com.unis.framework.excel.core.util.ExcelUtils;

import com.unis.framework.apilog.core.annotation.ApiAccessLog;
import static com.unis.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 省已选")
@RestController
@RequestMapping("/delivery/province")
@Validated
public class ProvinceController {

    @Resource
    private ProvinceService provinceService;

    @PostMapping("/create")
    @Operation(summary = "创建省已选")
    @PreAuthorize("@ss.hasPermission('delivery:province:create')")
    public CommonResult<Long> createProvince(@Valid @RequestBody ProvinceSaveReqVO createReqVO) {
        return success(provinceService.createProvince(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新省已选")
    @PreAuthorize("@ss.hasPermission('delivery:province:update')")
    public CommonResult<Boolean> updateProvince(@Valid @RequestBody ProvinceSaveReqVO updateReqVO) {
        provinceService.updateProvince(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除省已选")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('delivery:province:delete')")
    public CommonResult<Boolean> deleteProvince(@RequestParam("id") Long id) {
        provinceService.deleteProvince(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得省已选")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('delivery:province:query')")
    public CommonResult<ProvinceRespVO> getProvince(@RequestParam("id") Long id) {
        ProvinceDO province = provinceService.getProvince(id);
        return success(BeanUtils.toBean(province, ProvinceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得省已选分页")
    @PreAuthorize("@ss.hasPermission('delivery:province:query')")
    public CommonResult<PageResult<ProvinceRespVO>> getProvincePage(@Valid ProvincePageReqVO pageReqVO) {
        PageResult<ProvinceDO> pageResult = provinceService.getProvincePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProvinceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出省已选 Excel")
    @PreAuthorize("@ss.hasPermission('delivery:province:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProvinceExcel(@Valid ProvincePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProvinceDO> list = provinceService.getProvincePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "省已选.xls", "数据", ProvinceRespVO.class,
                        BeanUtils.toBean(list, ProvinceRespVO.class));
    }

}