package cn.iocoder.yudao.module.bpm.service.oa.expense;

import cn.iocoder.yudao.module.bpm.controller.admin.oa.expense.vo.OaExpensePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.expense.vo.OaExpenseSaveReqVO;
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 cn.iocoder.yudao.module.bpm.dal.dataobject.oa.expense.OaExpenseDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.expense.OaExpenseObjDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.bpm.dal.mysql.oa.expense.OaExpenseMapper;
import cn.iocoder.yudao.module.bpm.dal.mysql.oa.expense.OaExpenseObjMapper;

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

/**
 * 报销流程信息 Service 实现类
 *
 * @author dp
 */
@Service
@Validated
public class OaExpenseServiceImpl implements OaExpenseService {

    @Resource
    private OaExpenseMapper oaExpenseMapper;
    @Resource
    private OaExpenseObjMapper oaExpenseObjMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createOaExpense(OaExpenseSaveReqVO createReqVO) {
        // 插入
        OaExpenseDO oaExpense = BeanUtils.toBean(createReqVO, OaExpenseDO.class);
        oaExpenseMapper.insert(oaExpense);

        // 插入子表
        createOaExpenseObjList(oaExpense.getId(), createReqVO.getOaExpenseObjs());
        // 返回
        return oaExpense.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOaExpense(OaExpenseSaveReqVO updateReqVO) {
        // 校验存在
        validateOaExpenseExists(updateReqVO.getId());
        // 更新
        OaExpenseDO updateObj = BeanUtils.toBean(updateReqVO, OaExpenseDO.class);
        oaExpenseMapper.updateById(updateObj);

        // 更新子表
        updateOaExpenseObjList(updateReqVO.getId(), updateReqVO.getOaExpenseObjs());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOaExpense(Long id) {
        // 校验存在
        validateOaExpenseExists(id);
        // 删除
        oaExpenseMapper.deleteById(id);

        // 删除子表
        deleteOaExpenseObjByExpenseId(id);
    }

    private void validateOaExpenseExists(Long id) {
        if (oaExpenseMapper.selectById(id) == null) {
            throw exception(OA_EXPENSE_NOT_EXISTS);
        }
    }

    @Override
    public OaExpenseDO getOaExpense(Long id) {
        return oaExpenseMapper.selectById(id);
    }

    @Override
    public PageResult<OaExpenseDO> getOaExpensePage(OaExpensePageReqVO pageReqVO) {
        return oaExpenseMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（报销流程信息子） ====================

    @Override
    public List<OaExpenseObjDO> getOaExpenseObjListByExpenseId(Long expenseId) {
        return oaExpenseObjMapper.selectListByExpenseId(expenseId);
    }

    private void createOaExpenseObjList(Long expenseId, List<OaExpenseObjDO> list) {
        list.forEach(o -> o.setExpenseId(expenseId));
        oaExpenseObjMapper.insertBatch(list);
    }

    private void updateOaExpenseObjList(Long expenseId, List<OaExpenseObjDO> list) {
        deleteOaExpenseObjByExpenseId(expenseId);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createOaExpenseObjList(expenseId, list);
    }

    private void deleteOaExpenseObjByExpenseId(Long expenseId) {
        oaExpenseObjMapper.deleteByExpenseId(expenseId);
    }

}