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.FunctionDTO;
import com.fzm.mall.server.admin.opration.entity.dto.FunctionSaveDTO;
import com.fzm.mall.server.admin.opration.entity.dto.FunctionUpdateDTO;
import com.fzm.mall.server.admin.opration.model.Banner;
import com.fzm.mall.server.admin.opration.service.IFunctionService;
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>
 * 运营中心-功能区 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-22
 */
@Api(tags = "运营中心")
@RestController
@RequestMapping("/function/functionPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FunctionController {

    private final IFunctionService functionService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

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

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

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

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

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

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

    /**
     * 获取当前用户所属的对应banner的商户id（平台设置为1）
     *
     * @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;
    }
}
