package cn.iocoder.yudao.module.mes.service.dvcheck;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.dvcheck.vo.DvCheckPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dvcheck.vo.DvCheckSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvcheck.DvCheckDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvcheckdevice.DvCheckDeviceDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvchecksubject.DvCheckSubjectDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dvcheck.DvCheckMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.dvcheckdevice.DvCheckDeviceMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.dvchecksubject.DvCheckSubjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.DV_CHECK_NOT_EXISTS;

/**
 * 维保计划 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class DvCheckServiceImpl implements DvCheckService {

    @Resource
    private DvCheckMapper dvCheckMapper;
    @Resource
    private DvCheckDeviceMapper dvCheckDeviceMapper;
    @Resource
    private DvCheckSubjectMapper dvCheckSubjectMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createDvCheck(DvCheckSaveReqVO createReqVO) {
        // 插入
        DvCheckDO dvCheck = BeanUtils.toBean(createReqVO, DvCheckDO.class);
        dvCheckMapper.insert(dvCheck);

        // 插入子表
        createDvCheckDeviceList(dvCheck.getId(), createReqVO.getDvCheckDevices());
        createDvCheckSubjectList(dvCheck.getId(), createReqVO.getDvCheckSubjects());
        // 返回
        return dvCheck.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDvCheck(DvCheckSaveReqVO updateReqVO) {
        // 校验存在
        validateDvCheckExists(updateReqVO.getId());
        // 更新
        DvCheckDO updateObj = BeanUtils.toBean(updateReqVO, DvCheckDO.class);
        dvCheckMapper.updateById(updateObj);

        // 更新子表
        updateDvCheckDeviceList(updateReqVO.getId(), updateReqVO.getDvCheckDevices());
        updateDvCheckSubjectList(updateReqVO.getId(), updateReqVO.getDvCheckSubjects());
    }

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

        // 删除子表
        deleteDvCheckDeviceByPlanId(id);
        deleteDvCheckSubjectByPlanId(id);
    }

    private void validateDvCheckExists(Long id) {
        if (dvCheckMapper.selectById(id) == null) {
            throw exception(DV_CHECK_NOT_EXISTS);
        }
    }

    @Override
    public DvCheckDO getDvCheck(Long id) {
        return dvCheckMapper.selectById(id);
    }

    @Override
    public PageResult<DvCheckDO> getDvCheckPage(DvCheckPageReqVO pageReqVO) {
        return dvCheckMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（维保计划设备） ====================

    @Override
    public List<DvCheckDeviceDO> getDvCheckDeviceListByPlanId(Long planId) {
        return dvCheckDeviceMapper.selectListByPlanId(planId);
    }

    private void createDvCheckDeviceList(Long planId, List<DvCheckDeviceDO> list) {
        list.forEach(o -> o.setPlanId(planId));
        dvCheckDeviceMapper.insertBatch(list);
    }

    private void updateDvCheckDeviceList(Long planId, List<DvCheckDeviceDO> list) {
        deleteDvCheckDeviceByPlanId(planId);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createDvCheckDeviceList(planId, list);
    }

    private void deleteDvCheckDeviceByPlanId(Long planId) {
        dvCheckDeviceMapper.deleteByPlanId(planId);
    }

    // ==================== 子表（维保计划项目） ====================

    @Override
    public List<DvCheckSubjectDO> getDvCheckSubjectListByPlanId(Long planId) {
        return dvCheckSubjectMapper.selectListByPlanId(planId);
    }

    private void createDvCheckSubjectList(Long planId, List<DvCheckSubjectDO> list) {
        list.forEach(o -> o.setPlanId(planId));
        dvCheckSubjectMapper.insertBatch(list);
    }

    private void updateDvCheckSubjectList(Long planId, List<DvCheckSubjectDO> list) {
        deleteDvCheckSubjectByPlanId(planId);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createDvCheckSubjectList(planId, list);
    }

    private void deleteDvCheckSubjectByPlanId(Long planId) {
        dvCheckSubjectMapper.deleteByPlanId(planId);
    }

}