package com.ruoyi.web.controller.drill;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DeletedEnum;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.drill.domain.DrillContingent;
import com.ruoyi.drill.service.IDrillContingentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * DrillContingentController
 *
 * @Auther: mtx
 * @Date: 2023/09/09 9:31
 */
@Api(tags = "演习队伍")
@RestController
@RequestMapping("/drill/contingent")
public class DrillContingentController extends BaseController {

    @Resource
    private IDrillContingentService drillContingentService;

    /**
     * 列表页
     */
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('drill:contingent:list')")
    @GetMapping("/list")
    @ApiOperation("查询演习队伍列表")
    public TableDataInfo list(DrillContingent drillContingent) {
        startPage();
        LambdaQueryWrapper<DrillContingent> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(drillContingent.getContingentName())) {
            wrapper.like(DrillContingent::getContingentName, drillContingent.getContingentName());
        }
        wrapper.eq(DrillContingent::getIsDel, DeletedEnum.VALID.getValue());
        wrapper.orderByDesc(DrillContingent::getUpdateDate);
        List<DrillContingent> list = drillContingentService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 编辑演习队伍
     */
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('drill:contingent:add')")
    @Log(title = "新增演习队伍", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增演习队伍")
    public AjaxResult add(@Validated @RequestBody DrillContingent drillContingent) {
        return drillContingentService.saveAndUpdate(drillContingent);
    }

    //TODO 20230920PreAuthorize("@("@ss.hasPermi('drill:contingent:edit')")
    @Log(title = "编辑演习队伍", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("编辑演习队伍")
    public AjaxResult edit(@Validated @RequestBody DrillContingent drillContingent) {
        return drillContingentService.saveAndUpdate(drillContingent);
    }

    /**
     * 删除演习队伍
     */
    //TODO 20230920PreAuthorize("@("@ss.hasPermi('drill:contingent:remove')")
    @Log(title = "删除演习队伍", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("删除演习队伍")
    public AjaxResult remove(String ids) {
        String regex = ",";
        List<String> list = Collections.singletonList(ids);
        if (StringUtils.isNotBlank(ids) && ids.contains(regex)) {
            String[] split = ids.split(regex);
            list = Arrays.asList(split);
        }
        return toAjax(drillContingentService.removeBatchByIds(list));
    }

}
