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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fzm.mall.server.admin.goods_center.entity.vo.GoodsListVO;
import com.fzm.mall.server.admin.goods_center.service.ISpuService;
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.IntegralReturnMerchantDTO;
import com.fzm.mall.server.admin.opration.entity.dto.IntegralReturnMerchantEditDTO;
import com.fzm.mall.server.admin.opration.entity.vo.IntegralReturnMerchantDetailVO;
import com.fzm.mall.server.admin.opration.entity.vo.IntegralReturnMerchantVO;
import com.fzm.mall.server.admin.opration.service.IIntegralReturnMerchantService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
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;

/**
 * @author wangp
 * @date 2021/7/29 10:50
 * @description 积分返现——商户
 * @since JDK 1.8
 */
@Api(tags = "运营中心-积分返现—商户")
@RestController
@RequestMapping("/integralReturnMerchant")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IntegralReturnMerchantController {

    private final IIntegralReturnMerchantService integralReturnMerchantService;
    private final ISpuService spuService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

//    @ApiIdempotent
    @ApiOperation("保存")
    @PostMapping("/add")
    public ResponseVO add(@RequestBody @Valid IntegralReturnMerchantDTO dto, @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        integralReturnMerchantService.add(dto, admin.getMerchantId());
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation("编辑")
    @PutMapping("/edit")
    public ResponseVO edit(@RequestBody @Valid IntegralReturnMerchantEditDTO dto, @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        integralReturnMerchantService.edit(dto, admin.getMerchantId());
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "根据商品编号或名称查询商品信息")
    @GetMapping("/queryByNameOrId")
    public ResponseVO queryByNameOrId(
            @ApiParam(name = "goods", value = "商品编号或名称")
            @RequestParam(required = false) String goods,
            @ApiParam(name = "adminId", value = "管理员id")
            @RequestHeader String adminId) {
        String merchantId = adminService.getByAdminId(adminId).getMerchantId();
        List<GoodsListVO> vos = spuService.queryByNameOrId(merchantId, goods);
        return resFac.getSuccessResponseWithData(vos);
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseVO page(
            @ApiParam(name = "returnId", value = "活动编号")
            @RequestParam(required = false) String returnId,
            @ApiParam(name = "name", value = "活动名称")
            @RequestParam(required = false) String name,
            @ApiParam(name = "status", value = "状态(1.未开始 2.可用 3.不可用 4.已过期)")
            @RequestParam(required = false) Integer status,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        IPage<IntegralReturnMerchantVO> page = integralReturnMerchantService.page(returnId, name, status, pageNum, pageSize);
        return resFac.getSuccessResponseWithData(page);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{returnId}")
    public ResponseVO delete(@ApiParam(name = "returnId", value = "返现id") @PathVariable String returnId) {
        integralReturnMerchantService.delete(returnId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/deleteBatch")
    public ResponseVO deleteBatch(@RequestBody List<String> returnIds) {
        integralReturnMerchantService.deleteBatch(returnIds);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation("详情")
    @GetMapping("/detail")
    public ResponseVO detail(@ApiParam(name = "returnId", value = "返现id") @RequestParam String returnId) {
        IntegralReturnMerchantDetailVO detail = integralReturnMerchantService.detail(returnId);
        return resFac.getSuccessResponseWithData(detail);
    }

    @ApiOperation("下架")
    @PutMapping("/offShelf")
    public ResponseVO offShelf(@ApiParam(name = "returnId", value = "返现id") @RequestParam String returnId) {
        integralReturnMerchantService.offShelf(returnId);
        return resFac.getSimpleSuccessResponse();
    }
}
