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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlySpecialNursingPlanItemDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.SpecialNursingPlanItemMapper;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlySpecialNursingPlanDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.SpecialNursingPlanMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

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

@Service
@Validated
public class SpecialNursingPlanServiceImpl implements SpecialNursingPlanService {

    @Resource
    private SpecialNursingPlanMapper specialNursingPlanMapper;
    @Resource
    private SpecialNursingPlanItemMapper specialNursingPlanItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(SpecialNursingPlanSaveReqVO createReqVO) {
        ElderlySpecialNursingPlanDO specialNursingPlan = BeanUtils.toBean(createReqVO, ElderlySpecialNursingPlanDO.class);
        specialNursingPlanMapper.insert(specialNursingPlan);
        saveItems(specialNursingPlan.getId(), createReqVO.getTenantId(), createReqVO.getItems());
        return specialNursingPlan.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SpecialNursingPlanSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlySpecialNursingPlanDO updateObj = BeanUtils.toBean(updateReqVO, ElderlySpecialNursingPlanDO.class);
        specialNursingPlanMapper.updateById(updateObj);
        // 按要求使用先删除后插入的方式更新明细
        specialNursingPlanItemMapper.delete(new LambdaQueryWrapperX<ElderlySpecialNursingPlanItemDO>()
                .eq(ElderlySpecialNursingPlanItemDO::getSpecialNursingPlanId, updateReqVO.getId()));
        saveItems(updateReqVO.getId(), updateReqVO.getTenantId(), updateReqVO.getItems());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        validateExists(id);
        specialNursingPlanItemMapper.delete(new LambdaQueryWrapperX<ElderlySpecialNursingPlanItemDO>()
                .eq(ElderlySpecialNursingPlanItemDO::getSpecialNursingPlanId, id));
        specialNursingPlanMapper.deleteById(id);
    }

    @Override
    public SpecialNursingPlanRespVO get(Long id) {
        ElderlySpecialNursingPlanDO record = specialNursingPlanMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        SpecialNursingPlanRespVO respVO = BeanUtils.toBean(record, SpecialNursingPlanRespVO.class);
        List<ElderlySpecialNursingPlanItemDO> items = specialNursingPlanItemMapper.selectList(
                new LambdaQueryWrapperX<ElderlySpecialNursingPlanItemDO>()
                        .eq(ElderlySpecialNursingPlanItemDO::getSpecialNursingPlanId, id)
                        .orderByAsc(ElderlySpecialNursingPlanItemDO::getId)
        );
        respVO.setItems(BeanUtils.toBean(items, SpecialNursingPlanItemRespVO.class));
        return respVO;
    }

    @Override
    public PageResult<SpecialNursingPlanRespVO> getPage(SpecialNursingPlanPageReqVO pageReqVO) {
        PageResult<ElderlySpecialNursingPlanDO> pageResult = specialNursingPlanMapper.selectPage(pageReqVO);
        return BeanUtils.toBean(pageResult, SpecialNursingPlanRespVO.class);
    }

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

    private void saveItems(Long specialNursingPlanId, Long tenantId, List<SpecialNursingPlanItemSaveReqVO> items) {
        if (items == null || items.isEmpty()) {
            return;
        }
        for (SpecialNursingPlanItemSaveReqVO item : items) {
            ElderlySpecialNursingPlanItemDO itemDO = BeanUtils.toBean(item, ElderlySpecialNursingPlanItemDO.class);
            itemDO.setId(null);
            itemDO.setSpecialNursingPlanId(specialNursingPlanId);
            itemDO.setTenantId(itemDO.getTenantId() == null ? tenantId : itemDO.getTenantId());
            specialNursingPlanItemMapper.insert(itemDO);
        }
    }
}
