package com.willalp.web.controller.clockingin;

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.poi.ExcelUtil;
import com.willalp.stat.domain.HsStatClockingIn;
import com.willalp.stat.service.IHsStatClockingInService;
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 2022-08-03
 */
@RestController
@RequestMapping("/clockingin/stat")
public class HsStatClockingInController extends BaseController {
    @Resource
    private IHsStatClockingInService hsStatClockingInService;

    /**
     * 查询统计考勤列表
     */
    @PreAuthorize("@ss.hasPermi('clockingin:stat:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsStatClockingIn hsStatClockingIn) {
        startPage();
        String username = SecurityUtils.getUsername();
        hsStatClockingIn.setUserCode(username);
        List<HsStatClockingIn> list = hsStatClockingInService.selectHsStatClockingInList(hsStatClockingIn);
        return getDataTable(list);
    }

    /**
     * 导出统计考勤列表
     */
    @PreAuthorize("@ss.hasPermi('clockingin:stat:export')")
    @Log(title = "统计考勤", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsStatClockingIn hsStatClockingIn) {
        List<HsStatClockingIn> list = hsStatClockingInService.selectHsStatClockingInList(hsStatClockingIn);
        ExcelUtil<HsStatClockingIn> util = new ExcelUtil<HsStatClockingIn>(HsStatClockingIn.class);
        util.createTitle();
        return util.exportExcel(list, "统计考勤数据");
    }

    @Log(title = "工资条基础", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<HsStatClockingIn> util = new ExcelUtil<>(HsStatClockingIn.class);
        //获取数据库对应数据
        List<HsStatClockingIn> list = util.importExcel(file.getInputStream(), 1);
        return AjaxResult.success(hsStatClockingInService.importData(list, util, file.getInputStream()));
    }

    @GetMapping("/importTemp")
    public AjaxResult importTemp() {
        ExcelUtil<HsStatClockingIn> util = new ExcelUtil<>(HsStatClockingIn.class);
        return util.importTemplateExcel("【统计考勤导入模板】");
    }

    /**
     * 获取统计考勤详细信息
     */
    @PreAuthorize("@ss.hasPermi('clockingin:stat:query')")
    @GetMapping(value = "/s/{id}")
    public AjaxResult getInfos(@PathVariable("id") String id) {
        return AjaxResult.success(hsStatClockingInService.getById(id).getZend());
    }


    /**
     * 获取统计考勤详细信息
     */
    @PreAuthorize("@ss.hasPermi('clockingin:stat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsStatClockingInService.getById(id));
    }

    /**
     * 新增统计考勤
     */
    @PreAuthorize("@ss.hasPermi('clockingin:stat:add')")
    @Log(title = "统计考勤", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsStatClockingIn hsStatClockingIn) {
        hsStatClockingIn.setCreateBy(SecurityUtils.getUsername());
        hsStatClockingIn.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsStatClockingInService.save(hsStatClockingIn));
    }

    /**
     * 修改统计考勤
     */
    @PreAuthorize("@ss.hasPermi('clockingin:stat:edit')")
    @Log(title = "统计考勤", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsStatClockingIn hsStatClockingIn) {
        hsStatClockingIn.setUpdateBy(SecurityUtils.getUsername());
        hsStatClockingIn.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsStatClockingInService.updateById(hsStatClockingIn));
    }

    /**
     * 删除统计考勤
     */
    @PreAuthorize("@ss.hasPermi('clockingin:stat:remove')")
    @Log(title = "统计考勤", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsStatClockingInService.removeByIds(Arrays.asList(ids)));
    }
}
