package com.seer.teach.mall.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.common.ResultBean;
import com.seer.teach.mall.convert.SenderConfigConvert;
import com.seer.teach.mall.sys.service.IAdminSenderConfigService;
import com.seer.teach.mall.entity.SenderConfigEntity;
import com.seer.teach.mall.request.SenderConfigReq;
import com.seer.teach.mall.response.SenderConfigResp;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mall/sys/sender-config")
@Tag(name = "发件信息地址配置管理")
@Validated
@RequiredArgsConstructor
public class AdminSenderConfigController {

    private final IAdminSenderConfigService adminSenderConfigService;

    @Operation(summary = "发件信件地址配置列表查询")
    @GetMapping("/list")
    @SaCheckPermission("admin:sender-config:list")
    public ResultBean<List<SenderConfigResp>> list(){
        List<SenderConfigEntity> senderConfigEntities = adminSenderConfigService.list();
        return ResultBean.success(SenderConfigConvert.INSTANCE.list(senderConfigEntities));
    }

    @Operation(summary = "发件地址配置详情")
    @GetMapping("/{id}")
    @SaCheckPermission("admin:sender-config:detail")
    public ResultBean<SenderConfigResp> getById(@PathVariable("id") Integer id){
        SenderConfigEntity config = adminSenderConfigService.getById(id);
        return ResultBean.success(SenderConfigConvert.INSTANCE.one(config));
    }

    @Operation(summary = "新增发件地址配置")
    @PostMapping("/save")
    @SaCheckPermission("admin:sender-config:save")
    public ResultBean<Boolean> create(@RequestBody @Validated SenderConfigReq req){
        SenderConfigEntity senderConfig = SenderConfigConvert.INSTANCE.req2entity(req);
        return ResultBean.success(adminSenderConfigService.save(senderConfig));
    }

    @Operation(summary = "更新")
    @PutMapping("/update")
    @SaCheckPermission("admin:sender-config:update")
    public ResultBean<Boolean> update(@RequestBody @Validated SenderConfigReq senderConfigReq){
        SenderConfigEntity senderConfig = SenderConfigConvert.INSTANCE.req2entity(senderConfigReq);
        return ResultBean.success(adminSenderConfigService.updateSenderConfig(senderConfig));
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @SaCheckPermission("admin:sender-config:delete")
    public ResultBean<Boolean> delete(@PathVariable("id") Integer id){
        return ResultBean.success(adminSenderConfigService.removeById(id));
    }
}
