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

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
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.controller.admin.biz.vo.TransferChangeDetailRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.TransferSyncDataReqVO;
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.ElderlyChangeRecordMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.PriceChangeRecordMapper;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

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

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

@Service
@Validated
public class TransferChangeRecordServiceImpl implements TransferChangeRecordService{

    @Resource
    private ElderlyChangeRecordMapper changeRecordMapper;

    @Resource
    private BuildService buildService;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public TransferChangeDetailRespVO getTransferDetail(Long id) {
        TransferChangeDetailRespVO detailRespVO = changeRecordMapper.selectDetail(id);
        if (detailRespVO == null) {
            throw exception(TRANSFER_CHANGE_RECORD_NOT_EXISTS);
        }
        detailRespVO.setBedName(buildService.getFullBedName(detailRespVO.getElderlyId()));
        return detailRespVO;
    }

    @Override
    @TenantIgnore
    @LogRecord(type = ELDER_TRANSFER_CHANGE_TYPE, subType = ELDER_TRANSFER_CHANGE_SYNC_DATA_SUB_TYPE, bizNo = "{{#elderlyId}}",
            success = ELDER_TRANSFER_CHANGE_SYNC_DATA_SUCCESS, extra = "{{#elderlyId}}")
    public Long transferSyncData(Long loginUserId, String nickname, TransferSyncDataReqVO createReqVO) {
        ElderlyChangeRecordDO changeRecordDO = changeRecordMapper.selectById(createReqVO.getId());
        if (0 == createReqVO.getTransferType()) {

            ElderlyInfoDO elderlyInfoDO = elderlyInfoMapper.selectById(changeRecordDO.getElderlyId());

            ElderlyInfoDO checkInfo = elderlyInfoMapper.selectOne(new LambdaQueryWrapperX<ElderlyInfoDO>()
                    .eq(ElderlyInfoDO::getElderName, elderlyInfoDO.getElderName())
                    .eq(ElderlyInfoDO::getIdCard, elderlyInfoDO.getIdCard())
                    .eq(ElderlyInfoDO::getTenantId, changeRecordDO.getExpectId()));

            if (checkInfo != null) {
                throw exception(TRANSFER_CHANGE_SYNC_DATA_EXISTS);
            }

            elderlyInfoDO.setId(null);
            elderlyInfoDO.setTenantId(changeRecordDO.getExpectId());
            elderlyInfoDO.setCreateBy(SecurityFrameworkUtils.getLoginUserNickname());
            elderlyInfoDO.setCreateTime(new Date());
            elderlyInfoDO.setInStatus(0);
            elderlyInfoMapper.insert(elderlyInfoDO);
            LogRecordContext.putVariable("elderName", elderlyInfoDO.getElderName());
        }
        LogRecordContext.putVariable("elderlyId", changeRecordDO.getElderlyId());
        LogRecordContext.putVariable("originalName", changeRecordDO.getOriginalName());
        LogRecordContext.putVariable("expectName", changeRecordDO.getExpectName());

        return null;
    }
}
