/*
 * Author: Zev
 * 生成controller
 */
package com.tofly.gpsboot.controller.sign;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;

import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.plan.dto.PdasignDto;
import com.tofly.pojo.plan.vo.PdaSignMonitorVo;
import com.tofly.pojo.plan.vo.PdasignVo;

import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Pdasign;

import com.tofly.gpsboot.service.sign.PdasignService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.validation.Valid;
import java.util.Optional;

/**
 * 上下班签到瞬时信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/pdasign")
@Api(tags="上下班签到瞬时信息")
public class PdasignController extends BaseController<Pdasign, PdasignVo> {
    @Autowired
    private PdasignService service;

    @Autowired
    public PdasignController(PdasignService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询上下班签到瞬时信息")
    public ResultRespone<Page<PdasignVo>> getPdasignPage(Page page, @ModelAttribute PdasignDto parDto) {
        return super.getInfoPage(page, JSON.parseObject(JSON.toJSONString(parDto), Pdasign.class));
    }

    @ApiOperation(value = "用户签到")
    @PostMapping("/userSign")
    @ToFlyAppLog(title = "用户签到")
    public ResultRespone<Object> userSign(@Valid @RequestBody PdasignDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
                rest.setResult(service.userSign(parDto));
            }
        }catch (BusinessException e){
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @GetMapping("/getUserSignState/{id}")
    @ApiOperation(value = "根据用户id 查询当前用户是否签到,签到类型；1：上班 2：下班")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "根据用户id 查询当前用户是否签到,签到类型；1：上班 2：下班")
    public ResultRespone<String> getByIdBuild(@PathVariable Long id) {
        ResultRespone<String> rest = new ResultRespone<>();
        try {
            String userSignState = service.getUserSignState(id);
            rest.setResult(userSignState);
        }catch (BusinessException e){
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;

    }


    @GetMapping("/getUserSignList")
    @ApiOperation(value = "获取当前用户下部门中所有人员的签到信息")
    @ToFlyAppLog(title = "获取当前用户下部门中所有人员的签到信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "分组id", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "time", value = "查询时间 不传默认今天,格式 YYYY-MM-DD HH:mm:ss", required = false, dataType = "String"),
            @ApiImplicitParam(name = "deptId", value = "部门id,不传默认为当前部门", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "userIds", value = "人员ids,不传默认为当前部门所有", required = false, dataType = "String")

    })
    public ResultRespone<PdaSignMonitorVo> getUserSignList(String groupId,String time,Long deptId,String userIds) {
        return ResultRespone.success(service.getUserSignList(groupId,time,deptId,userIds));
    }




}
