package com.docus.server.service.impl;

import com.docus.core.util.DateUtil;
import com.docus.infrastructure.core.db.dao.IBaseDao;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.common.auth.ApiRequestContext;
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.entity.recovery.RcvBasic;
import com.docus.server.enums.RecoveryStateEnum;
import com.docus.server.infrastructure.dao.IRcvBasicDao;
import com.docus.server.service.IRcvBasicService;
import com.docus.server.vo.recovery.rcvbasic.RcvBasicVO;
import com.docus.server.vo.recovery.rcvbasic.SearchRcvBasicVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 患者信息表 服务实现类
 *
 * @author AutoGenerator
 * @since 2023-09-13
 */
@Service
public class RcvBasicServiceImpl implements IRcvBasicService {
    @Resource
    private IRcvBasicDao iRcvBasicDao;
    @Resource
    private IdService idService;

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @Override
    public RcvBasicVO findById(Serializable id) {
        return RcvBasicConvert.INSTANCE.convertVO(iRcvBasicDao.findById(id));
    }

    /**
     * 按主键集合查询
     *
     * @param ids 主键ids
     * @return 实体列表
     */
    @Override
    public List<RcvBasicVO> findByIds(List<Serializable> ids) {
        return RcvBasicConvert.INSTANCE.convertVO(iRcvBasicDao.findByIds(ids));
    }

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

    /**
     * 按属性查询
     *
     * @param propertyName  属性名
     * @param propertyValue 属性值
     * @return 实体列表
     */
    @Override
    public List<RcvBasicVO> findBy(String propertyName, Collection<?> propertyValue) {
        return RcvBasicConvert.INSTANCE.convertVO(iRcvBasicDao.findBy(propertyName, propertyValue));
    }

    /**
     * 按属性查询
     *
     * @param propertyName  属性名
     * @param propertyValue 属性值
     * @return 实体
     */
    @Override
    public RcvBasicVO findOneBy(String propertyName, Object propertyValue) {
        return RcvBasicConvert.INSTANCE.convertVO(iRcvBasicDao.findOneBy(propertyName, propertyValue));
    }

    /**
     * 按属性查询
     *
     * @param propertyName  属性名
     * @param propertyValue 属性值
     * @return 实体
     */
    @Override
    public RcvBasicVO findOneBy(String propertyName, Collection<?> propertyValue) {
        return RcvBasicConvert.INSTANCE.convertVO(iRcvBasicDao.findOneBy(propertyName, propertyValue));
    }

