package com.ylx.attendanceconfig.controller;

import com.ylx.attendanceconfig.domain.dto.AttendanceRuleAddDTO;
import com.ylx.attendanceconfig.domain.vo.AttendanceRuleDetailVO;
import com.ylx.attendanceconfig.service.AttendanceConfigService;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/attendance/config")
@Api(tags = {"考勤规则配置"})
public class AttendanceConfigController {

    @Resource
    private AttendanceConfigService attendanceConfigService;

    /**
     * 新增考勤规则
     *
     * @param dto 考勤规则参数
     * @return 操作结果
     */
    @PostMapping
    @ApiOperation("新增考勤规则")
    @Log(title = "新增考勤规则", businessType = BusinessType.INSERT)
    public R<?> add(@Validated @RequestBody AttendanceRuleAddDTO dto) {
        try {
            attendanceConfigService.addAttendanceRule(dto);
            return R.ok();
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("新增考勤规则异常", e);
            return R.fail("新增考勤规则失败");
        }
    }

    /**
     * 查询全部考勤规则
     *
     * @return R<List<AttendanceRuleDetailVO>> 考勤规则详情集合
     */
    @GetMapping
    @ApiOperation("查询全部考勤规则")
    public R<List<AttendanceRuleDetailVO>> query() {
        try {
            List<AttendanceRuleDetailVO> list = attendanceConfigService.listAttendanceRules();
            return R.ok(list);
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询全部考勤规则异常", e);
            return R.fail("查询全部考勤规则失败");
        }
    }

    /**
     * 删除考勤规则
     *
     * @param id 考勤规则ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除考勤规则")
    @Log(title = "删除考勤规则", businessType = BusinessType.DELETE)
    public R<?> delete(@PathVariable("id") Long id) {
        try {
            attendanceConfigService.deleteAttendanceRule(id);
            return R.ok();
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除考勤规则异常", e);
            return R.fail("删除考勤规则失败");
        }
    }

    /**
     * 删除考勤扣款区间
     *
     * @param id 扣款区间ID
     * @return 操作结果
     */
    @DeleteMapping("/deduction/{id}")
    @ApiOperation("删除考勤扣款区间")
    @Log(title = "删除考勤扣款区间", businessType = BusinessType.DELETE)
    public R<?> deleteDeduction(@PathVariable("id") Long id) {
        try {
            attendanceConfigService.deleteDeductionRule(id);
            return R.ok();
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除考勤扣款区间异常", e);
            return R.fail("删除考勤扣款区间失败");
        }
    }
}
