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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.devicegroup.IotDeviceGroupDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.devicegroup.IotDeviceGroupDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.devicegroup.IotDeviceGroupPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.devicegroup.IotDeviceGroupSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.devicegroup.IotDeviceGroupDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.devicegroup.IotDeviceGroupDetailDO;
import cn.iocoder.yudao.module.pms.dal.mysql.devicegroup.IotDeviceGroupDetailMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.devicegroup.IotDeviceGroupMapper;
import cn.iocoder.yudao.module.pms.service.IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * PMS成套 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotDeviceGroupServiceImpl implements IotDeviceGroupService {

    @Resource
    private IotDeviceGroupMapper iotDeviceGroupMapper;
    @Autowired
    private IotDeviceGroupDetailMapper iotDeviceGroupDetailMapper;
    @Autowired
    private IotDeviceService iotDeviceService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createIotDeviceGroup(IotDeviceGroupSaveReqVO createReqVO) {
        // 插入
        List<IotDeviceGroupDetailSaveReqVO> details = createReqVO.getDetails();
        IotDeviceGroupDO iotDeviceGroup = BeanUtils.toBean(createReqVO, IotDeviceGroupDO.class);
        iotDeviceGroup.setDeleted(false);
        iotDeviceGroupMapper.insert(iotDeviceGroup);
        //子表插入
        details.forEach(e ->{
            IotDeviceGroupDetailDO bean = BeanUtil.toBean(e, IotDeviceGroupDetailDO.class);
            bean.setGroupId(iotDeviceGroup.getId());
            bean.setDeleted(false);
            iotDeviceGroupDetailMapper.insert(bean);
        });
        // 返回
        return iotDeviceGroup.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateIotDeviceGroup(IotDeviceGroupSaveReqVO updateReqVO) {
        // 校验存在
        List<IotDeviceGroupDetailSaveReqVO> details = updateReqVO.getDetails();
        validateIotDeviceGroupExists(updateReqVO.getId());
        // 更新
        IotDeviceGroupDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceGroupDO.class);
        iotDeviceGroupMapper.updateById(updateObj);
        //删除子表数据
        iotDeviceGroupDetailMapper.delete("group_id", String.valueOf(updateObj.getId()));
        //插入子表数据
        details.forEach(e ->{
           IotDeviceGroupDetailDO bean = BeanUtils.toBean(e, IotDeviceGroupDetailDO.class);
           bean.setGroupId(updateReqVO.getId());
           bean.setDeleted(false);
           iotDeviceGroupDetailMapper.insert(bean);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteIotDeviceGroup(Long id) {
        // 校验存在
        validateIotDeviceGroupExists(id);
        // 删除
        iotDeviceGroupMapper.deleteById(id);
        //删除子表
        iotDeviceGroupDetailMapper.delete("group_id", String.valueOf(id));
    }

    private void validateIotDeviceGroupExists(Long id) {
        if (iotDeviceGroupMapper.selectById(id) == null) {
            throw exception(new ErrorCode(2,"不存在"));
        }
    }

    @Override
    public IotDeviceGroupDO getIotDeviceGroup(Long id) {
        return iotDeviceGroupMapper.selectById(id);
    }

    @Override
    public PageResult<IotDeviceGroupDO> getIotDeviceGroupPage(IotDeviceGroupPageReqVO pageReqVO) {
        return iotDeviceGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<IotDeviceGroupDetailDO> getDeviceGroupList(IotDeviceGroupDetailPageReqVO reqVO) {
        IotDeviceDO iotDevice = iotDeviceService.getIotDevice(reqVO.getDeviceId());
        List<IotDeviceGroupDetailDO> deviceCodes = iotDeviceGroupDetailMapper.selectList("device_code", iotDevice.getDeviceCode());
        if (CollUtil.isEmpty(deviceCodes)) {
            return new PageResult<>();
        }
        IotDeviceGroupDetailDO iotDeviceGroupDetailDO = deviceCodes.get(0);
        Long groupId = iotDeviceGroupDetailDO.getGroupId();
//        IotDeviceGroupDetailPageReqVO pageReqVO = new IotDeviceGroupDetailPageReqVO();
        IotDeviceGroupDetailPageReqVO pageReqVO = new IotDeviceGroupDetailPageReqVO();
        pageReqVO.setGroupId(groupId);
        pageReqVO.setDeviceCode(reqVO.getDeviceCode());
        PageResult<IotDeviceGroupDetailDO> iotDeviceGroupDetailDOPageResult = iotDeviceGroupDetailMapper.selectPage(pageReqVO);
        return iotDeviceGroupDetailDOPageResult;
    }

}
