package com.flossom.system.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.flossom.common.core.domain.entity.IntegralClock;
import com.flossom.common.core.utils.poi.ExcelUtil;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
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.flossom.system.service.IIntegralClockService;

/**
 * 打卡奖励积分Controller
 *
 * @author flossom
 * @date 2024-01-25
 */
@RestController
@RequestMapping("/clockIntegral")
public class IntegralClockController extends BaseController
{
    @Autowired
    private IIntegralClockService integralClockService;

    /**
     * 查询打卡奖励积分列表
     */
    @RequiresPermissions("system:clockIntegral:list")
    @GetMapping("/list")
    public TableDataInfo list(IntegralClock integralClock)
    {
        startPage();
        List<IntegralClock> list = integralClockService.selectIntegralClockList(integralClock);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (IntegralClock clock:list) {
            if (Objects.nonNull(clock.getStartTime())) {
                clock.getTimeRange().add(sd.format(clock.getStartTime()));
            }
            if (Objects.nonNull(clock.getEndTime())) {
                clock.getTimeRange().add(sd.format(clock.getEndTime()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出打卡奖励积分列表
     */
    @RequiresPermissions("system:clockIntegral:export")
    @Log(title = "打卡奖励积分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IntegralClock integralClock)
    {
        List<IntegralClock> list = integralClockService.selectIntegralClockList(integralClock);
        ExcelUtil<IntegralClock> util = new ExcelUtil<IntegralClock>(IntegralClock.class);
        util.exportExcel(response, list, "打卡奖励积分数据");
    }

    /**
     * 获取打卡奖励积分详细信息
     */
    @RequiresPermissions("system:clockIntegral:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(integralClockService.selectIntegralClockById(id));
    }

    /**
     * 新增打卡奖励积分
     */
    @RequiresPermissions("system:clockIntegral:add")
    @Log(title = "打卡奖励积分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntegralClock integralClock)
    {
        return toAjax(integralClockService.insertIntegralClock(integralClock));
    }

    /**
     * 修改打卡奖励积分
     */
    @RequiresPermissions("system:clockIntegral:edit")
    @Log(title = "打卡奖励积分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntegralClock integralClock)
    {
        return toAjax(integralClockService.updateIntegralClock(integralClock));
    }

    /**
     * 删除打卡奖励积分
     */
    @RequiresPermissions("system:clockIntegral:remove")
    @Log(title = "打卡奖励积分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(integralClockService.deleteIntegralClockByIds(ids));
    }
}
