package cn.common.api;

import cn.common.req.GridUnitAddReq;
import cn.common.req.GridUnitReq;
import cn.common.req.GridUnitUpdateReq;
import cn.common.req.base.BaseDeleteReq;
import cn.common.interaction.resp.base.ApiResponse;
import cn.common.interaction.resp.page.Pagination;
import cn.common.resp.GridUnitResp;
import cn.common.service.GridUnitService;
import cn.common.common.anon.ApiLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
* @author Singer create by Singer email:singer-coder@qq.com
* @packageName cn.common.api.controller
* @Description: 网格信息前端控制器
* @date 2021-04-23
*/
@RestController
@RequestMapping(value = "api/v1/gridUnit")
@Slf4j
public class GridUnitController extends BaseApiController {

    @Resource
    private GridUnitService gridUnitService;

    @Resource
    private HttpServletResponse response;

    /**
     * 新增网格信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/2/15
     * @param addReq 新增Req
     */
    @PostMapping(value = "/addItem")
    @ApiLog(value = "新增信息")
    @RequiresPermissions("gridUnit:add")
    public ApiResponse addItem(@RequestBody @Valid GridUnitAddReq addReq){
        gridUnitService.addItem(addReq);
        return apiResponse();
    }

    /**
     * 数据导入
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2022/3/21
     * @param file 文件
     * @return
    */
    @PostMapping(value = "/importData")
    @ApiLog(value = "数据导入")
    public ApiResponse importData(@RequestParam("file") MultipartFile file){
        gridUnitService.importData(file);
        return apiResponse();
    }

    /**
     * 导出网格信息数据
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2022/3/21
     * @param pageReq
     * @return java.util.List
    */
    @PostMapping(value = "/exportData")
    @ApiLog(value = "网格信息数据导出")
    public void exportData(@RequestBody(required = false) @Valid GridUnitReq pageReq){
        gridUnitService.exportData(pageReq);
    }

    /**
     * 主键ID集合批量删除网格信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/2/2
     * @param req 需要被删除的信息
     */
    @PostMapping(value = "/batchDeleteItem")
    @ApiLog(value = "根据主键ID集合批量删除")
    @RequiresPermissions("gridUnit:batchDelete")
    public ApiResponse batchDeleteItem(@RequestBody BaseDeleteReq req){
        gridUnitService.batchDeleteItem(req);
        return apiResponse();
    }

    /**
     * 更新网格信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/4/2
     * @param updateReq 更新请求参数
     */
    @PutMapping(value = "/updateItem")
    @ApiLog(value = "更新信息")
    @RequiresPermissions("gridUnit:edit")
    public ApiResponse updateItem(@RequestBody @Valid GridUnitUpdateReq updateReq){
        gridUnitService.updateItem(updateReq);
        return apiResponse();
    }

    /**
     * 查询所有网格信息信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2022/3/13
     * @param
     * @return java.util.List
     */
    @GetMapping(value = "/queryAllGridUnit")
    @ApiLog(value = "查询所有信息")
    public ApiResponse<List<GridUnitResp>> queryAllGridUnit(){
        return apiResponse(gridUnitService.queryAllGridUnit());
    }

    /**
     * 分页查询网格信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/2/15
     * @param  pageReq 分页查询Req
     * @return Pagination
     */
    @PostMapping(value = "/queryByPage")
    @ApiLog(value = "分页查询信息")
    @RequiresPermissions("gridUnit:itemList")
    public ApiResponse<Pagination<GridUnitResp>> queryByPage(
        @RequestBody @Valid GridUnitReq pageReq){
        return apiResponse(gridUnitService.queryByPage(pageReq));
    }
}
