package com.tuoheng.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.enums.MarkTypeEnum;
import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.entity.Area;
import com.tuoheng.model.query.AreaQuery;
import com.tuoheng.service.AreaService;
import com.tuoheng.service.CurrentUser;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * (Area)表控制层
 *
 * @author xiaoying
 * @since 2023-03-06 14:59:16
 */
@RestController
@RequestMapping("/area")
public class AreaController {
    /**
     * 服务对象
     */
    @Autowired
    private AreaService areaService;

    /**
     * 查询区域信息
     *
     * @param query
     * @return
     */
    @GetMapping("/index")
    public JsonResult index(AreaQuery query) {
        return areaService.index(query);
    }

    /**
     * 查询存在的区域信息
     *
     * @return
     */
    @GetMapping("/list")
    public JsonResult list() {
        return JsonResult.success(areaService.list(Wrappers.<Area>lambdaQuery().eq(Area::getMark, MarkTypeEnum.VALID.getCode())));
    }

    /**
     * 新增区域信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody @Validated Area entity, @CurrentUser LoginUser loginUser) {
        return areaService.edit(entity, loginUser);
    }

    /**
     * 新增区域信息
     *
     * @param entity
     * @return
     */
    @PutMapping("/edit")
    public JsonResult edit(@RequestBody @Validated Area entity, @CurrentUser LoginUser loginUser) {
        return areaService.edit(entity, loginUser);
    }

    /**
     * 逻辑删除/批量
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    public JsonResult delete(@PathVariable("ids") Long[] ids) {
        return areaService.deleteByIds(ids);
    }
}

