package com.xjrsoft.module.banding.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.banding.dto.AddBandingRuleDto;
import com.xjrsoft.module.banding.dto.BandingRulePageDto;
import com.xjrsoft.module.banding.dto.UpdateBandingRuleDto;
import com.xjrsoft.module.banding.entity.BandingRule;
import com.xjrsoft.module.banding.service.IBandingRuleService;
import com.xjrsoft.module.banding.vo.BandingRulePageVo;
import com.xjrsoft.module.banding.vo.BandingRuleVo;
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 org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

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


    private final IBandingRuleService bandingRuleService;

    @GetMapping(value = "/page")
    @ApiOperation(value="新生分班规则列表(分页)")
    @SaCheckPermission("bandingrule:detail")
    public RT<PageOutput<BandingRulePageVo>> page(@Valid BandingRulePageDto dto){

        LambdaQueryWrapper<BandingRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(BandingRule::getId)
                .select(BandingRule.class,x -> VoToColumnUtil.fieldsToColumns(BandingRulePageVo.class).contains(x.getProperty()));
        IPage<BandingRule> page = bandingRuleService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<BandingRulePageVo> pageOutput = ConventPage.getPageOutput(page, BandingRulePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询新生分班规则信息")
    @SaCheckPermission("bandingrule:detail")
    public RT<BandingRuleVo> info(@RequestParam Long id){
        BandingRule bandingRule = bandingRuleService.getById(id);
        if (bandingRule == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(bandingRule, BandingRuleVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增新生分班规则")
    @SaCheckPermission("bandingrule:add")
    public RT<Boolean> add(@Valid @RequestBody AddBandingRuleDto dto){
        BandingRule bandingRule = BeanUtil.toBean(dto, BandingRule.class);
        boolean isSuccess = bandingRuleService.save(bandingRule);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改新生分班规则")
    @SaCheckPermission("bandingrule:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBandingRuleDto dto){

        BandingRule bandingRule = BeanUtil.toBean(dto, BandingRule.class);
        return RT.ok(bandingRuleService.updateById(bandingRule));

    }

    @DeleteMapping
    @ApiOperation(value = "删除新生分班规则")
    @SaCheckPermission("bandingrule:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(bandingRuleService.removeBatchByIds(ids));

    }
    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public RT<Boolean> importData(@RequestParam MultipartFile file) throws IOException {
        List<BandingRulePageVo> savedDataList = EasyExcel.read(file.getInputStream()).head(BandingRulePageVo.class).sheet().doReadSync();
        Boolean result = bandingRuleService.saveBatch(BeanUtil.copyToList(savedDataList, BandingRule.class));
        return RT.ok(result);
    }

}