package com.fzm.mall.server.admin.message.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.message.entity.dto.NoticeDTO;
import com.fzm.mall.server.admin.message.entity.dto.NoticeSaveDTO;
import com.fzm.mall.server.admin.message.entity.dto.NoticeUpdateDTO;
import com.fzm.mall.server.admin.message.service.INoticeService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 消息中心-公告 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-09
 */
@Api(tags = "消息中心-公告")
@RestController
@RequestMapping("/notice")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class NoticeController {

    private final INoticeService noticeService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "消息中心-公告-新增", module = OperateModuleEnum.MESSAGE_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/noticeCreatePermission/save")
    public ResponseVO save(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                           @RequestBody @Valid NoticeSaveDTO noticeSaveDTO) {
        Admin admin = adminService.getByAdminId(adminId);
        noticeSaveDTO.setMerchantId(admin.getMerchantId());

        noticeService.save(noticeSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "消息中心-公告-编辑", module = OperateModuleEnum.MESSAGE_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/noticePermission/update")
    public ResponseVO update(@RequestBody @Valid NoticeUpdateDTO noticeUpdateDTO) {
        noticeService.update(noticeUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "删除")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "消息中心-公告-删除", module = OperateModuleEnum.MESSAGE_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/noticePermission/delete/{noticeId}")
    public ResponseVO delete(
            @ApiParam(value = "noticeId", required = true) @PathVariable(value = "noticeId") String noticeId) {
        noticeService.delete(noticeId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/noticePermission/{noticeId}")
    public ResponseVO<NoticeDTO> read(
            @ApiParam(value = "noticeId", required = true) @PathVariable(value = "noticeId") String noticeId) {
        NoticeDTO result = noticeService.read(noticeId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/noticePermission/list")
    public ResponseVO list(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                           @ApiParam(value = "标题或内容") @RequestParam(required = false) String content,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum) {
        Page<NoticeDTO> page = new Page<>();
        if(ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)){
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }
        Admin admin = adminService.getByAdminId(adminId);
        String merchantId = null;
        if (!admin.getMerchantId().equals(MALLGlobalConfig.PLATFORM_MERCHANT_ID)) {
            merchantId = admin.getMerchantId();
        }
        IPage<NoticeDTO> result = noticeService.page(page, content, merchantId);
        return resFac.getSuccessResponseWithData(result);
    }
}
