package cn.ey.biz.module.system.controller.timesheet.approve;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.controller.timesheet.approve.vo.TimesheetApproveRuleVO;
import cn.ey.biz.module.system.convert.timesheet.TimesheetApproveRuleConvert;
import cn.ey.biz.module.system.dal.dataobject.timesheet.TimesheetApproveRuleDO;
import cn.ey.biz.module.system.service.timesheet.TimesheetApproveRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.BlockingDeque;

import static cn.ey.biz.common.pojo.R.success;

@Api(tags = "工时管理-审批规则")
@RestController
@RequestMapping("system/timesheet/approve/rule")
@Validated
@Slf4j
public class TimesheetApproveRuleController {
    @Resource
    private TimesheetApproveRuleService timesheetApproveRuleService;

    @PostMapping("submit")
    @ApiOperation("提交审批规则")
    @PreAuthorize("@ss.hasPermission('system:timesheet-approve:submit')")
    public R<?> submitApproveRule(@RequestBody List<TimesheetApproveRuleVO> timesheetApproveRuleVOS){
        timesheetApproveRuleService.submitApproveRule(timesheetApproveRuleVOS);
        return R.success(null);
    }

    @GetMapping("/list")
    @ApiOperation("审批规则列表")
    @PreAuthorize("@ss.hasPermission('system:timesheet-approve:query')")
    public R<List<TimesheetApproveRuleVO>> listApproveRule() {
        log.info("/system/timesheet/approve/rule/list 接口进入时间:{}.", System.currentTimeMillis());
        List<TimesheetApproveRuleDO> approveRuleSaveVOS = timesheetApproveRuleService.listApproveRule();
        log.info("/system/timesheet/approve/rule/list 接口返回时间:{}.", System.currentTimeMillis());
        return success(TimesheetApproveRuleConvert.INSTANCE.convertPage(approveRuleSaveVOS));
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermission('system:timesheet-approve:delete')")
    public R<Boolean> deleteTimesheetApprove(@RequestParam("id") Long id){
        timesheetApproveRuleService.deleteTimesheetApprove(id);
        return success(true);
    }

    @PostMapping("/list/page")
    @ApiOperation("审批规则列表")
    @PreAuthorize("@ss.hasPermission('system:timesheet-approve:query')")
    public R<PageResult<TimesheetApproveRuleVO>> listPageApproveRule(@Valid @RequestBody PageParam pageParam) {
        PageResult<TimesheetApproveRuleDO> approveRuleSaveVOS = timesheetApproveRuleService.listPageApproveRule(pageParam);
        return success(TimesheetApproveRuleConvert.INSTANCE.convertListPage(approveRuleSaveVOS));
    }

    @PostMapping("/save")
    @ApiOperation("保存审批规则")
    @PreAuthorize("@ss.hasPermission('system:timesheet-approve:submit')")
    public R<?> saveApproveRule(@RequestBody List<TimesheetApproveRuleVO> timesheetApproveRuleVOS){
        timesheetApproveRuleService.saveApproveRule(timesheetApproveRuleVOS);
        return R.success(null);
    }

}
