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.*;
import com.eleadmin.oa.entity.Assets;
import com.eleadmin.oa.param.AssetsParam;
import com.eleadmin.oa.server.AssetsService;
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/assets")
public class AssetsController extends BaseController {
    @Resource
    private AssetsService assetsService;

    @PreAuthorize("hasAuthority('sys:assets:list')")
    @OperationLog
    @ApiOperation("分页查询资产")
    @GetMapping("/page")
    public ApiResult<PageResult<Assets>> page(AssetsParam param) {
        PageParam<Assets, AssetsParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number");
        return success(assetsService.page(page, page.getWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:assets:list')")
    @OperationLog
    @ApiOperation("查询全部资产")
    @GetMapping()
    public ApiResult<List<Assets>> list(AssetsParam param) {
        PageParam<Assets, AssetsParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number");
        return success(assetsService.list(page.getOrderWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:assets:list')")
    @OperationLog
    @ApiOperation("根据id查询资产")
    @GetMapping("/{id}")
    public ApiResult<Assets> get(@PathVariable("id") Integer id) {
        return success(assetsService.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:assets:save')")
    @ApiOperation("添加资产")
    @PostMapping()
    public ApiResult<?> add(@RequestBody Assets assets) {
        if (assetsService.count(new LambdaQueryWrapper<Assets>()
                .eq(Assets::getCode, assets.getCode())) > 0) {
            return fail("资产标识已存在");
        }
        if (assetsService.count(new LambdaQueryWrapper<Assets>()
                .eq(Assets::getName, assets.getName())) > 0) {
            return fail("资产名称已存在");
        }
        if (assetsService.save(assets)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:assets:update')")
    @OperationLog
    @ApiOperation("修改资产")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Assets assets) {
        if (assetsService.count(new LambdaQueryWrapper<Assets>()
                .eq(Assets::getCode, assets.getCode())
                .ne(Assets::getAssetsId, assets.getAssetsId())) > 0) {
            return fail("资产标识已存在");
        }
        if (assetsService.count(new LambdaQueryWrapper<Assets>()
                .eq(Assets::getName, assets.getName())
                .ne(Assets::getAssetsId, assets.getAssetsId())) > 0) {
            return fail("资产名称已存在");
        }
        if (assetsService.updateById(assets)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

}
