package com.yc.ship.module.resource.service.roommodel;

import com.yc.ship.module.resource.controller.app.roommodel.vo.AppResourceRoomModelPageReqVO;
import com.yc.ship.module.resource.dal.dataobject.room.ResourceRoomDO;
import com.yc.ship.module.resource.dal.dataobject.roommodelrooms.ResourceRoomModelRoomsDO;
import com.yc.ship.module.resource.dal.dataobject.roommodelservice.ResourceRoomModelServiceDO;
import com.yc.ship.module.resource.dal.mysql.roomModelRooms.ResourceRoomModelRoomsMapper;
import com.yc.ship.module.resource.dal.mysql.roommodelservice.ResourceRoomModelServiceMapper;
import org.apache.commons.lang3.StringUtils;
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 com.yc.ship.module.resource.controller.admin.roommodel.vo.*;
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.util.object.BeanUtils;

import com.yc.ship.module.resource.dal.mysql.roommodel.ResourceRoomModelMapper;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.*;

/**
 * 资源管理-房型管理 Service 实现类
 *
 * @author lfy
 */
@Service
@Validated
public class ResourceRoomModelServiceImpl implements ResourceRoomModelService {

    @Resource
    private ResourceRoomModelMapper roomModelMapper;

    @Resource
    private ResourceRoomModelRoomsMapper roomModelRoomsMapper;

    @Resource
    private ResourceRoomModelServiceMapper roomModelServiceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createRoomModel(ResourceRoomModelSaveReqVO createReqVO) {
        // 插入
        ResourceRoomModelDO roomModel = BeanUtils.toBean(createReqVO, ResourceRoomModelDO.class);
        roomModelMapper.insert(roomModel);
        //处理关联房间
        handleRoomModelRooms(roomModel.getId(), createReqVO.getRoomNums());
        //处理服务设施
        handleServices(roomModel.getId(), BeanUtils.toBean(createReqVO.getServices(), ResourceRoomModelServiceDO.class));
        // 返回
        return roomModel.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoomModel(ResourceRoomModelSaveReqVO updateReqVO) {
        // 校验存在
        validateRoomModelExists(updateReqVO.getId());
        // 更新
        ResourceRoomModelDO updateObj = BeanUtils.toBean(updateReqVO, ResourceRoomModelDO.class);
        roomModelMapper.updateById(updateObj);
        //处理关联房间
        handleRoomModelRooms(updateReqVO.getId(), updateReqVO.getRoomNums());
        //处理服务设施
        handleServices(updateReqVO.getId(), BeanUtils.toBean(updateReqVO.getServices(), ResourceRoomModelServiceDO.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoomModel(Long id) {
        // 校验存在
        validateRoomModelExists(id);
        // 删除
        roomModelMapper.deleteById(id);
        roomModelRoomsMapper.deleteByRoomModelId(id);
        roomModelServiceMapper.deleteServiceByRoomModelId(id);
    }

    private void validateRoomModelExists(Long id) {
        if (roomModelMapper.selectById(id) == null) {
            throw exception(ROOM_MODEL_NOT_EXISTS);
        }
    }

    private void handleRoomModelRooms(Long modelId, String roomNumIds) {
        roomModelRoomsMapper.deleteByRoomModelId(modelId);
        if(StringUtils.isNotBlank(roomNumIds)) {
            String[] split = roomNumIds.split(",");
            List<ResourceRoomModelRoomsDO> list = new ArrayList<>();
            for (String s : split) {
                ResourceRoomModelRoomsDO roomModelRooms = ResourceRoomModelRoomsDO.builder()
                        .roomId(Long.valueOf(s))
                        .roomModelId(modelId)
                        .build();
                list.add(roomModelRooms);
            }
            roomModelRoomsMapper.insertBatch(list);
        }
    }

    private void handleServices(Long modelId, List<ResourceRoomModelServiceDO> services) {
        roomModelServiceMapper.deleteServiceByRoomModelId(modelId);
        services.stream().forEach(service -> {
            service.setRoomModelId(modelId);
        });
        if(services.size() > 0) {
            roomModelServiceMapper.insertBatch(services);
        }

    }

    @Override
    public ResourceRoomModelDO getRoomModel(Long id) {
        return roomModelMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceRoomModelDO> getRoomModelPage(ResourceRoomModelPageReqVO pageReqVO) {
        return roomModelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourceRoomModelDO> getRoomModelListByShipId(String shipId) {
        return roomModelMapper.selectListByShipId(shipId);
    }

    @Override
    public List<ResourceRoomModelDO> getList(List<Long> modelIds) {
        if(modelIds.isEmpty()) {
            return Collections.emptyList();
        }
        return roomModelMapper.selectByIds(modelIds);
    }

    @Override
    public List<ResourceRoomModelDO> getRoomModelCanSaleListByShipId(String shipId) {
        return roomModelMapper.selectRoomModelCanSaleListByShipId(shipId);
    }

    @Override
    public PageResult<ResourceRoomModelDO> getAppRoomModelPage(AppResourceRoomModelPageReqVO pageReqVO) {
        return roomModelMapper.selectAppPage(pageReqVO);
    }

}