package cn.common.api;

import cn.common.common.anon.ApiLog;
import cn.common.interaction.resp.base.ApiResponse;
import cn.common.interaction.resp.page.Pagination;
import cn.common.req.MedicalHistoryAddReq;
import cn.common.req.MedicalHistoryReq;
import cn.common.req.MedicalHistoryUpdateReq;
import cn.common.req.base.BaseDeleteReq;
import cn.common.resp.MedicalHistoryResp;
import cn.common.service.MedicalHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
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/medicalHistory")
@Slf4j
public class MedicalHistoryController extends BaseApiController {

    @Resource
    private MedicalHistoryService medicalHistoryService;

    @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("medicalHistory:add")
    public ApiResponse addItem(@RequestBody @Valid MedicalHistoryAddReq addReq){
        medicalHistoryService.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){
        medicalHistoryService.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 MedicalHistoryReq pageReq){
        medicalHistoryService.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("medicalHistory:batchDelete")
    public ApiResponse batchDeleteItem(@RequestBody BaseDeleteReq req){
        medicalHistoryService.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("medicalHistory:edit")
    public ApiResponse updateItem(@RequestBody @Valid MedicalHistoryUpdateReq updateReq){
        medicalHistoryService.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 = "/queryAllMedicalHistory")
    @ApiLog(value = "查询所有信息")
    public ApiResponse<List<MedicalHistoryResp>> queryAllMedicalHistory(){
        return apiResponse(medicalHistoryService.queryAllMedicalHistory());
    }

    /**
     * 分页查询病历信息
     * @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("medicalHistory:itemList")
    public ApiResponse<Pagination<MedicalHistoryResp>> queryByPage(
        @RequestBody @Valid MedicalHistoryReq pageReq){
        return apiResponse(medicalHistoryService.queryByPage(pageReq));
    }
}
