package com.staffing.web.controller.merchant;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.staffing.common.annotation.Excel;
import com.staffing.common.annotation.Log;
import com.staffing.common.core.controller.BaseController;
import com.staffing.common.core.domain.AjaxResult;
import com.staffing.common.core.page.TableDataInfo;
import com.staffing.common.enums.BusinessType;
import com.staffing.common.utils.poi.ExcelUtil;
import com.staffing.custom.domain.StEmployeeFundRecord;
import com.staffing.custom.domain.StTask;
import com.staffing.custom.domain.StTaskSign;
import com.staffing.custom.service.IStEmployeeFundRecordService;
import com.staffing.custom.service.IStTaskService;
import com.staffing.custom.service.IStTaskSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * 任务Controller
 *
 * @author staffing
 * @date 2024-08-27
 */
@RestController
@RequestMapping("/merchant/task")
public class MerchantTaskController extends BaseController
{
    @Autowired
    private IStTaskService stTaskService;
    @Autowired
    private IStTaskSignService stTaskSignService;
    @Autowired
    private IStEmployeeFundRecordService employeeFundRecordService;
    /**
     * 查询任务列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('task:edit')")
    public TableDataInfo list(StTask stTask)
    {
        startPage();
        stTask.setStatus(1);
        // 获取当前时间
        ZonedDateTime now = ZonedDateTime.now();
        // 将当前时间加上一天，‌然后设置为0点0分
        ZonedDateTime nextDayStart = now.plusDays(0).truncatedTo(ChronoUnit.DAYS);
        Date dayZero = Date.from(nextDayStart.toInstant());
        if (stTask.getDateStatus() != 0){
            stTask.setTaskDateCompare(dayZero);
        }
        stTask.getParams().put("order", dayZero);
        List<StTask> list = stTaskService.selectStTaskList(stTask, 0);
        return getDataTable(list);
    }

    /**
     * 获取任务详细信息
     */
    @GetMapping(value = "/details/{id}")
    @PreAuthorize("@ss.hasPermi('task:edit')")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stTaskService.selectStTaskById(id));
    }

    /**
     * 获取任务详细信息
     */
    @GetMapping(value = "/fxInfo/{id}")
    @PreAuthorize("@ss.hasPermi('task:edit')")
    public AjaxResult fxInfo(@PathVariable("id") Long id)
    {
        return success(stTaskService.selectStTaskById(id, true));
    }

    /**
     * 修改签到
     */
    @PutMapping("/editSign")
    @PreAuthorize("@ss.hasPermi('task:edit')")
    public AjaxResult signUp(@RequestBody StTaskSign updateStTaskSign)
    {
        return toAjax(stTaskSignService.signUp(updateStTaskSign));
    }


}
