package cn.iocoder.lyzsys.module.tjt.service.yearkvalue;

import cn.iocoder.lyzsys.framework.common.pojo.PageResult;
import cn.iocoder.lyzsys.framework.common.util.object.BeanUtils;
import cn.iocoder.lyzsys.module.tjt.controller.admin.yearkvalue.vo.YearKValuePageReqVO;
import cn.iocoder.lyzsys.module.tjt.controller.admin.yearkvalue.vo.YearKValueRespVO;
import cn.iocoder.lyzsys.module.tjt.controller.admin.yearkvalue.vo.YearKValueSaveReqVO;
import cn.iocoder.lyzsys.module.tjt.dal.dataobject.yearkvalue.YearKValueDO;
import cn.iocoder.lyzsys.module.tjt.dal.mysql.yearkvalue.YearKValueMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.lyzsys.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.lyzsys.module.tjt.enums.ErrorCodeConstants.YEAR_K_VALUE_DUPLICATE;
import static cn.iocoder.lyzsys.module.tjt.enums.ErrorCodeConstants.YEAR_K_VALUE_NOT_EXISTS;

@Service
@Validated
public class YearKValueServiceImpl implements YearKValueService {

    @Resource
    private YearKValueMapper yearKValueMapper;

    @Override
    public Long createYearKValue(YearKValueSaveReqVO createReqVO) {
        validateYearUnique(null, createReqVO.getKYear());
        YearKValueDO yearKValue = BeanUtils.toBean(createReqVO, YearKValueDO.class);
        if (yearKValue.getEnabledFlag() == null) {
            yearKValue.setEnabledFlag(Boolean.TRUE);
        }
        yearKValueMapper.insert(yearKValue);
        return yearKValue.getId();
    }

    @Override
    public void updateYearKValue(YearKValueSaveReqVO updateReqVO) {
        validateYearKValueExists(updateReqVO.getId());
        validateYearUnique(updateReqVO.getId(), updateReqVO.getKYear());
        YearKValueDO updateObj = BeanUtils.toBean(updateReqVO, YearKValueDO.class);
        yearKValueMapper.updateById(updateObj);
    }

    @Override
    public void deleteYearKValue(Long id) {
        validateYearKValueExists(id);
        yearKValueMapper.deleteById(id);
    }

    @Override
    public YearKValueRespVO getYearKValue(Long id) {
        return BeanUtils.toBean(validateYearKValueExists(id), YearKValueRespVO.class);
    }

    @Override
    public PageResult<YearKValueRespVO> getYearKValuePage(YearKValuePageReqVO pageReqVO) {
        return BeanUtils.toBean(yearKValueMapper.selectPage(pageReqVO), YearKValueRespVO.class);
    }

    private void validateYearUnique(Long id, Integer kYear) {
        YearKValueDO yearKValue = yearKValueMapper.selectByKYear(kYear);
        if (yearKValue == null) {
            return;
        }
        if (id == null || !yearKValue.getId().equals(id)) {
            throw exception(YEAR_K_VALUE_DUPLICATE);
        }
    }

    private YearKValueDO validateYearKValueExists(Long id) {
        YearKValueDO yearKValue = yearKValueMapper.selectById(id);
        if (yearKValue == null) {
            throw exception(YEAR_K_VALUE_NOT_EXISTS);
        }
        return yearKValue;
    }

}
