package com.docus.server.controller;

import com.docus.infrastructure.util.easyexcel.ExcelUtil;
import com.docus.infrastructure.web.exception.ApiException;
import com.docus.infrastructure.web.exception.ExceptionCode;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.api.recovery.RcvBasicApi;
import com.docus.server.common.IgnoreValidate;
import com.docus.server.convert.RcvBasicConvert;
import com.docus.server.dto.recovery.rcvbasic.AddRcvBasicDTO;
import com.docus.server.dto.recovery.rcvbasic.DeleteRcvBasicDTO;
import com.docus.server.dto.recovery.rcvbasic.EditRcvBasicDTO;
import com.docus.server.dto.recovery.rcvbasic.MedicalInfoExcelModel;
import com.docus.server.service.IRcvBasicService;
import com.docus.server.validate.MedicalImportExcelValidator;
import com.docus.server.vo.recovery.rcvbasic.RcvBasicVO;
import com.docus.server.vo.recovery.rcvbasic.SearchRcvBasicVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 患者信息表 控制器类
 *
 * @author AutoGenerator
 * @since 2023-09-13
 */
@Api(value = "患者信息表", tags = "患者信息表")
@RestController
public class RcvBasicController implements RcvBasicApi {
    @Resource
    private IRcvBasicService iRcvBasicService;
    @Resource
    private MedicalImportExcelValidator medicalImportExcelValidator;

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @ApiOperation("按主键查询")
    @Override
    public RcvBasicVO findById(Serializable id) {
        return iRcvBasicService.findById(id);
    }

    /**
     * 按主键集合查询
     *
     * @param ids 主键Ids
     * @return 实体列表
     */
    @ApiOperation("按主键集合查询")
    @Override
    public List<RcvBasicVO> findByIds(List<Serializable> ids) {
        return iRcvBasicService.findByIds(ids);
    }


    /**
     * 按属性查询多实体
     *
     * @param propertyName  属性名
     * @param propertyValue 属性值
     * @return 实体列表
     */
    @ApiOperation("按属性查询多实体")
    @Override
    public List<RcvBasicVO> findBy(String propertyName, Object propertyValue) {
        return iRcvBasicService.findBy(propertyName, propertyValue);
    }

    /**
     * 按属性值集合查询
     *
     * @param propertyName  属性名
     * @param propertyValue 属性值
     * @return 实体列表
     */
    @ApiOperation("按属性值集合查询")
    @Override
    public List<RcvBasicVO> findBy(String propertyName, Collection<Serializable> propertyValue) {
        return iRcvBasicService.findBy(propertyName, propertyValue);
    }

    /**
     * 按属性查询单实体
     *
     * @param propertyName  属性名
     * @param propertyValue 属性值
     * @return 实体列表
     */
    @ApiOperation("按属性查询单实体")
    @Override
    public RcvBasicVO findOneBy(String propertyName, Object propertyValue) {
        return iRcvBasicService.findOneBy(propertyName, propertyValue);
    }

    /**
     * 按属性值集合查询
     *
     * @param propertyName  属性名
     * @param propertyValue 属性值
     * @return 实体列表
     */
    @ApiOperation("按属性值集合查询")
    @Override
    public RcvBasicVO findOneBy(String propertyName, Collection<Serializable> propertyValue) {
        return iRcvBasicService.findOneBy(propertyName, propertyValue);
    }

    /**
     * 查询所有
     *
     * @return 实体
     */
    @ApiOperation("查询所有")
    @Override
    public List<RcvBasicVO> findAll() {
        return iRcvBasicService.findAll();
    }

    /**
     * 关键字搜索(安卓)
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @ApiOperation("关键字搜索(安卓)")
    @Override
    public PageResult<SearchRcvBasicVO> search(SearchDTO searchDTO) {
        return iRcvBasicService.search(searchDTO);
    }

    /**
     * 关键字搜索(PC)
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @ApiOperation("关键字搜索(PC)")
    @Override
    public PageResult<RcvBasicVO> page(SearchDTO searchDTO) {
        return iRcvBasicService.page(searchDTO);
    }

    /**
     * 新增
     *
     * @param addRcvBasicDTO 编辑参数
     */
    @ApiOperation("新增")
    @Override
    public void add(AddRcvBasicDTO addRcvBasicDTO) {
        iRcvBasicService.add(addRcvBasicDTO);
    }

