package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsEmailService;
import com.gxwebsoft.oa.entity.OaAssetsEmail;
import com.gxwebsoft.oa.param.OaAssetsEmailParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 企业邮箱记录表控制器
 *
 * @author 科技小王子
 * @since 2024-10-18 18:27:02
 */
@Tag(name = "企业邮箱记录表管理")
@RestController
@RequestMapping("/api/oa/oa-assets-email")
public class OaAssetsEmailController extends BaseController {
    @Resource
    private OaAssetsEmailService oaAssetsEmailService;

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:list')")
    @Operation(summary = "分页查询企业邮箱记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsEmail>> page(OaAssetsEmailParam param) {
        // 使用关联查询
        return success(oaAssetsEmailService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:list')")
    @Operation(summary = "查询全部企业邮箱记录表")
    @GetMapping()
    public ApiResult<List<OaAssetsEmail>> list(OaAssetsEmailParam param) {
        // 使用关联查询
        return success(oaAssetsEmailService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:list')")
    @OperationLog
    @Operation(summary = "根据id查询企业邮箱记录表")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsEmail> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsEmailService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:save')")
    @OperationLog
    @Operation(summary = "添加企业邮箱记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsEmail oaAssetsEmail) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsEmail.setUserId(loginUser.getUserId());
        }
        if (oaAssetsEmailService.save(oaAssetsEmail)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:update')")
    @Operation(summary = "修改企业邮箱记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsEmail oaAssetsEmail) {
        if (oaAssetsEmailService.updateById(oaAssetsEmail)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:remove')")
    @Operation(summary = "删除企业邮箱记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsEmailService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:save')")
    @Operation(summary = "批量添加企业邮箱记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsEmail> list) {
        if (oaAssetsEmailService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:update')")
    @Operation(summary = "批量修改企业邮箱记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsEmail> batchParam) {
        if (batchParam.update(oaAssetsEmailService, "email_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsEmail:remove')")
    @Operation(summary = "批量删除企业邮箱记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsEmailService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
