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

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
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.AccountPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.AccountRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.AccountSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.AccountDO;
import com.zt.plat.module.base.service.base.AccountService;
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/account")
@Validated
public class AccountController {


    @Resource
    private AccountService accountService;

    @PostMapping("/create")
    @Operation(summary = "创建账户条款")
   // @PreAuthorize("@ss.hasPermission('base:account:create')")
    public CommonResult<AccountRespVO> createAccount(@Valid @RequestBody AccountSaveReqVO createReqVO) {
        return success(accountService.createAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账户条款")
    //@PreAuthorize("@ss.hasPermission('base:account:update')")
    public CommonResult<Boolean> updateAccount(@Valid @RequestBody AccountSaveReqVO updateReqVO) {
        accountService.updateAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账户条款")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('base:account:delete')")
    public CommonResult<Boolean> deleteAccount(@RequestParam("id") Long id) {
        accountService.deleteAccount(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除账户条款")
    //@PreAuthorize("@ss.hasPermission('base:account:delete')")
    public CommonResult<Boolean> deleteAccountList(@RequestBody BatchDeleteReqVO req) {
        accountService.deleteAccountListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账户条款")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:account:query')")
    public CommonResult<AccountRespVO> getAccount(@RequestParam("id") Long id) {
        AccountDO account = accountService.getAccount(id);
        return success(BeanUtils.toBean(account, AccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账户条款分页")
    @PreAuthorize("@ss.hasPermission('base:account:query')")
    public CommonResult<PageResult<AccountRespVO>> getAccountPage(@Valid AccountPageReqVO pageReqVO) {
        PageResult<AccountDO> pageResult = accountService.getAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账户条款 Excel")
    @PreAuthorize("@ss.hasPermission('base:account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAccountExcel(@Valid AccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AccountDO> list = accountService.getAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账户条款.xls", "数据", AccountRespVO.class,
                        BeanUtils.toBean(list, AccountRespVO.class));
    }

    @PutMapping("/enable-list")
    @Operation(summary = "批量更新账户条款")
   // @PreAuthorize("@ss.hasPermission('base:account:update')")
    public CommonResult<Boolean> enableAccountList(@RequestBody List<AccountSaveReqVO> saveReqVOS) {
        accountService.enableAccountList(saveReqVOS);
        return success(true);
    }

}