package cn.iocoder.yudao.module.system.service.biz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivitytext.ElderlyBuildFloorActivityTextItemRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivitytext.ElderlyBuildFloorActivityTextItemSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivitytext.ElderlyBuildFloorActivityTextPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivitytext.ElderlyBuildFloorActivityTextRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivitytext.ElderlyBuildFloorActivityTextSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyBuildFloorActivityTextDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyBuildFloorActivityTextItemDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyBuildFloorActivityTextItemMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyBuildFloorActivityTextMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMMON_NOT_FOUND;

/**
 * 楼层活动文字记录 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ElderlyBuildFloorActivityTextServiceImpl implements ElderlyBuildFloorActivityTextService {

    @Resource
    private ElderlyBuildFloorActivityTextMapper elderlyBuildFloorActivityTextMapper;

    @Resource
    private ElderlyBuildFloorActivityTextItemMapper textItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createElderlyBuildFloorActivityText(ElderlyBuildFloorActivityTextSaveReqVO createReqVO) {
        ElderlyBuildFloorActivityTextDO record = BeanUtils.toBean(createReqVO, ElderlyBuildFloorActivityTextDO.class);
        elderlyBuildFloorActivityTextMapper.insert(record);
        saveItems(record.getId(), createReqVO.getItems());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateElderlyBuildFloorActivityText(ElderlyBuildFloorActivityTextSaveReqVO updateReqVO) {
        validateElderlyBuildFloorActivityTextExists(updateReqVO.getId());
        ElderlyBuildFloorActivityTextDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyBuildFloorActivityTextDO.class);
        elderlyBuildFloorActivityTextMapper.updateById(updateObj);
        textItemMapper.deleteByTextId(updateReqVO.getId());
        saveItems(updateReqVO.getId(), updateReqVO.getItems());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteElderlyBuildFloorActivityText(Long id) {
        validateElderlyBuildFloorActivityTextExists(id);
        textItemMapper.deleteByTextId(id);
        elderlyBuildFloorActivityTextMapper.deleteById(id);
    }

    @Override
    public ElderlyBuildFloorActivityTextRespVO getElderlyBuildFloorActivityText(Long id) {
        ElderlyBuildFloorActivityTextDO record = elderlyBuildFloorActivityTextMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        ElderlyBuildFloorActivityTextRespVO respVO = BeanUtils.toBean(record, ElderlyBuildFloorActivityTextRespVO.class);
        respVO.setItems(getItems(record.getId()));
        return respVO;
    }

    @Override
    public PageResult<ElderlyBuildFloorActivityTextRespVO> getElderlyBuildFloorActivityTextPage(ElderlyBuildFloorActivityTextPageReqVO pageReqVO) {
        PageResult<ElderlyBuildFloorActivityTextDO> pageResult = elderlyBuildFloorActivityTextMapper.selectPage(pageReqVO);
        List<ElderlyBuildFloorActivityTextRespVO> respList = BeanUtils.toBean(pageResult.getList(), ElderlyBuildFloorActivityTextRespVO.class);
        fillItems(respList);
        return new PageResult<>(respList, pageResult.getTotal());
    }

    private void validateElderlyBuildFloorActivityTextExists(Long id) {
        if (id == null || elderlyBuildFloorActivityTextMapper.selectById(id) == null) {
            throw exception(COMMON_NOT_FOUND);
        }
    }

    private void saveItems(Long textId, List<ElderlyBuildFloorActivityTextItemSaveReqVO> items) {
        if (items == null || items.isEmpty()) {
            return;
        }
        List<ElderlyBuildFloorActivityTextItemDO> saveItems = new ArrayList<>();
        for (ElderlyBuildFloorActivityTextItemSaveReqVO item : items) {
            ElderlyBuildFloorActivityTextItemDO itemDO = BeanUtils.toBean(item, ElderlyBuildFloorActivityTextItemDO.class);
            itemDO.setTextId(textId);
            saveItems.add(itemDO);
        }
        textItemMapper.insertBatch(saveItems);
    }

    private List<ElderlyBuildFloorActivityTextItemRespVO> getItems(Long textId) {
        List<ElderlyBuildFloorActivityTextItemDO> items = textItemMapper.selectByTextId(textId);
        if (items.isEmpty()) {
            return Collections.emptyList();
        }
        return BeanUtils.toBean(items, ElderlyBuildFloorActivityTextItemRespVO.class);
    }

    private void fillItems(List<ElderlyBuildFloorActivityTextRespVO> respList) {
        if (respList == null || respList.isEmpty()) {
            return;
        }
        List<Long> ids = respList.stream().map(ElderlyBuildFloorActivityTextRespVO::getId).collect(Collectors.toList());
        List<ElderlyBuildFloorActivityTextItemDO> items = textItemMapper.selectByTextIds(ids);
        if (items.isEmpty()) {
            return;
        }
        Map<Long, List<ElderlyBuildFloorActivityTextItemRespVO>> itemMap = items.stream()
                .collect(Collectors.groupingBy(
                        ElderlyBuildFloorActivityTextItemDO::getTextId,
                        Collectors.mapping(
                                item -> BeanUtils.toBean(item, ElderlyBuildFloorActivityTextItemRespVO.class),
                                Collectors.toList())));
        for (ElderlyBuildFloorActivityTextRespVO resp : respList) {
            resp.setItems(itemMap.getOrDefault(resp.getId(), Collections.emptyList()));
        }
    }
}
