package com.tofly.filling.controller;

import com.alibaba.fastjson.JSONObject;
import com.tofly.common.core.entity.ResultRespone;
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.car.UseCarDto;
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("userCar")
public class UseCarController {

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

    /**
     * 新增数据
     *
     * @param useCarDto 实体
     * @return 新增结果
     */
    @ToFlyAppLog(title = "用车记录填报-新增数据")
    @ApiOperation(value = "新增数据")
    @PostMapping
    public ResultRespone insert(@Validated @RequestBody UseCarDto useCarDto) {
        Filling filling = new Filling();
        BeanUtils.copyProperties(useCarDto, filling);
        filling.setCategory("1");
        filling.setName("用车记录填报");
        if (useCarDto.getCompanyUnit() == null) {
            filling.setCompanyUnit(SecurityUtils.getUserDeptId());
        }
        String content = JSONObject.toJSONString(useCarDto);
        filling.setContent(content);
        if(filling.getDutyDate()==null){
            filling.setDutyDate(new Date());

        }
        return ResultRespone.success(fillingService.save(filling), "新增成功");
    }

    /**
     * 修改数据
     *
     * @param useCarDto 实体
     * @return 新增结果
     */
    @ToFlyAppLog(title = "用车记录填报-修改数据")
    @ApiOperation(value = "修改数据")
    @PutMapping
    public ResultRespone edit(@Validated @RequestBody UseCarDto useCarDto) {
        //FillingController.verificationDate(useCarDto.getCreateTime(),new Date());
        Filling filling = new Filling();
        BeanUtils.copyProperties(useCarDto, filling);
        filling.setCategory("1");
        filling.setName("用车记录填报");
        if (useCarDto.getCompanyUnit() == null) {
            filling.setCompanyUnit(SecurityUtils.getUserDeptId());
        }
        String content = JSONObject.toJSONString(useCarDto);
        filling.setContent(content);
        return ResultRespone.success(fillingService.updateById(filling), "修改成功");
    }
}
