package cn.ey.biz.module.system.controller.engagement.task;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.controller.engagement.task.vo.*;
import cn.ey.biz.module.system.convert.engagement.task.EngagementTaskRuleConvert;
import cn.ey.biz.module.system.dal.dataobject.engagement.task.SystemEngagementTaskRuleDO;
import cn.ey.biz.module.system.enums.engagement.DateTypeEnum;
import cn.ey.biz.module.system.enums.engagement.TaskTypeEnum;
import cn.ey.biz.module.system.service.engagement.task.EngagementTaskRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

@Api(tags = "项目管理 - 任务生成规则")
@RestController
@RequestMapping("/system/engagement/task/rule")
public class EngagementTaskRuleController {

    @Resource
    private EngagementTaskRuleService taskRuleService;

    @PostMapping("/create")
    @ApiOperation("创建任务规则")
    @PreAuthorize("@ss.hasPermission('system:engagement-task:create')")
    public R<List<Long>> createTaskRule(
            @Validated(value = EngagementTaskRuleCreateReqVO.Save.class)
            @RequestBody EngagementTaskRuleCreateReqVO reqVO) {
        List<Long> taskRuleId = taskRuleService.createTaskRule(reqVO);
        return success(taskRuleId);
    }

    @PostMapping("/update")
    @ApiOperation("编辑任务规则")
    @PreAuthorize("@ss.hasPermission('system:engagement-task:update')")
    public R<Boolean> updateTaskRule(
            @Validated(value = EngagementTaskRuleReqVO.Update.class)
            @RequestBody EngagementTaskRuleReqVO reqVO) {
        taskRuleService.updateTaskRule(reqVO);
        return success(true);
    }


    @PostMapping("/delete")
    @ApiOperation("删除任务规则")
    @PreAuthorize("@ss.hasPermission('system:engagement-task:delete')")
    public R<Boolean> deleteTaskRule(
            @Validated(value = EngagementTaskRuleInfoReqVO.Deleted.class)
            @RequestBody EngagementTaskRuleInfoReqVO reqVO) {
        taskRuleService.deleteTaskRule(reqVO.getIds());
        return success(true);
    }

    @PostMapping("/get")
    @ApiOperation("查询任务规则详细")
    @PreAuthorize("@ss.hasPermission('system:engagement-task:query')")
    public R<EngagementTaskRuleRespVO> getTaskRule(
            @Validated(value = EngagementTaskRuleInfoReqVO.QueryById.class)
            @RequestBody EngagementTaskRuleInfoReqVO reqVO) {
        EngagementTaskRuleRespVO taskRule = taskRuleService.getTaskRule(reqVO.getId());
        return success(taskRule);
    }

    @PostMapping("/page")
    @ApiOperation("查询任务规则分页列表")
    @PreAuthorize("@ss.hasPermission('system:engagement-task:query')")
    public R<PageResult<EngagementTaskRuleRespVO>> getTaskRulePage(@RequestBody EngagementTaskRuleInfoReqVO reqVO) {
        PageResult<EngagementTaskRuleRespVO> taskRule = taskRuleService.getTaskRulePage(reqVO);
        return success(taskRule);
    }

    @PostMapping("/list-all-simple")
    @ApiOperation("获得全部任务生成规则列表")
    public R<List<EngagementTaskRuleSimpleRespVO>> getTaskRuleSimpleList() {
        List<SystemEngagementTaskRuleDO> codeRuleSimpleList = taskRuleService.getTaskRuleSimpleList();
        List<EngagementTaskRuleSimpleRespVO> doList = EngagementTaskRuleConvert.INSTANCE.doConvertSimpleVo(codeRuleSimpleList);
        return success(doList);
    }
}
