package com.eleadmin.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.utils.CommonUtil;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.oa.entity.Demo;
import com.eleadmin.oa.param.DemoParam;
import com.eleadmin.oa.server.DemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 测试控制器
 *
 * @author gxwebsoft.com
 * @since 2020-03-14 11:29:03
 */
@Api(tags = "测试管理")
@RestController
@RequestMapping("/api/demo")
public class DemoController extends BaseController {
    @Resource
    private DemoService demoService;

    @PreAuthorize("hasAuthority('sys:demo:list')")
    @OperationLog
    @ApiOperation("分页查询测试")
    @GetMapping("/page")
    public ApiResult<PageResult<Demo>> page(DemoParam param) {
        PageParam<Demo, DemoParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number,create_time desc");
        return success(demoService.page(page, page.getWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:demo:list')")
    @OperationLog
    @ApiOperation("查询全部测试")
    @GetMapping()
    public ApiResult<List<Demo>> list(DemoParam param) {
        PageParam<Demo, DemoParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number,create_time desc");
        return success(demoService.list(page.getOrderWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:demo:list')")
    @OperationLog
    @ApiOperation("根据id查询测试")
    @GetMapping("/{id}")
    public ApiResult<Demo> get(@PathVariable("id") Integer id) {
        return success(demoService.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:demo:save')")
    @ApiOperation("添加测试")
    @PostMapping()
    public ApiResult<?> add(@RequestBody Demo demo) {
        if (demoService.count(new LambdaQueryWrapper<Demo>()
                .eq(Demo::getDemoName, demo.getDemoName())) > 0) {
            return fail("测试名称已存在");
        }
        if (demoService.save(demo)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:demo:update')")
    @OperationLog
    @ApiOperation("修改测试")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Demo demo) {
        if (demoService.count(new LambdaQueryWrapper<Demo>()
                .eq(Demo::getDemoName, demo.getDemoName())
                .ne(Demo::getDemoId, demo.getDemoId())) > 0) {
            return fail("测试名称已存在");
        }
        if (demoService.updateById(demo)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:demo:remove')")
    @OperationLog
    @ApiOperation("删除测试")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (demoService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:demo:save')")
    @OperationLog
    @ApiOperation("批量添加测试")
    @PostMapping("/batch")
    public ApiResult<List<String>> saveBatch(@RequestBody List<Demo> list) {
        if (CommonUtil.checkRepeat(list, Demo::getDemoId)) {
            return fail("测试标识不能重复", null);
        }
        if (CommonUtil.checkRepeat(list, Demo::getDemoName)) {
            return fail("测试名称不能重复", null);
        }
        List<Demo> nameExists = demoService.list(new LambdaQueryWrapper<Demo>()
                .in(Demo::getDemoName, list.stream().map(Demo::getDemoName)
                        .collect(Collectors.toList())));
        if (nameExists.size() > 0) {
            return fail("测试名称已存在", nameExists.stream().map(Demo::getDemoName)
                    .collect(Collectors.toList())).setCode(3);
        }
        if (demoService.saveBatch(list)) {
            return success("添加成功", null);
        }
        return fail("添加失败", null);
    }

    @PreAuthorize("hasAuthority('sys:demo:remove')")
    @OperationLog
    @ApiOperation("批量删除测试")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (demoService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