    /**
     * 查询所有
     *
     * @return 实体列表
     */
    @Override
    public List<RcvBasicVO> findAll() {
        return RcvBasicConvert.INSTANCE.convertVO(iRcvBasicDao.findAll());
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<SearchRcvBasicVO> search(SearchDTO searchDTO) {
        return RcvBasicConvert.INSTANCE.convertPageVO(iRcvBasicDao.search(searchDTO));

        //PageResult<RcvBasicVO> result = RcvBasicConvert.INSTANCE.convertVO(iRcvBasicDao.search(searchDTO));

        //if (CollectionUtils.isEmpty(result.getList())) {
        //return new PageResult<>();
        //}

        //Map<String, SchSystemParams> map = iSchSystemParamsService.find(ListUtils.distinctSelect(result.getList(), SchCollectorVO::getCollectorId));

        //result.getList().forEach(p -> {
        // String collectorId = String.valueOf(p.getCollectorId());
        // if (map.containsKey(collectorId)) {
        //     p.setCollectorName(map.get(collectorId).getParamName());
        // }
        //});

        //return result;
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<RcvBasicVO> page(SearchDTO searchDTO) {
        return RcvBasicConvert.INSTANCE.convertVO(iRcvBasicDao.search(searchDTO));
    }

    /**
     * 新增
     *
     * @param addRcvBasicDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AddRcvBasicDTO addRcvBasicDTO) {
        RcvBasic rcvBasic = RcvBasicConvert.INSTANCE.convertDO(addRcvBasicDTO);
        rcvBasic.setId(idService.getDateSeq());
        return iRcvBasicDao.add(rcvBasic);

        // String name = addRcvBasicDTO.getName();

        //RcvBasic rcvBasic = iRcvBasicDao.findOneBy("name", name);

        //if (Func.notNull(rcvBasic)) {
        // throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "患者信息表已经存在");
        // }

        // RcvBasic rcvBasic =RcvBasicConvert.INSTANCE.convertDO(addRcvBasicDTO);
        // rcvBasic.setId(idService.getDateSeq());

        // return iRcvBasicDao.add(schCollector);

    }

    /**
     * 批量新增
     *
     * @param addRcvBasicDTOList 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addBatch(List<AddRcvBasicDTO> addRcvBasicDTOList) {

        List<RcvBasic> rcvBasicList = RcvBasicConvert.INSTANCE.convertAddDOList(addRcvBasicDTOList);

        rcvBasicList.forEach(p -> {
            p.setId(idService.getDateSeq());
        });

        return iRcvBasicDao.saveBatch(rcvBasicList, IBaseDao.DEFAULT_BATCH_SIZE);
    }

    /**
     * 新增或编辑
     *
     * @param editRcvBasicDTO 新增或编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrEdit(EditRcvBasicDTO editRcvBasicDTO) {
        RcvBasic rcvBasic = RcvBasicConvert.INSTANCE.convertDO(editRcvBasicDTO);
        return iRcvBasicDao.saveOrUpdate(rcvBasic);
    }

    /**
     * 批量新增或编辑
     *
     * @param editRcvBasicDTOList 新增或编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrEditBatch(List<EditRcvBasicDTO> editRcvBasicDTOList) {
        List<RcvBasic> rcvBasicList = RcvBasicConvert.INSTANCE.convertEditDOList(editRcvBasicDTOList);
        return iRcvBasicDao.saveOrUpdateBatch(rcvBasicList, IBaseDao.DEFAULT_BATCH_SIZE);
    }

    /**
     * 编辑
     *
     * @param editRcvBasicDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(EditRcvBasicDTO editRcvBasicDTO) {
        RcvBasic rcvBasic = RcvBasicConvert.INSTANCE.convertDO(editRcvBasicDTO);
        return iRcvBasicDao.edit(rcvBasic);

        // Long id = editRcvBasicDTO.getId();

        //  RcvBasic rcvBasic = iRcvBasicDao.findById(id);

        // if (Func.isNull(rcvBasic)) {
        //     throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "患者信息表，无法更新！");
        // }

        // rcvBasic.setUpdateTime(LocalDateTime.now());
        // return iRcvBasicDao.edit(rcvBasic);
    }

    /**
     * 批量编辑
     *
     * @param serialNumbers 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editBatchById(List<Serializable> serialNumbers) {

        List<RcvBasic> rcvBasics = iRcvBasicDao.findBy("serialNumber", serialNumbers);

        rcvBasics.forEach(p -> {
            p.setRecoveryState(RecoveryStateEnum.RECYCLED_ALREADY);
            p.setRevoveryUser(ApiRequestContext.get().getUserId());
            p.setRevoveryTime(DateUtil.now());
        });

        return iRcvBasicDao.updateBatchById(rcvBasics, IBaseDao.DEFAULT_BATCH_SIZE);
    }

    /**
     * 批量删除
     *
     * @param deleteRcvBasicDTO 删除参数
     * @return 成功或失败
     */
    @Override
    public int delete(DeleteRcvBasicDTO deleteRcvBasicDTO) {
        return iRcvBasicDao.delete(deleteRcvBasicDTO.getIds());
    }

    /**
     * 按主键删除
     *
     * @param id 删除参数
     * @return 成功或失败
     */
    @Override
    public int delete(Serializable id) {
        return iRcvBasicDao.delete(id);
    }

    /**
     * 按属性删除
     *
     * @param propertyName  删除属性名
     * @param propertyValue 删除属性值
     * @return 成功或失败
     */
    @Override
    public int deleteBy(String propertyName, Object propertyValue) {
        return iRcvBasicDao.deleteBy(propertyName, propertyValue);
    }

    /**
     * 按属性删除
     *
     * @param propertyName  删除属性名
     * @param propertyValue 删除属性值集合
     * @return 成功或失败
     */
    @Override
    public int deleteBy(String propertyName, Collection<?> propertyValue) {
        return iRcvBasicDao.deleteBy(propertyName, propertyValue);
    }

    /**
     * 按主键集合批量删除
     *
     * @param ids 删除参数
     * @return 成功或失败
     */
    @Override
    public int deleteByIds(Collection<? extends Serializable> ids) {
        return iRcvBasicDao.deleteByIds(ids);
    }

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

}

