package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyConsumerVouchersPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyConsumerVouchersRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyConsumerVouchersSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.ElderlyConsumerVouchersImportExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyConsumerVouchersDO;
import cn.iocoder.yudao.module.system.service.biz.ElderlyConsumerVouchersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 长者消费券")
@RestController
@RequestMapping("/elderly/consumer-vouchers")
@Validated
public class ElderlyConsumerVouchersController {

    @Resource
    private ElderlyConsumerVouchersService elderlyConsumerVouchersService;

    @PostMapping("/create")
    @Operation(summary = "创建长者消费券")
    @TenantIgnore
    public CommonResult<Long> create(@Valid @RequestBody ElderlyConsumerVouchersDO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(elderlyConsumerVouchersService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新长者消费券")
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlyConsumerVouchersSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(updateReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        elderlyConsumerVouchersService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除长者消费券")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        elderlyConsumerVouchersService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者消费券")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ElderlyConsumerVouchersRespVO> get(@RequestParam("id") Long id) {
        ElderlyConsumerVouchersDO data = elderlyConsumerVouchersService.get(id);
        return success(BeanUtils.toBean(data, ElderlyConsumerVouchersRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者消费券分页")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyConsumerVouchersRespVO>> getPage(@Valid ElderlyConsumerVouchersPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<ElderlyConsumerVouchersDO> pageResult = elderlyConsumerVouchersService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ElderlyConsumerVouchersRespVO.class));
    }

    @GetMapping("/import-template")
    @Operation(summary = "下载长者消费券导入模板")
    @TenantIgnore
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtils.write(response, "长者消费券导入模板.xlsx", "模板",
                ElderlyConsumerVouchersImportExcelVO.class, Collections.emptyList());
    }

    @PostMapping("/import")
    @Operation(summary = "导入长者消费券")
    @TenantIgnore
    public CommonResult<Boolean> importData(@RequestParam("file") MultipartFile file) throws IOException {
        List<ElderlyConsumerVouchersImportExcelVO> list = ExcelUtils.read(file, ElderlyConsumerVouchersImportExcelVO.class);
        elderlyConsumerVouchersService.importData(list);
        return success(true);
    }
}
