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.CouponDTO;
import com.fzm.mall.server.admin.marketing.entity.dto.CouponSaveDTO;
import com.fzm.mall.server.admin.marketing.entity.dto.CouponUpdateDTO;
import com.fzm.mall.server.admin.marketing.service.ICouponService;
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.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
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-10
 */
@Api(tags = "运营中心")
@RestController
@RequestMapping("/coupon/couponPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CouponController {

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

        couponService.save(couponSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

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

        couponService.update(couponUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

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

    @ApiOperation(value = "运营中心-商品优惠券-主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{couponId}")
    public ResponseVO<CouponDTO> read(
            @ApiParam(value = "couponId", required = true) @PathVariable(value = "couponId") String couponId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        CouponDTO result = couponService.read(couponId, admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "运营中心-商品优惠券-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "优惠券编号") @RequestParam(required = false) String couponId,
                           @ApiParam(value = "名称") @RequestParam(required = false) String name,
                           @ApiParam(value = "状态 0->不可用;1->可用;2->未开始;3->已过期") @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<CouponDTO> page = new Page<>();
        if (ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)) {
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        Admin admin = adminService.getByAdminId(adminId);
        IPage<CouponDTO> result = couponService.page(page, couponId, name, status, admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "运营中心-商品优惠券-批量删除")
    @ApiOperationSupport(order = 6)
    @AdminLog(description = "运营中心-商品优惠券-批量删除", module = OperateModuleEnum.OPERATE_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/batchDelete")
    public ResponseVO batchDelete(
            @ApiParam(value = "couponIdList", required = true) @RequestParam(value = "couponIdList") List<String> couponIdList,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        if (CollectionUtils.isNotEmpty(couponIdList)) {
            couponIdList.forEach(couponId -> couponService.delete(couponId, admin.getMerchantId()));
        }
        return resFac.getSimpleSuccessResponse();
    }
}