    /**
     * 批量新增
     *
     * @param addRcvBasicDTO 编辑参数
     */
    @ApiOperation("批量新增")
    @Override
    public void addBatch(List<AddRcvBasicDTO> addRcvBasicDTO) {
        iRcvBasicService.addBatch(addRcvBasicDTO);
    }

    /**
     * 新增或编辑
     *
     * @param editRcvBasicDTO 新增或编辑参数
     */
    @ApiOperation("新增或编辑")
    @Override
    public void addOrEdit(EditRcvBasicDTO editRcvBasicDTO) {
        iRcvBasicService.addOrEdit(editRcvBasicDTO);
    }

    /**
     * 批量新增或编辑
     *
     * @param editRcvBasicDTOList 编辑参数
     */
    @ApiOperation("批量新增或编辑")
    @Override
    public void addOrEditBatch(List<EditRcvBasicDTO> editRcvBasicDTOList) {
        iRcvBasicService.addOrEditBatch(editRcvBasicDTOList);
    }

    /**
     * 编辑
     *
     * @param editRcvBasicDTO 编辑参数
     */
    @ApiOperation("编辑")
    @Override
    public void edit(EditRcvBasicDTO editRcvBasicDTO) {
        iRcvBasicService.edit(editRcvBasicDTO);
    }

    /**
     * 批量编辑
     *
     * @param serialNumbers 编辑参数
     */
    @ApiOperation("批量编辑")
    @Override
    public void editBatchById(List<Serializable> serialNumbers) {
        iRcvBasicService.editBatchById(serialNumbers);
    }

    /**
     * 批量删除
     *
     * @param deleteRcvBasicDTO 删除参数
     */
    @ApiOperation("批量删除")
    @Override
    public void delete(DeleteRcvBasicDTO deleteRcvBasicDTO) {
        iRcvBasicService.delete(deleteRcvBasicDTO);
    }

    /**
     * 按主键删除
     *
     * @param id 删除参数
     */
    @ApiOperation("按主键删除")
    @Override
    public void deleteById(Serializable id) {
        iRcvBasicService.delete(id);
    }

    /**
     * 按属性删除
     *
     * @param propertyName  删除属性名
     * @param propertyValue 删除属性值
     */
    @ApiOperation("按属性删除")
    @Override
    public void deleteBy(String propertyName, Object propertyValue) {
        iRcvBasicService.deleteBy(propertyName, propertyValue);
    }

    /**
     * 按属性值集合删除
     *
     * @param propertyName  删除属性名
     * @param propertyValue 删除属性值集合
     */
    @ApiOperation("按属性值集合删除")
    @Override
    public void deleteBy(String propertyName, Collection<Serializable> propertyValue) {
        iRcvBasicService.deleteBy(propertyName, propertyValue);
    }

    /**
     * 按主键集合批量删除
     *
     * @param ids 删除参数
     */
    @ApiOperation("按主键集合批量删除")
    @Override
    public void deleteByIds(Collection<Serializable> ids) {
        iRcvBasicService.deleteByIds(ids);
    }

    /**
     * 名称不重复
     *
     * @param id   主键
     * @param name 名称
     * @return 名称重复数量
     */
    @ApiOperation("名称不重复")
    @Override
    public int findByIdAndName(Serializable id, String name) {
        return iRcvBasicService.findByIdAndName(id, name);
    }

//    @IgnoreValidate
//    @Override
//    @ApiOperation("下载病案回收导入基础数据excel模板")
//    public void downloadExcelTemplate(HttpServletResponse response) throws Exception {
//        super.downloadExcelTemplate(response, "病案回收导入基础数据模板.xlsx", "/tpl/");
//    }

    @IgnoreValidate
    @PostMapping("/importExcel")
    @ApiOperation("病案回收基础数据导入模板")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "导入带数据的excel模板", required = true, dataTypeClass = MultipartFile.class)
    })
    public void importExcel(@RequestPart(value = "file") MultipartFile file, HttpServletRequest request) {
        System.out.println("开始导入");
        List<Object> excelData = ExcelUtil.readExcel(file, new MedicalInfoExcelModel(), 1, 2);
        final int importMax = 100000;
        if (excelData.size() > importMax) {
            throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "单次导入最多支持" + importMax + "条数据！");
        }
        medicalImportExcelValidator.validate(excelData);
        List<AddRcvBasicDTO> addRcvBasicDTOList = RcvBasicConvert.INSTANCE.convert(excelData);
        iRcvBasicService.addRecord(addRcvBasicDTOList);
    }
}
