package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.entity.TaBuildingIntention;
import com.huiju.estateagents.entity.TdBizEvent;
import com.huiju.estateagents.mapper.TaBuildingIntentionMapper;
import com.huiju.estateagents.mapper.TdBizEventMapper;
import com.huiju.estateagents.service.ITaBuildingIntentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 项目意向表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-08-07
 */
@Service
public class TaBuildingIntentionServiceImpl extends ServiceImpl<TaBuildingIntentionMapper, TaBuildingIntention> implements ITaBuildingIntentionService {

    @Autowired
    private TaBuildingIntentionMapper taBuildingIntentionMapper;

    @Autowired
    private TdBizEventMapper tdBizEventMapper;

    @Override
    public ResponseBean taBuildingIntentionAddOrUpdate(List<TdBizEvent>tdBizEventsList, String buildingId, Integer orgId) {
        ResponseBean responseBean = new ResponseBean();

        // 先删除库中存在的
        taBuildingIntentionMapper.deleteByBuildingId(buildingId);


        // 获取所有需要 保存 或者 修改
        List<TaBuildingIntention> taBuildingIntentions = tdBizEventsList.stream().filter(e -> e.getCheckbox()).map(e -> {
                TaBuildingIntention taBuildingIntention = new TaBuildingIntention();
                taBuildingIntention.setIntentionId(e.getIntentionId());
                taBuildingIntention.setEventId(e.getEventId());
                taBuildingIntention.setEventCode(e.getEventCode());
                taBuildingIntention.setEventName(e.getEventName());
                taBuildingIntention.setIntention(e.getIntention());
                taBuildingIntention.setCreateDate(e.getCreateDate());
                taBuildingIntention.setBuildingId(buildingId);
                taBuildingIntention.setStatus(1);
                taBuildingIntention.setOrgId(orgId);
                return taBuildingIntention;
        }).collect(Collectors.toList());

        this.saveOrUpdateBatch(taBuildingIntentions);
        responseBean.addSuccess("操作成功！");
        return responseBean;
    }

    /**
     * 保存楼盘默认值
     *
     * @param orgId
     * @param buildingId
     */
    @Override
    public void saveDefaultIntention(Integer orgId, String buildingId) {
        //获取所有的意向值类型
        List<TdBizEvent> tdBizEventList = tdBizEventMapper.selectList(new QueryWrapper<>());
        //构造意向值
        List<TaBuildingIntention> taBuildingIntentions = new ArrayList<>();
        tdBizEventList.forEach(e -> {
            TaBuildingIntention taBuildingIntention = new TaBuildingIntention();
            taBuildingIntention.setIntentionId(e.getIntentionId());
            taBuildingIntention.setEventId(e.getEventId());
            taBuildingIntention.setEventCode(e.getEventCode());
            taBuildingIntention.setEventName(e.getEventName());
            taBuildingIntention.setIntention(e.getPoints());
            taBuildingIntention.setCreateDate(LocalDateTime.now());
            taBuildingIntention.setBuildingId(buildingId);
            taBuildingIntention.setStatus(1);
            taBuildingIntention.setOrgId(orgId);
            taBuildingIntentions.add(taBuildingIntention);
        });
        this.saveBatch(taBuildingIntentions);
    }
}
