package cn.iocoder.yudao.module.iot.service.formula;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.formula.vo.FormulaPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.formula.vo.FormulaSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.formula.FormulaDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.formula.FormulaDetailDO;
import cn.iocoder.yudao.module.iot.dal.mysql.formula.FormulaDetailMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.formula.FormulaMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.FORMULA_DETAIL_NOT_EXISTS;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.FORMULA_NOT_EXISTS;

/**
 * 计算公式 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class FormulaServiceImpl implements FormulaService {

    @Resource
    private FormulaMapper formulaMapper;
    @Resource
    private FormulaDetailMapper formulaDetailMapper;

    @Override
    public Long createFormula(FormulaSaveReqVO createReqVO) {
        // 插入
        FormulaDO formula = BeanUtils.toBean(createReqVO, FormulaDO.class);
        formulaMapper.insert(formula);
        // 返回
        return formula.getId();
    }

    @Override
    public void updateFormula(FormulaSaveReqVO updateReqVO) {
        // 校验存在
        validateFormulaExists(updateReqVO.getId());
        // 更新
        FormulaDO updateObj = BeanUtils.toBean(updateReqVO, FormulaDO.class);
        formulaMapper.updateById(updateObj);
    }

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

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

    private void validateFormulaExists(Long id) {
        if (formulaMapper.selectById(id) == null) {
            throw exception(FORMULA_NOT_EXISTS);
        }
    }

    @Override
    public FormulaDO getFormula(Long id) {
        return formulaMapper.selectById(id);
    }

    @Override
    public PageResult<FormulaDO> getFormulaPage(FormulaPageReqVO pageReqVO) {
        return formulaMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（计算公式明细） ====================

    @Override
    public PageResult<FormulaDetailDO> getFormulaDetailPage(PageParam pageReqVO, Long formulaId) {
        return formulaDetailMapper.selectPage(pageReqVO, formulaId);
    }

    @Override
    public Long createFormulaDetail(FormulaDetailDO formulaDetail) {
        formulaDetailMapper.insert(formulaDetail);
        return formulaDetail.getId();
    }

    @Override
    public void updateFormulaDetail(FormulaDetailDO formulaDetail) {
        // 校验存在
        validateFormulaDetailExists(formulaDetail.getId());
        // 更新
        formulaDetailMapper.updateById(formulaDetail);
    }

    @Override
    public void deleteFormulaDetail(Long id) {
        // 校验存在
        validateFormulaDetailExists(id);
        // 删除
        formulaDetailMapper.deleteById(id);
    }

    @Override
    public FormulaDetailDO getFormulaDetail(Long id) {
        return formulaDetailMapper.selectById(id);
    }

    private void validateFormulaDetailExists(Long id) {
        if (formulaDetailMapper.selectById(id) == null) {
            throw exception(FORMULA_DETAIL_NOT_EXISTS);
        }
    }

    private void deleteFormulaDetailByFormulaId(Long formulaId) {
        formulaDetailMapper.deleteByFormulaId(formulaId);
    }

}