package com.unis.module.coding.service.cwxxyear;

import com.github.yitter.idgen.YitIdHelper;
import com.unis.framework.common.util.object.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.unis.module.coding.controller.admin.cwxxyear.vo.*;
import com.unis.module.coding.dal.dataobject.cwxxyear.CwxxYearDO;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.util.object.BeanUtils;

import com.unis.module.coding.dal.mysql.cwxxyear.CwxxYearMapper;

import static com.unis.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.unis.module.core.enums.ErrorCodeConstants.CWXX_YEAR_NOT_EXISTS;

/**
 * 财年务情况 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CwxxYearServiceImpl implements CwxxYearService {

    @Resource
    private CwxxYearMapper cwxxYearMapper;

    @Override
    public Long createCwxxYear(CwxxYearSaveReqVO createReqVO) {
        // 插入
        CwxxYearDO cwxxYear = BeanUtils.toBean(createReqVO, CwxxYearDO.class);
        cwxxYear.setId(YitIdHelper.nextId());
        System.out.println(cwxxYear.getId());
        cwxxYearMapper.insert(cwxxYear);
        // 返回
        return cwxxYear.getId();
    }

    @Override
    public void updateCwxxYear(CwxxYearSaveReqVO updateReqVO) {
        // 校验存在
        validateCwxxYearExists(updateReqVO.getId());
        // 更新
        CwxxYearDO updateObj = BeanUtils.toBean(updateReqVO, CwxxYearDO.class);
        cwxxYearMapper.updateById(updateObj);
    }

    @Override
    public void deleteCwxxYear(Long id) {
        // 校验存在
        validateCwxxYearExists(id);
        // 删除
        cwxxYearMapper.deleteById(id);
    }

    private void validateCwxxYearExists(Long id) {
        if (cwxxYearMapper.selectById(id) == null) {
            throw exception(CWXX_YEAR_NOT_EXISTS);
        }
    }

    @Override
    public CwxxYearDO getCwxxYear(Long id) {
        return cwxxYearMapper.selectById(id);
    }

    @Override
    public PageResult<CwxxYearDO> getCwxxYearPage(CwxxYearPageReqVO pageReqVO) {
        return cwxxYearMapper.selectPage(pageReqVO);
    }

}