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

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.api.bpm.vo.PriceChangeRecordReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.PriceChangeDetailRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyChangeRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.PriceChangeRecordMapper;
import org.springframework.stereotype.Service;
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.PRICE_CHANGE_RECORD_NOT_EXISTS;

/**
 * @Author: jinyd
 * @CreateTime: 2024/8/29  10:10
 * @Description:
 * @Version: 4.0
 */

@Service
@Validated
public class PriceChangeRecordServiceImpl implements PriceChangeRecordService{

    @Resource
    private PriceChangeRecordMapper priceChangeRecordMapper;

    @Resource
    private BuildService buildService;


    @Resource
    private ChangeRecordService changeRecordService;
    @Resource
    private ElderlyInfoService elderlyInfoService;

    @Override
    @TenantIgnore
    public PriceChangeDetailRespVO getPriceDetail(Long id) {
        ElderlyChangeRecordDO changeRecordDO = changeRecordService.selectById(id);
        if (changeRecordDO == null) {
            throw exception(PRICE_CHANGE_RECORD_NOT_EXISTS);
        }
        ElderlyInfoDO elderlyInfoDO = elderlyInfoService.selectByIdTenantIgnore(changeRecordDO.getElderlyId());
        List<PriceChangeRecordReqVO> priceChangeList = priceChangeRecordMapper.selectListByChangeId(changeRecordDO.getElderlyId(), changeRecordDO.getChangeDate());
        PriceChangeDetailRespVO result = BeanUtils.toBean(changeRecordDO, PriceChangeDetailRespVO.class);
        result.setPriceChangeList(priceChangeList);
        result.setElderName(elderlyInfoDO.getElderName());
        result.setIdCard(elderlyInfoDO.getIdCard());
        result.setTenantId(elderlyInfoDO.getTenantId());
        result.setBedName(buildService.getFullBedName(changeRecordDO.getElderlyId()));
        return result;
    }
}
