package jnpf.attendance.controller;

import com.github.pagehelper.PageInfo;
import jnpf.attendance.service.OvertimeRuleService;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.attendance.dto.OvertimeRuleDto;
import jnpf.model.attendance.dto.OvertimeRuleQueryDto;
import jnpf.model.attendance.vo.attendance.OvertimeRulePageVo;
import jnpf.model.attendance.vo.attendance.OvertimeRuleVo;
import jnpf.util.DateDetail;
import jnpf.util.FtbUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 加班规则
 *
 * @author yanwenfu
 * @create 2025-09-17
 */
@RestController
@RequestMapping(value = "/overtime-rule")
public class OvertimeRuleController {

    @Resource
    private OvertimeRuleService overtimeRuleService;

    /**
     * 新增加班规则
     * @param overtimeRuleDto 加班规则dto
     * @return java.lang.Object
     */
    @PostMapping
    public Object addOvertimeRule(@RequestBody @Valid OvertimeRuleDto overtimeRuleDto) throws Exception {

        overtimeRuleService.addOvertimeRule(overtimeRuleDto);
        return ActionResult.success();
    }

    /**
     * 编辑加班规则
     * @param id 加班规则id
     * @param overtimeRuleDto 加班规则dto
     * @return java.lang.Object
     */
    @PutMapping(value = "/{id}")
    public Object updateOvertimeRule(@PathVariable(value = "id") String id, @RequestBody @Valid OvertimeRuleDto overtimeRuleDto) throws Exception {

        overtimeRuleService.updateOvertimeRule(id, overtimeRuleDto);
        return ActionResult.success();
    }

    /**
     * 删除加班规则
     * @param id 加班规则id
     * @return java.lang.Object
     */
    @DeleteMapping(value = "/{id}")
    public Object deleteOvertimeRule(@PathVariable(value = "id") String id) {

        overtimeRuleService.deleteOvertimeRule(id);
        return ActionResult.success();
    }

    /**
     * 禁用/启用加班规则
     * @param id 加班规则id
     * @return java.lang.Object
     */
    @PutMapping(value = "/enable/{id}")
    public Object updateEnableStatus(@PathVariable(value = "id") String id) throws Exception {

        overtimeRuleService.updateEnableStatus(id);
        return ActionResult.success();
    }

    /**
     * 查询加班规则详情
     * @param id 加班规则id
     * @return jnpf.base.ActionResult<jnpf.model.attendance.vo.attendance.OvertimeRuleVo>
     */
    @GetMapping(value = "/detail/{id}")
    public ActionResult<OvertimeRuleVo> getDetail(@PathVariable(value = "id") String id) {

        OvertimeRuleVo vo = overtimeRuleService.getDetail(id);
        return ActionResult.success(vo);
    }

    /**
     * 查询加班规则列表(分页)
     * @param queryDto 查询条件
     * @return jnpf.base.ActionResult<jnpf.base.vo.PageListVO<jnpf.model.attendance.vo.attendance.OvertimeRulePageVo>>
     */
    @GetMapping(value = "/page")
    public ActionResult<PageListVO<OvertimeRulePageVo>> getPage(OvertimeRuleQueryDto queryDto) {

        PageInfo<OvertimeRulePageVo> page = overtimeRuleService.getPage(queryDto);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * 查询用户加班规则
     * @param userId 用户id
     * @param queryDate 查询日期(yyyy-MM-dd)
     * @return jnpf.base.ActionResult<jnpf.model.attendance.vo.attendance.OvertimeRuleVo>
     */
    @GetMapping(value = "/user")
    public ActionResult<OvertimeRuleVo> getUserOvertimeRule(@RequestParam String userId, @RequestParam String queryDate) {

        OvertimeRuleVo effectDetail = overtimeRuleService.getEffectDetail(userId, null, DateDetail.getStr2Date(queryDate));
        return ActionResult.success(effectDetail);
    }
}
