package com.xjrsoft.module.banding.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.banding.dto.AddBandingTaskDto;
import com.xjrsoft.module.banding.dto.AddBandingTaskRuleDto;
import com.xjrsoft.module.banding.dto.AutomaticBandingTaskDto;
import com.xjrsoft.module.banding.dto.BandingTaskPageDto;
import com.xjrsoft.module.banding.dto.SureBandingTaskDto;
import com.xjrsoft.module.banding.dto.UpdateBandingTaskDto;
import com.xjrsoft.module.banding.entity.BandingRule;
import com.xjrsoft.module.banding.entity.BandingTask;
import com.xjrsoft.module.banding.entity.BandingTaskRule;
import com.xjrsoft.module.banding.service.IBandingRuleService;
import com.xjrsoft.module.banding.service.IBandingTaskRuleService;
import com.xjrsoft.module.banding.service.IBandingTaskService;
import com.xjrsoft.module.banding.vo.BandingTaskPageVo;
import com.xjrsoft.module.banding.vo.BandingTaskRuleVo;
import com.xjrsoft.module.banding.vo.BandingTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @title: 新生分班任务
* @Author dzx
* @Date: 2024-07-01
* @Version 1.0
*/
@RestController
@RequestMapping("/banding" + "/bandingTask")
@Api(value = "/banding"  + "/bandingTask",tags = "新生分班任务代码")
@AllArgsConstructor
public class BandingTaskController {


    private final IBandingTaskService bandingTaskService;
    private final IBandingRuleService ruleService;
    private final IBandingTaskRuleService taskRuleService;

    @GetMapping(value = "/page")
    @ApiOperation(value="新生分班任务列表(分页)")
    @SaCheckPermission("bandingtask:detail")
    public RT<PageOutput<BandingTaskPageVo>> page(@Valid BandingTaskPageDto dto){

        IPage<BandingTaskPageVo> page = bandingTaskService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<BandingTaskPageVo> pageOutput = ConventPage.getPageOutput(page, BandingTaskPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询新生分班任务信息")
    @SaCheckPermission("bandingtask:detail")
    public RT<BandingTaskVo> info(@RequestParam Long id){
        BandingTask bandingTask = bandingTaskService.getById(id);
        if (bandingTask == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(bandingTask, BandingTaskVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增新生分班任务")
    @SaCheckPermission("bandingtask:add")
    public RT<Boolean> add(@Valid @RequestBody AddBandingTaskDto dto){
        List<BandingTask> list = bandingTaskService.list(
                new QueryWrapper<BandingTask>().lambda()
                        .eq(BandingTask::getGradeId, dto.getGradeId())
                        .eq(BandingTask::getEnrollType, dto.getEnrollType())
        );
        if (!list.isEmpty()) {
            return RT.error("已存在一个任务，无法添加");
        }
        BandingTask bandingTask = BeanUtil.toBean(dto, BandingTask.class);
        boolean isSuccess = bandingTaskService.add(bandingTask);
        return RT.ok(isSuccess);
    }

    @PostMapping("/set-rule")
    @ApiOperation(value = "设置规则")
    @SaCheckPermission("bandingtask:set-rule")
    public RT<Boolean> setRule(@Valid @RequestBody AddBandingTaskRuleDto dto){
        taskRuleService.remove(
                new QueryWrapper<BandingTaskRule>().lambda()
                        .eq(BandingTaskRule::getBandingTaskId, dto.getBandingTaskId())
        );

        List<BandingTaskRule> ruleList = new ArrayList<>();
        for (Long bandingRuleId : dto.getBandingRuleIds()) {
            BandingTaskRule taskRule = new BandingTaskRule();
            taskRule.setBandingTaskId(dto.getBandingTaskId());
            taskRule.setBandingRuleId(bandingRuleId);
            taskRule.setCreateDate(new Date());
            ruleList.add(taskRule);
        }
        boolean isSuccess = taskRuleService.saveBatch(ruleList);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改新生分班任务")
    @SaCheckPermission("bandingtask:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBandingTaskDto dto){

        BandingTask bandingTask = BeanUtil.toBean(dto, BandingTask.class);
        return RT.ok(bandingTaskService.update(bandingTask));

    }

    @DeleteMapping
    @ApiOperation(value = "删除新生分班任务")
    @SaCheckPermission("bandingtask:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(bandingTaskService.delete(ids));
    }

    @GetMapping(value = "/rule-list")
    @ApiOperation(value="查询所有规则")
    @SaCheckPermission("bandingtask:detail")
    public RT<List<BandingTaskRuleVo>> ruleList(@RequestParam Long id){
        List<BandingRule> ruleList = ruleService.list();

        Set<Long> collect = taskRuleService.list(new QueryWrapper<BandingTaskRule>().lambda().eq(BandingTaskRule::getBandingTaskId, id))
                .stream().map(BandingTaskRule::getBandingRuleId).collect(Collectors.toSet());

        List<BandingTaskRuleVo> list = new ArrayList<>();
        for (BandingRule bandingRule : ruleList) {
            BandingTaskRuleVo bandingTaskRuleVo = BeanUtil.toBean(bandingRule, BandingTaskRuleVo.class);
            bandingTaskRuleVo.setIsSelected(0);
            if(collect.contains(bandingRule.getId())){
                bandingTaskRuleVo.setIsSelected(1);
            }
            list.add(bandingTaskRuleVo);
        }
        return RT.ok(list);
    }
    @PostMapping("/sure")
    @ApiOperation(value = "确认分班")
    @SaCheckPermission("bandingTaskClass:sure")
    public RT<Boolean> sure(@Valid @RequestBody SureBandingTaskDto dto){
        Boolean isSuccess = bandingTaskService.sure(dto);
        return RT.ok(isSuccess);
    }



    @PostMapping("/automatic-banding")
    @ApiOperation(value = "自动分班")
    @SaCheckPermission("bandingTaskClass:automatic-banding")
    public RT<Boolean> automaticBanding(@Valid @RequestBody AutomaticBandingTaskDto dto){
        Boolean aBoolean = bandingTaskService.automaticBanding(dto);
        return RT.ok(aBoolean);
    }

}