package com.willalp.web.controller.clockingin;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.BusinessType;
import com.willalp.clockingin.domain.HsPnsClockingIn;
import com.willalp.clockingin.service.IHsPnsClockingInService;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 每日考勤信息Controller
 *
 * @author willalp
 * @date 2022-04-26
 */
@RestController
@RequestMapping("/clockingin/clockingin")
public class HsPnsClockingInController extends BaseController
{
    @Resource
    private IHsPnsClockingInService hsPnsClockingInService;

    /**
     * 查询每日考勤信息列表
     */
    @PreAuthorize("@ss.hasPermi('clockingin:clockingin:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsPnsClockingIn hsPnsClockingIn)
    {
        startPage();
        List<HsPnsClockingIn> list = hsPnsClockingInService.selectHsPnsClockingInList(hsPnsClockingIn);
        return getDataTable(list);
    }

    /**
     * 导出每日考勤信息列表
     */
    @PreAuthorize("@ss.hasPermi('clockingin:clockingin:export')")
    @Log(title = "每日考勤信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsPnsClockingIn hsPnsClockingIn)
    {
        List<HsPnsClockingIn> list = hsPnsClockingInService.selectHsPnsClockingInList(hsPnsClockingIn);
        ExcelUtil<HsPnsClockingIn> util = new ExcelUtil<HsPnsClockingIn>(HsPnsClockingIn.class);
        return util.exportExcel(list, "每日考勤信息数据");
    }

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

    /**
     * 新增每日考勤信息
     */
    @PreAuthorize("@ss.hasPermi('clockingin:clockingin:add')")
    @Log(title = "每日考勤信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsPnsClockingIn hsPnsClockingIn)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsPnsClockingIn byId = hsPnsClockingInService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsPnsClockingIn.setClockingInId(idStr);
                break;
            }
        }
        hsPnsClockingIn.setCreateBy(SecurityUtils.getUsername());
        hsPnsClockingIn.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsPnsClockingInService.save(hsPnsClockingIn));
    }

    /**
     * 修改每日考勤信息
     */
    @PreAuthorize("@ss.hasPermi('clockingin:clockingin:edit')")
    @Log(title = "每日考勤信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsPnsClockingIn hsPnsClockingIn)
    {
        hsPnsClockingIn.setUpdateBy(SecurityUtils.getUsername());
        hsPnsClockingIn.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsPnsClockingInService.updateById(hsPnsClockingIn));
    }

    /**
     * 删除每日考勤信息
     */
    @PreAuthorize("@ss.hasPermi('clockingin:clockingin:remove')")
    @Log(title = "每日考勤信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{clockingInIds}")
    public AjaxResult remove(@PathVariable String[] clockingInIds)
    {
        return toAjax(hsPnsClockingInService.removeByIds(Arrays.asList(clockingInIds)));
    }
}
