package com.fzm.mall.server.admin.marketing.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.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.marketing.entity.dto.PromotionDTO;
import com.fzm.mall.server.admin.marketing.entity.dto.PromotionSaveDTO;
import com.fzm.mall.server.admin.marketing.entity.dto.PromotionUpdateDTO;
import com.fzm.mall.server.admin.marketing.service.IPromotionService;
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-10
 */
@Api(tags = "运营中心")
@RestController
@RequestMapping("/promotion/promotionPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PromotionController {

    private final IPromotionService promotionService;
    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 PromotionSaveDTO promotionSaveDTO,
                           @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        promotionSaveDTO.setMerchantId(admin.getMerchantId());

        promotionService.save(promotionSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "运营中心-促销活动-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "运营中心-促销活动-编辑", module = OperateModuleEnum.OPERATE_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid PromotionUpdateDTO promotionUpdateDTO,
                             @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        promotionUpdateDTO.setMerchantId(admin.getMerchantId());

        promotionService.update(promotionUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "运营中心-促销活动-删除")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "运营中心-促销活动-删除", module = OperateModuleEnum.OPERATE_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{promotionId}")
    public ResponseVO delete(
            @ApiParam(value = "promotionId", required = true) @PathVariable(value = "promotionId") String promotionId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        promotionService.delete(promotionId, admin.getMerchantId());
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "运营中心-促销活动-主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{promotionId}")
    public ResponseVO<PromotionDTO> read(
            @ApiParam(value = "promotionId", required = true) @PathVariable(value = "promotionId") String promotionId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        PromotionDTO result = promotionService.read(promotionId, admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "运营中心-促销活动-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "促销编号") @RequestParam(required = false) String promotionId,
                           @ApiParam(value = "名称") @RequestParam(required = false) String name,
                           @ApiParam(value = "状态 0->不可用;1->可用;") @RequestParam(required = false) Integer status,
                           @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum) {
        Page<PromotionDTO> page = new Page<>();
        if(ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)){
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        Admin admin = adminService.getByAdminId(adminId);
        IPage<PromotionDTO> result = promotionService.page(page, promotionId, name, status, admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }
}
