package com.tofly.filling.controller;

import com.alibaba.fastjson.JSONObject;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.filling.entity.Filling;
import com.tofly.filling.entity.dto.FillingDto;
import com.tofly.filling.entity.duty.DutyDto;
import com.tofly.filling.entity.duty.WorkOvertimeDto;
import com.tofly.filling.service.FillingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author HaiQiu
 * @since 2023/1/10
 */
@Api(tags = "交接班人员当面签")
@RestController
@RequestMapping("shiftPersonnel")
public class ShiftPersonnelController {

    /**
     * 服务对象
     */
    @Resource
    private FillingService fillingService;

    /**
     * 新增数据
     *
     * @param workOvertimeDto 实体
     * @return 新增结果
     */
    @ToFlyAppLog(title = "交接班人员当面签-新增数据")
    @ApiOperation(value = "新增数据")
    @PostMapping
    public ResultRespone insert(@Validated @RequestBody WorkOvertimeDto workOvertimeDto) {
        Filling filling = new Filling();
        BeanUtils.copyProperties(workOvertimeDto, filling);
        filling.setCategory("0");
        filling.setName("交接班人员当面签");
        if (workOvertimeDto.getCompanyUnit() == null) {
            filling.setCompanyUnit(SecurityUtils.getUserDeptId());
        }
        if(filling.getDutyDate()==null){
            filling.setDutyDate(new Date());

        }
        String content = JSONObject.toJSONString(workOvertimeDto);
        filling.setContent(content);
        return ResultRespone.success(fillingService.save(filling), "新增成功");
    }

    /**
     * 修改数据
     *
     * @param workOvertimeDto 实体
     * @return 新增结果
     */
    @ToFlyAppLog(title = "交接班人员当面签-修改数据")
    @ApiOperation(value = "修改数据")
    @PutMapping
    public ResultRespone edit(@Validated @RequestBody WorkOvertimeDto workOvertimeDto) {
        //FillingController.verificationDate(workOvertimeDto.getCreateTime(),new Date());
        Filling filling = new Filling();
        BeanUtils.copyProperties(workOvertimeDto, filling);
        filling.setCategory("0");
        filling.setName("交接班人员当面签");
        if (workOvertimeDto.getCompanyUnit() == null) {
            filling.setCompanyUnit(SecurityUtils.getUserDeptId());
        }
        String content = JSONObject.toJSONString(workOvertimeDto);
        filling.setContent(content);
        return ResultRespone.success(fillingService.updateById(filling), "修改成功");
    }
}
