package com.zt.plat.module.base.controller.admin.base;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.base.controller.admin.base.vo.TaxPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.TaxRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.TaxSaveReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.TaxStsVO;
import com.zt.plat.module.base.dal.dataobject.base.TaxDO;
import com.zt.plat.module.base.service.base.TaxService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

@Tag(name = "管理后台 - 税码信息")
@RestController
@RequestMapping("/base/tax")
@Validated
public class TaxController {


    @Resource
    private TaxService taxService;

    @PostMapping("/create")
    @Operation(summary = "创建税码信息")
    @PreAuthorize("@ss.hasPermission('basic:tax-config:create')")
    public CommonResult<TaxRespVO> createTax(@Valid @RequestBody TaxSaveReqVO createReqVO) {
        return success(taxService.createTax(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新税码信息")
    @PreAuthorize("@ss.hasPermission('basic:tax-config:update')")
    public CommonResult<Boolean> updateTax(@Valid @RequestBody TaxSaveReqVO updateReqVO) {
        taxService.updateTax(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除税码信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('basic:tax-config:delete')")
    public CommonResult<Boolean> deleteTax(@RequestParam("id") Long id) {
        taxService.deleteTax(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除税码信息")
                @PreAuthorize("@ss.hasPermission('basic:tax-config:delete')")
    public CommonResult<Boolean> deleteTaxList(@RequestBody BatchDeleteReqVO req) {
        taxService.deleteTaxListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得税码信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:tax:query')")
    public CommonResult<TaxRespVO> getTax(@RequestParam("id") Long id) {
        TaxDO tax = taxService.getTax(id);
        return success(BeanUtils.toBean(tax, TaxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得税码信息分页")
    @PreAuthorize("@ss.hasPermission('base:tax:query')")
    public CommonResult<PageResult<TaxRespVO>> getTaxPage(@Valid TaxPageReqVO pageReqVO) {
        PageResult<TaxDO> pageResult = taxService.getTaxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出税码信息 Excel")
    @PreAuthorize("@ss.hasPermission('base:tax:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaxExcel(@Valid TaxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaxDO> list = taxService.getTaxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "税码信息.xls", "数据", TaxRespVO.class,
                        BeanUtils.toBean(list, TaxRespVO.class));
    }
    //启用和禁用
    @PostMapping("/enable-list")
    @Operation(summary = "启用禁用税码信息")
    //@PreAuthorize("@ss.hasPermission('base:tax:update')")
    public CommonResult<Boolean> enableTaxList(@Validated @RequestBody List<TaxStsVO> req) {
        taxService.enableTaxList(req);
        return success(true);
    }

}