package jnpf.attendance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import jnpf.attendance.service.AttendanceLeaveRulesService;
import jnpf.attendance.service.AttendanceLeaveTypeService;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.entity.attendance.AttendanceLeaveType;
import jnpf.enums.attendance.LeaveTypeEnum;
import jnpf.enums.attendance.LeaveUnitEnum;
import jnpf.exception.ApproveException;
import jnpf.model.attendance.dto.AttendanceLeaveRulesDto;
import jnpf.model.attendance.dto.AttendanceLeaveRulesQueryDto;
import jnpf.model.attendance.vo.attendance.AttendanceLeaveRulesVo;
import jnpf.model.attendance.vo.attendance.GroupMiniVo;
import jnpf.util.FtbUtil;
import jnpf.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 假期规则
 *
 * @author panpan
 * @create 2025-09-18
 */
@RestController
@RequestMapping(value = "/attendance/leave-rules")
@Slf4j
public class AttendanceLeaveRulesController {

    @Autowired
    private AttendanceLeaveRulesService attendanceLeaveRulesService;

    @Resource
    private AttendanceLeaveTypeService attendanceLeaveTypeService;

    /**
     * 获取考勤假期管理列表
     * @param attendanceLeaveRulesQueryDto 查询参数
     */
    @GetMapping("/list")
    public ActionResult<PageListVO<AttendanceLeaveRulesVo>> list(AttendanceLeaveRulesQueryDto attendanceLeaveRulesQueryDto) {
        PageInfo<AttendanceLeaveRulesVo> vo = attendanceLeaveRulesService.list(attendanceLeaveRulesQueryDto);
        return ActionResult.page(vo.getList(), FtbUtil.getPagination(vo));
    }

    /**
     * 新增假期
     * @param attendanceLeaveRulesDto 假期
     */
    @PostMapping()
    public ActionResult<Void> add(@RequestBody AttendanceLeaveRulesDto attendanceLeaveRulesDto) throws Exception {
        attendanceLeaveRulesService.create(attendanceLeaveRulesDto);
        return ActionResult.success();
    }


    /**
     * 修改假期
     * @param attendanceLeaveRulesDto 假期
     */
    @PutMapping("/{id}")
    public ActionResult<Void> put(@PathVariable("id") String id, @RequestBody AttendanceLeaveRulesDto attendanceLeaveRulesDto) throws Exception {
        attendanceLeaveRulesDto.setId(id);
        attendanceLeaveRulesService.update(attendanceLeaveRulesDto);
        return ActionResult.success();
    }

    /**
     * 获取假期详情
     * @param id 假期 ID
     */
    @GetMapping("/{id}")
    public ActionResult<AttendanceLeaveRulesVo> detail(@PathVariable("id") String id) {
        return ActionResult.success(attendanceLeaveRulesService.detail(id));
    }


    /**
     * 删除假期
     * @param id 假期 ID
     */
    @DeleteMapping("/{id}")
    public ActionResult<Void> delete(@PathVariable("id") String id) {
        attendanceLeaveRulesService.delete(id);
        return ActionResult.success();
    }

    /**
     * 启用、停用假期
     * @param attendanceLeaveRulesDto 假期
     */
    @PutMapping("/updateState")
    public ActionResult<Void> updateState( @RequestBody AttendanceLeaveRulesDto attendanceLeaveRulesDto) {
        attendanceLeaveRulesService.updateState(attendanceLeaveRulesDto);
        return ActionResult.success();
    }

    /**
     * 获取用户请假列表
     */
    @GetMapping("/getUserLeaveList")
    public ActionResult<List<AttendanceLeaveRulesVo>> getUserLeaveList(@RequestParam(required = false) boolean isFromOaCondition) {
        if (isFromOaCondition){
            List<AttendanceLeaveType> list = attendanceLeaveTypeService.list(new LambdaQueryWrapper<AttendanceLeaveType>()
                    .eq(AttendanceLeaveType::getDeleteMark, 0)
                    .orderByAsc(AttendanceLeaveType::getCreatorTime));
            return ActionResult.success(list.stream().map(vo->{
                AttendanceLeaveRulesVo attendanceLeaveRulesVo = new AttendanceLeaveRulesVo();
                attendanceLeaveRulesVo.setLeaveTypeId(vo.getId());
                attendanceLeaveRulesVo.setLeaveTypeName(vo.getName());
                return attendanceLeaveRulesVo;
            }).collect(Collectors.toList()));
        }
        return ActionResult.success(attendanceLeaveTypeService.getUserLeaveList());
    }

    /**
     * 获取用户请假详情
     */

    @GetMapping("/getUserLeaveDetail")
    public ActionResult<List<AttendanceLeaveRulesVo>> getUserLeaveDetail(AttendanceLeaveType attendanceLeaveType ,@RequestParam(required = false) boolean isFromOaCondition) throws ApproveException {
        if (isFromOaCondition) {
            List<AttendanceLeaveRulesVo> list = new ArrayList<>();
            for (LeaveTypeEnum value : LeaveTypeEnum.values()) {
                AttendanceLeaveRulesVo attendanceLeaveRulesVo = new AttendanceLeaveRulesVo();
                attendanceLeaveRulesVo.setUnit(value.getCode());
                attendanceLeaveRulesVo.setUnitName(value.getMsg());
                list.add(attendanceLeaveRulesVo);
            }
            return ActionResult.success(list);
        }
        AttendanceLeaveRulesVo vo = attendanceLeaveTypeService.getUserLeaveDetail(attendanceLeaveType.getId(),null);
        List<AttendanceLeaveRulesVo> list = new ArrayList<>();
        list.add(vo);
        return ActionResult.success(list);
    }

    /**
     * 获取用户请假详情
     */
    @GetMapping("/getLeaveDetailForOa")
    public ActionResult<AttendanceLeaveRulesVo> getLeaveDetailForOa(AttendanceLeaveType attendanceLeaveType) throws ApproveException  {
        return ActionResult.success(attendanceLeaveTypeService.getUserLeaveDetail(attendanceLeaveType.getId(),null));
    }

    /**
     * 获取请假类型列表
     */
    @GetMapping("/type/list")
    public ActionResult<List<AttendanceLeaveType>> leaveTypeList(@RequestParam(value = "keyword", required = false) String keyword) {
        List<AttendanceLeaveType> vo = attendanceLeaveTypeService.list(new LambdaQueryWrapper<AttendanceLeaveType>()
                        .eq(AttendanceLeaveType::getDeleteMark, 0)
                        .like(StringUtil.isNotEmpty(keyword), AttendanceLeaveType::getName, keyword)
                        .orderByAsc(AttendanceLeaveType::getCreatorTime));
        return ActionResult.success(vo);
    }

    /**
     * 新增/修改假期类型
     * @param attendanceLeaveType 假期类型
     */
    @PostMapping("/type")
    public ActionResult<Void> typeSaveOrUpdate(@RequestBody AttendanceLeaveType attendanceLeaveType)  {
        attendanceLeaveTypeService.typeSaveOrUpdate(attendanceLeaveType);
        return ActionResult.success();
    }


    /**
     * 删除假期类型
     * @param id 假期类型Id
     */
    @DeleteMapping("/type/{id}")
    public ActionResult<Void> deleteType(@PathVariable("id") String id)  {
        attendanceLeaveTypeService.deleteType(id);
        return ActionResult.success();
    }

}
