package jnpf.attendance.controller;


import com.github.pagehelper.PageInfo;
import jnpf.attendance.service.PublicHolidayRulesService;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.attendance.dto.AttendancePublicHolidayRulesDto;
import jnpf.model.attendance.vo.attendance.AttendancePublicHolidayRulesVo;
import jnpf.model.common.PageDto;
import jnpf.util.FtbUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 考勤公休规则
 * @author panpan
 * @version V2.0
 */

@RestController
@RequestMapping(value = "/attendance/public-holiday")
@Slf4j
public class PublicHolidayRulesController {

    @Autowired
    private PublicHolidayRulesService publicHolidayRulesService;

    /**
     * 获取考勤公休规则列表
     * @param iText 规则名称模糊查询 非必传
     */
    @GetMapping("/list")
    public ActionResult<PageListVO<AttendancePublicHolidayRulesVo>> list(@RequestParam(value = "iText", required = false) String iText, PageDto pageDto) {
        PageInfo<AttendancePublicHolidayRulesVo> vo = publicHolidayRulesService.list(iText,pageDto);
        return ActionResult.page(vo.getList(), FtbUtil.getPagination(vo));
    }

    /**
     * 新增公休规则
     * @param publicHolidayRulesDto 公休规则
     */
    @PostMapping()
    public ActionResult<Void> add(@RequestBody AttendancePublicHolidayRulesDto publicHolidayRulesDto) throws Exception {
        publicHolidayRulesService.add(publicHolidayRulesDto);
        return ActionResult.success();
    }


    /**
     * 修改公休规则
     * @param publicHolidayRulesDto 公休规则
     */
    @PutMapping("/{id}")
    public ActionResult<Void> put(@PathVariable("id") String id, @RequestBody AttendancePublicHolidayRulesDto publicHolidayRulesDto) throws  Exception{
        publicHolidayRulesDto.setId(id);
        publicHolidayRulesService.update(publicHolidayRulesDto);
        return ActionResult.success();
    }

    /**
     * 获取公休规则详情
     * @param id 公休规则 ID
     */
    @GetMapping("/{id}")
    public ActionResult<AttendancePublicHolidayRulesVo> detail(@PathVariable("id") String id) {
        return ActionResult.success(publicHolidayRulesService.selectOne(id));
    }


    /**
     * 删除公休规则
     * @param id 公休规则 ID
     */
    @DeleteMapping("/{id}")
    public ActionResult<Void> delete(@PathVariable("id") String id) {
        publicHolidayRulesService.delete(id);
        return ActionResult.success();
    }

    /**
     * 启用、停用公休规则
     * @param publicHolidayRulesDto 公休规则
     */
    @PutMapping("/updateState")
    public ActionResult<Void> updateState( @RequestBody AttendancePublicHolidayRulesDto publicHolidayRulesDto) {
        publicHolidayRulesService.updateState(publicHolidayRulesDto);
        return ActionResult.success();
    }

}
