package jnpf.attendance.controller;

import com.github.pagehelper.PageInfo;
import jnpf.attendance.service.AttendanceMachineManageService;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.dto.PageDto;
import jnpf.model.attendance.dto.MachineDto;
import jnpf.model.attendance.dto.MachineQueryDto;
import jnpf.model.attendance.dto.MachineUpdateDto;
import jnpf.model.attendance.vo.AttendanceMachineManageVo;
import jnpf.model.attendance.vo.MachineScopeVo;
import jnpf.model.attendance.vo.attendance.GroupMiniVo;
import jnpf.model.attendance.vo.attendance.GroupUserMiniVo;
import jnpf.model.attendance.vo.attendance.LogMiniVo;
import jnpf.util.FtbUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 考勤机管理
 *
 * @author yanwenfu
 * @create 2024-09-10
 */
@RestController
@RequestMapping(value = "/machineManage")
public class AttendanceMachineManageController {

    @Resource
    private AttendanceMachineManageService attendanceMachineManageService;

    /**
     * 考勤机管理 - 考勤组列表
     * @param machineId 考勤机id
     * @return java.lang.Object
     */
    @GetMapping(value = "/group/list")
    public Object getGroupList(@RequestParam(required = false) String machineId) {

        List<GroupMiniVo> list = attendanceMachineManageService.getGroupList(machineId);
        return ActionResult.success(list);
    }

    /**
     * 考勤机管理 - 考勤机列表
     * @param queryDto 查询条件
     * @return java.lang.Object
     */
    @GetMapping(value = "/machine/list")
    public Object getMachineList(MachineQueryDto queryDto) {

        PageListVO<AttendanceMachineManageVo> page = attendanceMachineManageService.getMachineList(queryDto);
        return ActionResult.page(page.getList(), page.getPagination());
    }

    /**
     * 考勤机管理 - 查询考勤机更新信息
     * @param id 考勤机id
     * @return jnpf.base.ActionResult<jnpf.model.attendance.vo.MachineScopeVo>
     */
    @GetMapping(value = "/update-detail/{id}")
    public ActionResult<MachineScopeVo> getUpdateDetail(@PathVariable(value = "id") String id) {

        MachineScopeVo machineScope = attendanceMachineManageService.getUpdateDetail(id);
        return ActionResult.success(machineScope);
    }

    /**
     * 考勤机管理 - 添加考勤机
     * @param machineDto 考勤机信息
     * @return java.lang.Object
     */
    @PostMapping(value = "/machine")
    public Object addMachine(@RequestBody @Valid MachineDto machineDto) throws Exception {

        attendanceMachineManageService.addMachine(machineDto);
        return ActionResult.success();
    }

    /**
     * 考勤机管理 - 编辑考勤机
     * @param id 考勤机id
     * @param machineUpdateDto 考勤机信息
     * @return java.lang.Object
     */
    @PutMapping(value = "/machine/{id}")
    public Object updateMachine(@PathVariable(value = "id") String id, @RequestBody @Valid MachineUpdateDto machineUpdateDto) throws Exception {

        attendanceMachineManageService.updateMachine(id, machineUpdateDto);
        return ActionResult.success();
    }

    /**
     * 考勤机管理 - 移除
     * @param id 考勤机id
     * @return java.lang.Object
     */
    @DeleteMapping(value = "/machine/{id}")
    public Object deleteMachine(@PathVariable(value = "id") String id) throws Exception {

        attendanceMachineManageService.deleteMachine(id);
        return ActionResult.success();
    }

    /**
     * 考勤机管理 - 考勤组成员列表
     * @param groupId 考勤组id
     * @return java.lang.Object
     */
    @GetMapping(value = "/group/user/{groupId}")
    public Object getGroupUserList(@PathVariable(value = "groupId") String groupId) {

        List<GroupUserMiniVo> list = attendanceMachineManageService.getGroupUserList(groupId);
        return ActionResult.success(list);
    }

    @GetMapping(value = "/group/user")
    public Object getGroupUserList2(@RequestParam(value = "groupId") String groupId) {

        List<GroupUserMiniVo> list = attendanceMachineManageService.getGroupUserList(groupId);
        return ActionResult.success(list);
    }

    /**
     * 同步考勤机成员
     * @param id 考勤机id
     * @return java.lang.Object
     */
    @PutMapping("/sync/{id}")
    public Object syncMachineMemberData(@PathVariable(value = "id") String id) {

        List<GroupUserMiniVo> list = attendanceMachineManageService.syncMachineMemberData(id);
        return ActionResult.success(list);
    }

    /**
     * 考勤机管理 - 人脸库
     * @param id 考勤机id
     * @return java.lang.Object
     */
    @GetMapping(value = "/member/list/{id}")
    public Object getMachineMemberList(@PathVariable(value = "id") String id) {

        List<GroupUserMiniVo> list = attendanceMachineManageService.getMachineMemberList(id);
        return ActionResult.success(list);
    }

    /**
     * 查询考勤机打卡记录(查往前3个月的数据)
     * @param id 考勤机id
     * @return java.lang.Object
     */
    @GetMapping(value = "/record/list/{id}")
    public ActionResult<PageListVO<LogMiniVo>> getLogList(@PathVariable(value = "id") String id, PageDto pageQuery) {

        PageInfo<LogMiniVo> page = attendanceMachineManageService.getLogList(id, pageQuery);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }
}
