package com.mosty.base.manager.controller;

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.mosty.base.manager.service.TbJcglXfllFzService;
import com.mosty.base.model.entity.jcgl.TbJcglXfllFz;
import com.mosty.base.model.query.jcgl.TbJcglXfllFzQuery;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.token.JwtSysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@AllArgsConstructor
@Api(tags = "巡防分组接口")
@RequestMapping("/tbJcglXfllFz")
public class TbJcglXfllFzController {

    private final TbJcglXfllFzService tbJcglXfllFzService;

    @GetMapping("{id}")
    @JwtSysUser
    @ApiOperation(value = "通过ID查询单条数据")
    @Log(title = "巡防力量ID查询", businessType = BusinessType.OTHER)
    @ApiImplicitParam(name = "id", value = "巡防分组ID", paramType = "path", required = true, dataType = "String")
    public ResponseResult<TbJcglXfllFz> queryById(@PathVariable("id") String id) {
        return ResponseResult.success(tbJcglXfllFzService.getById(id));
    }

    @ApiOperation(value = "分页查询", httpMethod = "GET")
    @JwtSysUser
    @GetMapping
    @Log(title = "巡防分组分页查询", businessType = BusinessType.OTHER)
    public ResponseResult<IPage<TbJcglXfllFz>> paginQuery(TbJcglXfllFzQuery query) {
        IPage<TbJcglXfllFz> page = new Page<>(query.getPageCurrent(), query.getPageSize());
        page = this.tbJcglXfllFzService.page(page, new QueryWrapper<TbJcglXfllFz>().lambda()
                .like(StringUtils.isNotBlank(query.getJzmc()), TbJcglXfllFz::getJzmc, query.getJzmc())
                .like(StringUtils.isNotBlank(query.getKeyword()), TbJcglXfllFz::getJzry, query.getKeyword())
                .orderByDesc(TbJcglXfllFz::getXtCjsj)
        );
        return ResponseResult.success(page);
    }

    @ApiOperation("新增数据")
    @PostMapping
    @JwtSysUser
    @Log(title = "巡防力量分组", businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> add(@RequestBody TbJcglXfllFz fz) {
        tbJcglXfllFzService.save(fz);
        return ResponseResult.success();
    }

    @ApiOperation("修改数据")
    @PutMapping
    @JwtSysUser
    @Log(title = "修改巡防分组", businessType = BusinessType.UPDATE)
    public ResponseResult<Boolean> update(@RequestBody TbJcglXfllFz fz) {
        tbJcglXfllFzService.updateById(fz);
        return ResponseResult.success();
    }

    @ApiOperation("通过主键删除数据")
    @JwtSysUser
    @PostMapping("deleteList")
    @Log(title = "巡防分组删除", businessType = BusinessType.DELETE)
    @ApiImplicitParam(name = "id", value = "ID", paramType = "query", required = true, dataType = "List")
    public ResponseResult deleteById(@RequestBody List<String> id) {
        return ResponseResult.success(tbJcglXfllFzService.removeByIds(id));
    }

    @ApiOperation("通过主键删除数据(单个)")
    @PostMapping("delXfll/{id}")
    @Log(title = "通过主键删除数据(单个)", businessType = BusinessType.DELETE)
    public ResponseResult deleteById(@PathVariable("id") String id) {
        return ResponseResult.success(tbJcglXfllFzService.removeById(id));
    }

}