package com.yuchen.saas.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.entity.park.PmgBatParkFileInfo;
import com.yuchen.saas.park.api.entity.park.PmgBatParkFileRelevance;
import com.yuchen.saas.park.api.entity.park.PmgBatParkRoomInfo;
import com.yuchen.saas.park.api.vo.park.PmgbatParkRoomVo;
import com.yuchen.saas.park.api.vo.park.RoomVo;
import com.yuchen.saas.park.service.mapper.PmgBatParkFileInfoMapper;
import com.yuchen.saas.park.service.mapper.PmgBatParkRoomInfoMapper;
import com.yuchen.saas.park.service.service.PmgBatParkFileInfoService;
import com.yuchen.saas.park.service.service.PmgBatParkFileRelevanceService;
import com.yuchen.saas.park.service.service.PmgBatParkRoomInfoService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author 刘碧清
 * @date 2021/04/26 11:04
 */
@Service
public class PmgBatParkRoomInfoServiceImpl extends BaseServiceImpl<PmgBatParkRoomInfoMapper, PmgBatParkRoomInfo> implements PmgBatParkRoomInfoService {

    @Resource
    PmgBatParkFileInfoMapper pmgBatParkFileInfoMapper;

    @Autowired
    PmgBatParkFileInfoService pmgBatParkFileInfoService;

    @Autowired
    PmgBatParkFileRelevanceService pmgBatParkFileRelevanceService;

    @Override
    public List<PmgbatParkRoomVo> getList(Long floorId, RoomVo roomVo) {
        List<PmgbatParkRoomVo> list = new ArrayList<>();
        List<PmgBatParkRoomInfo> roomList = list(Wrappers.<PmgBatParkRoomInfo>lambdaQuery()
                .eq(PmgBatParkRoomInfo::getFloorId, floorId)
                .eq(roomVo.getRoomType() != 0, PmgBatParkRoomInfo::getRoomType, roomVo.getRoomType())
                .eq(roomVo.getRoomStatus() != 0, PmgBatParkRoomInfo::getLeaseStatus, roomVo.getRoomStatus())
                .eq(roomVo.getDecorateStatus() != 0, PmgBatParkRoomInfo::getDecorateStatus, roomVo.getDecorateStatus())
                .lt(roomVo.getRoomArea() == 1, PmgBatParkRoomInfo::getRoomArea, 60)  // 60以下
                .between(roomVo.getRoomArea() == 2, PmgBatParkRoomInfo::getRoomArea, 60, 100)   //60-100
                .between(roomVo.getRoomArea() == 3, PmgBatParkRoomInfo::getRoomArea, 100, 200)   //60-100
                .between(roomVo.getRoomArea() == 4, PmgBatParkRoomInfo::getRoomArea, 200, 300)   //60-100
                .gt(roomVo.getRoomArea() == 5, PmgBatParkRoomInfo::getRoomArea, 300)  //100 以上
                .lt(roomVo.getRoomPrice() == 1, PmgBatParkRoomInfo::getRentPrice, 60)   // 60以下
                .between(roomVo.getRoomPrice() == 2, PmgBatParkRoomInfo::getRentPrice, 60, 70)  //60-70
                .gt(roomVo.getRoomPrice() == 3, PmgBatParkRoomInfo::getRentPrice, 100)//70 以上
                .like(roomVo.getRoomNum() != null, PmgBatParkRoomInfo::getRoomNum, roomVo.getRoomNum()));  //搜索框查询
        if (roomList.size() > 0) {
            roomList.forEach(s -> {
                PmgbatParkRoomVo pmgbatParkRoomVo = new PmgbatParkRoomVo();
                List<PmgBatParkFileInfo> fileInfoList = pmgBatParkFileInfoMapper.getById(s.getId());
                pmgbatParkRoomVo.setPmgBatParkRoomInfo(s).setPmgBatParkFileRelevance(fileInfoList);
                list.add(pmgbatParkRoomVo);
            });
        }
        return list;
    }

    @Override
    public R submit(List<PmgbatParkRoomVo> pmgBatParkRoomVos) {

        if (pmgBatParkRoomVos != null && pmgBatParkRoomVos.size() > 0) {
            for (PmgbatParkRoomVo s : pmgBatParkRoomVos) {
                if (s.getPmgBatParkRoomInfo() != null) {
                    s.getPmgBatParkRoomInfo().setRoomName(s.getPmgBatParkRoomInfo().getRoomNum());
                    // 判断楼层名字和唯一标识是否重复
                    LambdaQueryWrapper<PmgBatParkRoomInfo> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(PmgBatParkRoomInfo::getFloorId,s.getPmgBatParkRoomInfo().getFloorId());
                    queryWrapper.ne(Func.isNotEmpty(s.getPmgBatParkRoomInfo().getId()), PmgBatParkRoomInfo::getId,
                            s.getPmgBatParkRoomInfo().getId());
                    queryWrapper.nested(qw -> qw.eq(PmgBatParkRoomInfo::getRMark,s.getPmgBatParkRoomInfo().getRMark()).or().eq(PmgBatParkRoomInfo::getRoomName,
                            s.getPmgBatParkRoomInfo().getRoomName()));
                    if (this.count(queryWrapper) > 0){
                        return R.error("房间名称或房间标识重复！");
                    }
                    saveOrUpdate(s.getPmgBatParkRoomInfo());
                }
                if (s.getPmgBatParkFileRelevance() != null && s.getPmgBatParkFileRelevance().size() > 0) {
                    List<PmgBatParkFileRelevance> rList = getFileRel(s);
                    //新增文件关联
                    pmgBatParkFileRelevanceService.saveOrUpdateBatch(rList);
                }
                //删除掉相关文件
                if (s.getDelFileIds() != null && s.getDelFileIds().size() > 0) {
                    pmgBatParkFileRelevanceService.remove(Wrappers.<PmgBatParkFileRelevance>lambdaQuery().in(PmgBatParkFileRelevance::getFileId, s.getDelFileIds()));
                    pmgBatParkFileRelevanceService.deleteLogic(s.getDelFileIds());
                }
            };
        }
        return R.status(true);
    }


    private List<PmgBatParkFileRelevance> getFileRel(PmgbatParkRoomVo s) {
        List<PmgBatParkFileRelevance> rList = new ArrayList<>();
        PmgBatParkFileRelevance fileRelevance = new PmgBatParkFileRelevance();
        s.getPmgBatParkFileRelevance().forEach(l -> {
            fileRelevance.setRelevanceId(s.getPmgBatParkRoomInfo().getId())
                    .setFileId(l.getId()).setParentId(l.getParkId()).setTypeId(1L);
            rList.add(fileRelevance);
        });
        return rList;
    }
}
