package cn.ey.biz.module.system.service.contract;

import cn.ey.biz.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ey.biz.module.system.controller.admin.contract.v2.vo.ContractArchivedVO;
import cn.ey.biz.module.system.convert.contract.ContractArchivedConvert;
import cn.ey.biz.module.system.dal.dataobject.contract.ContractArchivedDO;
import cn.ey.biz.module.system.dal.mapper.contract.ContractArchivedMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

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

@Service
@Log4j2
public class ContractArchivedServiceImpl implements ContractArchivedService {

    @Resource
    private ContractArchivedMapper contractArchivedMapper;

    @Override
    public void saveContractArchived(ContractArchivedVO archived) {
        ContractArchivedDO entity = contractArchivedMapper.selectOne(new LambdaQueryWrapperX<ContractArchivedDO>()
                .eq(ContractArchivedDO::getSl, archived.getSl())
                .eq(ContractArchivedDO::getMr, archived.getMr())
                .eq(ContractArchivedDO::getGeo, archived.getGeo())
                .eq(ContractArchivedDO::getType, archived.getType()));

        if (Objects.isNull(entity)) {
            contractArchivedMapper.insert(ContractArchivedConvert.INSTANCE.vo2Do(archived));
        } else {
            entity.setActivate(archived.getActivate());
            entity.setDataRole(archived.getDataRole());
            entity.setDataRoleId(archived.getDataRoleId());
            contractArchivedMapper.updateById(entity);
        }
    }

    @Override
    public ContractArchivedVO getContractArchived(String sl, String mr, String geo, String type) {
        ContractArchivedDO entity = contractArchivedMapper.selectOne(new LambdaQueryWrapperX<ContractArchivedDO>()
                .eq(ContractArchivedDO::getSl, sl).eq(ContractArchivedDO::getMr, mr)
                .eq(ContractArchivedDO::getGeo, geo).eq(ContractArchivedDO::getType, type));
        return ContractArchivedConvert.INSTANCE.do2VO(entity);
    }
}
