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.Items;
import com.eleadmin.oa.param.ItemsParam;
import com.eleadmin.oa.server.ItemsService;
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/items")
public class ItemsController extends BaseController {
    @Resource
    private ItemsService itemsService;

    @PreAuthorize("hasAuthority('sys:items:list')")
    @OperationLog
    @ApiOperation("分页查询项目")
    @GetMapping("/page")
    public ApiResult<PageResult<Items>> page(ItemsParam param) {
        PageParam<Items, ItemsParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number,items_id desc");
        return success(itemsService.page(page, page.getWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:items:list')")
    @OperationLog
    @ApiOperation("查询全部项目")
    @GetMapping()
    public ApiResult<List<Items>> list(ItemsParam param) {
        PageParam<Items, ItemsParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number,items_id desc");
        return success(itemsService.list(page.getOrderWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:items:list')")
    @OperationLog
    @ApiOperation("根据id查询项目")
    @GetMapping("/{id}")
    public ApiResult<Items> get(@PathVariable("id") Integer id) {
        return success(itemsService.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:items:save')")
    @ApiOperation("添加项目")
    @PostMapping()
    public ApiResult<?> add(@RequestBody Items items) {
        if (itemsService.count(new LambdaQueryWrapper<Items>()
                .eq(Items::getItemsCode, items.getItemsCode())) > 0) {
            return fail("项目标识已存在");
        }
        if (itemsService.count(new LambdaQueryWrapper<Items>()
                .eq(Items::getItemsName, items.getItemsName())) > 0) {
            return fail("项目名称已存在");
        }
        if (itemsService.save(items)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:items:update')")
    @OperationLog
    @ApiOperation("修改项目")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Items items) {
        if (itemsService.count(new LambdaQueryWrapper<Items>()
                .eq(Items::getItemsCode, items.getItemsCode())
                .ne(Items::getItemsId, items.getItemsId())) > 0) {
            return fail("项目标识已存在");
        }
        if (itemsService.count(new LambdaQueryWrapper<Items>()
                .eq(Items::getItemsName, items.getItemsName())
                .ne(Items::getItemsId, items.getItemsId())) > 0) {
            return fail("项目名称已存在");
        }
        if (itemsService.updateById(items)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

}
