package cn.iocoder.yudao.module.pms.service.iotoperationplan;

import cn.iocoder.yudao.module.pms.dal.dataobject.iotoperationplandev.IotOperationPlanDevDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotoperationplandev.IotOperationPlanDevMapper;
import com.google.common.collect.ImmutableBiMap;
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.pms.controller.admin.iotoperationplan.vo.*;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotoperationplan.IotOperationPlanDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pms.dal.mysql.iotoperationplan.IotOperationPlanMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_OPERATION_PLAN_NOT_EXISTS;


/**
 * 运行记录计划 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotOperationPlanServiceImpl implements IotOperationPlanService {

    @Resource
    private IotOperationPlanMapper iotOperationPlanMapper;
    @Resource
    private IotOperationPlanDevMapper planDevMapper;

    @Override
    public Long createIotOperationPlan(IotOperationPlanSaveReqVO createReqVO) {
        // 插入
        IotOperationPlanDO iotOperationPlan = BeanUtils.toBean(createReqVO, IotOperationPlanDO.class);
        iotOperationPlanMapper.insert(iotOperationPlan);
        // 返回
        for (IotOperationPlanDevDO planDevDO:createReqVO.getPlanDevList()) {
            planDevDO.setPlanId(iotOperationPlan.getId());
            System.out.println(planDevDO.getId());
        }

        System.out.println(createReqVO.getPlanDevList().size());


        planDevMapper.insertBatch(createReqVO.getPlanDevList());
        return iotOperationPlan.getId();
    }

    @Override
    public void updateIotOperationPlan(IotOperationPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateIotOperationPlanExists(updateReqVO.getId());
        // 更新
        IotOperationPlanDO updateObj = BeanUtils.toBean(updateReqVO, IotOperationPlanDO.class);
        iotOperationPlanMapper.updateById(updateObj);
        int del = planDevMapper.delByPlanId(updateReqVO.getId());
        if(del>0){
            for (IotOperationPlanDevDO planDevDO:updateReqVO.getPlanDevList()) {
                planDevDO.setPlanId(updateReqVO.getId());
            }
            planDevMapper.insertBatch(updateReqVO.getPlanDevList());
        }

    }

    @Override
    public void deleteIotOperationPlan(Long id) {
        // 校验存在
        validateIotOperationPlanExists(id);
        // 删除
        iotOperationPlanMapper.deleteById(id);
    }

    private void validateIotOperationPlanExists(Long id) {
        if (iotOperationPlanMapper.selectById(id) == null) {
            throw exception(IOT_OPERATION_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public IotOperationPlanDO getIotOperationPlan(Long id) {
        return iotOperationPlanMapper.selectById(id);
    }

    @Override
    public PageResult<IotOperationPlanDO> getIotOperationPlanPage(IotOperationPlanPageReqVO pageReqVO,Collection<Long> deptIds) {
        return iotOperationPlanMapper.selectPage(pageReqVO,deptIds);
    }

    @Override
    public void updateInspectPlanStatus(Long id, Integer status) {
        IotOperationPlanDO planDO = iotOperationPlanMapper.selectById(id).setStatus(status);
        iotOperationPlanMapper.updateById(planDO);
    }

}