package com.xjrsoft.module.room.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.room.dto.AddRoomRuleItemDto;
import com.xjrsoft.module.room.dto.RoomRulePageDto;
import com.xjrsoft.module.room.entity.RoomRule;
import com.xjrsoft.module.room.entity.RoomRuleItem;
import com.xjrsoft.module.room.mapper.RoomRuleItemMapper;
import com.xjrsoft.module.room.mapper.RoomRuleMapper;
import com.xjrsoft.module.room.service.IRoomRuleService;
import com.xjrsoft.module.room.vo.RoomRulePageVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
* @title: 寝室生成规则
* @Author dzx
* @Date: 2024-01-02
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class RoomRuleServiceImpl extends MPJBaseServiceImpl<RoomRuleMapper, RoomRule> implements IRoomRuleService {
    private final RoomRuleMapper roomRuleRoomRuleMapper;

    private final RoomRuleItemMapper roomRuleRoomRuleItemMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(RoomRule roomRule) {
        roomRule.setSortCode(roomRuleRoomRuleMapper.getMaxSortCode() + 1);
        roomRuleRoomRuleMapper.insert(roomRule);
//        for (RoomRuleItem roomRuleItem : roomRule.getRoomRuleItemList()) {
//            roomRuleItem.setRoomRuleId(roomRule.getId());
//            roomRuleRoomRuleItemMapper.insert(roomRuleItem);
//        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(RoomRule roomRule) {
        roomRuleRoomRuleMapper.updateById(roomRule);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        roomRuleRoomRuleMapper.deleteBatchIds(ids);
        roomRuleRoomRuleItemMapper.delete(Wrappers.lambdaQuery(RoomRuleItem.class).in(RoomRuleItem::getRoomRuleId, ids));

        return true;
    }

    @Override
    public Page<RoomRulePageVo> getPage(Page<RoomRulePageDto> page, RoomRulePageDto dto) {
        Page<RoomRulePageVo> result = roomRuleRoomRuleMapper.getPage(page, dto);
        List<RoomRulePageVo> records = result.getRecords();

        for (RoomRulePageVo record : records) {
            String[] ruleArray = record.getRule().split(",");
            List<String> ruleList = Arrays.asList(ruleArray);
            int houseNumber = 0;
            if(ruleList.contains("1")){
                houseNumber  = houseNumber + 1;
            }
            if(ruleList.contains("2")){
                houseNumber  = houseNumber + 1;
            }
            if(ruleList.contains("3")){
                houseNumber  = houseNumber + 2;
            }
            record.setHouseNumber(houseNumber + "位");

            List<RoomRuleItem> roomRuleItems = roomRuleRoomRuleItemMapper.selectList(
                    new QueryWrapper<RoomRuleItem>().lambda().eq(RoomRuleItem::getRoomRuleId, record.getId())
            );
            String buildIds = "";
            for (int i = 0; i < roomRuleItems.size(); i ++){
                if(i > 0){
                    buildIds += ",";
                }
                buildIds = buildIds + roomRuleItems.get(i).getOfficeBuildId();
            }
            record.setBuildIds(buildIds);
        }
        return result;
    }

    /**
     * 绑定楼栋
     * @param dto
     * @return
     */
    @Override
    public Boolean bindBuilds(AddRoomRuleItemDto dto) {
        //先清空之前的数据
        roomRuleRoomRuleItemMapper.delete(new QueryWrapper<RoomRuleItem>().lambda().eq(RoomRuleItem::getRoomRuleId, dto.getRoomRuleId()));
        //再插入最新传入的楼栋
        Integer itemMaxSortCode = roomRuleRoomRuleMapper.getItemMaxSortCode();
        for (Long buildId : dto.getOfficeBuildIds()) {
            if(buildId == null || dto.getRoomRuleId() == null){
                continue;
            }
            RoomRuleItem roomRuleItem = new RoomRuleItem();
            itemMaxSortCode ++;
            roomRuleItem.setSortCode(itemMaxSortCode);
            roomRuleItem.setRoomRuleId(dto.getRoomRuleId());
            roomRuleItem.setOfficeBuildId(buildId);

            roomRuleRoomRuleItemMapper.insert(roomRuleItem);
        }
        return true;
    }
}
