package cn.common.api;

import cn.common.req.MonitorRecordAddReq;
import cn.common.req.MonitorRecordReq;
import cn.common.req.MonitorRecordUpdateReq;
import cn.common.req.base.BaseDeleteReq;
import cn.common.interaction.resp.base.ApiResponse;
import cn.common.interaction.resp.page.Pagination;
import cn.common.resp.MonitorRecordResp;
import cn.common.service.MonitorRecordService;
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/monitorRecord")
@Slf4j
public class MonitorRecordController extends BaseApiController {

    @Resource
    private MonitorRecordService monitorRecordService;

    @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("monitorRecord:add")
    public ApiResponse addItem(@RequestBody @Valid MonitorRecordAddReq addReq){
        monitorRecordService.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){
        monitorRecordService.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 MonitorRecordReq pageReq){
        monitorRecordService.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("monitorRecord:batchDelete")
    public ApiResponse batchDeleteItem(@RequestBody BaseDeleteReq req){
        monitorRecordService.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("monitorRecord:edit")
    public ApiResponse updateItem(@RequestBody @Valid MonitorRecordUpdateReq updateReq){
        monitorRecordService.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 = "/queryAllMonitorRecord")
    @ApiLog(value = "查询所有信息")
    public ApiResponse<List<MonitorRecordResp>> queryAllMonitorRecord(){
        return apiResponse(monitorRecordService.queryAllMonitorRecord());
    }

    /**
     * 分页查询健康监测信息
     * @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("monitorRecord:itemList")
    public ApiResponse<Pagination<MonitorRecordResp>> queryByPage(
        @RequestBody @Valid MonitorRecordReq pageReq){
        return apiResponse(monitorRecordService.queryByPage(pageReq));
    }
}
