package com.staffing.web.controller.employee;

import com.staffing.common.core.controller.BaseController;
import com.staffing.common.core.domain.AjaxResult;
import com.staffing.common.core.domain.entity.SysUser;
import com.staffing.common.core.page.TableDataInfo;
import com.staffing.common.utils.SecurityUtils;
import com.staffing.custom.domain.StEmployee;
import com.staffing.custom.domain.StTask;
import com.staffing.custom.domain.StTaskSign;
import com.staffing.custom.service.IStEmployeeService;
import com.staffing.custom.service.IStTaskService;
import com.staffing.custom.service.IStTaskSignService;
import com.staffing.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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("/employee/task")
public class EmployeeTaskController extends BaseController
{
    @Autowired
    private IStTaskService stTaskService;
    @Autowired
    private IStTaskSignService stTaskSignService;

    @Autowired
    private IStEmployeeService stEmployeeService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StTask stTask, @RequestParam(required = false) boolean self, @RequestParam(required = false) boolean xinchou)
    {

        if (!xinchou){
            stTask.setDateStatus(1);
        }else{
            stTask.setDateStatus(0);
        }
        stTask.setStatus(1);
        // 获取当前时间
        ZonedDateTime now = ZonedDateTime.now();
        // 今日0点
        ZonedDateTime nextDayStart = now.plusDays(0).truncatedTo(ChronoUnit.DAYS);
        Date dayZero = Date.from(nextDayStart.toInstant());

        if (stTask.getDateStatus() != 0){
            stTask.setTaskDateCompare(dayZero);
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        long userId = user.getUserId();
        List<StTask> list;
        stTask.getParams().put("order", dayZero);
        if (!self){
            list = stTaskService.selectStTaskList(stTask, userId);
        }else{
            stTask.setDateStatus(0);
            list = stTaskService.selectStTaskListByUserId(stTask, userId, xinchou);
        }
        return getDataTable(list);
    }



    @GetMapping("/baoming")
    public AjaxResult baoming(StTaskSign taskSign)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        long userId = user.getUserId();

        StEmployee stEmploye = new StEmployee();
        stEmploye.setId(userId);
        stEmploye.setRealName(taskSign.getEmployeeName());
        stEmploye.setIdCard(taskSign.getIdCard());
        stEmployeeService.updateStEmployee(stEmploye);


        user = SecurityUtils.getLoginUser().getUser();
        user.setNickName(taskSign.getEmployeeName());
        user.setPhonenumber(taskSign.getPhone());
        user.setIdCard(taskSign.getIdCard());
        tokenService.refreshToken(SecurityUtils.getLoginUser());




        return stTaskSignService.baoming(taskSign);
    }
    /**
     * 修改签到
     */
    @GetMapping("/cancelBaoming")
    public AjaxResult cancelBaoming(StTaskSign taskSign)
    {
        return toAjax(stTaskSignService.deleteStTaskSignById(taskSign.getId()));
    }


    /**
     * 获取任务详细信息
     */
    @GetMapping(value = "/getTask")
    public AjaxResult getInfo(@RequestParam("taskId") Long id)
    {
        return success(stTaskService.selectStTaskById(id));
    }



    private long getUid(){
        return 1l;
    }

}
