package io.hmit.modules.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.meeting.dao.MeetingDao;
import io.hmit.modules.meeting.dto.MeetingDTO;
import io.hmit.modules.meeting.dto.WlMeetingStaffDTO;
import io.hmit.modules.meeting.entity.MeetingEntity;
import io.hmit.modules.meeting.entity.WlMeetingStaffEntity;
import io.hmit.modules.meeting.service.MeetingService;
import io.hmit.modules.meeting.service.WlMeetingStaffService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会议管理
 *
 * @author syf syf0412@qq.com
 * @since 1.0.0 2020-12-25
 */
@Service
public class MeetingServiceImpl extends CrudServiceImpl<MeetingDao, MeetingEntity, MeetingDTO> implements MeetingService {

    @Autowired
    private WlMeetingStaffService wlMeetingStaffService;

    @Override
    public QueryWrapper<MeetingEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String title = (String)params.get("name");

        QueryWrapper<MeetingEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id)
        .like(StringUtils.isNotBlank(title), "title", title);

        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MeetingDTO dto) {
        MeetingEntity entity = ConvertUtils.sourceToTarget(dto, currentModelClass());
        insert(entity);

        //copy主键值到dto
        BeanUtils.copyProperties(entity, dto);

        //会议人员对应关系保存
        Map<String, Object> params = new HashMap<>();
        params.put("meetingId",dto.getId());
        List<WlMeetingStaffDTO> wl= wlMeetingStaffService.list(params);
        if(wl.size()>0){
            wl.forEach(wlMeetingStaffDTO -> {
                wlMeetingStaffService.deleteById(wlMeetingStaffDTO.getId());
            });
        }
        List<WlMeetingStaffEntity> wlMeetingStaffList = new ArrayList<>();

        dto.getInvitedUserIDs().stream().forEach(userId->{
            WlMeetingStaffEntity meetingStaff = new WlMeetingStaffEntity();
            meetingStaff = meetingStaff.assembleWlMeetingStaff(dto.getId(),userId,null);
            wlMeetingStaffList.add(meetingStaff);
        });
        wlMeetingStaffService.insertBatch(wlMeetingStaffList);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MeetingDTO dto) {
        MeetingEntity entity = ConvertUtils.sourceToTarget(dto, currentModelClass());
        updateById(entity);

        //copy主键值到dto
//        BeanUtils.copyProperties(entity, dto);

        //会议人员对应关系查询是否存在，有先删除再保存
        Map<String, Object> params = new HashMap<>();
        params.put("meetingId",dto.getId());
        List<WlMeetingStaffDTO> wl= wlMeetingStaffService.list(params);
        if(wl.size()>0){
            wl.forEach(wlMeetingStaffDTO -> {
                wlMeetingStaffService.deleteById(wlMeetingStaffDTO.getId());
            });
        }
        List<WlMeetingStaffEntity> wlMeetingStaffList = new ArrayList<>();

        dto.getInvitedUserIDs().stream().forEach(userId->{
            WlMeetingStaffEntity meetingStaff = new WlMeetingStaffEntity();
            meetingStaff = meetingStaff.assembleWlMeetingStaff(dto.getId(),userId,null);
            wlMeetingStaffList.add(meetingStaff);
        });
        wlMeetingStaffService.insertBatch(wlMeetingStaffList);

    }

}
