package com.willalp.web.controller.clockingin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.event.domain.HsWelfareApplyRecord;
import com.willalp.event.service.IHsWelfareApplyRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 福利报名记录Controller
 *
 * @author willalp
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/event/applyRecord")
public class HsWelfareApplyRecordController extends BaseController {
    @Resource
    private IHsWelfareApplyRecordService hsWelfareApplyRecordService;

    /**
     * 查询福利报名记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HsWelfareApplyRecord hsWelfareApplyRecord) {
        startPage();
        List<HsWelfareApplyRecord> list = hsWelfareApplyRecordService.selectHsWelfareApplyRecordList(hsWelfareApplyRecord);
        List<HsWelfareApplyRecord> source = hsWelfareApplyRecordService.list(new QueryWrapper<HsWelfareApplyRecord>()
                .groupBy("source"));
        return getDataTable(list);
    }

    /**
     * 查询福利报名记录列表
     */
    @GetMapping("/sourceList")
    public TableDataInfo sourceList() {
        startPage();
        List<HsWelfareApplyRecord> source = hsWelfareApplyRecordService.list(new QueryWrapper<HsWelfareApplyRecord>()
                .groupBy("source"));
        return getDataTable(source);
    }

    /**
     * 查询福利报名记录列表
     */
    @GetMapping("/stat")
    public AjaxResult stat(String source) {
        return AjaxResult.success(hsWelfareApplyRecordService.stat(source));
    }

    /**
     * 查询福利报名记录列表
     */
    @GetMapping("/doUnregistered")
    public AjaxResult doUnregistered(String source) {
        return toAjax(hsWelfareApplyRecordService.doUnregistered(source));
    }

    /**
     * 导出福利报名记录列表
     */
    @PreAuthorize("@ss.hasPermi('event:applyRecord:export')")
    @Log(title = "福利报名记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsWelfareApplyRecord hsWelfareApplyRecord) {
        List<HsWelfareApplyRecord> list = hsWelfareApplyRecordService.exportDataAndInterpret(hsWelfareApplyRecord);
        ExcelUtil<HsWelfareApplyRecord> util = new ExcelUtil<>(HsWelfareApplyRecord.class);
        return util.exportExcel(list, "福利报名记录数据");
    }

    @GetMapping("/importTemp")
    public AjaxResult importTemp() {
        ExcelUtil<HsWelfareApplyRecord> util = new ExcelUtil<>(HsWelfareApplyRecord.class);
        return util.exportExcel(hsWelfareApplyRecordService.importInitDataTemp(), "福利领取初始人员信息模板");
    }

    @Log(title = "福利报名记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<HsWelfareApplyRecord> util = new ExcelUtil<>(HsWelfareApplyRecord.class);
        //获取数据库对应数据
        List<HsWelfareApplyRecord> list = util.importExcel(file.getInputStream(), 0);
        String originalFilename = file.getOriginalFilename();
        originalFilename = originalFilename.substring(0, originalFilename.indexOf("."));
        return AjaxResult.success(hsWelfareApplyRecordService.importData(list, originalFilename));
    }

    /**
     * 获取福利报名记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('event:applyRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsWelfareApplyRecordService.getById(id));
    }

    /**
     * 新增福利报名记录
     */
    @PreAuthorize("@ss.hasPermi('event:applyRecord:add')")
    @Log(title = "福利报名记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsWelfareApplyRecord hsWelfareApplyRecord) {
        hsWelfareApplyRecord.setCreateBy(SecurityUtils.getUsername());
        hsWelfareApplyRecord.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsWelfareApplyRecordService.save(hsWelfareApplyRecord));
    }

    /**
     * 修改福利报名记录
     */
    @PreAuthorize("@ss.hasPermi('event:applyRecord:edit')")
    @Log(title = "福利报名记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsWelfareApplyRecord hsWelfareApplyRecord) {
        hsWelfareApplyRecord.setUpdateBy(SecurityUtils.getUsername());
        hsWelfareApplyRecord.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsWelfareApplyRecordService.updateById(hsWelfareApplyRecord));
    }

    /**
     * 删除福利报名记录
     */
    @PreAuthorize("@ss.hasPermi('event:applyRecord:remove')")
    @Log(title = "福利报名记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsWelfareApplyRecordService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 删除指定excel导入的数据
     *
     * @param source
     * @return
     */
    @GetMapping("/deleteSource")
    public AjaxResult deleteSource(String source) {
        if (StringUtils.isBlank(source)) {
            return AjaxResult.error();
        }
        return toAjax(hsWelfareApplyRecordService.remove(new QueryWrapper<HsWelfareApplyRecord>()
                .eq("source", source)));
    }
}
