package top.iletter.modules.baitutools.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.iletter.common.annotation.LogOperation;
import top.iletter.common.constant.Constant;
import top.iletter.common.page.PageData;
import top.iletter.common.utils.ExcelUtils;
import top.iletter.common.utils.Result;
import top.iletter.common.validator.AssertUtils;
import top.iletter.common.validator.ValidatorUtils;
import top.iletter.common.validator.group.AddGroup;
import top.iletter.common.validator.group.DefaultGroup;
import top.iletter.common.validator.group.UpdateGroup;
import top.iletter.modules.baitutools.dto.DlRenewalRemindDTO;
import top.iletter.modules.baitutools.excel.DlRenewalRemindExcel;
import top.iletter.modules.baitutools.service.DlRenewalRemindService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 临期续费
 *
 * @author dellevin dellevin99@gmail.com
 * @since 1.0.0 2026-02-04
 */
@RestController
@RequestMapping("baitutools/dlrenewalremind")
@Tag(name="临期续费")
public class DlRenewalRemindController {
    private static final Logger log = LoggerFactory.getLogger(DlRenewalRemindController.class);
    @Autowired
    private DlRenewalRemindService dlRenewalRemindService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref="int") ,
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY,required = true, ref="int") ,
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref="String") ,
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref="String"),
        @Parameter(name = "名称", description = "名称搜索", in = ParameterIn.QUERY, ref="String")
    })
    @RequiresPermissions("baitutools:dlrenewalremind:page")
    public Result<PageData<DlRenewalRemindDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        PageData<DlRenewalRemindDTO> page = dlRenewalRemindService.page(params);

        return new Result<PageData<DlRenewalRemindDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("baitutools:dlrenewalremind:info")
    public Result<DlRenewalRemindDTO> get(@PathVariable("id") Long id){
        DlRenewalRemindDTO data = dlRenewalRemindService.get(id);

        return new Result<DlRenewalRemindDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("baitutools:dlrenewalremind:save")
    public Result save(@RequestBody DlRenewalRemindDTO dto){
        log.info( ">>>>>>>>>"+dto.toString());
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dlRenewalRemindService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("baitutools:dlrenewalremind:update")
    public Result update(@RequestBody DlRenewalRemindDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dlRenewalRemindService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("baitutools:dlrenewalremind:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        dlRenewalRemindService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("baitutools:dlrenewalremind:export")
    public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<DlRenewalRemindDTO> list = dlRenewalRemindService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "临期续费", list, DlRenewalRemindExcel.class);
    }

}
