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


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.opration.entity.dto.BannerDTO;
import com.fzm.mall.server.admin.opration.entity.dto.BannerSaveDTO;
import com.fzm.mall.server.admin.opration.entity.dto.BannerUpdateDTO;
import com.fzm.mall.server.admin.opration.model.Banner;
import com.fzm.mall.server.admin.opration.service.IBannerService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 运营中心-banner 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-22
 */
@Api(tags = "运营中心")
@RestController
@RequestMapping("/banner/bannerPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BannerController {

    private final IBannerService bannerService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "运营中心-banner-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "运营中心-banner-新增", module = OperateModuleEnum.OPERATE_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid BannerSaveDTO bannerSaveDTO,
                           @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        bannerSaveDTO.setMerchantId(initMerchantId(adminId));
        bannerService.save(bannerSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "运营中心-banner-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "运营中心-banner-编辑", module = OperateModuleEnum.OPERATE_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid BannerUpdateDTO bannerUpdateDTO,
                             @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        bannerUpdateDTO.setMerchantId(initMerchantId(adminId));
        bannerService.update(bannerUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "运营中心-banner-删除")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "运营中心-banner-删除", module = OperateModuleEnum.OPERATE_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{operationBannerId}")
    public ResponseVO delete(
            @ApiParam(value = "banner编号", required = true) @PathVariable(value = "operationBannerId") String operationBannerId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        bannerService.delete(operationBannerId, initMerchantId(adminId));
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "运营中心-banner-主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{operationBannerId}")
    public ResponseVO<BannerDTO> read(
            @ApiParam(value = "banner编号", required = true) @PathVariable(value = "operationBannerId") String operationBannerId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        BannerDTO result = bannerService.read(operationBannerId, initMerchantId(adminId));
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "运营中心-banner-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {

        List<BannerDTO> result = bannerService.list(initMerchantId(adminId));
        return resFac.getSuccessResponseWithData(result);
    }

    /**
     * 获取当前用户所属的对应banner的商户id
     *
     * @param adminId
     * @return
     */
    private String initMerchantId(String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        String merchantId;
        if (admin.getType().equals(Admin.PALT_TYPE)) {
            merchantId = MALLGlobalConfig.PLATFORM_MERCHANT_ID;
        } else {
            merchantId = admin.getMerchantId();
        }
        return merchantId;
    }
}
