package com.ylx.giftCard.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import com.ylx.giftCard.domain.GiftCard;
import com.ylx.giftCard.domain.dto.GiftCardManageQueryDTO;
import com.ylx.giftCard.domain.dto.GiftCardManageSaveDTO;
import com.ylx.giftCard.domain.dto.GiftCardManageUpdateDTO;
import com.ylx.giftCard.domain.dto.GiftCardPublishStatusDTO;
import com.ylx.giftCard.domain.vo.GiftCardManageDetailVO;
import com.ylx.giftCard.domain.vo.GiftCardManageExportVO;
import com.ylx.giftCard.domain.vo.GiftCardManagePageVO;
import com.ylx.giftCard.service.IGiftCardService;
import com.ylx.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/gift/card/manage")
@Api(tags = {"购物卡管理"})
public class GiftCardManageController {

    @Resource
    private IGiftCardService giftCardService;

    /**
     * 分页查询购物卡
     * @param page
     * @param dto
     * @return R<Page<GiftCardManagePageVO>>
     */
    @GetMapping("/page")
    @ApiOperation("分页查询购物卡")
    public R<Page<GiftCardManagePageVO>> page(Page<GiftCard> page, GiftCardManageQueryDTO dto) {
        try {
            return R.ok(giftCardService.getManagePage(page, dto));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("分页查询购物卡异常", e);
            return R.fail("分页查询购物卡失败");
        }
    }

    /**
     * 导出购物卡
     * @param response
     * @param dto
     */
    @PostMapping("/export")
    @ApiOperation("导出购物卡")
    @Log(title = "导出购物卡", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, GiftCardManageQueryDTO dto) {
        try {
            List<GiftCardManageExportVO> list = giftCardService.getManageExportList(dto);
            ExcelUtil<GiftCardManageExportVO> util = new ExcelUtil<>(GiftCardManageExportVO.class);
            util.exportExcel(response, list, "购物卡管理");
        } catch (ServiceException e) {
            log.error("导出购物卡参数异常", e);
            throw e;
        } catch (Exception e) {
            log.error("导出购物卡异常", e);
            throw new RuntimeException("导出购物卡失败", e);
        }
    }

    /**
     * 查询购物卡详情
     * @param id
     * @return R<GiftCardManageDetailVO>
     */
    @GetMapping("/{id}")
    @ApiOperation("查询购物卡详情")
    public R<GiftCardManageDetailVO> detail(@PathVariable("id") Long id) {
        try {
            return R.ok(giftCardService.getManageDetail(id));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询购物卡详情异常", e);
            return R.fail("查询购物卡详情失败");
        }
    }

    /**
     * 新增购物卡
     * @param dto
     * @return R<?>
     */
    @PostMapping
    @ApiOperation("新增购物卡")
    @Log(title = "新增购物卡", businessType = BusinessType.INSERT)
    public R<?> add(@Validated @RequestBody GiftCardManageSaveDTO dto) {
        try {
            giftCardService.addGiftCard(dto);
            return R.ok();
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("新增购物卡异常", e);
            return R.fail("新增购物卡失败");
        }
    }

    /**
     * 编辑购物卡
     * @param dto
     * @return R<?>
     */
    @PutMapping
    @ApiOperation("编辑购物卡")
    @Log(title = "编辑购物卡", businessType = BusinessType.UPDATE)
    public R<?> edit(@Validated @RequestBody GiftCardManageUpdateDTO dto) {
        try {
            giftCardService.updateGiftCard(dto);
            return R.ok();
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("编辑购物卡异常", e);
            return R.fail("编辑购物卡失败");
        }
    }

    @PutMapping("/publish")
    @ApiOperation("修改购物卡上架状态")
    @Log(title = "修改购物卡上架状态", businessType = BusinessType.UPDATE)
    public R<?> updatePublishStatus(@Validated @RequestBody GiftCardPublishStatusDTO dto) {
        try {
            giftCardService.updatePublishStatus(dto);
            return R.ok();
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("修改购物卡上架状态异常", e);
            return R.fail("修改购物卡上架状态失败");
        }
    }

    /**
     * 删除购物卡
     * @param id
     * @return R<?>
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除购物卡")
    @Log(title = "删除购物卡", businessType = BusinessType.DELETE)
    public R<?> delete(@PathVariable("id") Long id) {
        try {
            giftCardService.deleteGiftCard(id);
            return R.ok();
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除购物卡异常", e);
            return R.fail("删除购物卡失败");
        }
    }
}
