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.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.oa.entity.AssetsData;
import com.eleadmin.oa.param.AssetsDataParam;
import com.eleadmin.oa.server.AssetsDataService;
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;

/**
 * 资产项控制器
 *
 * @author gxwebsoft.com
 * @since 2020-03-14 11:29:04
 */
@Api(tags = "资产项管理")
@RestController
@RequestMapping("/api/assets-data")
public class AssetsDataController extends BaseController {
    @Resource
    private AssetsDataService assetsDataService;

    @PreAuthorize("hasAuthority('sys:assets:list')")
    @OperationLog
    @ApiOperation("分页查询资产项")
    @GetMapping("/page")
    public ApiResult<PageResult<AssetsData>> page(AssetsDataParam param) {
        return success(assetsDataService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:assets:list')")
    @OperationLog
    @ApiOperation("查询全部资产项")
    @GetMapping()
    public ApiResult<List<AssetsData>> list(AssetsDataParam param) {
        return success(assetsDataService.listRel(param));
    }

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

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

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

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

    @PreAuthorize("hasAuthority('sys:assets:save')")
    @OperationLog
    @ApiOperation("批量添加资产项")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<AssetsData> assetsDataList) {
        if (assetsDataService.saveBatch(assetsDataList)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

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

}
