package org.dromara.ygpassport.controller;

import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.ygpassport.listener.VolunteeringImportListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ygpassport.domain.vo.VolunteeringVo;
import org.dromara.ygpassport.domain.bo.VolunteeringBo;
import org.dromara.ygpassport.service.IVolunteeringService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 志愿服务
 *
 * @author Lion Li
 * @date 2024-03-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/volunteering")
public class VolunteeringController extends BaseController {

    private final IVolunteeringService volunteeringService;

    /**
     * 查询志愿服务列表
     */
    @SaCheckPermission("ygpassport:volunteering:list")
    @GetMapping("/list")
    public TableDataInfo<VolunteeringVo> list(VolunteeringBo bo, PageQuery pageQuery) {
        return volunteeringService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出志愿服务列表
     */
    @SaCheckPermission("ygpassport:volunteering:export")
    @Log(title = "志愿服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VolunteeringBo bo, HttpServletResponse response) {
        List<VolunteeringVo> list = volunteeringService.queryList(bo);
        ExcelUtil.exportExcel(list, "志愿服务", VolunteeringVo.class, response);
    }

    /**
     * 导入志愿服务数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "志愿服务", businessType = BusinessType.IMPORT)
    @SaCheckPermission("ygpassport:volunteering:import")
    @PostMapping(value = "/importVolunteeringData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        VolunteeringImportListener listener = new VolunteeringImportListener(updateSupport);
        ExcelResult<VolunteeringVo> result = ExcelUtil.importExcel(file.getInputStream(), VolunteeringVo.class, listener);
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "志愿服务信息", VolunteeringVo.class, response);
    }

    /**
     * 获取志愿服务详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ygpassport:volunteering:query")
    @GetMapping("/{id}")
    public R<VolunteeringVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(volunteeringService.queryById(id));
    }

    /**
     * 新增志愿服务
     */
    @SaCheckPermission("ygpassport:volunteering:add")
    @Log(title = "志愿服务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VolunteeringBo bo) {
        return toAjax(volunteeringService.insertByBo(bo));
    }

    /**
     * 修改志愿服务
     */
    @SaCheckPermission("ygpassport:volunteering:edit")
    @Log(title = "志愿服务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VolunteeringBo bo) {
        return toAjax(volunteeringService.updateByBo(bo));
    }

    /**
     * 删除志愿服务
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ygpassport:volunteering:remove")
    @Log(title = "志愿服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(volunteeringService.deleteWithValidByIds(List.of(ids), true));
    }
}
